Reputation: 73
i'm in mid of implementing a proxy using apache's HttpCore .
This is my HttpRequestHandler it is based on reverse proxy exampleby apache link :
static class ProxyHandler implements HttpRequestHandler {
private final HttpHost target;
private final HttpProcessor httpproc;
private final HttpRequestExecutor httpexecutor;
private final ConnectionReuseStrategy connStrategy;
private SocketFactory sf;
public ProxyHandler(
final HttpHost target,
final HttpProcessor httpproc,
final HttpRequestExecutor httpexecutor,
SocketFactory sf) {
super();
this.target = target;
this.httpproc = httpproc;
this.httpexecutor = httpexecutor;
this.sf = sf;
this.connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
}
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final int bufsize = 8 * 1024;
URI uri = null;
try {
uri = new URI(request.getRequestLine().getUri());
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
String domain = uri.getHost();
HttpHost host = new HttpHost(domain, 80);
System.out.println("host "+domain);
DefaultBHttpClientConnection outconn = (DefaultBHttpClientConnection) context.getAttribute(HTTP_OUT_CONN);
final Socket outsocket = sf.createSocket(host.getHostName(), this.target.getPort());
outconn = new DefaultBHttpClientConnection(bufsize);
outconn.bind(outsocket);
System.out.println("My Outgoing connection to " + outsocket.getInetAddress());
context.setAttribute(HTTP_OUT_CONN, outconn);
final HttpClientConnection conn = (HttpClientConnection) context.getAttribute(
HTTP_OUT_CONN);
context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
System.out.println(">> Request URI: " + request.getRequestLine().getUri());
// Remove hop-by-hop headers
request.removeHeaders(HTTP.CONTENT_LEN);
request.removeHeaders(HTTP.TRANSFER_ENCODING);
request.removeHeaders(HTTP.CONN_DIRECTIVE);
request.removeHeaders("Keep-Alive");
request.removeHeaders("Proxy-Authenticate");
request.removeHeaders("TE");
request.removeHeaders("Trailers");
request.removeHeaders("Upgrade");
this.httpexecutor.preProcess(request, this.httpproc, context);
final HttpResponse targetResponse = this.httpexecutor.execute(request, conn, context);
this.httpexecutor.postProcess(response, this.httpproc, context);
// Remove hop-by-hop headers
targetResponse.removeHeaders(HTTP.CONTENT_LEN);
targetResponse.removeHeaders(HTTP.TRANSFER_ENCODING);
targetResponse.removeHeaders(HTTP.CONN_DIRECTIVE);
targetResponse.removeHeaders("Keep-Alive");
targetResponse.removeHeaders("TE");
targetResponse.removeHeaders("Trailers");
targetResponse.removeHeaders("Upgrade");
response.setStatusLine(targetResponse.getStatusLine());
response.setHeaders(targetResponse.getAllHeaders());
response.setEntity(targetResponse.getEntity());
System.out.println("<< Response: " + response.getStatusLine());
final boolean keepalive = this.connStrategy.keepAlive(response, context);
context.setAttribute(HTTP_CONN_KEEPALIVE, new Boolean(keepalive));
outsocket.close();
}
}
i have changed the handler function and in it i create ,
DefaultBHttpClientConnection outconn based on the host in request URI.
As you can see i create a new socket (outsocket) using a SocketFactory .
the problem i don't know how to close the socket .
i tried closing it at the end of handle() but then i would start receiving
I/O error: Socket closed
and the proxy stops working .
The Thread that calls the httpservice.handleRequest() is identical to the one in the example in the link (i can post it if needed, i didn't cause it would take space in the question ).
Upvotes: 0
Views: 238
Reputation: 73
I've solved the problem. it was the line :
outconn = new DefaultBHttpClientConnection(bufsize);
inside the handler.
so every time the handler gets called it creates a new outconn . and the one thats setup before the call to :
httpservice.handleRequest(this.inconn, context);
even if it gets shutdown it won't close the socket since it is binded to a different copy of the outconn .
Upvotes: 1
Reputation: 35106
If you are making a proxy, the only time you should be closing connections is if you get an IOException on one side which closes that half of the conversation; at that point, it's safe to close the other side of the conversation
Upvotes: 0