src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestImpl.java
changeset 58758 2b13d126a2d8
parent 53521 41fa3e6f2785
equal deleted inserted replaced
58756:b7aa58d7f5aa 58758:2b13d126a2d8
    39 import java.util.Objects;
    39 import java.util.Objects;
    40 import java.util.Optional;
    40 import java.util.Optional;
    41 import java.net.http.HttpClient;
    41 import java.net.http.HttpClient;
    42 import java.net.http.HttpHeaders;
    42 import java.net.http.HttpHeaders;
    43 import java.net.http.HttpRequest;
    43 import java.net.http.HttpRequest;
       
    44 
    44 import jdk.internal.net.http.common.HttpHeadersBuilder;
    45 import jdk.internal.net.http.common.HttpHeadersBuilder;
    45 import jdk.internal.net.http.common.Utils;
    46 import jdk.internal.net.http.common.Utils;
    46 import jdk.internal.net.http.websocket.OpeningHandshake;
    47 import jdk.internal.net.http.websocket.OpeningHandshake;
    47 import jdk.internal.net.http.websocket.WebSocketRequest;
    48 import jdk.internal.net.http.websocket.WebSocketRequest;
    48 
    49 
   150     }
   151     }
   151 
   152 
   152     /** Returns a new instance suitable for redirection. */
   153     /** Returns a new instance suitable for redirection. */
   153     public static HttpRequestImpl newInstanceForRedirection(URI uri,
   154     public static HttpRequestImpl newInstanceForRedirection(URI uri,
   154                                                             String method,
   155                                                             String method,
   155                                                             HttpRequestImpl other) {
   156                                                             HttpRequestImpl other,
   156         return new HttpRequestImpl(uri, method, other);
   157                                                             boolean mayHaveBody) {
       
   158         return new HttpRequestImpl(uri, method, other, mayHaveBody);
   157     }
   159     }
   158 
   160 
   159     /** Returns a new instance suitable for authentication. */
   161     /** Returns a new instance suitable for authentication. */
   160     public static HttpRequestImpl newInstanceForAuthentication(HttpRequestImpl other) {
   162     public static HttpRequestImpl newInstanceForAuthentication(HttpRequestImpl other) {
   161         HttpRequestImpl request = new HttpRequestImpl(other.uri(), other.method(), other);
   163         HttpRequestImpl request = new HttpRequestImpl(other.uri(), other.method(), other, true);
   162         if (request.isWebSocket()) {
   164         if (request.isWebSocket()) {
   163             Utils.setWebSocketUpgradeHeaders(request);
   165             Utils.setWebSocketUpgradeHeaders(request);
   164         }
   166         }
   165         return request;
   167         return request;
   166     }
   168     }
   169      * Creates a HttpRequestImpl using fields of an existing request impl.
   171      * Creates a HttpRequestImpl using fields of an existing request impl.
   170      * The newly created HttpRequestImpl does not copy the system headers.
   172      * The newly created HttpRequestImpl does not copy the system headers.
   171      */
   173      */
   172     private HttpRequestImpl(URI uri,
   174     private HttpRequestImpl(URI uri,
   173                             String method,
   175                             String method,
   174                             HttpRequestImpl other) {
   176                             HttpRequestImpl other,
       
   177                             boolean mayHaveBody) {
   175         assert method == null || Utils.isValidName(method);
   178         assert method == null || Utils.isValidName(method);
   176         this.method = method == null? "GET" : method;
   179         this.method = method == null? "GET" : method;
   177         this.userHeaders = other.userHeaders;
   180         this.userHeaders = other.userHeaders;
   178         this.isWebSocket = other.isWebSocket;
   181         this.isWebSocket = other.isWebSocket;
   179         this.systemHeadersBuilder = new HttpHeadersBuilder();
   182         this.systemHeadersBuilder = new HttpHeadersBuilder();
   182         }
   185         }
   183         this.uri = uri;
   186         this.uri = uri;
   184         this.proxy = other.proxy;
   187         this.proxy = other.proxy;
   185         this.expectContinue = other.expectContinue;
   188         this.expectContinue = other.expectContinue;
   186         this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
   189         this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
   187         this.requestPublisher = other.requestPublisher;  // may be null
   190         this.requestPublisher = mayHaveBody ? publisher(other) : null; // may be null
   188         this.acc = other.acc;
   191         this.acc = other.acc;
   189         this.timeout = other.timeout;
   192         this.timeout = other.timeout;
   190         this.version = other.version();
   193         this.version = other.version();
   191         this.authority = null;
   194         this.authority = null;
       
   195     }
       
   196 
       
   197     private BodyPublisher publisher(HttpRequestImpl other) {
       
   198         BodyPublisher res = other.requestPublisher;
       
   199         if (!Objects.equals(method, other.method)) {
       
   200             res = null;
       
   201         }
       
   202         return res;
   192     }
   203     }
   193 
   204 
   194     /* used for creating CONNECT requests  */
   205     /* used for creating CONNECT requests  */
   195     HttpRequestImpl(String method, InetSocketAddress authority, HttpHeaders headers) {
   206     HttpRequestImpl(String method, InetSocketAddress authority, HttpHeaders headers) {
   196         // TODO: isWebSocket flag is not specified, but the assumption is that
   207         // TODO: isWebSocket flag is not specified, but the assumption is that