Issue #1661 Portable onProxyResponseFailure

avoid using sendError(-1)
This commit is contained in:
Greg Wilkins 2017-07-13 14:14:36 +02:00 committed by Joakim Erdfelt
parent 15cc8f1d40
commit 6a2dd626e0
2 changed files with 32 additions and 36 deletions

View File

@ -638,35 +638,14 @@ public abstract class AbstractProxyServlet extends HttpServlet
if (_log.isDebugEnabled())
_log.debug(getRequestId(clientRequest) + " proxying failed", failure);
if (proxyResponse.isCommitted())
{
try
{
// Use Jetty specific behavior to close connection.
proxyResponse.sendError(-1);
if (clientRequest.isAsyncStarted())
{
AsyncContext asyncContext = clientRequest.getAsyncContext();
asyncContext.complete();
}
}
catch (Throwable x)
{
if (_log.isDebugEnabled())
_log.debug(getRequestId(clientRequest) + " could not close the connection", failure);
}
}
else
{
proxyResponse.resetBuffer();
int status = failure instanceof TimeoutException ?
HttpStatus.GATEWAY_TIMEOUT_504 :
HttpStatus.BAD_GATEWAY_502;
int serverStatus = serverResponse == null ? status : serverResponse.getStatus();
if (expects100Continue(clientRequest) && serverStatus >= HttpStatus.OK_200)
status = serverStatus;
sendProxyResponseError(clientRequest, proxyResponse, status);
}
int status = failure instanceof TimeoutException ?
HttpStatus.GATEWAY_TIMEOUT_504 :
HttpStatus.BAD_GATEWAY_502;
int serverStatus = serverResponse == null ? status : serverResponse.getStatus();
if (expects100Continue(clientRequest) && serverStatus >= HttpStatus.OK_200)
status = serverStatus;
sendProxyResponseError(clientRequest, proxyResponse, status);
}
protected int getRequestId(HttpServletRequest clientRequest)
@ -676,10 +655,27 @@ public abstract class AbstractProxyServlet extends HttpServlet
protected void sendProxyResponseError(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, int status)
{
proxyResponse.setStatus(status);
proxyResponse.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
if (clientRequest.isAsyncStarted())
clientRequest.getAsyncContext().complete();
try
{
System.err.println("sendError "+status+" "+proxyResponse.isCommitted());
new Throwable().printStackTrace();
if (!proxyResponse.isCommitted())
{
proxyResponse.resetBuffer();
proxyResponse.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
}
proxyResponse.sendError(status);
}
catch(Exception e)
{
e.printStackTrace();
_log.ignore(e);
}
finally
{
if (clientRequest.isAsyncStarted())
clientRequest.getAsyncContext().complete();
}
}
protected void onContinue(HttpServletRequest clientRequest, Request proxyRequest)

View File

@ -1087,10 +1087,10 @@ public class ProxyServletTest
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(504, response.getStatus());
// Make sure there is no content, as the proxy-to-client response has been reset.
// Make sure there is error page content, as the proxy-to-client response has been reset.
InputStream input = listener.getInputStream();
Assert.assertEquals(-1, input.read());
String body = IO.toString(input);
Assert.assertThat(body,Matchers.containsString("HTTP ERROR: 504"));
chunk1Latch.countDown();
// Result succeeds because a 504 is a valid HTTP response.