Issue #1661 Portable onProxyResponseFailure

avoid using sendError(-1)
This commit is contained in:
Greg Wilkins 2017-07-13 14:14:36 +02:00
parent 070a58ff59
commit 8795781923
2 changed files with 32 additions and 36 deletions

View File

@ -626,35 +626,14 @@ public abstract class AbstractProxyServlet extends HttpServlet
if (_log.isDebugEnabled()) if (_log.isDebugEnabled())
_log.debug(getRequestId(clientRequest) + " proxying failed", failure); _log.debug(getRequestId(clientRequest) + " proxying failed", failure);
if (proxyResponse.isCommitted()) int status = failure instanceof TimeoutException ?
{ HttpStatus.GATEWAY_TIMEOUT_504 :
try HttpStatus.BAD_GATEWAY_502;
{ int serverStatus = serverResponse == null ? status : serverResponse.getStatus();
// Use Jetty specific behavior to close connection. if (expects100Continue(clientRequest) && serverStatus >= HttpStatus.OK_200)
proxyResponse.sendError(-1); status = serverStatus;
if (clientRequest.isAsyncStarted()) sendProxyResponseError(clientRequest, proxyResponse, status);
{
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);
}
} }
protected int getRequestId(HttpServletRequest clientRequest) protected int getRequestId(HttpServletRequest clientRequest)
@ -664,10 +643,27 @@ public abstract class AbstractProxyServlet extends HttpServlet
protected void sendProxyResponseError(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, int status) protected void sendProxyResponseError(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, int status)
{ {
proxyResponse.setStatus(status); try
proxyResponse.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString()); {
if (clientRequest.isAsyncStarted()) System.err.println("sendError "+status+" "+proxyResponse.isCommitted());
clientRequest.getAsyncContext().complete(); 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) protected void onContinue(HttpServletRequest clientRequest, Request proxyRequest)

View File

@ -1074,10 +1074,10 @@ public class ProxyServletTest
Response response = listener.get(5, TimeUnit.SECONDS); Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(504, response.getStatus()); 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(); InputStream input = listener.getInputStream();
Assert.assertEquals(-1, input.read()); String body = IO.toString(input);
Assert.assertThat(body,Matchers.containsString("HTTP ERROR: 504"));
chunk1Latch.countDown(); chunk1Latch.countDown();
// Result succeeds because a 504 is a valid HTTP response. // Result succeeds because a 504 is a valid HTTP response.