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.client5.http.protocol;
29  
30  import java.io.IOException;
31  
32  import org.apache.hc.client5.http.config.RequestConfig;
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.HttpException;
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.HttpRequest;
39  import org.apache.hc.core5.http.HttpRequestInterceptor;
40  import org.apache.hc.core5.http.Method;
41  import org.apache.hc.core5.http.ProtocolVersion;
42  import org.apache.hc.core5.http.protocol.HttpContext;
43  import org.apache.hc.core5.util.Args;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  /**
48   * @since 5.4
49   */
50  @Contract(threading = ThreadingBehavior.STATELESS)
51  public final class RequestUpgrade implements HttpRequestInterceptor {
52  
53      private static final Logger LOG = LoggerFactory.getLogger(RequestUpgrade.class);
54  
55      public RequestUpgrade() {
56      }
57  
58      @Override
59      public void process(
60              final HttpRequest request,
61              final EntityDetails entity,
62              final HttpContext context) throws HttpException, IOException {
63          Args.notNull(request, "HTTP request");
64          Args.notNull(context, "HTTP context");
65  
66          final HttpClientContext clientContext = HttpClientContext.cast(context);
67          final RequestConfig requestConfig = clientContext.getRequestConfigOrDefault();
68          if (requestConfig.isProtocolUpgradeEnabled()) {
69              final ProtocolVersion version = request.getVersion() != null ? request.getVersion() : clientContext.getProtocolVersion();
70              if (!request.containsHeader(HttpHeaders.UPGRADE) && version.getMajor() == 1 && version.getMinor() >= 1) {
71                  if (LOG.isDebugEnabled()) {
72                      LOG.debug("Connection is upgradable: protocol version = {}", version);
73                  }
74                  final String method = request.getMethod();
75                  if ((Method.OPTIONS.isSame(method) || Method.HEAD.isSame(method) || Method.GET.isSame(method)) &&
76                          clientContext.getSSLSession() == null) {
77                      LOG.debug("Connection is upgradable to TLS: method = {}", method);
78                      request.addHeader(HttpHeaders.UPGRADE, "TLS/1.2");
79                      request.addHeader(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE);
80                  }
81              }
82          }
83      }
84  
85  }