Skip to content

Commit

Permalink
Some method renaming for consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan De Marino committed Jul 24, 2014
1 parent 5b74f1e commit dae3005
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 68 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
53 changes: 28 additions & 25 deletions src/main/java/org/littleshoot/proxy/HttpFilters.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
/**
* <p>
* Interface for objects that filter {@link HttpObject}s, including both
* requests and responses.
* requests and responses, and informs of different steps in request/response.
* </p>
*
* <p>
* 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).
* </p>
*
* <p>
* 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
Expand Down Expand Up @@ -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.
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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.
Expand All @@ -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();

}
20 changes: 10 additions & 10 deletions src/main/java/org/littleshoot/proxy/HttpFiltersAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -43,7 +43,7 @@ public void proxyToServerRequestSent() {
}

@Override
public HttpObject serverToProxyResponsePreProcessing(HttpObject httpObject) {
public HttpObject serverToProxyResponse(HttpObject httpObject) {
return httpObject;
}

Expand All @@ -56,36 +56,36 @@ 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
public void proxyToServerConnectionFailed() {
}

@Override
public void proxyToServerConnectionSuccess() {
public void proxyToServerConnectionSucceeded() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -260,7 +260,7 @@ private ConnectionState doReadHTTPInitial(HttpRequest httpRequest) {
}

modifyRequestHeadersToReflectProxying(httpRequest);
if (shortCircuitRespond(currentFilters.proxyToServerRequestPreProcessing(httpRequest))) {
if (shortCircuitRespond(currentFilters.proxyToServerRequest(httpRequest))) {
return DISCONNECT_REQUESTED;
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private ProxyToServerConnection(
this.currentFilters = initialFilters;

// Report connection status to HttpFilters
this.currentFilters.proxyToServerAwaitingConnection();
this.currentFilters.proxyToServerConnectionQueued();

setupConnectionParameters();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/littleshoot/proxy/HttpFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")) {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading

0 comments on commit dae3005

Please sign in to comment.