View Javadoc

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