src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestImpl.java
changeset 49765 ee6f7a61f3a5
parent 48083 b1c1b4ef4be2
child 50681 4254bed3c09d
child 56451 9585061fdb04
equal deleted inserted replaced
49707:f7fd051519ac 49765:ee6f7a61f3a5
       
     1 /*
       
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.internal.net.http;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.net.InetSocketAddress;
       
    30 import java.net.Proxy;
       
    31 import java.net.ProxySelector;
       
    32 import java.net.URI;
       
    33 import java.security.AccessControlContext;
       
    34 import java.security.AccessController;
       
    35 import java.security.PrivilegedAction;
       
    36 import java.time.Duration;
       
    37 import java.util.List;
       
    38 import java.util.Locale;
       
    39 import java.util.Objects;
       
    40 import java.util.Optional;
       
    41 import java.net.http.HttpClient;
       
    42 import java.net.http.HttpHeaders;
       
    43 import java.net.http.HttpRequest;
       
    44 import jdk.internal.net.http.common.HttpHeadersImpl;
       
    45 import jdk.internal.net.http.common.Utils;
       
    46 import jdk.internal.net.http.websocket.WebSocketRequest;
       
    47 
       
    48 import static jdk.internal.net.http.common.Utils.ALLOWED_HEADERS;
       
    49 
       
    50 class HttpRequestImpl extends HttpRequest implements WebSocketRequest {
       
    51 
       
    52     private final HttpHeaders userHeaders;
       
    53     private final HttpHeadersImpl systemHeaders;
       
    54     private final URI uri;
       
    55     private volatile Proxy proxy; // ensure safe publishing
       
    56     private final InetSocketAddress authority; // only used when URI not specified
       
    57     private final String method;
       
    58     final BodyPublisher requestPublisher;
       
    59     final boolean secure;
       
    60     final boolean expectContinue;
       
    61     private volatile boolean isWebSocket;
       
    62     private volatile AccessControlContext acc;
       
    63     private final Duration timeout;  // may be null
       
    64     private final Optional<HttpClient.Version> version;
       
    65 
       
    66     private static String userAgent() {
       
    67         PrivilegedAction<String> pa = () -> System.getProperty("java.version");
       
    68         String version = AccessController.doPrivileged(pa);
       
    69         return "Java-http-client/" + version;
       
    70     }
       
    71 
       
    72     /** The value of the User-Agent header for all requests sent by the client. */
       
    73     public static final String USER_AGENT = userAgent();
       
    74 
       
    75     /**
       
    76      * Creates an HttpRequestImpl from the given builder.
       
    77      */
       
    78     public HttpRequestImpl(HttpRequestBuilderImpl builder) {
       
    79         String method = builder.method();
       
    80         this.method = method == null ? "GET" : method;
       
    81         this.userHeaders = ImmutableHeaders.of(builder.headers().map(), ALLOWED_HEADERS);
       
    82         this.systemHeaders = new HttpHeadersImpl();
       
    83         this.uri = builder.uri();
       
    84         assert uri != null;
       
    85         this.proxy = null;
       
    86         this.expectContinue = builder.expectContinue();
       
    87         this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
       
    88         this.requestPublisher = builder.bodyPublisher();  // may be null
       
    89         this.timeout = builder.timeout();
       
    90         this.version = builder.version();
       
    91         this.authority = null;
       
    92     }
       
    93 
       
    94     /**
       
    95      * Creates an HttpRequestImpl from the given request.
       
    96      */
       
    97     public HttpRequestImpl(HttpRequest request, ProxySelector ps) {
       
    98         String method = request.method();
       
    99         if (method != null && !Utils.isValidName(method))
       
   100             throw new IllegalArgumentException("illegal method \""
       
   101                     + method.replace("\n","\\n")
       
   102                     .replace("\r", "\\r")
       
   103                     .replace("\t", "\\t")
       
   104                     + "\"");
       
   105         URI requestURI = Objects.requireNonNull(request.uri(),
       
   106                 "uri must be non null");
       
   107         Duration timeout = request.timeout().orElse(null);
       
   108         this.method = method == null ? "GET" : method;
       
   109         this.userHeaders = ImmutableHeaders.validate(request.headers());
       
   110         if (request instanceof HttpRequestImpl) {
       
   111             // all cases exception WebSocket should have a new system headers
       
   112             this.isWebSocket = ((HttpRequestImpl) request).isWebSocket;
       
   113             if (isWebSocket) {
       
   114                 this.systemHeaders = ((HttpRequestImpl) request).systemHeaders;
       
   115             } else {
       
   116                 this.systemHeaders = new HttpHeadersImpl();
       
   117             }
       
   118         } else {
       
   119             HttpRequestBuilderImpl.checkURI(requestURI);
       
   120             checkTimeout(timeout);
       
   121             this.systemHeaders = new HttpHeadersImpl();
       
   122         }
       
   123         this.systemHeaders.setHeader("User-Agent", USER_AGENT);
       
   124         this.uri = requestURI;
       
   125         if (isWebSocket) {
       
   126             // WebSocket determines and sets the proxy itself
       
   127             this.proxy = ((HttpRequestImpl) request).proxy;
       
   128         } else {
       
   129             if (ps != null)
       
   130                 this.proxy = retrieveProxy(ps, uri);
       
   131             else
       
   132                 this.proxy = null;
       
   133         }
       
   134         this.expectContinue = request.expectContinue();
       
   135         this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
       
   136         this.requestPublisher = request.bodyPublisher().orElse(null);
       
   137         this.timeout = timeout;
       
   138         this.version = request.version();
       
   139         this.authority = null;
       
   140     }
       
   141 
       
   142     private static void checkTimeout(Duration duration) {
       
   143         if (duration != null) {
       
   144             if (duration.isNegative() || Duration.ZERO.equals(duration))
       
   145                 throw new IllegalArgumentException("Invalid duration: " + duration);
       
   146         }
       
   147     }
       
   148 
       
   149     /** Returns a new instance suitable for redirection. */
       
   150     public static HttpRequestImpl newInstanceForRedirection(URI uri,
       
   151                                                             String method,
       
   152                                                             HttpRequestImpl other) {
       
   153         return new HttpRequestImpl(uri, method, other);
       
   154     }
       
   155 
       
   156     /** Returns a new instance suitable for authentication. */
       
   157     public static HttpRequestImpl newInstanceForAuthentication(HttpRequestImpl other) {
       
   158         return new HttpRequestImpl(other.uri(), other.method(), other);
       
   159     }
       
   160 
       
   161     /**
       
   162      * Creates a HttpRequestImpl using fields of an existing request impl.
       
   163      * The newly created HttpRequestImpl does not copy the system headers.
       
   164      */
       
   165     private HttpRequestImpl(URI uri,
       
   166                             String method,
       
   167                             HttpRequestImpl other) {
       
   168         assert method == null || Utils.isValidName(method);
       
   169         this.method = method == null? "GET" : method;
       
   170         this.userHeaders = other.userHeaders;
       
   171         this.isWebSocket = other.isWebSocket;
       
   172         this.systemHeaders = new HttpHeadersImpl();
       
   173         this.systemHeaders.setHeader("User-Agent", USER_AGENT);
       
   174         this.uri = uri;
       
   175         this.proxy = other.proxy;
       
   176         this.expectContinue = other.expectContinue;
       
   177         this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
       
   178         this.requestPublisher = other.requestPublisher;  // may be null
       
   179         this.acc = other.acc;
       
   180         this.timeout = other.timeout;
       
   181         this.version = other.version();
       
   182         this.authority = null;
       
   183     }
       
   184 
       
   185     /* used for creating CONNECT requests  */
       
   186     HttpRequestImpl(String method, InetSocketAddress authority, HttpHeaders headers) {
       
   187         // TODO: isWebSocket flag is not specified, but the assumption is that
       
   188         // such a request will never be made on a connection that will be returned
       
   189         // to the connection pool (we might need to revisit this constructor later)
       
   190         assert "CONNECT".equalsIgnoreCase(method);
       
   191         this.method = method;
       
   192         this.systemHeaders = new HttpHeadersImpl();
       
   193         this.userHeaders = ImmutableHeaders.of(headers);
       
   194         this.uri = URI.create("socket://" + authority.getHostString() + ":"
       
   195                               + Integer.toString(authority.getPort()) + "/");
       
   196         this.proxy = null;
       
   197         this.requestPublisher = null;
       
   198         this.authority = authority;
       
   199         this.secure = false;
       
   200         this.expectContinue = false;
       
   201         this.timeout = null;
       
   202         // The CONNECT request sent for tunneling is only used in two cases:
       
   203         //   1. websocket, which only supports HTTP/1.1
       
   204         //   2. SSL tunneling through a HTTP/1.1 proxy
       
   205         // In either case we do not want to upgrade the connection to the proxy.
       
   206         // What we want to possibly upgrade is the tunneled connection to the
       
   207         // target server (so not the CONNECT request itself)
       
   208         this.version = Optional.of(HttpClient.Version.HTTP_1_1);
       
   209     }
       
   210 
       
   211     final boolean isConnect() {
       
   212         return "CONNECT".equalsIgnoreCase(method);
       
   213     }
       
   214 
       
   215     /**
       
   216      * Creates a HttpRequestImpl from the given set of Headers and the associated
       
   217      * "parent" request. Fields not taken from the headers are taken from the
       
   218      * parent.
       
   219      */
       
   220     static HttpRequestImpl createPushRequest(HttpRequestImpl parent,
       
   221                                              HttpHeadersImpl headers)
       
   222         throws IOException
       
   223     {
       
   224         return new HttpRequestImpl(parent, headers);
       
   225     }
       
   226 
       
   227     // only used for push requests
       
   228     private HttpRequestImpl(HttpRequestImpl parent, HttpHeadersImpl headers)
       
   229         throws IOException
       
   230     {
       
   231         this.method = headers.firstValue(":method")
       
   232                 .orElseThrow(() -> new IOException("No method in Push Promise"));
       
   233         String path = headers.firstValue(":path")
       
   234                 .orElseThrow(() -> new IOException("No path in Push Promise"));
       
   235         String scheme = headers.firstValue(":scheme")
       
   236                 .orElseThrow(() -> new IOException("No scheme in Push Promise"));
       
   237         String authority = headers.firstValue(":authority")
       
   238                 .orElseThrow(() -> new IOException("No authority in Push Promise"));
       
   239         StringBuilder sb = new StringBuilder();
       
   240         sb.append(scheme).append("://").append(authority).append(path);
       
   241         this.uri = URI.create(sb.toString());
       
   242         this.proxy = null;
       
   243         this.userHeaders = ImmutableHeaders.of(headers.map(), ALLOWED_HEADERS);
       
   244         this.systemHeaders = parent.systemHeaders;
       
   245         this.expectContinue = parent.expectContinue;
       
   246         this.secure = parent.secure;
       
   247         this.requestPublisher = parent.requestPublisher;
       
   248         this.acc = parent.acc;
       
   249         this.timeout = parent.timeout;
       
   250         this.version = parent.version;
       
   251         this.authority = null;
       
   252     }
       
   253 
       
   254     @Override
       
   255     public String toString() {
       
   256         return (uri == null ? "" : uri.toString()) + " " + method;
       
   257     }
       
   258 
       
   259     @Override
       
   260     public HttpHeaders headers() {
       
   261         return userHeaders;
       
   262     }
       
   263 
       
   264     InetSocketAddress authority() { return authority; }
       
   265 
       
   266     void setH2Upgrade(Http2ClientImpl h2client) {
       
   267         systemHeaders.setHeader("Connection", "Upgrade, HTTP2-Settings");
       
   268         systemHeaders.setHeader("Upgrade", "h2c");
       
   269         systemHeaders.setHeader("HTTP2-Settings", h2client.getSettingsString());
       
   270     }
       
   271 
       
   272     @Override
       
   273     public boolean expectContinue() { return expectContinue; }
       
   274 
       
   275     /** Retrieves the proxy, from the given ProxySelector, if there is one. */
       
   276     private static Proxy retrieveProxy(ProxySelector ps, URI uri) {
       
   277         Proxy proxy = null;
       
   278         List<Proxy> pl = ps.select(uri);
       
   279         if (!pl.isEmpty()) {
       
   280             Proxy p = pl.get(0);
       
   281             if (p.type() == Proxy.Type.HTTP)
       
   282                 proxy = p;
       
   283         }
       
   284         return proxy;
       
   285     }
       
   286 
       
   287     InetSocketAddress proxy() {
       
   288         if (proxy == null || proxy.type() != Proxy.Type.HTTP
       
   289                 || method.equalsIgnoreCase("CONNECT")) {
       
   290             return null;
       
   291         }
       
   292         return (InetSocketAddress)proxy.address();
       
   293     }
       
   294 
       
   295     boolean secure() { return secure; }
       
   296 
       
   297     @Override
       
   298     public void setProxy(Proxy proxy) {
       
   299         assert isWebSocket;
       
   300         this.proxy = proxy;
       
   301     }
       
   302 
       
   303     @Override
       
   304     public void isWebSocket(boolean is) {
       
   305         isWebSocket = is;
       
   306     }
       
   307 
       
   308     boolean isWebSocket() {
       
   309         return isWebSocket;
       
   310     }
       
   311 
       
   312     @Override
       
   313     public Optional<BodyPublisher> bodyPublisher() {
       
   314         return requestPublisher == null ? Optional.empty()
       
   315                                         : Optional.of(requestPublisher);
       
   316     }
       
   317 
       
   318     /**
       
   319      * Returns the request method for this request. If not set explicitly,
       
   320      * the default method for any request is "GET".
       
   321      */
       
   322     @Override
       
   323     public String method() { return method; }
       
   324 
       
   325     @Override
       
   326     public URI uri() { return uri; }
       
   327 
       
   328     @Override
       
   329     public Optional<Duration> timeout() {
       
   330         return timeout == null ? Optional.empty() : Optional.of(timeout);
       
   331     }
       
   332 
       
   333     HttpHeaders getUserHeaders() { return userHeaders; }
       
   334 
       
   335     HttpHeadersImpl getSystemHeaders() { return systemHeaders; }
       
   336 
       
   337     @Override
       
   338     public Optional<HttpClient.Version> version() { return version; }
       
   339 
       
   340     void addSystemHeader(String name, String value) {
       
   341         systemHeaders.addHeader(name, value);
       
   342     }
       
   343 
       
   344     @Override
       
   345     public void setSystemHeader(String name, String value) {
       
   346         systemHeaders.setHeader(name, value);
       
   347     }
       
   348 
       
   349     InetSocketAddress getAddress() {
       
   350         URI uri = uri();
       
   351         if (uri == null) {
       
   352             return authority();
       
   353         }
       
   354         int p = uri.getPort();
       
   355         if (p == -1) {
       
   356             if (uri.getScheme().equalsIgnoreCase("https")) {
       
   357                 p = 443;
       
   358             } else {
       
   359                 p = 80;
       
   360             }
       
   361         }
       
   362         final String host = uri.getHost();
       
   363         final int port = p;
       
   364         if (proxy() == null) {
       
   365             PrivilegedAction<InetSocketAddress> pa = () -> new InetSocketAddress(host, port);
       
   366             return AccessController.doPrivileged(pa);
       
   367         } else {
       
   368             return InetSocketAddress.createUnresolved(host, port);
       
   369         }
       
   370     }
       
   371 }