From dae3005d3ca6940803be008830bdc94ebae03b7d Mon Sep 17 00:00:00 2001 From: Ivan De Marino Date: Thu, 24 Jul 2014 08:55:56 +0100 Subject: [PATCH] Some method renaming for consistency. --- README.md | 8 +-- .../org/littleshoot/proxy/HttpFilters.java | 53 ++++++++++--------- .../littleshoot/proxy/HttpFiltersAdapter.java | 20 +++---- .../proxy/impl/ClientToProxyConnection.java | 12 ++--- .../proxy/impl/ProxyToServerConnection.java | 14 ++--- .../proxy/EndToEndStoppingTest.java | 2 +- .../org/littleshoot/proxy/HttpFilterTest.java | 8 +-- .../proxy/HttpStreamingFilterTest.java | 2 +- .../org/littleshoot/proxy/MitmProxyTest.java | 20 +++---- 9 files changed, 71 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 40c19a5fa..c43edac34 100644 --- a/README.md +++ b/README.md @@ -40,25 +40,25 @@ HttpProxyServer server = public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) { return new HttpFiltersAdapter(originalRequest) { @Override - public HttpResponse clientToProxyRequestPreProcessing(HttpObject httpObject) { + public HttpResponse clientToProxyRequest(HttpObject httpObject) { // TODO: implement your filtering here return null; } @Override - public HttpResponse proxyToServerRequestPreProcessing(HttpObject httpObject) { + public HttpResponse proxyToServerRequest(HttpObject httpObject) { // TODO: implement your filtering here return null; } @Override - public HttpObject serverToProxyResponsePreProcessing(HttpObject httpObject) { + public HttpObject serverToProxyResponse(HttpObject httpObject) { // TODO: implement your filtering here return httpObject; } @Override - public HttpObject proxyToClientResponsePreProcessing(HttpObject httpObject) { + public HttpObject proxyToClientResponse(HttpObject httpObject) { // TODO: implement your filtering here return httpObject; } diff --git a/src/main/java/org/littleshoot/proxy/HttpFilters.java b/src/main/java/org/littleshoot/proxy/HttpFilters.java index ae8a4a365..3b9de3857 100644 --- a/src/main/java/org/littleshoot/proxy/HttpFilters.java +++ b/src/main/java/org/littleshoot/proxy/HttpFilters.java @@ -8,18 +8,21 @@ /** *

* Interface for objects that filter {@link HttpObject}s, including both - * requests and responses. + * requests and responses, and informs of different steps in request/response. *

* *

* Multiple methods are defined, corresponding to different steps in the request - * processing lifecycle. Each of these methods is given the current object - * (request, response or chunk) and is allowed to modify it in place. + * processing lifecycle. Some of these methods is given the current object + * (request, response or chunk) and is allowed to modify it in place. Others + * provide a notification of when specific operations happen (i.e. connection + * in queue, DNS resolution, SSL handshaking and so forth). *

* *

* Because HTTP transfers can be chunked, for any given request or response, the - * filter methods may be called multiple times, once for the initial + * filter methods that can modify request/response in place + * may be called multiple times, once for the initial * {@link HttpRequest} or {@link HttpResponse}, and once for each subsequent * {@link HttpContent}. The last chunk will always be a {@link LastHttpContent} * and can be checked for being last using @@ -53,7 +56,7 @@ public interface HttpFilters { * the client, return it here, otherwise return null to continue * processing as usual */ - HttpResponse clientToProxyRequestPreProcessing(HttpObject httpObject); + HttpResponse clientToProxyRequest(HttpObject httpObject); /** * Filters requests on their way from the proxy to the server. @@ -63,15 +66,15 @@ public interface HttpFilters { * the client, return it here, otherwise return null to continue * processing as usual */ - HttpResponse proxyToServerRequestPreProcessing(HttpObject httpObject); + HttpResponse proxyToServerRequest(HttpObject httpObject); /** - * TODO + * Informs filter that proxy to server request is being sent. */ void proxyToServerRequestSending(); /** - * TODO + * Informs filter that proxy to server request has been sent. */ void proxyToServerRequestSent(); @@ -82,15 +85,15 @@ public interface HttpFilters { * @return the modified (or unmodified) HttpObject. Returning null will * force a disconnect. */ - HttpObject serverToProxyResponsePreProcessing(HttpObject httpObject); + HttpObject serverToProxyResponse(HttpObject httpObject); /** - * TODO + * Informs filter that server to proxy response is being received. */ void serverToProxyResponseReceiving(); /** - * TODO + * Informs filter that server to proxy response has been received. */ void serverToProxyResponseReceived(); @@ -101,12 +104,12 @@ public interface HttpFilters { * @return the modified (or unmodified) HttpObject. Returning null will * force a disconnect. */ - HttpObject proxyToClientResponsePreProcessing(HttpObject httpObject); + HttpObject proxyToClientResponse(HttpObject httpObject); /** - * Inform filter that proxy to server connection is in queue. + * Informs filter that proxy to server connection is in queue. */ - void proxyToServerAwaitingConnection(); + void proxyToServerConnectionQueued(); /** * Filter DNS resolution from proxy to server. @@ -115,34 +118,34 @@ public interface HttpFilters { * @return alternative address resolution. Returning null will let * normal DNS resolution continue. */ - InetSocketAddress proxyToServerResolving(String resolvingServerHostAndPort); + InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort); /** - * Inform filter that proxy to server DNS resolution has happened. + * Informs filter that proxy to server DNS resolution has happened. * * @param serverHostAndPort Server "HOST:PORT" - * @param resolvedRemoteAddress Address it was proxyToServerResolved to + * @param resolvedRemoteAddress Address it was proxyToServerResolutionSucceeded to */ - void proxyToServerResolved(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress); + void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress); /** - * Inform filter that proxy to server connection is initiating. + * Informs filter that proxy to server connection is initiating. */ - void proxyToServerConnecting(); + void proxyToServerConnectionStarted(); /** - * Inform filter that proxy to server ssl handshake is initiating. + * Informs filter that proxy to server ssl handshake is initiating. */ - void proxyToServerSSLHandshaking(); + void proxyToServerConnectionSSLHandshakeStarted(); /** - * Inform filter that proxy to server connection has failed. + * Informs filter that proxy to server connection has failed. */ void proxyToServerConnectionFailed(); /** - * Inform filter that proxy to server connection has succeeded. + * Informs filter that proxy to server connection has succeeded. */ - void proxyToServerConnectionSuccess(); + void proxyToServerConnectionSucceeded(); } diff --git a/src/main/java/org/littleshoot/proxy/HttpFiltersAdapter.java b/src/main/java/org/littleshoot/proxy/HttpFiltersAdapter.java index f7dc93b63..6dc87c4e2 100644 --- a/src/main/java/org/littleshoot/proxy/HttpFiltersAdapter.java +++ b/src/main/java/org/littleshoot/proxy/HttpFiltersAdapter.java @@ -25,12 +25,12 @@ public HttpFiltersAdapter(HttpRequest originalRequest) { } @Override - public HttpResponse clientToProxyRequestPreProcessing(HttpObject httpObject) { + public HttpResponse clientToProxyRequest(HttpObject httpObject) { return null; } @Override - public HttpResponse proxyToServerRequestPreProcessing(HttpObject httpObject) { + public HttpResponse proxyToServerRequest(HttpObject httpObject) { return null; } @@ -43,7 +43,7 @@ public void proxyToServerRequestSent() { } @Override - public HttpObject serverToProxyResponsePreProcessing(HttpObject httpObject) { + public HttpObject serverToProxyResponse(HttpObject httpObject) { return httpObject; } @@ -56,29 +56,29 @@ public void serverToProxyResponseReceived() { } @Override - public HttpObject proxyToClientResponsePreProcessing(HttpObject httpObject) { + public HttpObject proxyToClientResponse(HttpObject httpObject) { return httpObject; } @Override - public void proxyToServerAwaitingConnection() { + public void proxyToServerConnectionQueued() { } @Override - public InetSocketAddress proxyToServerResolving(String resolvingServerHostAndPort) { + public InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort) { return null; } @Override - public void proxyToServerResolved(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress) { + public void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress) { } @Override - public void proxyToServerConnecting() { + public void proxyToServerConnectionStarted() { } @Override - public void proxyToServerSSLHandshaking() { + public void proxyToServerConnectionSSLHandshakeStarted() { } @Override @@ -86,6 +86,6 @@ public void proxyToServerConnectionFailed() { } @Override - public void proxyToServerConnectionSuccess() { + public void proxyToServerConnectionSucceeded() { } } diff --git a/src/main/java/org/littleshoot/proxy/impl/ClientToProxyConnection.java b/src/main/java/org/littleshoot/proxy/impl/ClientToProxyConnection.java index 66d315583..4fd4f69bc 100644 --- a/src/main/java/org/littleshoot/proxy/impl/ClientToProxyConnection.java +++ b/src/main/java/org/littleshoot/proxy/impl/ClientToProxyConnection.java @@ -198,7 +198,7 @@ private ConnectionState doReadHTTPInitial(HttpRequest httpRequest) { originalRequest, ctx); // Do the pre filtering - if (shortCircuitRespond(currentFilters.clientToProxyRequestPreProcessing(httpRequest))) { + if (shortCircuitRespond(currentFilters.clientToProxyRequest(httpRequest))) { return DISCONNECT_REQUESTED; } @@ -260,7 +260,7 @@ private ConnectionState doReadHTTPInitial(HttpRequest httpRequest) { } modifyRequestHeadersToReflectProxying(httpRequest); - if (shortCircuitRespond(currentFilters.proxyToServerRequestPreProcessing(httpRequest))) { + if (shortCircuitRespond(currentFilters.proxyToServerRequest(httpRequest))) { return DISCONNECT_REQUESTED; } @@ -279,8 +279,8 @@ private ConnectionState doReadHTTPInitial(HttpRequest httpRequest) { @Override protected void readHTTPChunk(HttpContent chunk) { - currentFilters.clientToProxyRequestPreProcessing(chunk); - currentFilters.proxyToServerRequestPreProcessing(chunk); + currentFilters.clientToProxyRequest(chunk); + currentFilters.proxyToServerRequest(chunk); currentServerConnection.write(chunk); } @@ -312,7 +312,7 @@ protected void readRaw(ByteBuf buf) { void respond(ProxyToServerConnection serverConnection, HttpFilters filters, HttpRequest currentHttpRequest, HttpResponse currentHttpResponse, HttpObject httpObject) { - httpObject = filters.serverToProxyResponsePreProcessing(httpObject); + httpObject = filters.serverToProxyResponse(httpObject); if (httpObject == null) { forceDisconnect(serverConnection); return; @@ -324,7 +324,7 @@ void respond(ProxyToServerConnection serverConnection, HttpFilters filters, modifyResponseHeadersToReflectProxying(httpResponse); } - httpObject = filters.proxyToClientResponsePreProcessing(httpObject); + httpObject = filters.proxyToClientResponse(httpObject); if (httpObject == null) { forceDisconnect(serverConnection); return; diff --git a/src/main/java/org/littleshoot/proxy/impl/ProxyToServerConnection.java b/src/main/java/org/littleshoot/proxy/impl/ProxyToServerConnection.java index 2c6ffa4e9..a6c0be1df 100644 --- a/src/main/java/org/littleshoot/proxy/impl/ProxyToServerConnection.java +++ b/src/main/java/org/littleshoot/proxy/impl/ProxyToServerConnection.java @@ -150,7 +150,7 @@ private ProxyToServerConnection( this.currentFilters = initialFilters; // Report connection status to HttpFilters - this.currentFilters.proxyToServerAwaitingConnection(); + this.currentFilters.proxyToServerConnectionQueued(); setupConnectionParameters(); } @@ -306,18 +306,18 @@ protected void writeHttp(HttpObject httpObject) { protected void become(ConnectionState newState) { // Report connection status to HttpFilters if (getCurrentState() == DISCONNECTED && newState == CONNECTING) { - currentFilters.proxyToServerConnecting(); + currentFilters.proxyToServerConnectionStarted(); } else if (getCurrentState() == CONNECTING) { if (newState == HANDSHAKING) { - currentFilters.proxyToServerSSLHandshaking(); + currentFilters.proxyToServerConnectionSSLHandshakeStarted(); } else if (newState == AWAITING_INITIAL) { - currentFilters.proxyToServerConnectionSuccess(); + currentFilters.proxyToServerConnectionSucceeded(); } else if (newState == DISCONNECTED) { currentFilters.proxyToServerConnectionFailed(); } } else if (getCurrentState() == HANDSHAKING && newState == AWAITING_INITIAL) { - currentFilters.proxyToServerConnectionSuccess(); + currentFilters.proxyToServerConnectionSucceeded(); } else if (getCurrentState() == AWAITING_CHUNK && newState != AWAITING_CHUNK) { currentFilters.serverToProxyResponseReceived(); @@ -705,11 +705,11 @@ private void setupConnectionParameters() throws UnknownHostException { this.transportProtocol = TransportProtocol.TCP; // Report DNS resolution to HttpFilters - this.remoteAddress = this.currentFilters.proxyToServerResolving(serverHostAndPort); + this.remoteAddress = this.currentFilters.proxyToServerResolutionStarted(serverHostAndPort); if (this.remoteAddress == null) { this.remoteAddress = addressFor(serverHostAndPort, proxyServer); } - this.currentFilters.proxyToServerResolved(serverHostAndPort, this.remoteAddress); + this.currentFilters.proxyToServerResolutionSucceeded(serverHostAndPort, this.remoteAddress); this.localAddress = null; } diff --git a/src/test/java/org/littleshoot/proxy/EndToEndStoppingTest.java b/src/test/java/org/littleshoot/proxy/EndToEndStoppingTest.java index 2456db665..47375299a 100644 --- a/src/test/java/org/littleshoot/proxy/EndToEndStoppingTest.java +++ b/src/test/java/org/littleshoot/proxy/EndToEndStoppingTest.java @@ -104,7 +104,7 @@ private void runSiteTestWithHttpClient(final String site) throws Exception { public HttpFilters filterRequest(HttpRequest originalRequest) { return new HttpFiltersAdapter(originalRequest) { @Override - public io.netty.handler.codec.http.HttpResponse proxyToServerRequestPreProcessing( + public io.netty.handler.codec.http.HttpResponse proxyToServerRequest( HttpObject httpObject) { System.out .println("Request with through proxy"); diff --git a/src/test/java/org/littleshoot/proxy/HttpFilterTest.java b/src/test/java/org/littleshoot/proxy/HttpFilterTest.java index 134b0ea5e..cedb6650b 100644 --- a/src/test/java/org/littleshoot/proxy/HttpFilterTest.java +++ b/src/test/java/org/littleshoot/proxy/HttpFilterTest.java @@ -54,7 +54,7 @@ public HttpFilters filterRequest(HttpRequest originalRequest) { return new HttpFiltersAdapter(originalRequest) { @Override - public HttpResponse clientToProxyRequestPreProcessing(HttpObject httpObject) { + public HttpResponse clientToProxyRequest(HttpObject httpObject) { fullHttpRequestsReceived.incrementAndGet(); if (httpObject instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) httpObject; @@ -68,7 +68,7 @@ public HttpResponse clientToProxyRequestPreProcessing(HttpObject httpObject) { } @Override - public HttpResponse proxyToServerRequestPreProcessing(HttpObject httpObject) { + public HttpResponse proxyToServerRequest(HttpObject httpObject) { if (httpObject instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) httpObject; if (httpRequest.getUri().equals("/testing2")) { @@ -80,7 +80,7 @@ public HttpResponse proxyToServerRequestPreProcessing(HttpObject httpObject) { return null; } - public HttpObject serverToProxyResponsePreProcessing(HttpObject httpObject) { + public HttpObject serverToProxyResponse(HttpObject httpObject) { if (originalRequest.getUri().contains("testing3")) { return new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, @@ -97,7 +97,7 @@ public HttpObject serverToProxyResponsePreProcessing(HttpObject httpObject) { return httpObject; }; - public HttpObject proxyToClientResponsePreProcessing(HttpObject httpObject) { + public HttpObject proxyToClientResponse(HttpObject httpObject) { if (originalRequest.getUri().contains("testing4")) { return new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, diff --git a/src/test/java/org/littleshoot/proxy/HttpStreamingFilterTest.java b/src/test/java/org/littleshoot/proxy/HttpStreamingFilterTest.java index ed7f20aad..4a82ea5af 100644 --- a/src/test/java/org/littleshoot/proxy/HttpStreamingFilterTest.java +++ b/src/test/java/org/littleshoot/proxy/HttpStreamingFilterTest.java @@ -45,7 +45,7 @@ public void setUp() throws Exception { public HttpFilters filterRequest(HttpRequest originalRequest) { return new HttpFiltersAdapter(originalRequest) { @Override - public HttpResponse clientToProxyRequestPreProcessing(HttpObject httpObject) { + public HttpResponse clientToProxyRequest(HttpObject httpObject) { if (httpObject instanceof HttpRequest) { numberOfInitialRequestsFiltered .incrementAndGet(); diff --git a/src/test/java/org/littleshoot/proxy/MitmProxyTest.java b/src/test/java/org/littleshoot/proxy/MitmProxyTest.java index f3c8f41ac..903b1684c 100644 --- a/src/test/java/org/littleshoot/proxy/MitmProxyTest.java +++ b/src/test/java/org/littleshoot/proxy/MitmProxyTest.java @@ -43,7 +43,7 @@ public void lookupChainedProxies(HttpRequest httpRequest, public HttpFilters filterRequest(HttpRequest originalRequest) { return new HttpFiltersAdapter(originalRequest) { @Override - public HttpResponse clientToProxyRequestPreProcessing(HttpObject httpObject) { + public HttpResponse clientToProxyRequest(HttpObject httpObject) { if (httpObject instanceof HttpRequest) { requestPreMethodsSeen .add(((HttpRequest) httpObject) @@ -53,7 +53,7 @@ public HttpResponse clientToProxyRequestPreProcessing(HttpObject httpObject) { } @Override - public HttpResponse proxyToServerRequestPreProcessing( + public HttpResponse proxyToServerRequest( HttpObject httpObject) { if (httpObject instanceof HttpRequest) { requestPostMethodsSeen @@ -64,7 +64,7 @@ public HttpResponse proxyToServerRequestPreProcessing( } @Override - public HttpObject serverToProxyResponsePreProcessing(HttpObject httpObject) { + public HttpObject serverToProxyResponse(HttpObject httpObject) { if (httpObject instanceof HttpResponse) { responsePreOriginalRequestMethodsSeen .add(originalRequest.getMethod()); @@ -77,7 +77,7 @@ public HttpObject serverToProxyResponsePreProcessing(HttpObject httpObject) { } @Override - public HttpObject proxyToClientResponsePreProcessing(HttpObject httpObject) { + public HttpObject proxyToClientResponse(HttpObject httpObject) { if (httpObject instanceof HttpResponse) { responsePostOriginalRequestMethodsSeen .add(originalRequest.getMethod()); @@ -134,29 +134,29 @@ public void testSimplePostRequestOverHTTPS() throws Exception { } private void assertMethodSeenInRequestFilters(HttpMethod method) { - assertTrue(method + " should have been seen in clientToProxyRequestPreProcessing filter", + assertTrue(method + " should have been seen in clientToProxyRequest filter", requestPreMethodsSeen.contains(method)); - assertTrue(method + " should have been seen in proxyToServerRequestPreProcessing filter", + assertTrue(method + " should have been seen in proxyToServerRequest filter", requestPostMethodsSeen.contains(method)); } private void assertMethodSeenInResponseFilters(HttpMethod method) { assertTrue( method - + " should have been seen as the original requests's method in serverToProxyResponsePreProcessing filter", + + " should have been seen as the original requests's method in serverToProxyResponse filter", responsePreOriginalRequestMethodsSeen.contains(method)); assertTrue( method - + " should have been seen as the original requests's method in proxyToClientResponsePreProcessing filter", + + " should have been seen as the original requests's method in proxyToClientResponse filter", responsePostOriginalRequestMethodsSeen.contains(method)); } private void assertResponseFromFiltersMatchesActualResponse() { assertEquals( - "Data received through HttpFilters.serverToProxyResponsePreProcessing should match response", + "Data received through HttpFilters.serverToProxyResponse should match response", lastResponse, responsePreBody.toString()); assertEquals( - "Data received through HttpFilters.proxyToClientResponsePreProcessing should match response", + "Data received through HttpFilters.proxyToClientResponse should match response", lastResponse, responsePostBody.toString()); }