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.hc.core5.http.protocol;
29  
30  import java.io.IOException;
31  import java.util.Iterator;
32  
33  import org.apache.hc.core5.annotation.Contract;
34  import org.apache.hc.core5.annotation.ThreadingBehavior;
35  import org.apache.hc.core5.http.EntityDetails;
36  import org.apache.hc.core5.http.HeaderElement;
37  import org.apache.hc.core5.http.HeaderElements;
38  import org.apache.hc.core5.http.HttpException;
39  import org.apache.hc.core5.http.HttpHeaders;
40  import org.apache.hc.core5.http.HttpRequest;
41  import org.apache.hc.core5.http.HttpResponse;
42  import org.apache.hc.core5.http.HttpResponseInterceptor;
43  import org.apache.hc.core5.http.HttpStatus;
44  import org.apache.hc.core5.http.HttpVersion;
45  import org.apache.hc.core5.http.ProtocolVersion;
46  import org.apache.hc.core5.http.message.MessageSupport;
47  import org.apache.hc.core5.util.Args;
48  
49  /**
50   * This response interceptor is responsible for adding {@code Connection} header
51   * to outgoing responses, which is essential for managing persistence of
52   * {@code HTTP/1.0} connections.
53   * <p>
54   * This interceptor is recommended for the HTTP protocol conformance and
55   * the correct operation of the server-side message processing pipeline.
56   * </p>
57   *
58   * @since 4.0
59   */
60  @Contract(threading = ThreadingBehavior.IMMUTABLE)
61  public class ResponseConnControl implements HttpResponseInterceptor {
62  
63      public static final ResponseConnControl INSTANCE = new ResponseConnControl();
64  
65      public ResponseConnControl() {
66          super();
67      }
68  
69      @Override
70      public void process(final HttpResponse response, final EntityDetails entity, final HttpContext context)
71              throws HttpException, IOException {
72          Args.notNull(response, "HTTP response");
73          Args.notNull(context, "HTTP context");
74  
75          // Always drop connection after certain type of responses
76          final int status = response.getCode();
77          if (status == HttpStatus.SC_BAD_REQUEST ||
78                  status == HttpStatus.SC_REQUEST_TIMEOUT ||
79                  status == HttpStatus.SC_LENGTH_REQUIRED ||
80                  status == HttpStatus.SC_REQUEST_TOO_LONG ||
81                  status == HttpStatus.SC_REQUEST_URI_TOO_LONG ||
82                  status == HttpStatus.SC_SERVICE_UNAVAILABLE ||
83                  status == HttpStatus.SC_NOT_IMPLEMENTED) {
84              response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
85              return;
86          }
87          if (!response.containsHeader(HttpHeaders.CONNECTION)) {
88              // Always drop connection for HTTP/1.0 responses and below
89              // if the content body cannot be correctly delimited
90              final ProtocolVersion ver = context.getProtocolVersion();
91              if (entity != null && entity.getContentLength() < 0 && ver.lessEquals(HttpVersion.HTTP_1_0)) {
92                  response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
93              } else {
94                  final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
95                  final HttpRequest request = coreContext.getRequest();
96                  boolean closeRequested = false;
97                  boolean keepAliveRequested = false;
98                  if (request != null) {
99                      final Iterator<HeaderElement> it = MessageSupport.iterate(request, HttpHeaders.CONNECTION);
100                     while (it.hasNext()) {
101                         final HeaderElement he = it.next();
102                         if (he.getName().equalsIgnoreCase(HeaderElements.CLOSE)) {
103                             closeRequested = true;
104                             break;
105                         } else if (he.getName().equalsIgnoreCase(HeaderElements.KEEP_ALIVE)) {
106                             keepAliveRequested = true;
107                         }
108                     }
109                 }
110                 if (closeRequested) {
111                     response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
112                 } else {
113                     if (response.containsHeader(HttpHeaders.UPGRADE)) {
114                         response.addHeader(HttpHeaders.CONNECTION, HeaderElements.UPGRADE);
115                     } else {
116                         if (keepAliveRequested) {
117                             response.addHeader(HttpHeaders.CONNECTION, HeaderElements.KEEP_ALIVE);
118                         } else {
119                             if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
120                                 response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
121                             }
122                         }
123                     }
124                 }
125             }
126         }
127     }
128 
129 }