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   * ResponseConnControl is responsible for adding {@code Connection} header
51   * to the outgoing responses, which is essential for managing persistence of
52   * {@code HTTP/1.0} connections. This interceptor is recommended for
53   * server side protocol processors.
54   *
55   * @since 4.0
56   */
57  @Contract(threading = ThreadingBehavior.IMMUTABLE)
58  public class ResponseConnControl implements HttpResponseInterceptor {
59  
60      public ResponseConnControl() {
61          super();
62      }
63  
64      @Override
65      public void process(final HttpResponse response, final EntityDetails entity, final HttpContext context)
66              throws HttpException, IOException {
67          Args.notNull(response, "HTTP response");
68          Args.notNull(context, "HTTP context");
69  
70          // Always drop connection after certain type of responses
71          final int status = response.getCode();
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(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
80              return;
81          }
82          if (!response.containsHeader(HttpHeaders.CONNECTION)) {
83              // Always drop connection for HTTP/1.0 responses and below
84              // if the content body cannot be correctly delimited
85              final ProtocolVersion ver = context.getProtocolVersion();
86              if (entity != null && entity.getContentLength() < 0 && ver.lessEquals(HttpVersion.HTTP_1_0)) {
87                  response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
88              } else {
89                  final HttpCoreContext coreContext = HttpCoreContext.adapt(context);
90                  final HttpRequest request = coreContext.getRequest();
91                  boolean closeRequested = false;
92                  boolean keepAliveRequested = false;
93                  if (request != null) {
94                      final Iterator<HeaderElement> it = MessageSupport.iterate(request, HttpHeaders.CONNECTION);
95                      while (it.hasNext()) {
96                          final HeaderElement he = it.next();
97                          if (he.getName().equalsIgnoreCase(HeaderElements.CLOSE)) {
98                              closeRequested = true;
99                              break;
100                         } else if (he.getName().equalsIgnoreCase(HeaderElements.KEEP_ALIVE)) {
101                             keepAliveRequested = true;
102                         }
103                     }
104                 }
105                 if (closeRequested) {
106                     response.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
107                 } else {
108                     if (response.containsHeader(HttpHeaders.UPGRADE)) {
109                         response.addHeader(HttpHeaders.CONNECTION, HeaderElements.UPGRADE);
110                     } else {
111                         if (keepAliveRequested || ver.lessEquals(HttpVersion.HTTP_1_0)) {
112                             response.addHeader(HttpHeaders.CONNECTION, HeaderElements.KEEP_ALIVE);
113                         }
114                     }
115                 }
116             }
117         }
118     }
119 
120 }