if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
if (!request.containsHeader(HTTP.TARGET_HOST)) {
HttpHost targethost = (HttpHost) context
.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (targethost == null) {
HttpConnection conn = (HttpConnection) context
.getAttribute(ExecutionContext.HTTP_CONNECTION);
if (conn instanceof HttpInetConnection) {
// Populate the context with a default HTTP host based on the
// inet address of the target host
InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
int port = ((HttpInetConnection) conn).getRemotePort();
if (address != null) {
targethost = new HttpHost(address.getHostName(), port);
}
}
if (targethost == null) {
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
} else {
throw new ProtocolException("Target host missing");
}
}
}
request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
}