if (isUpgradeRequest(httpTxContext.getHandler()) && isWebSocket) {
httpTxContext.setWSRequest(true);
convertToUpgradeRequest(httpTxContext);
}
HttpRequestPacket requestPacket = requestCache.poll();
if (requestPacket == null) {
requestPacket = new HttpRequestPacketImpl();
}
final Method method = Method.valueOf(request.getMethod());
requestPacket.setMethod(method);
requestPacket.setProtocol(Protocol.HTTP_1_1);
// Special handling for CONNECT.
if (method == Method.CONNECT) {
final int port = uri.getPort();
requestPacket.setRequestURI(uri.getHost() + ':' + (port == -1 ? 443 : port));
} else if ((secure || isWebSocket) && config.isUseRelativeURIsWithConnectProxies()) {
requestPacket.setRequestURI(getNonEmptyPath(uri));
} else {
requestPacket.setRequestURI(uri.toUrl());
}
final BodyHandler bodyHandler = isPayloadAllowed(method) ?
bodyHandlerFactory.getBodyHandler(request) :
null;
if (bodyHandler != null) {
final long contentLength = request.getContentLength();
if (contentLength >= 0) {
requestPacket.setContentLengthLong(contentLength);
requestPacket.setChunked(false);
} else {
requestPacket.setChunked(true);
}
}
if (httpTxContext.isWSRequest()) {
try {
final URI wsURI = httpTxContext.getWsRequestURI().toJavaNetURI();
httpTxContext.setProtocolHandler(Version.RFC6455.createHandler(true));
httpTxContext.setHandshake(httpTxContext.getProtocolHandler().createHandShake(wsURI));
requestPacket = (HttpRequestPacket) httpTxContext.getHandshake().composeHeaders().getHttpHeader();
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid WS URI: " + httpTxContext.getWsRequestURI());
}
}
requestPacket.setSecure(secure);
addQueryString(request, requestPacket);
addHostHeader(request, uri, requestPacket);
addGeneralHeaders(request, requestPacket);
addCookies(request, requestPacket);
addAuthorizationHeader(request, requestPacket);
initTransferCompletionHandler(request, httpTxContext.getHandler());
final HttpRequestPacket requestPacketLocal = requestPacket;
FilterChainContext sendingCtx = ctx;
if (secure) {
// Check to see if the ProtocolNegotiator has given
// us a different FilterChain to use. If so, we need
// use a different FilterChainContext when invoking sendRequest().
sendingCtx = checkAndHandleFilterChainUpdate(ctx, sendingCtx);
}
final Connection c = ctx.getConnection();
final HttpContext httpCtx;
if (!Utils.isSpdyConnection(c)) {
httpCtx = HttpContext.newInstance(c, c, c, requestPacketLocal);
} else {
SpdySession session = SpdySession.get(c);
final Lock lock = session.getNewClientStreamLock();
try {
lock.lock();
SpdyStream stream = session.openStream(requestPacketLocal, session.getNextLocalStreamId(), 0, 0, 0, false,
!requestPacketLocal.isExpectContent());
httpCtx = HttpContext.newInstance(stream, stream, stream, requestPacketLocal);
} finally {
lock.unlock();
}
}
httpCtx.attach(ctx);
HttpTxContext.set(ctx, httpTxContext);
requestPacketLocal.getProcessingState().setHttpContext(httpCtx);
requestPacketLocal.setConnection(c);
return sendRequest(sendingCtx, request, requestPacketLocal,
wrapWithExpectHandlerIfNeeded(bodyHandler, requestPacket));
}