int port = 443;
if (hostPort.length > 1) {
port = Integer.parseInt(hostPort[1]);
}
if (log.isLoggable(Level.FINE)) {
HttpChannel server = serverHttpReq.getHttpChannel();
log.info("NEW: " + server.getId() + " " + dstHost + " " +
server.getRequest().getMethod() +
" " + server.getRequest().getRequestURI() + " " +
server.getIn());
}
try {
getSelector().connect(host, port,
new ProxyConnectClientConnection(serverHttpReq.getHttpChannel()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
CBuffer origURIx = serverHttpReq.requestURI();
// String origURI = origURIx.toString();
// if (origURI.startsWith("http://")) {
// // Real proxy - extract client address, modify the uri.
// // TODO: optimize the strings.
// int start = origURI.indexOf('/', 7);
// String hostPortS = (start == -1) ?
// origURI.subSequence(7, origURI.length()).toString() :
// origURI.subSequence(7, start).toString();
// String[] hostPort = hostPortS.split(":");
//
// dstHost = hostPort[0];
// dstPort = (hostPort.length > 1) ? Integer.parseInt(hostPort[1]) :
// 80;
//
// if (start >= 0) {
// serverHttpReq.requestURI().set(origURI.substring(start));
// } else {
// serverHttpReq.requestURI().set("/");
// }
// } else {
// Adjust the host header.
CBuffer hostHdr =
serverHttpReq.getMimeHeaders().getHeader("host");
if (hostHdr != null) {
hostHdr.recycle();
CBuffer cb = hostHdr;
cb.append(dstHost);
if (dstPort != 80) {
cb.append(':');
cb.appendInt(dstPort);
}
}
// }
if (debug) {
HttpChannel server = serverHttpReq.getHttpChannel();
log.info("START: " + server.getId() + " " + dstHost + " " +
server.getRequest().getMethod() +
" " + server.getRequest().getRequestURI() + " " +
server.getIn());
}
// Send the request with a non-blocking write
HttpChannel serverHttp = serverHttpReq.getHttpChannel();
// Client connection
HttpChannel httpClient = getHttpConnector().get(dstHost, dstPort);
serverHttp.getRequest().setAttribute("CLIENT", httpClient);
httpClient.getRequest().setAttribute("SERVER", serverHttp);
serverHttp.getRequest().setAttribute("P", httpClient);
httpClient.getRequest().setAttribute("P", serverHttp);
httpClient.setHttpService(clientHeadersReceived);
// Will send the original request (TODO: small changes)
// Response is not affected ( we use the callback )
httpClient.getRequest().method().set(serverHttp.getRequest().method());
httpClient.getRequest().requestURI().set(serverHttp.getRequest().requestURI());
if (serverHttp.getRequest().queryString().length() != 0) {
httpClient.getRequest().queryString().set(serverHttp.getRequest().queryString());
}
httpClient.getRequest().protocol().set(serverHttp.getRequest().protocol());
//cstate.reqHeaders.addValue(name)
copyHeaders(serverHttp.getRequest().getMimeHeaders(),
httpClient.getRequest().getMimeHeaders() /*dest*/);
// For debug
httpClient.getRequest().getMimeHeaders().remove("Accept-Encoding");
if (!keepOpen) {
httpClient.getRequest().getMimeHeaders().setValue("Connection").set("Close");
}
// Any data
serverHttp.setDataReceivedCallback(copy);
copy.handleReceived(serverHttp);
httpClient.send();
//serverHttp.handleReceived(serverHttp.getSink());
//httpClient.flush(); // send any data still there
httpClient.setCompletedCallback(done);
// Will call release()
serverHttp.setCompletedCallback(done);
serverHttpReq.async();
}