@Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { if (proxyServer.isStopped()) { log.warn("Aborting request to {} because proxy is stopped", originalRequest.getUri()); HttpResponse abortedResponse = new DefaultFullHttpResponse(originalRequest.getProtocolVersion(), HttpResponseStatus.SERVICE_UNAVAILABLE); HttpHeaders.setContentLength(abortedResponse, 0L); return abortedResponse; } for (HttpFilters filter : filters) { try { HttpResponse filterResponse = filter.clientToProxyRequest(httpObject); if (filterResponse != null) { // if we are short-circuiting the response to an HttpRequest, update ModifiedRequestAwareFilter instances // with this (possibly) modified HttpRequest before returning the short-circuit response if (httpObject instanceof HttpRequest) { updateFiltersWithModifiedResponse((HttpRequest) httpObject); } return filterResponse; } } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } // if this httpObject is the HTTP request, set the modified request object on all ModifiedRequestAwareFilter // instances, so they have access to all modifications the request filters made while filtering if (httpObject instanceof HttpRequest) { updateFiltersWithModifiedResponse((HttpRequest) httpObject); } return null; }
@Override public HttpObject proxyToClientResponse(HttpObject httpObject) { HttpObject processedHttpObject = httpObject; for (HttpFilters filter : filters) { try { processedHttpObject = filter.proxyToClientResponse(processedHttpObject); if (processedHttpObject == null) { return null; } } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } return processedHttpObject; }
@Override public void proxyToServerConnectionFailed() { for (HttpFilters filter : filters) { try { filter.proxyToServerConnectionFailed(); } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } }
/*************************************************************************** * Lifecycle **************************************************************************/ @Override protected void become(ConnectionState newState) { // Report connection status to HttpFilters if (getCurrentState() == DISCONNECTED && newState == CONNECTING) { currentFilters.proxyToServerConnectionStarted(); } else if (getCurrentState() == CONNECTING) { if (newState == HANDSHAKING) { currentFilters.proxyToServerConnectionSSLHandshakeStarted(); } else if (newState == AWAITING_INITIAL) { currentFilters.proxyToServerConnectionSucceeded(ctx); } else if (newState == DISCONNECTED) { currentFilters.proxyToServerConnectionFailed(); } } else if (getCurrentState() == HANDSHAKING) { if (newState == AWAITING_INITIAL) { currentFilters.proxyToServerConnectionSucceeded(ctx); } else if (newState == DISCONNECTED) { currentFilters.proxyToServerConnectionFailed(); } } else if (getCurrentState() == AWAITING_CHUNK && newState != AWAITING_CHUNK) { currentFilters.serverToProxyResponseReceived(); } super.become(newState); }
@Override protected void readHTTPChunk(HttpContent chunk) { currentFilters.clientToProxyRequest(chunk); currentFilters.proxyToServerRequest(chunk); currentServerConnection.write(chunk); }
httpObject = filters.serverToProxyResponse(httpObject); if (httpObject == null) { forceDisconnect(serverConnection); httpObject = filters.proxyToClientResponse(httpObject); if (httpObject == null) { forceDisconnect(serverConnection);
@Override public void proxyToServerConnectionQueued() { for (HttpFilters filter : filters) { try { filter.proxyToServerConnectionQueued(); } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } }
@Override public void proxyToServerRequestSent() { for (HttpFilters filter : filters) { try { filter.proxyToServerRequestSent(); } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } }
@Override public void proxyToServerRequestSending() { for (HttpFilters filter : filters) { try { filter.proxyToServerRequestSending(); } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } }
@Override public void proxyToServerConnectionSSLHandshakeStarted() { for (HttpFilters filter : filters) { try { filter.proxyToServerConnectionSSLHandshakeStarted(); } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } }
@Override public void proxyToServerConnectionSucceeded(ChannelHandlerContext serverCtx) { for (HttpFilters filter : filters) { try { filter.proxyToServerConnectionSucceeded(serverCtx); } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } }
@Override public void proxyToServerConnectionStarted() { for (HttpFilters filter : filters) { try { filter.proxyToServerConnectionStarted(); } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } }
@Override public HttpResponse proxyToServerRequest(HttpObject httpObject) { for (HttpFilters filter : filters) { try { HttpResponse filterResponse = filter.proxyToServerRequest(httpObject); if (filterResponse != null) { return filterResponse; } } catch (RuntimeException e) { log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e); } } return null; }
/*************************************************************************** * Lifecycle **************************************************************************/ @Override protected void become(ConnectionState newState) { // Report connection status to HttpFilters if (getCurrentState() == DISCONNECTED && newState == CONNECTING) { currentFilters.proxyToServerConnectionStarted(); } else if (getCurrentState() == CONNECTING) { if (newState == HANDSHAKING) { currentFilters.proxyToServerConnectionSSLHandshakeStarted(); } else if (newState == AWAITING_INITIAL) { currentFilters.proxyToServerConnectionSucceeded(ctx); } else if (newState == DISCONNECTED) { currentFilters.proxyToServerConnectionFailed(); } } else if (getCurrentState() == HANDSHAKING) { if (newState == AWAITING_INITIAL) { currentFilters.proxyToServerConnectionSucceeded(ctx); } else if (newState == DISCONNECTED) { currentFilters.proxyToServerConnectionFailed(); } } else if (getCurrentState() == AWAITING_CHUNK && newState != AWAITING_CHUNK) { currentFilters.serverToProxyResponseReceived(); } super.become(newState); }
@Override protected void readHTTPChunk(HttpContent chunk) { currentFilters.clientToProxyRequest(chunk); currentFilters.proxyToServerRequest(chunk); currentServerConnection.write(chunk); }
httpObject = filters.serverToProxyResponse(httpObject); if (httpObject == null) { forceDisconnect(serverConnection); httpObject = filters.proxyToClientResponse(httpObject); if (httpObject == null) { forceDisconnect(serverConnection);
@Override public void proxyToServerConnectionQueued() { delegate.proxyToServerConnectionQueued(); }
@Override protected void contentWritten(HttpContent httpContent) { if (httpContent instanceof LastHttpContent) { currentFilters.proxyToServerRequestSent(); } } };
@Override public void proxyToServerRequestSending() { delegate.proxyToServerRequestSending(); }
@Override public void proxyToServerConnectionSSLHandshakeStarted() { delegate.proxyToServerConnectionSSLHandshakeStarted(); }