Skip to content

Commit

Permalink
Enhanced filtering API for adamfisk#77 and adamfisk#78
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Aug 30, 2013
1 parent 0463ade commit ef2851b
Show file tree
Hide file tree
Showing 16 changed files with 477 additions and 334 deletions.
45 changes: 34 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,47 @@ HttpProxyServer server =
.start();
```

There are lots of filters and such you can also add to LittleProxy. You can add request and reponse filters, for example, as in:
There are lots of filters and such you can also add to LittleProxy. You can add
request and response filters using an `HttpFiltersSource(Adapter)`, for example:

```
HttpProxyServer server =
DefaultHttpProxyServer.bootstrap()
.withPort(8080)
.withRequestFilter(new DefaultHttpProxyServer(PROXY_PORT, new HttpRequestFilter() {
@Override
public void filter(HttpRequest httpRequest) {
System.out.println("Request went through proxy");
}
})
.withResponseFilters(new HttpResponseFilters() {
@Override
public HttpFilter getFilter(String hostAndPort) {
.withFiltersSource(new HttpFiltersSourceAdapter() {
public HttpFilters filterRequest(HttpRequest originalRequest) {
// Check the originalRequest to see if we want to filter it
boolean wantToFilterRequest = ...;
if (!wantToFilterRequest) {
return null;
} else {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse requestPre(HttpObject httpObject) {
// TODO: implement your filtering here
return null;
}
@Override
public HttpResponse requestPost(HttpObject httpObject) {
// TODO: implement your filtering here
return null;
}
@Override
public void responsePre(HttpObject httpObject) {
// TODO: implement your filtering here
}
@Override
public void responsePost(HttpObject httpObject) {
// TODO: implement your filtering here
}
};
}
})
}
});
.start();
```

Expand Down
31 changes: 0 additions & 31 deletions src/main/java/org/littleshoot/proxy/HttpFilter.java

This file was deleted.

86 changes: 86 additions & 0 deletions src/main/java/org/littleshoot/proxy/HttpFilters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.littleshoot.proxy;

import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;

import org.littleshoot.proxy.impl.ProxyUtils;

/**
* <p>
* Interface for objects that filter {@link HttpObject}s, including both
* requests and responses.
* </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.
* </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
* {@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>
* {@link HttpFiltersSource#getMaximumRequestBufferSizeInBytes()} and
* {@link HttpFiltersSource#getMaximumResponseBufferSizeInBytes()} can be
* used to instruct the proxy to buffer the {@link HttpObject}s sent to all of
* its request/response filters, in which case it will buffer up to the
* specified limit and then send either complete {@link HttpRequest}s or
* {@link HttpResponse}s to the filter methods. When buffering, if the proxy
* receives more data than fits in the specified maximum bytes to buffer, the
* proxy will stop processing the request and respond with a 502 Bad Gateway
* error.
* </p>
*
* <p>
* A new instance of {@link HttpFilters} is created for each request, so these
* objects can be stateful.
* </p>
*
*/
public interface HttpFilters {
/**
* Filters requests on their way from the client to the proxy.
*
* @param httpObject
* @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
*/
HttpResponse requestPre(HttpObject httpObject);

/**
* Filters requests on their way from the proxy to the server.
*
* @param httpObject
* @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
*/
HttpResponse requestPost(HttpObject httpObject);

/**
* Filters responses on their way from the server to the proxy.
*
* @param httpObject
* @return
*/
void responsePre(HttpObject httpObject);

/**
* Filters responses on their way from the proxy to the client.
*
* @param httpObject
* @return
*/
void responsePost(HttpObject httpObject);
}
36 changes: 36 additions & 0 deletions src/main/java/org/littleshoot/proxy/HttpFiltersAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.littleshoot.proxy;

import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;

/**
* Convenience base class for implementations of {@link HttpFilters}.
*/
public class HttpFiltersAdapter implements HttpFilters {
protected final HttpRequest originalRequest;

public HttpFiltersAdapter(HttpRequest originalRequest) {
super();
this.originalRequest = originalRequest;
}

@Override
public HttpResponse requestPre(HttpObject httpObject) {
return null;
}

@Override
public HttpResponse requestPost(HttpObject httpObject) {
return null;
}

@Override
public void responsePre(HttpObject httpObject) {
}

@Override
public void responsePost(HttpObject httpObject) {
}

}
39 changes: 39 additions & 0 deletions src/main/java/org/littleshoot/proxy/HttpFiltersSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.littleshoot.proxy;

import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;

/**
* Factory for {@link HttpFilters}.
*/
public interface HttpFiltersSource {
/**
* Return an {@link HttpFilters} object for this request if and only if we
* want to filter the request and/or its responses.
*
* @param originalRequest
* @return
*/
HttpFilters filterRequest(HttpRequest originalRequest);

/**
* Indicate how many (if any) bytes to buffer for incoming
* {@link HttpRequest}s. A value of 0 or less indicates that no buffering
* should happen and that messages will be passed to the request filters'
* {@link #filter(HttpObject)} methods chunk by chunk.
*
* @return
*/
int getMaximumRequestBufferSizeInBytes();

/**
* Indicate how many (if any) bytes to buffer for incoming
* {@link HttpResponse}s. A value of 0 or less indicates that no buffering
* should happen and that messages will be passed to the response filters'
* {@link #filter(HttpObject)} methods chunk by chunk.
*
* @return
*/
int getMaximumResponseBufferSizeInBytes();
}
25 changes: 25 additions & 0 deletions src/main/java/org/littleshoot/proxy/HttpFiltersSourceAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.littleshoot.proxy;

import io.netty.handler.codec.http.HttpRequest;

/**
* Convenience base class for implementations of {@link HttpFiltersSource}.
*/
public class HttpFiltersSourceAdapter implements HttpFiltersSource {

@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest);
}

@Override
public int getMaximumRequestBufferSizeInBytes() {
return 0;
}

@Override
public int getMaximumResponseBufferSizeInBytes() {
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ public abstract HttpProxyServerBootstrap withProxyAuthenticator(
public abstract HttpProxyServerBootstrap withChainProxyManager(
ChainedProxyManager chainProxyManager);

public abstract HttpProxyServerBootstrap withRequestFilter(
HttpRequestFilter requestFilter);

public abstract HttpProxyServerBootstrap withResponseFilters(
HttpResponseFilters responseFilters);
public abstract HttpProxyServerBootstrap withFiltersSource(
HttpFiltersSource filtersSource);

public abstract HttpProxyServerBootstrap withUseDnsSec(
boolean useDnsSec);
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/org/littleshoot/proxy/HttpRequestFilter.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/org/littleshoot/proxy/HttpRequestMatcher.java

This file was deleted.

16 changes: 0 additions & 16 deletions src/main/java/org/littleshoot/proxy/HttpResponseFilters.java

This file was deleted.

Loading

0 comments on commit ef2851b

Please sign in to comment.