001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/protocol/RequestTargetHost.java $
003: * $Revision: 573864 $
004: * $Date: 2007-09-08 17:53:25 +0200 (Sat, 08 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.protocol;
033:
034: import java.io.IOException;
035: import java.net.InetAddress;
036:
037: import org.apache.http.HttpConnection;
038: import org.apache.http.HttpException;
039: import org.apache.http.HttpHost;
040: import org.apache.http.HttpInetConnection;
041: import org.apache.http.HttpRequest;
042: import org.apache.http.HttpRequestInterceptor;
043: import org.apache.http.HttpVersion;
044: import org.apache.http.ProtocolVersion;
045: import org.apache.http.ProtocolException;
046:
047: /**
048: * A request interceptor that sets the Host header for HTTP/1.1 requests.
049: *
050: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
051: *
052: * @version $Revision: 573864 $
053: *
054: * @since 4.0
055: */
056: public class RequestTargetHost implements HttpRequestInterceptor {
057:
058: public RequestTargetHost() {
059: super ();
060: }
061:
062: public void process(final HttpRequest request,
063: final HttpContext context) throws HttpException,
064: IOException {
065: if (request == null) {
066: throw new IllegalArgumentException(
067: "HTTP request may not be null");
068: }
069: if (context == null) {
070: throw new IllegalArgumentException(
071: "HTTP context may not be null");
072: }
073: if (!request.containsHeader(HTTP.TARGET_HOST)) {
074: HttpHost targethost = (HttpHost) context
075: .getAttribute(ExecutionContext.HTTP_TARGET_HOST);
076: if (targethost == null) {
077: HttpConnection conn = (HttpConnection) context
078: .getAttribute(ExecutionContext.HTTP_CONNECTION);
079: if (conn instanceof HttpInetConnection) {
080: // Populate the context with a default HTTP host based on the
081: // inet address of the target host
082: InetAddress address = ((HttpInetConnection) conn)
083: .getRemoteAddress();
084: int port = ((HttpInetConnection) conn)
085: .getRemotePort();
086: if (address != null) {
087: targethost = new HttpHost(
088: address.getHostName(), port);
089: }
090: }
091: if (targethost == null) {
092: ProtocolVersion ver = request.getRequestLine()
093: .getProtocolVersion();
094: if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
095: return;
096: } else {
097: throw new ProtocolException(
098: "Target host missing");
099: }
100: }
101: }
102: request.addHeader(HTTP.TARGET_HOST, targethost
103: .toHostString());
104: }
105: }
106:
107: }
|