mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
fix timeout for 404 proxy resources
This commit is contained in:
@@ -2,10 +2,11 @@ package sonia.scm;
|
|||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -24,6 +25,8 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public final class ProxyPushStateDispatcher implements PushStateDispatcher {
|
public final class ProxyPushStateDispatcher implements PushStateDispatcher {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ProxyPushStateDispatcher.class);
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
interface ConnectionFactory {
|
interface ConnectionFactory {
|
||||||
|
|
||||||
@@ -57,43 +60,38 @@ public final class ProxyPushStateDispatcher implements PushStateDispatcher {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException {
|
public void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException {
|
||||||
try {
|
|
||||||
proxy(request, response, uri);
|
|
||||||
} catch (FileNotFoundException ex) {
|
|
||||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void proxy(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException {
|
|
||||||
URL url = createProxyUrl(uri);
|
URL url = createProxyUrl(uri);
|
||||||
|
|
||||||
HttpURLConnection connection = connectionFactory.open(url);
|
HttpURLConnection connection = connectionFactory.open(url);
|
||||||
connection.setRequestMethod(request.getMethod());
|
connection.setRequestMethod(request.getMethod());
|
||||||
copyRequestHeaders(request, connection);
|
|
||||||
|
|
||||||
|
copyRequestHeaders(request, connection);
|
||||||
if (request.getContentLength() > 0) {
|
if (request.getContentLength() > 0) {
|
||||||
copyRequestBody(request, connection);
|
copyRequestBody(request, connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
int responseCode = connection.getResponseCode();
|
int responseCode = connection.getResponseCode();
|
||||||
response.setStatus(responseCode);
|
response.setStatus(responseCode);
|
||||||
|
|
||||||
copyResponseHeaders(response, connection);
|
copyResponseHeaders(response, connection);
|
||||||
|
if (connection.getContentLength() > 0) {
|
||||||
appendProxyHeader(response, url);
|
|
||||||
|
|
||||||
copyResponseBody(response, connection);
|
copyResponseBody(response, connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void appendProxyHeader(HttpServletResponse response, URL url) {
|
|
||||||
response.addHeader("X-Forwarded-Port", String.valueOf(url.getPort()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copyResponseBody(HttpServletResponse response, HttpURLConnection connection) throws IOException {
|
private void copyResponseBody(HttpServletResponse response, HttpURLConnection connection) throws IOException {
|
||||||
try (InputStream input = connection.getInputStream(); OutputStream output = response.getOutputStream()) {
|
try (InputStream input = getConnectionInput(connection); OutputStream output = response.getOutputStream()) {
|
||||||
ByteStreams.copy(input, output);
|
ByteStreams.copy(input, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private InputStream getConnectionInput(HttpURLConnection connection) throws IOException {
|
||||||
|
if (connection.getErrorStream() != null) {
|
||||||
|
return connection.getErrorStream();
|
||||||
|
}
|
||||||
|
return connection.getInputStream();
|
||||||
|
}
|
||||||
|
|
||||||
private void copyResponseHeaders(HttpServletResponse response, HttpURLConnection connection) {
|
private void copyResponseHeaders(HttpServletResponse response, HttpURLConnection connection) {
|
||||||
Map<String, List<String>> headerFields = connection.getHeaderFields();
|
Map<String, List<String>> headerFields = connection.getHeaderFields();
|
||||||
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
|
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
|
||||||
|
|||||||
@@ -51,11 +51,15 @@ public class ProxyPushStateDispatcherTest {
|
|||||||
when(request.getHeaders("Content-Type")).thenReturn(toEnum("application/json"));
|
when(request.getHeaders("Content-Type")).thenReturn(toEnum("application/json"));
|
||||||
|
|
||||||
// configure proxy url connection mock
|
// configure proxy url connection mock
|
||||||
when(connection.getInputStream()).thenReturn(new ByteArrayInputStream("hitchhicker".getBytes(Charsets.UTF_8)));
|
byte[] data = "hitchhicker".getBytes(Charsets.UTF_8);
|
||||||
|
when(connection.getErrorStream()).thenReturn(null);
|
||||||
|
when(connection.getInputStream()).thenReturn(new ByteArrayInputStream(data));
|
||||||
|
|
||||||
Map<String, List<String>> headerFields = new HashMap<>();
|
Map<String, List<String>> headerFields = new HashMap<>();
|
||||||
headerFields.put("Content-Type", Lists.newArrayList("application/yaml"));
|
headerFields.put("Content-Type", Lists.newArrayList("application/yaml"));
|
||||||
when(connection.getHeaderFields()).thenReturn(headerFields);
|
when(connection.getHeaderFields()).thenReturn(headerFields);
|
||||||
when(connection.getResponseCode()).thenReturn(200);
|
when(connection.getResponseCode()).thenReturn(200);
|
||||||
|
when(connection.getContentLength()).thenReturn(data.length);
|
||||||
|
|
||||||
// configure response mock
|
// configure response mock
|
||||||
DevServletOutputStream output = new DevServletOutputStream();
|
DevServletOutputStream output = new DevServletOutputStream();
|
||||||
@@ -70,6 +74,41 @@ public class ProxyPushStateDispatcherTest {
|
|||||||
// verify response
|
// verify response
|
||||||
verify(response).setStatus(200);
|
verify(response).setStatus(200);
|
||||||
verify(response).addHeader("Content-Type", "application/yaml");
|
verify(response).addHeader("Content-Type", "application/yaml");
|
||||||
|
|
||||||
|
assertEquals("hitchhicker", output.stream.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetNotFound() throws IOException {
|
||||||
|
// configure request mock
|
||||||
|
when(request.getMethod()).thenReturn("GET");
|
||||||
|
when(request.getHeaderNames()).thenReturn(toEnum("Content-Type"));
|
||||||
|
when(request.getHeaders("Content-Type")).thenReturn(toEnum("application/json"));
|
||||||
|
|
||||||
|
// configure proxy url connection mock
|
||||||
|
byte[] data = "hitchhicker".getBytes(Charsets.UTF_8);
|
||||||
|
when(connection.getErrorStream()).thenReturn(new ByteArrayInputStream(data));
|
||||||
|
|
||||||
|
Map<String, List<String>> headerFields = new HashMap<>();
|
||||||
|
headerFields.put("Content-Type", Lists.newArrayList("application/yaml"));
|
||||||
|
when(connection.getHeaderFields()).thenReturn(headerFields);
|
||||||
|
when(connection.getResponseCode()).thenReturn(404);
|
||||||
|
when(connection.getContentLength()).thenReturn(data.length);
|
||||||
|
|
||||||
|
// configure response mock
|
||||||
|
DevServletOutputStream output = new DevServletOutputStream();
|
||||||
|
when(response.getOutputStream()).thenReturn(output);
|
||||||
|
|
||||||
|
dispatcher.dispatch(request, response, "/people/trillian");
|
||||||
|
|
||||||
|
// verify connection
|
||||||
|
verify(connection).setRequestMethod("GET");
|
||||||
|
verify(connection).setRequestProperty("Content-Type", "application/json");
|
||||||
|
|
||||||
|
// verify response
|
||||||
|
verify(response).setStatus(404);
|
||||||
|
verify(response).addHeader("Content-Type", "application/yaml");
|
||||||
|
|
||||||
assertEquals("hitchhicker", output.stream.toString());
|
assertEquals("hitchhicker", output.stream.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,14 +121,10 @@ public class ProxyPushStateDispatcherTest {
|
|||||||
when(request.getContentLength()).thenReturn(1);
|
when(request.getContentLength()).thenReturn(1);
|
||||||
|
|
||||||
// configure proxy url connection mock
|
// configure proxy url connection mock
|
||||||
when(connection.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[0]));
|
|
||||||
Map<String, List<String>> headerFields = new HashMap<>();
|
Map<String, List<String>> headerFields = new HashMap<>();
|
||||||
when(connection.getHeaderFields()).thenReturn(headerFields);
|
when(connection.getHeaderFields()).thenReturn(headerFields);
|
||||||
when(connection.getResponseCode()).thenReturn(204);
|
when(connection.getResponseCode()).thenReturn(204);
|
||||||
|
|
||||||
// configure response mock
|
|
||||||
when(response.getOutputStream()).thenReturn(new DevServletOutputStream());
|
|
||||||
|
|
||||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||||
when(connection.getOutputStream()).thenReturn(output);
|
when(connection.getOutputStream()).thenReturn(output);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user