} 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) {
headers.set(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET)//
.set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE)//
.set(HttpHeaders.Names.ORIGIN, "http://" + uri.getHost() + ":" + (uri.getPort() == -1 ? isSecure(uri.getScheme()) ? 443 : 80 : uri.getPort()))//
.set(HttpHeaders.Names.SEC_WEBSOCKET_KEY, getKey())//
.set(HttpHeaders.Names.SEC_WEBSOCKET_VERSION, "13");
} else if (!headers.contains(HttpHeaders.Names.CONNECTION)) {
headers.set(HttpHeaders.Names.CONNECTION, keepAliveHeaderValue(config));
}
if (!headers.contains(HttpHeaders.Names.HOST))
headers.set(HttpHeaders.Names.HOST, hostHeader(request, uri));
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
// don't override authorization but append
addAuthorizationHeader(headers, systematicAuthorizationHeader(request, uri, proxyServer, realm));
setProxyAuthorizationHeader(headers, systematicProxyAuthorizationHeader(request, proxyServer, method));
// Add default accept headers
if (!headers.contains(HttpHeaders.Names.ACCEPT))
headers.set(HttpHeaders.Names.ACCEPT, "*/*");
// Add default user agent
if (!headers.contains(HttpHeaders.Names.USER_AGENT) && config.getUserAgent() != null)
headers.set(HttpHeaders.Names.USER_AGENT, config.getUserAgent());
return nettyRequest;
}