1 /* 2 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/protocol/ResponseConnControl.java $ 3 * $Revision: 744532 $ 4 * $Date: 2009-02-14 18:12:18 +0100 (Sat, 14 Feb 2009) $ 5 * 6 * ==================================================================== 7 * Licensed to the Apache Software Foundation (ASF) under one 8 * or more contributor license agreements. See the NOTICE file 9 * distributed with this work for additional information 10 * regarding copyright ownership. The ASF licenses this file 11 * to you under the Apache License, Version 2.0 (the 12 * "License"); you may not use this file except in compliance 13 * with the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, 18 * software distributed under the License is distributed on an 19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20 * KIND, either express or implied. See the License for the 21 * specific language governing permissions and limitations 22 * under the License. 23 * ==================================================================== 24 * 25 * This software consists of voluntary contributions made by many 26 * individuals on behalf of the Apache Software Foundation. For more 27 * information on the Apache Software Foundation, please see 28 * <http://www.apache.org/>. 29 * 30 */ 31 32 package org.apache.http.protocol; 33 34 import java.io.IOException; 35 36 import org.apache.http.Header; 37 import org.apache.http.HttpEntity; 38 import org.apache.http.HttpException; 39 import org.apache.http.HttpRequest; 40 import org.apache.http.HttpResponse; 41 import org.apache.http.HttpResponseInterceptor; 42 import org.apache.http.HttpStatus; 43 import org.apache.http.HttpVersion; 44 import org.apache.http.ProtocolVersion; 45 46 /** 47 * ResponseConnControl is responsible for adding <code>Connection</code> header 48 * to the outgoing responses, which is essential for managing persistence of 49 * <code>HTTP/1.0</code> connections. This interceptor is recommended for 50 * server side protocol processors. 51 * 52 * 53 * @version $Revision: 744532 $ 54 * 55 * @since 4.0 56 */ 57 public class ResponseConnControl implements HttpResponseInterceptor { 58 59 public ResponseConnControl() { 60 super(); 61 } 62 63 public void process(final HttpResponse response, final HttpContext context) 64 throws HttpException, IOException { 65 if (response == null) { 66 throw new IllegalArgumentException("HTTP response may not be null"); 67 } 68 if (context == null) { 69 throw new IllegalArgumentException("HTTP context may not be null"); 70 } 71 // Always drop connection after certain type of responses 72 int status = response.getStatusLine().getStatusCode(); 73 if (status == HttpStatus.SC_BAD_REQUEST || 74 status == HttpStatus.SC_REQUEST_TIMEOUT || 75 status == HttpStatus.SC_LENGTH_REQUIRED || 76 status == HttpStatus.SC_REQUEST_TOO_LONG || 77 status == HttpStatus.SC_REQUEST_URI_TOO_LONG || 78 status == HttpStatus.SC_SERVICE_UNAVAILABLE || 79 status == HttpStatus.SC_NOT_IMPLEMENTED) { 80 response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); 81 return; 82 } 83 // Always drop connection for HTTP/1.0 responses and below 84 // if the content body cannot be correctly delimited 85 HttpEntity entity = response.getEntity(); 86 if (entity != null) { 87 ProtocolVersion ver = response.getStatusLine().getProtocolVersion(); 88 if (entity.getContentLength() < 0 && 89 (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0))) { 90 response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); 91 return; 92 } 93 } 94 // Drop connection if requested by the client 95 HttpRequest request = (HttpRequest) 96 context.getAttribute(ExecutionContext.HTTP_REQUEST); 97 if (request != null) { 98 Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE); 99 if (header != null) { 100 response.setHeader(HTTP.CONN_DIRECTIVE, header.getValue()); 101 } 102 } 103 } 104 105 }