I am setting up a MITM proxy and then planning to rewrite the URL (just the Host and Port) to forward that request to a different server.
Below is the code and I am getting Bad Request. Any help is much appreciated.
HttpProxyServer server = DefaultHttpProxyServer.bootstrap()
.withPort(9080)
.withManInTheMiddle(new SelfSignedMitmManager())
.withFiltersSource(new HttpFiltersSourceAdapter() {
@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
}
return null;
}
public HttpResponse proxyToServerRequest(HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest)httpObject;
httpRequest.headers().set(HttpHeaders.Names.HOST, "localhost:8080");
}
return null;
}
public HttpObject serverToProxyResponse(HttpObject httpObject) {
if (httpObject instanceof HttpResponse) {
} else if (httpObject instanceof HttpContent) {
}
return httpObject;
}
public HttpObject proxyToClientResponse(HttpObject httpObject) {
if (httpObject instanceof HttpResponse) {
} else if (httpObject instanceof HttpContent) {
}
return httpObject;
}
};
}
})
.start();
Below is the error from the log
[2015-04-28 10:56:50,199] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL): Configuring ChannelPipeline
[2015-04-28 10:56:50,200] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL): Created ClientToProxyConnection
[2015-04-28 10:56:50,200] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: Connected
[2015-04-28 10:56:50,200] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: Reading: DefaultHttpRequest(decodeResult: failure(java.lang.IllegalArgumentException: text is empty))
GET /bad-request HTTP/1.0
[2015-04-28 10:56:50,200] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: Got request: DefaultHttpRequest(decodeResult: failure(java.lang.IllegalArgumentException: text is empty))
GET /bad-request HTTP/1.0
[2015-04-28 10:56:50,200] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: serverHostAndPort
[2015-04-28 10:56:50,200] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: Ensuring that hostAndPort are available in /bad-request
[2015-04-28 10:56:50,200] WARN [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: No host and port found in /bad-request
[2015-04-28 10:56:50,201] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: Retaining reference counted message
[2015-04-28 10:56:50,201] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: Writing: DefaultFullHttpResponse(decodeResult: success)
HTTP/1.1 502 Bad Gateway
Content-Length: 25
Content-Type: text/html; charset=UTF-8
Connection: close
[2015-04-28 10:56:50,201] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: Writing an empty buffer to signal the end of our chunked transfer
[2015-04-28 10:56:50,201] DEBUG [oxy-ClientToProxyWorker-2] y.impl.ClientToProxyConnection -- (AWAITING_INITIAL) [id: 0x965835e1, /
127.0.0.1:53822 => /
127.0.0.1:9080]: Wrote: DefaultFullHttpResponse(decodeResult: success)
HTTP/1.1 502 Bad Gateway
Content-Length: 25
Content-Type: text/html; charset=UTF-8
Connection: close