[英]Close a socket created in handle of HttpRequestHandler
i'm in mid of implementing a proxy using apache's HttpCore . 我正在使用apache的HttpCore实现代理。
This is my HttpRequestHandler it is based on reverse proxy exampleby apache link : 这是我的HttpRequestHandler,它基于apache 链接的反向代理示例:
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. 我已经更改了处理程序函数,并在其中基于请求URI中的主机创建了DefaultBHttpClientConnection outconn 。
As you can see i create a new socket ( outsocket ) using a SocketFactory . 如您所见,我使用SocketFactory创建了一个新的套接字( outsocket )。 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 我试图在handle()的末尾将其关闭,但随后我将开始接收
I/O error: Socket closed I / O错误:套接字已关闭
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 ). 调用httpservice.handleRequest()的线程与链接示例中的线程相同(我可以在需要时将其发布,我没有引起它在问题中占用空间)。
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 . 因此,每次调用处理程序时,它都会创建一个新的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 . 即使它关闭了,也不会关闭套接字,因为它绑定到了outconn的不同副本。
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; 如果您要创建代理,则唯一应该关闭连接的情况是,如果一侧出现IOException,则该异常将关闭会话的一半。 at that point, it's safe to close the other side of the conversation 在这一点上,关闭对话的另一侧是安全的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.