Created
August 29, 2013 20:18
-
-
Save oxtoacart/6382909 to your computer and use it in GitHub Desktop.
HttpRequestFilter is currently applied after we've determined the downstream server based on the host header. It would be neat to have the option of applying request filters on the way in, such that the host header can be modified. This test shows an example of how one might use that, provided by Marco Palladino.
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
package org.littleshoot.proxy; | |
import io.netty.handler.codec.http.HttpRequest; | |
import io.netty.handler.codec.http.HttpResponse; | |
/** | |
* <p> | |
* This is based on some code from Marco Palladino that was giving him trouble. | |
* Use the below command to test: | |
* </p> | |
* | |
* <pre> | |
* curl -i http://127.0.0.1:9000 | |
* </pre> | |
* | |
* <p> | |
* It doesn't work because by the time the filter is applied, we've already | |
* identified and connected to the host (in this case our proxy itself). This | |
* results in the request going through the proxy twice. | |
* </p> | |
*/ | |
public class AmazonRedirector { | |
public static void main(String[] args) throws Exception { | |
final HttpProxyServer server = new DefaultHttpProxyServer(9000, | |
new HttpRequestFilter() { | |
@Override | |
public void filter(HttpRequest httpRequest) { | |
System.out.println("Filtering"); | |
httpRequest.headers().set("host", "www.amazon.com"); | |
} | |
}, new HttpResponseFilters() { | |
@Override | |
public HttpFilter getFilter(String hostAndPort) { | |
return new DefaultHttpFilter(new HttpResponseFilter() { | |
@Override | |
public HttpResponse filterResponse( | |
HttpRequest arg0, HttpResponse arg1) { | |
arg1.headers().add("x-test", "yes"); | |
return arg1; | |
} | |
}, new HttpRequestMatcher() { | |
@Override | |
public boolean filterResponses(HttpRequest arg0) { | |
return true; | |
} | |
}); | |
} | |
}); | |
server.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment