001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.client.protocol; 029 030import java.io.IOException; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.apache.http.HttpException; 035import org.apache.http.HttpRequest; 036import org.apache.http.HttpRequestInterceptor; 037import org.apache.http.annotation.Contract; 038import org.apache.http.annotation.ThreadingBehavior; 039import org.apache.http.conn.routing.RouteInfo; 040import org.apache.http.protocol.HTTP; 041import org.apache.http.protocol.HttpContext; 042import org.apache.http.util.Args; 043 044/** 045 * This protocol interceptor is responsible for adding {@code Connection} 046 * or {@code Proxy-Connection} headers to the outgoing requests, which 047 * is essential for managing persistence of {@code HTTP/1.0} connections. 048 * 049 * @since 4.0 050 */ 051@Contract(threading = ThreadingBehavior.IMMUTABLE) 052public class RequestClientConnControl implements HttpRequestInterceptor { 053 054 private final Log log = LogFactory.getLog(getClass()); 055 056 private static final String PROXY_CONN_DIRECTIVE = "Proxy-Connection"; 057 058 public RequestClientConnControl() { 059 super(); 060 } 061 062 @Override 063 public void process(final HttpRequest request, final HttpContext context) 064 throws HttpException, IOException { 065 Args.notNull(request, "HTTP request"); 066 067 final String method = request.getRequestLine().getMethod(); 068 if (method.equalsIgnoreCase("CONNECT")) { 069 request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); 070 return; 071 } 072 073 final HttpClientContext clientContext = HttpClientContext.adapt(context); 074 075 // Obtain the client connection (required) 076 final RouteInfo route = clientContext.getHttpRoute(); 077 if (route == null) { 078 this.log.debug("Connection route not set in the context"); 079 return; 080 } 081 082 if (route.getHopCount() == 1 || route.isTunnelled()) { 083 if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) { 084 request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); 085 } 086 } 087 if (route.getHopCount() == 2 && !route.isTunnelled()) { 088 if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) { 089 request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); 090 } 091 } 092 } 093 094}