Skip to content

Commit

Permalink
Applied standard auto-formatting to changes from PR adamfisk#153
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Sep 18, 2014
1 parent 1736671 commit d87b794
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/littleshoot/proxy/ActivityTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void requestSentToServer(FullFlowContext flowContext,

/**
* Record that the proxy received bytes from the server.
*
*
* @param flowContext
* provides contextual information about the flow
* @param numberOfBytes
Expand Down
56 changes: 32 additions & 24 deletions src/main/java/org/littleshoot/proxy/HttpFilters.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
* Multiple methods are defined, corresponding to different steps in the request
* 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).
* 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 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
* {@link ProxyUtils#isLastChunk(HttpObject)}.
* 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 {@link ProxyUtils#isLastChunk(HttpObject)}.
* </p>
*
* <p>
Expand All @@ -45,11 +44,11 @@
* A new instance of {@link HttpFilters} is created for each request, so these
* objects can be stateful.
* </p>
*
*
* <p>
* To monitor (and time measure?) the different steps the request/response goes
* through, many informative methods are provided.
* Those steps are reported in the following order:
* through, many informative methods are provided. Those steps are reported in
* the following order:
* <ol>
* <li>clientToProxyRequest</li>
* <li>proxyToServerConnectionQueued</li>
Expand All @@ -73,7 +72,8 @@ public interface HttpFilters {
/**
* Filters requests on their way from the client to the proxy.
*
* @param httpObject Client to Proxy HttpRequest (and HttpContent, if chunked)
* @param httpObject
* Client to Proxy HttpRequest (and HttpContent, if chunked)
* @return if you want to interrupted processing and return a response to
* the client, return it here, otherwise return null to continue
* processing as usual
Expand All @@ -83,7 +83,8 @@ public interface HttpFilters {
/**
* Filters requests on their way from the proxy to the server.
*
* @param httpObject Proxy to Server HttpRequest (and HttpContent, if chunked)
* @param httpObject
* Proxy to Server HttpRequest (and HttpContent, if chunked)
* @return if you want to interrupted processing and return a response to
* the client, return it here, otherwise return null to continue
* processing as usual
Expand All @@ -103,7 +104,8 @@ public interface HttpFilters {
/**
* Filters responses on their way from the server to the proxy.
*
* @param httpObject Server to Proxy HttpResponse (and HttpContent, if chunked)
* @param httpObject
* Server to Proxy HttpResponse (and HttpContent, if chunked)
* @return the modified (or unmodified) HttpObject. Returning null will
* force a disconnect.
*/
Expand All @@ -122,7 +124,8 @@ public interface HttpFilters {
/**
* Filters responses on their way from the proxy to the client.
*
* @param httpObject Proxy to Client HttpResponse (and HttpContent, if chunked)
* @param httpObject
* Proxy to Client HttpResponse (and HttpContent, if chunked)
* @return the modified (or unmodified) HttpObject. Returning null will
* force a disconnect.
*/
Expand All @@ -135,20 +138,25 @@ public interface HttpFilters {

/**
* Filter DNS resolution from proxy to server.
*
* @param resolvingServerHostAndPort Server "HOST:PORT"
* @return alternative address resolution. Returning null will let
* normal DNS resolution continue.
*
* @param resolvingServerHostAndPort
* Server "HOST:PORT"
* @return alternative address resolution. Returning null will let normal
* DNS resolution continue.
*/
InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort);
InetSocketAddress proxyToServerResolutionStarted(
String resolvingServerHostAndPort);

/**
* Informs filter that proxy to server DNS resolution has happened.
*
* @param serverHostAndPort Server "HOST:PORT"
* @param resolvedRemoteAddress Address it was proxyToServerResolutionSucceeded to
*
* @param serverHostAndPort
* Server "HOST:PORT"
* @param resolvedRemoteAddress
* Address it was proxyToServerResolutionSucceeded to
*/
void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress);
void proxyToServerResolutionSucceeded(String serverHostAndPort,
InetSocketAddress resolvedRemoteAddress);

/**
* Informs filter that proxy to server connection is initiating.
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/littleshoot/proxy/HttpFiltersAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ public void proxyToServerConnectionQueued() {
}

@Override
public InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort) {
public InetSocketAddress proxyToServerResolutionStarted(
String resolvingServerHostAndPort) {
return null;
}

@Override
public void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress) {
public void proxyToServerResolutionSucceeded(String serverHostAndPort,
InetSocketAddress resolvedRemoteAddress) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class ClientToProxyConnection extends ProxyConnection<HttpRequest> {
* Tracks whether or not this ClientToProxyConnection is current doing MITM.
*/
private volatile boolean mitming = false;

private AtomicBoolean authenticated = new AtomicBoolean();

ClientToProxyConnection(
Expand Down Expand Up @@ -177,7 +177,8 @@ private ConnectionState doReadHTTPInitial(HttpRequest httpRequest) {
originalRequest, ctx);

// Do the pre filtering
if (shortCircuitRespond(currentFilters.clientToProxyRequest(httpRequest))) {
if (shortCircuitRespond(currentFilters
.clientToProxyRequest(httpRequest))) {
return DISCONNECT_REQUESTED;
}

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

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

Expand Down Expand Up @@ -577,7 +579,7 @@ synchronized protected void serverBecameWriteable(
protected void exceptionCaught(Throwable cause) {
String message = "Caught an exception on ClientToProxyConnection";
boolean shouldWarn = cause instanceof ClosedChannelException ||
cause.getMessage().contains("Connection reset by peer");
cause.getMessage().contains("Connection reset by peer");
if (shouldWarn) {
LOG.warn(message, cause);
} else {
Expand Down Expand Up @@ -658,7 +660,7 @@ private void closeConnectionsAfterWriteIfNecessary(
disconnect();
}
}

private void forceDisconnect(ProxyToServerConnection serverConnection) {
LOG.debug("Forcing disconnect");
serverConnection.disconnect();
Expand Down Expand Up @@ -785,14 +787,16 @@ private boolean shouldCloseServerConnection(HttpRequest req,
* @return
*/
private boolean authenticationRequired(HttpRequest request) {

if (authenticated.get()) {
return false;
}

final ProxyAuthenticator authenticator = proxyServer.getProxyAuthenticator();

if (authenticator == null) return false;
final ProxyAuthenticator authenticator = proxyServer
.getProxyAuthenticator();

if (authenticator == null)
return false;

if (!request.headers().contains(HttpHeaders.Names.PROXY_AUTHORIZATION)) {
writeAuthenticationRequired();
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/littleshoot/proxy/impl/ProxyConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,12 @@ protected void resumeReading() {

/**
* Request the ProxyServer for Filters.
*
*
* By default, no-op filters are returned by DefaultHttpProxyServer.
* Subclasses of ProxyConnection can change this behaviour.
*
* @param httpRequest Filter attached to the give HttpRequest (if any)
*
* @param httpRequest
* Filter attached to the give HttpRequest (if any)
* @return
*/
protected HttpFilters getHttpFiltersFromProxyServer(HttpRequest httpRequest) {
Expand Down Expand Up @@ -770,15 +771,15 @@ public void write(ChannelHandlerContext ctx,
LOG.warn("Unable to record bytesRead", t);
} finally {
if (null != originalRequest) {
getHttpFiltersFromProxyServer(originalRequest)
.proxyToServerRequestSending();
getHttpFiltersFromProxyServer(originalRequest)
.proxyToServerRequestSending();
}

super.write(ctx, msg, promise);

if (null != originalRequest) {
getHttpFiltersFromProxyServer(originalRequest)
.proxyToServerRequestSent();
getHttpFiltersFromProxyServer(originalRequest)
.proxyToServerRequestSent();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ protected void become(ConnectionState newState) {
currentFilters.proxyToServerConnectionFailed();
}
} else if (getCurrentState() == HANDSHAKING
&& newState == AWAITING_INITIAL) {
&& newState == AWAITING_INITIAL) {
currentFilters.proxyToServerConnectionSucceeded();
} else if (getCurrentState() == AWAITING_CHUNK
&& newState != AWAITING_CHUNK) {
&& newState != AWAITING_CHUNK) {
currentFilters.serverToProxyResponseReceived();
}

Expand Down Expand Up @@ -420,7 +420,7 @@ public HttpRequest getInitialRequest() {

@Override
protected HttpFilters getHttpFiltersFromProxyServer(HttpRequest httpRequest) {
return currentFilters;
return currentFilters;
}

/***************************************************************************
Expand Down Expand Up @@ -705,11 +705,13 @@ private void setupConnectionParameters() throws UnknownHostException {
this.transportProtocol = TransportProtocol.TCP;

// Report DNS resolution to HttpFilters
this.remoteAddress = this.currentFilters.proxyToServerResolutionStarted(serverHostAndPort);
this.remoteAddress = this.currentFilters
.proxyToServerResolutionStarted(serverHostAndPort);
if (this.remoteAddress == null) {
this.remoteAddress = addressFor(serverHostAndPort, proxyServer);
this.remoteAddress = addressFor(serverHostAndPort, proxyServer);
}
this.currentFilters.proxyToServerResolutionSucceeded(serverHostAndPort, this.remoteAddress);
this.currentFilters.proxyToServerResolutionSucceeded(
serverHostAndPort, this.remoteAddress);

this.localAddress = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/littleshoot/proxy/EndToEndStoppingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void testWithHttpClient() throws Exception {
private void runSiteTestWithHttpClient(final String site) throws Exception {
final int PROXY_PORT = 9097;
final HttpClient client = TestUtils.createProxiedHttpClient(PROXY_PORT);

// final HttpPost get = new HttpPost(site);
final HttpGet get = new HttpGet(site);
// HttpResponse response = client.execute(get);
Expand All @@ -106,7 +106,7 @@ public HttpFilters filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public io.netty.handler.codec.http.HttpResponse proxyToServerRequest(
HttpObject httpObject) {
HttpObject httpObject) {
System.out
.println("Request with through proxy");
return null;
Expand Down
Loading

0 comments on commit d87b794

Please sign in to comment.