From e0963543d28087d7295476dbfa3d5b6e17ea339d Mon Sep 17 00:00:00 2001
From: Simone Bordet <simone.bordet@gmail.com>
Date: Thu, 5 Nov 2015 15:51:44 +0100
Subject: [PATCH] 476641 - Proxy rewriteTarget() null return does not call
 error handler.

Introduced sendProxyResponseError(), centralizing the place where an
error is returned to the client, so that applications may override the
behavior.
---
 .../jetty/proxy/AbstractProxyServlet.java     | 28 +++++++++++--------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java
index bd2f0cb590a..5e872d4f18c 100644
--- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java
+++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java
@@ -413,11 +413,11 @@ public abstract class AbstractProxyServlet extends HttpServlet
      * like {@link HttpServletResponse#sendError(int)}.</p>
      *
      * @param clientRequest the client request
-     * @param clientResponse the client response
+     * @param proxyResponse the client response
      */
-    protected void onProxyRewriteFailed(HttpServletRequest clientRequest, HttpServletResponse clientResponse)
+    protected void onProxyRewriteFailed(HttpServletRequest clientRequest, HttpServletResponse proxyResponse)
     {
-        clientResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
+        sendProxyResponseError(clientRequest, proxyResponse, HttpStatus.FORBIDDEN_403);
     }
 
     protected boolean hasContent(HttpServletRequest clientRequest)
@@ -549,8 +549,7 @@ public abstract class AbstractProxyServlet extends HttpServlet
             int status = failure instanceof TimeoutException ?
                     HttpStatus.REQUEST_TIMEOUT_408 :
                     HttpStatus.INTERNAL_SERVER_ERROR_500;
-            proxyResponse.setStatus(status);
-            clientRequest.getAsyncContext().complete();
+            sendProxyResponseError(clientRequest, proxyResponse, status);
         }
     }
 
@@ -636,13 +635,10 @@ public abstract class AbstractProxyServlet extends HttpServlet
         else
         {
             proxyResponse.resetBuffer();
-            if (failure instanceof TimeoutException)
-                proxyResponse.setStatus(HttpServletResponse.SC_GATEWAY_TIMEOUT);
-            else
-                proxyResponse.setStatus(HttpServletResponse.SC_BAD_GATEWAY);
-            proxyResponse.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
-            AsyncContext asyncContext = clientRequest.getAsyncContext();
-            asyncContext.complete();
+            int status = failure instanceof TimeoutException ?
+                    HttpStatus.GATEWAY_TIMEOUT_504 :
+                    HttpStatus.BAD_GATEWAY_502;
+            sendProxyResponseError(clientRequest, proxyResponse, status);
         }
     }
 
@@ -651,6 +647,14 @@ public abstract class AbstractProxyServlet extends HttpServlet
         return System.identityHashCode(clientRequest);
     }
 
+    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();
+    }
+
     /**
      * <p>Utility class that implement transparent proxy functionalities.</p>
      * <p>Configuration parameters:</p>