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.protocol; 029 030import java.io.IOException; 031 032import org.apache.http.Header; 033import org.apache.http.HttpEntity; 034import org.apache.http.HttpException; 035import org.apache.http.HttpRequest; 036import org.apache.http.HttpResponse; 037import org.apache.http.HttpResponseInterceptor; 038import org.apache.http.HttpStatus; 039import org.apache.http.HttpVersion; 040import org.apache.http.ProtocolVersion; 041import org.apache.http.annotation.ThreadingBehavior; 042import org.apache.http.annotation.Contract; 043import org.apache.http.util.Args; 044 045/** 046 * ResponseConnControl is responsible for adding {@code Connection} header 047 * to the outgoing responses, which is essential for managing persistence of 048 * {@code HTTP/1.0} connections. This interceptor is recommended for 049 * server side protocol processors. 050 * 051 * @since 4.0 052 */ 053@Contract(threading = ThreadingBehavior.IMMUTABLE) 054public class ResponseConnControl implements HttpResponseInterceptor { 055 056 public ResponseConnControl() { 057 super(); 058 } 059 060 @Override 061 public void process(final HttpResponse response, final HttpContext context) 062 throws HttpException, IOException { 063 Args.notNull(response, "HTTP response"); 064 065 final HttpCoreContext corecontext = HttpCoreContext.adapt(context); 066 067 // Always drop connection after certain type of responses 068 final int status = response.getStatusLine().getStatusCode(); 069 if (status == HttpStatus.SC_BAD_REQUEST || 070 status == HttpStatus.SC_REQUEST_TIMEOUT || 071 status == HttpStatus.SC_LENGTH_REQUIRED || 072 status == HttpStatus.SC_REQUEST_TOO_LONG || 073 status == HttpStatus.SC_REQUEST_URI_TOO_LONG || 074 status == HttpStatus.SC_SERVICE_UNAVAILABLE || 075 status == HttpStatus.SC_NOT_IMPLEMENTED) { 076 response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); 077 return; 078 } 079 final Header explicit = response.getFirstHeader(HTTP.CONN_DIRECTIVE); 080 if (explicit != null && HTTP.CONN_CLOSE.equalsIgnoreCase(explicit.getValue())) { 081 // Connection persistence explicitly disabled 082 return; 083 } 084 // Always drop connection for HTTP/1.0 responses and below 085 // if the content body cannot be correctly delimited 086 final HttpEntity entity = response.getEntity(); 087 if (entity != null) { 088 final ProtocolVersion ver = response.getStatusLine().getProtocolVersion(); 089 if (entity.getContentLength() < 0 && 090 (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0))) { 091 response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); 092 return; 093 } 094 } 095 // Drop connection if requested by the client or request was <= 1.0 096 final HttpRequest request = corecontext.getRequest(); 097 if (request != null) { 098 final Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE); 099 if (header != null) { 100 response.setHeader(HTTP.CONN_DIRECTIVE, header.getValue()); 101 } else if (request.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) { 102 response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE); 103 } 104 } 105 } 106 107}