HttpMethod method = forceConnect ? HttpMethod.CONNECT : HttpMethod.valueOf(request.getMethod());
HttpVersion httpVersion = method == HttpMethod.CONNECT ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1;
String requestUri = requestUri(uri, proxyServer, method);
NettyBody body = body(request, method);
HttpRequest httpRequest;
NettyRequest nettyRequest;
if (body instanceof NettyByteArrayBody) {
byte[] bytes = NettyByteArrayBody.class.cast(body).getBytes();
httpRequest = new DefaultFullHttpRequest(httpVersion, method, requestUri, Unpooled.wrappedBuffer(bytes));
// body is passed as null as it's written directly with the request
nettyRequest = new NettyRequest(httpRequest, null);
} else if (body == null) {
httpRequest = new DefaultFullHttpRequest(httpVersion, method, requestUri);
nettyRequest = new NettyRequest(httpRequest, null);
} else {
httpRequest = new DefaultHttpRequest(httpVersion, method, requestUri);
nettyRequest = new NettyRequest(httpRequest, body);
}
HttpHeaders headers = httpRequest.headers();
if (method != HttpMethod.CONNECT) {
// assign headers as configured on request
for (Entry<String, List<String>> header : request.getHeaders()) {
headers.set(header.getKey(), header.getValue());
}
if (isNonEmpty(request.getCookies()))
headers.set(HttpHeaders.Names.COOKIE, CookieEncoder.encode(request.getCookies()));
if (config.isCompressionEnforced() && !headers.contains(HttpHeaders.Names.ACCEPT_ENCODING))
headers.set(HttpHeaders.Names.ACCEPT_ENCODING, GZIP_DEFLATE);
}
if (body != null) {
if (body.getContentLength() < 0)
headers.set(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
else
headers.set(HttpHeaders.Names.CONTENT_LENGTH, body.getContentLength());
if (body.getContentType() != null)
headers.set(HttpHeaders.Names.CONTENT_TYPE, body.getContentType());
}
// connection header and friends
boolean webSocket = isWebSocket(uri.getScheme());
if (method != HttpMethod.CONNECT && webSocket) {