1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/ResponseConnControl.java $ 3 * $Revision: 618017 $ 4 * $Date: 2008-02-03 08:42:22 -0800 (Sun, 03 Feb 2008) $ 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 * A response interceptor that suggests connection keep-alive to the client. 48 * For use on the server side. 49 * 50 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 51 * 52 * @version $Revision: 618017 $ 53 * 54 * @since 4.0 55 */ 56 public class ResponseConnControl implements HttpResponseInterceptor { 57 ResponseConnControl()58 public ResponseConnControl() { 59 super(); 60 } 61 process(final HttpResponse response, final HttpContext context)62 public void process(final HttpResponse response, final HttpContext context) 63 throws HttpException, IOException { 64 if (response == null) { 65 throw new IllegalArgumentException("HTTP response may not be null"); 66 } 67 if (context == null) { 68 throw new IllegalArgumentException("HTTP context may not be null"); 69 } 70 // Always drop connection after certain type of responses 71 int status = response.getStatusLine().getStatusCode(); 72 if (status == HttpStatus.SC_BAD_REQUEST || 73 status == HttpStatus.SC_REQUEST_TIMEOUT || 74 status == HttpStatus.SC_LENGTH_REQUIRED || 75 status == HttpStatus.SC_REQUEST_TOO_LONG || 76 status == HttpStatus.SC_REQUEST_URI_TOO_LONG || 77 status == HttpStatus.SC_SERVICE_UNAVAILABLE || 78 status == HttpStatus.SC_NOT_IMPLEMENTED) { 79 response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); 80 return; 81 } 82 // Always drop connection for HTTP/1.0 responses and below 83 // if the content body cannot be correctly delimited 84 HttpEntity entity = response.getEntity(); 85 if (entity != null) { 86 ProtocolVersion ver = response.getStatusLine().getProtocolVersion(); 87 if (entity.getContentLength() < 0 && 88 (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0))) { 89 response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); 90 return; 91 } 92 } 93 // Drop connection if requested by the client 94 HttpRequest request = (HttpRequest) 95 context.getAttribute(ExecutionContext.HTTP_REQUEST); 96 if (request != null) { 97 Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE); 98 if (header != null) { 99 response.setHeader(HTTP.CONN_DIRECTIVE, header.getValue()); 100 } 101 } 102 } 103 104 } 105