fix timeout for 404 proxy resources

This commit is contained in:
Sebastian Sdorra
2018-08-29 09:56:33 +02:00
parent 5ae8494e94
commit c05e45271f
2 changed files with 57 additions and 24 deletions

View File

@@ -2,10 +2,11 @@ package sonia.scm;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.ByteStreams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -24,6 +25,8 @@ import java.util.Map;
*/
public final class ProxyPushStateDispatcher implements PushStateDispatcher {
private static final Logger LOG = LoggerFactory.getLogger(ProxyPushStateDispatcher.class);
@FunctionalInterface
interface ConnectionFactory {
@@ -57,43 +60,38 @@ public final class ProxyPushStateDispatcher implements PushStateDispatcher {
@Override
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);
HttpURLConnection connection = connectionFactory.open(url);
connection.setRequestMethod(request.getMethod());
copyRequestHeaders(request, connection);
copyRequestHeaders(request, connection);
if (request.getContentLength() > 0) {
copyRequestBody(request, connection);
}
int responseCode = connection.getResponseCode();
response.setStatus(responseCode);
copyResponseHeaders(response, connection);
appendProxyHeader(response, url);
copyResponseBody(response, connection);
}
private void appendProxyHeader(HttpServletResponse response, URL url) {
response.addHeader("X-Forwarded-Port", String.valueOf(url.getPort()));
if (connection.getContentLength() > 0) {
copyResponseBody(response, connection);
}
}
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);
}
}
private InputStream getConnectionInput(HttpURLConnection connection) throws IOException {
if (connection.getErrorStream() != null) {
return connection.getErrorStream();
}
return connection.getInputStream();
}
private void copyResponseHeaders(HttpServletResponse response, HttpURLConnection connection) {
Map<String, List<String>> headerFields = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {