forked from adamfisk/LittleProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhanced filtering API for adamfisk#77 and adamfisk#78
- Loading branch information
Showing
16 changed files
with
477 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
src/main/java/org/littleshoot/proxy/HttpFiltersAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
src/main/java/org/littleshoot/proxy/HttpFiltersSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/java/org/littleshoot/proxy/HttpFiltersSourceAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
src/main/java/org/littleshoot/proxy/HttpRequestFilter.java
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
src/main/java/org/littleshoot/proxy/HttpRequestMatcher.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/main/java/org/littleshoot/proxy/HttpResponseFilters.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.