Merge pull request #3631 from garydgregory/jetty-9.4.x_proxy_with_custom_status_code_redux

Allow subclasses to map exceptions to status codes.
This commit is contained in:
Simone Bordet 2019-05-07 16:25:45 +02:00 committed by GitHub
commit 8cdb36f32b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 6 deletions

View File

@ -568,13 +568,18 @@ public abstract class AbstractProxyServlet extends HttpServlet
boolean aborted = proxyRequest.abort(failure); boolean aborted = proxyRequest.abort(failure);
if (!aborted) if (!aborted)
{ {
int status = failure instanceof TimeoutException ? int status = clientRequestStatus(failure);
HttpStatus.REQUEST_TIMEOUT_408 :
HttpStatus.INTERNAL_SERVER_ERROR_500;
sendProxyResponseError(clientRequest, proxyResponse, status); sendProxyResponseError(clientRequest, proxyResponse, status);
} }
} }
protected int clientRequestStatus(Throwable failure)
{
return failure instanceof TimeoutException ?
HttpStatus.REQUEST_TIMEOUT_408 :
HttpStatus.INTERNAL_SERVER_ERROR_500;
}
protected void onServerResponseHeaders(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) protected void onServerResponseHeaders(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse)
{ {
for (HttpField field : serverResponse.getHeaders()) for (HttpField field : serverResponse.getHeaders())
@ -639,9 +644,7 @@ 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);
int status = failure instanceof TimeoutException ? int status = proxyResponseStatus(failure);
HttpStatus.GATEWAY_TIMEOUT_504 :
HttpStatus.BAD_GATEWAY_502;
int serverStatus = serverResponse == null ? status : serverResponse.getStatus(); int serverStatus = serverResponse == null ? status : serverResponse.getStatus();
if (expects100Continue(clientRequest) && serverStatus >= HttpStatus.OK_200) if (expects100Continue(clientRequest) && serverStatus >= HttpStatus.OK_200)
status = serverStatus; status = serverStatus;
@ -649,6 +652,13 @@ public abstract class AbstractProxyServlet extends HttpServlet
} }
protected int proxyResponseStatus(Throwable failure)
{
return failure instanceof TimeoutException ?
HttpStatus.GATEWAY_TIMEOUT_504 :
HttpStatus.BAD_GATEWAY_502;
}
protected int getRequestId(HttpServletRequest clientRequest) protected int getRequestId(HttpServletRequest clientRequest)
{ {
return System.identityHashCode(clientRequest); return System.identityHashCode(clientRequest);