use of org.eclipse.jetty.server.HttpTransport in project jetty.project by eclipse.
the class ConnectHandler method handleConnect.
/**
* <p>Handles a CONNECT request.</p>
* <p>CONNECT requests may have authentication headers such as {@code Proxy-Authorization}
* that authenticate the client with the proxy.</p>
*
* @param baseRequest Jetty-specific http request
* @param request the http request
* @param response the http response
* @param serverAddress the remote server address in the form {@code host:port}
*/
protected void handleConnect(Request baseRequest, HttpServletRequest request, HttpServletResponse response, String serverAddress) {
baseRequest.setHandled(true);
try {
boolean proceed = handleAuthentication(request, response, serverAddress);
if (!proceed) {
if (LOG.isDebugEnabled())
LOG.debug("Missing proxy authentication");
sendConnectResponse(request, response, HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
return;
}
HostPort hostPort = new HostPort(serverAddress);
String host = hostPort.getHost();
int port = hostPort.getPort(80);
if (!validateDestination(host, port)) {
if (LOG.isDebugEnabled())
LOG.debug("Destination {}:{} forbidden", host, port);
sendConnectResponse(request, response, HttpServletResponse.SC_FORBIDDEN);
return;
}
HttpTransport transport = baseRequest.getHttpChannel().getHttpTransport();
// TODO Handle CONNECT over HTTP2!
if (!(transport instanceof HttpConnection)) {
if (LOG.isDebugEnabled())
LOG.debug("CONNECT not supported for {}", transport);
sendConnectResponse(request, response, HttpServletResponse.SC_FORBIDDEN);
return;
}
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
if (LOG.isDebugEnabled())
LOG.debug("Connecting to {}:{}", host, port);
connectToServer(request, host, port, new Promise<SocketChannel>() {
@Override
public void succeeded(SocketChannel channel) {
ConnectContext connectContext = new ConnectContext(request, response, asyncContext, (HttpConnection) transport);
if (channel.isConnected())
selector.accept(channel, connectContext);
else
selector.connect(channel, connectContext);
}
@Override
public void failed(Throwable x) {
onConnectFailure(request, response, asyncContext, x);
}
});
} catch (Exception x) {
onConnectFailure(request, response, null, x);
}
}
Aggregations