author | vtewari |
Fri, 08 Jun 2018 15:56:23 +0530 | |
changeset 52165 | 6c014b7762a2 |
parent 50681 | 4254bed3c09d |
child 52554 | 5f1ca46703f9 |
child 56795 | 03ece2518428 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.nio.ByteBuffer; |
48083 | 31 |
import java.util.ArrayList; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.util.List; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.util.Map; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.net.InetSocketAddress; |
48083 | 35 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.util.concurrent.Flow; |
49765 | 37 |
import java.util.function.BiPredicate; |
38 |
import java.net.http.HttpHeaders; |
|
39 |
import java.net.http.HttpRequest; |
|
40 |
import jdk.internal.net.http.Http1Exchange.Http1BodySubscriber; |
|
50681 | 41 |
import jdk.internal.net.http.common.HttpHeadersBuilder; |
49765 | 42 |
import jdk.internal.net.http.common.Log; |
43 |
import jdk.internal.net.http.common.Logger; |
|
44 |
import jdk.internal.net.http.common.Utils; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
|
50681 | 46 |
import static java.lang.String.format; |
48083 | 47 |
import static java.nio.charset.StandardCharsets.US_ASCII; |
48 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
/** |
48083 | 50 |
* An HTTP/1.1 request. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
class Http1Request { |
49765 | 53 |
|
54 |
private static final String COOKIE_HEADER = "Cookie"; |
|
50681 | 55 |
private static final BiPredicate<String,String> NOCOOKIES = |
49765 | 56 |
(k,v) -> !COOKIE_HEADER.equalsIgnoreCase(k); |
57 |
||
48083 | 58 |
private final HttpRequestImpl request; |
59 |
private final Http1Exchange<?> http1Exchange; |
|
60 |
private final HttpConnection connection; |
|
61 |
private final HttpRequest.BodyPublisher requestPublisher; |
|
62 |
private final HttpHeaders userHeaders; |
|
50681 | 63 |
private final HttpHeadersBuilder systemHeadersBuilder; |
48083 | 64 |
private volatile boolean streaming; |
65 |
private volatile long contentLength; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
|
48083 | 67 |
Http1Request(HttpRequestImpl request, |
68 |
Http1Exchange<?> http1Exchange) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
69 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
this.request = request; |
48083 | 72 |
this.http1Exchange = http1Exchange; |
73 |
this.connection = http1Exchange.connection(); |
|
74 |
this.requestPublisher = request.requestPublisher; // may be null |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
this.userHeaders = request.getUserHeaders(); |
50681 | 76 |
this.systemHeadersBuilder = request.getSystemHeadersBuilder(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
|
48083 | 79 |
private void logHeaders(String completeHeaders) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
if (Log.headers()) { |
48083 | 81 |
//StringBuilder sb = new StringBuilder(256); |
82 |
//sb.append("REQUEST HEADERS:\n"); |
|
83 |
//Log.dumpHeaders(sb, " ", systemHeaders); |
|
84 |
//Log.dumpHeaders(sb, " ", userHeaders); |
|
85 |
//Log.logHeaders(sb.toString()); |
|
86 |
||
87 |
String s = completeHeaders.replaceAll("\r\n", "\n"); |
|
49765 | 88 |
if (s.endsWith("\n\n")) s = s.substring(0, s.length() - 2); |
89 |
Log.logHeaders("REQUEST HEADERS:\n{0}\n", s); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
|
49765 | 93 |
|
48083 | 94 |
private void collectHeaders0(StringBuilder sb) { |
50681 | 95 |
BiPredicate<String,String> filter = |
49765 | 96 |
connection.headerFilter(request); |
97 |
||
98 |
// Filter out 'Cookie:' headers, we will collect them at the end. |
|
50681 | 99 |
BiPredicate<String,String> nocookies = NOCOOKIES.and(filter); |
100 |
||
101 |
HttpHeaders systemHeaders = systemHeadersBuilder.build(); |
|
49765 | 102 |
|
103 |
// If we're sending this request through a tunnel, |
|
104 |
// then don't send any preemptive proxy-* headers that |
|
105 |
// the authentication filter may have saved in its |
|
106 |
// cache. |
|
107 |
collectHeaders1(sb, systemHeaders, nocookies); |
|
108 |
||
109 |
// If we're sending this request through a tunnel, |
|
110 |
// don't send any user-supplied proxy-* headers |
|
111 |
// to the target server. |
|
112 |
collectHeaders1(sb, userHeaders, nocookies); |
|
113 |
||
114 |
// Gather all 'Cookie:' headers and concatenate their |
|
115 |
// values in a single line. |
|
50681 | 116 |
collectCookies(sb, systemHeaders, userHeaders); |
49765 | 117 |
|
118 |
// terminate headers |
|
119 |
sb.append('\r').append('\n'); |
|
48083 | 120 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
|
49765 | 122 |
// Concatenate any 'Cookie:' header in a single line, as mandated |
123 |
// by RFC 6265, section 5.4: |
|
124 |
// |
|
125 |
// <<When the user agent generates an HTTP request, the user agent MUST |
|
126 |
// NOT attach more than one Cookie header field.>> |
|
127 |
// |
|
128 |
// This constraint is relaxed for the HTTP/2 protocol, which |
|
129 |
// explicitly allows sending multiple Cookie header fields. |
|
130 |
// RFC 7540 section 8.1.2.5: |
|
131 |
// |
|
132 |
// <<To allow for better compression efficiency, the Cookie header |
|
133 |
// field MAY be split into separate header fields, each with one or |
|
134 |
// more cookie-pairs.>> |
|
135 |
// |
|
136 |
// This method will therefore concatenate multiple Cookie header field |
|
137 |
// values into a single field, in a similar way than was implemented in |
|
138 |
// the legacy HttpURLConnection. |
|
139 |
// |
|
140 |
// Note that at this point this method performs no further validation |
|
141 |
// on the actual field-values, except to check that they do not contain |
|
142 |
// any illegal character for header field values. |
|
143 |
// |
|
144 |
private void collectCookies(StringBuilder sb, |
|
145 |
HttpHeaders system, |
|
50681 | 146 |
HttpHeaders user) { |
147 |
List<String> systemList = system.allValues(COOKIE_HEADER); |
|
148 |
List<String> userList = user.allValues(COOKIE_HEADER); |
|
49765 | 149 |
boolean found = false; |
150 |
if (systemList != null) { |
|
151 |
for (String cookie : systemList) { |
|
152 |
if (!found) { |
|
153 |
found = true; |
|
50681 | 154 |
sb.append(COOKIE_HEADER).append(':').append(' '); |
49765 | 155 |
} else { |
156 |
sb.append(';').append(' '); |
|
157 |
} |
|
158 |
sb.append(cookie); |
|
159 |
} |
|
160 |
} |
|
161 |
if (userList != null) { |
|
162 |
for (String cookie : userList) { |
|
163 |
if (!found) { |
|
164 |
found = true; |
|
50681 | 165 |
sb.append(COOKIE_HEADER).append(':').append(' '); |
49765 | 166 |
} else { |
167 |
sb.append(';').append(' '); |
|
168 |
} |
|
169 |
sb.append(cookie); |
|
170 |
} |
|
171 |
} |
|
172 |
if (found) sb.append('\r').append('\n'); |
|
173 |
} |
|
174 |
||
50681 | 175 |
private void collectHeaders1(StringBuilder sb, |
176 |
HttpHeaders headers, |
|
177 |
BiPredicate<String,String> filter) { |
|
48083 | 178 |
for (Map.Entry<String,List<String>> entry : headers.map().entrySet()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
String key = entry.getKey(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
List<String> values = entry.getValue(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
for (String value : values) { |
50681 | 182 |
if (!filter.test(key, value)) |
183 |
continue; |
|
49765 | 184 |
sb.append(key).append(':').append(' ') |
185 |
.append(value) |
|
186 |
.append('\r').append('\n'); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
191 |
private String getPathAndQuery(URI uri) { |
49765 | 192 |
String path = uri.getRawPath(); |
193 |
String query = uri.getRawQuery(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
if (path == null || path.equals("")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
path = "/"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
if (query == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
query = ""; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
if (query.equals("")) { |
49944 | 201 |
return Utils.encode(path); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
202 |
} else { |
49944 | 203 |
return Utils.encode(path + "?" + query); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
207 |
private String authorityString(InetSocketAddress addr) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
return addr.getHostString() + ":" + addr.getPort(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
210 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
211 |
private String hostString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
212 |
URI uri = request.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
int port = uri.getPort(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
214 |
String host = uri.getHost(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
boolean defaultPort; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
if (port == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
defaultPort = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
} else if (request.secure()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
defaultPort = port == 443; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
defaultPort = port == 80; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
if (defaultPort) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
return host; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
return host + ":" + Integer.toString(port); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
private String requestURI() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
URI uri = request.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
234 |
String method = request.method(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
|
48083 | 236 |
if ((request.proxy() == null && !method.equals("CONNECT")) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
|| request.isWebSocket()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
return getPathAndQuery(uri); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
if (request.secure()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
if (request.method().equals("CONNECT")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
// use authority for connect itself |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
return authorityString(request.authority()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
// requests over tunnel do not require full URL |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
return getPathAndQuery(uri); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
} |
48083 | 249 |
if (request.method().equals("CONNECT")) { |
250 |
// use authority for connect itself |
|
251 |
return authorityString(request.authority()); |
|
252 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
|
48083 | 254 |
return uri == null? authorityString(request.authority()) : uri.toString(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
256 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
private boolean finished; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
258 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
259 |
synchronized boolean finished() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
260 |
return finished; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
261 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
262 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
synchronized void setFinished() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
finished = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
|
48083 | 267 |
List<ByteBuffer> headers() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
268 |
if (Log.requests() && request != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
269 |
Log.logRequest(request.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
271 |
String uriString = requestURI(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
272 |
StringBuilder sb = new StringBuilder(64); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
sb.append(request.method()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
274 |
.append(' ') |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
275 |
.append(uriString) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
276 |
.append(" HTTP/1.1\r\n"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
277 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
278 |
URI uri = request.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
if (uri != null) { |
50681 | 280 |
systemHeadersBuilder.setHeader("Host", hostString()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
} |
49765 | 282 |
if (requestPublisher == null) { |
48083 | 283 |
// Not a user request, or maybe a method, e.g. GET, with no body. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
contentLength = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
} else { |
48083 | 286 |
contentLength = requestPublisher.contentLength(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
if (contentLength == 0) { |
50681 | 290 |
systemHeadersBuilder.setHeader("Content-Length", "0"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
291 |
} else if (contentLength > 0) { |
50681 | 292 |
systemHeadersBuilder.setHeader("Content-Length", Long.toString(contentLength)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
293 |
streaming = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
294 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
streaming = true; |
50681 | 296 |
systemHeadersBuilder.setHeader("Transfer-encoding", "chunked"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
297 |
} |
48083 | 298 |
collectHeaders0(sb); |
299 |
String hs = sb.toString(); |
|
300 |
logHeaders(hs); |
|
301 |
ByteBuffer b = ByteBuffer.wrap(hs.getBytes(US_ASCII)); |
|
302 |
return List.of(b); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
|
48083 | 305 |
Http1BodySubscriber continueRequest() { |
306 |
Http1BodySubscriber subscriber; |
|
307 |
if (streaming) { |
|
308 |
subscriber = new StreamSubscriber(); |
|
309 |
requestPublisher.subscribe(subscriber); |
|
310 |
} else { |
|
311 |
if (contentLength == 0) |
|
312 |
return null; |
|
313 |
||
314 |
subscriber = new FixedContentSubscriber(); |
|
315 |
requestPublisher.subscribe(subscriber); |
|
316 |
} |
|
317 |
return subscriber; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
318 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
319 |
|
49765 | 320 |
final class StreamSubscriber extends Http1BodySubscriber { |
321 |
||
322 |
StreamSubscriber() { super(debug); } |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
323 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
public void onSubscribe(Flow.Subscription subscription) { |
49765 | 326 |
if (isSubscribed()) { |
48083 | 327 |
Throwable t = new IllegalStateException("already subscribed"); |
328 |
http1Exchange.appendToOutgoing(t); |
|
329 |
} else { |
|
49765 | 330 |
setSubscription(subscription); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
331 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
332 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
333 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
334 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
335 |
public void onNext(ByteBuffer item) { |
48083 | 336 |
Objects.requireNonNull(item); |
337 |
if (complete) { |
|
338 |
Throwable t = new IllegalStateException("subscription already completed"); |
|
339 |
http1Exchange.appendToOutgoing(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
} else { |
48083 | 341 |
int chunklen = item.remaining(); |
342 |
ArrayList<ByteBuffer> l = new ArrayList<>(3); |
|
343 |
l.add(getHeader(chunklen)); |
|
344 |
l.add(item); |
|
345 |
l.add(ByteBuffer.wrap(CRLF)); |
|
346 |
http1Exchange.appendToOutgoing(l); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
@Override |
50681 | 351 |
public String currentStateMessage() { |
352 |
return "streaming request body " + (complete ? "complete" : "incomplete"); |
|
353 |
} |
|
354 |
||
355 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
public void onError(Throwable throwable) { |
48083 | 357 |
if (complete) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
return; |
48083 | 359 |
|
49765 | 360 |
cancelSubscription(); |
48083 | 361 |
http1Exchange.appendToOutgoing(throwable); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
public void onComplete() { |
48083 | 366 |
if (complete) { |
367 |
Throwable t = new IllegalStateException("subscription already completed"); |
|
368 |
http1Exchange.appendToOutgoing(t); |
|
369 |
} else { |
|
370 |
ArrayList<ByteBuffer> l = new ArrayList<>(2); |
|
371 |
l.add(ByteBuffer.wrap(EMPTY_CHUNK_BYTES)); |
|
372 |
l.add(ByteBuffer.wrap(CRLF)); |
|
373 |
complete = true; |
|
374 |
//setFinished(); |
|
375 |
http1Exchange.appendToOutgoing(l); |
|
376 |
http1Exchange.appendToOutgoing(COMPLETED); |
|
377 |
setFinished(); // TODO: before or after,? does it matter? |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
381 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
|
49765 | 383 |
final class FixedContentSubscriber extends Http1BodySubscriber { |
48083 | 384 |
|
385 |
private volatile long contentWritten; |
|
49765 | 386 |
FixedContentSubscriber() { super(debug); } |
48083 | 387 |
|
388 |
@Override |
|
389 |
public void onSubscribe(Flow.Subscription subscription) { |
|
49765 | 390 |
if (isSubscribed()) { |
48083 | 391 |
Throwable t = new IllegalStateException("already subscribed"); |
392 |
http1Exchange.appendToOutgoing(t); |
|
393 |
} else { |
|
49765 | 394 |
setSubscription(subscription); |
48083 | 395 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
} |
48083 | 397 |
|
398 |
@Override |
|
399 |
public void onNext(ByteBuffer item) { |
|
49765 | 400 |
if (debug.on()) debug.log("onNext"); |
48083 | 401 |
Objects.requireNonNull(item); |
402 |
if (complete) { |
|
403 |
Throwable t = new IllegalStateException("subscription already completed"); |
|
404 |
http1Exchange.appendToOutgoing(t); |
|
405 |
} else { |
|
406 |
long writing = item.remaining(); |
|
407 |
long written = (contentWritten += writing); |
|
408 |
||
409 |
if (written > contentLength) { |
|
49765 | 410 |
cancelSubscription(); |
48083 | 411 |
String msg = connection.getConnectionFlow() |
412 |
+ " [" + Thread.currentThread().getName() +"] " |
|
413 |
+ "Too many bytes in request body. Expected: " |
|
414 |
+ contentLength + ", got: " + written; |
|
415 |
http1Exchange.appendToOutgoing(new IOException(msg)); |
|
416 |
} else { |
|
417 |
http1Exchange.appendToOutgoing(List.of(item)); |
|
418 |
} |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
@Override |
|
50681 | 423 |
public String currentStateMessage() { |
424 |
return format("fixed content-length: %d, bytes sent: %d", |
|
425 |
contentLength, contentWritten); |
|
426 |
} |
|
427 |
||
428 |
@Override |
|
48083 | 429 |
public void onError(Throwable throwable) { |
49765 | 430 |
if (debug.on()) debug.log("onError"); |
48083 | 431 |
if (complete) // TODO: error? |
432 |
return; |
|
433 |
||
49765 | 434 |
cancelSubscription(); |
48083 | 435 |
http1Exchange.appendToOutgoing(throwable); |
436 |
} |
|
437 |
||
438 |
@Override |
|
439 |
public void onComplete() { |
|
49765 | 440 |
if (debug.on()) debug.log("onComplete"); |
48083 | 441 |
if (complete) { |
442 |
Throwable t = new IllegalStateException("subscription already completed"); |
|
443 |
http1Exchange.appendToOutgoing(t); |
|
444 |
} else { |
|
445 |
complete = true; |
|
446 |
long written = contentWritten; |
|
447 |
if (contentLength > written) { |
|
49765 | 448 |
cancelSubscription(); |
48083 | 449 |
Throwable t = new IOException(connection.getConnectionFlow() |
450 |
+ " [" + Thread.currentThread().getName() +"] " |
|
451 |
+ "Too few bytes returned by the publisher (" |
|
452 |
+ written + "/" |
|
453 |
+ contentLength + ")"); |
|
454 |
http1Exchange.appendToOutgoing(t); |
|
455 |
} else { |
|
456 |
http1Exchange.appendToOutgoing(COMPLETED); |
|
457 |
} |
|
458 |
} |
|
459 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
460 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
461 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
462 |
private static final byte[] CRLF = {'\r', '\n'}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
463 |
private static final byte[] EMPTY_CHUNK_BYTES = {'0', '\r', '\n'}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
464 |
|
48083 | 465 |
/** Returns a header for a particular chunk size */ |
466 |
private static ByteBuffer getHeader(int size) { |
|
467 |
String hexStr = Integer.toHexString(size); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
468 |
byte[] hexBytes = hexStr.getBytes(US_ASCII); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
469 |
byte[] header = new byte[hexStr.length()+2]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
470 |
System.arraycopy(hexBytes, 0, header, 0, hexBytes.length); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
471 |
header[hexBytes.length] = CRLF[0]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
header[hexBytes.length+1] = CRLF[1]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
return ByteBuffer.wrap(header); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
474 |
} |
48083 | 475 |
|
49765 | 476 |
final Logger debug = Utils.getDebugLogger(this::toString, Utils.DEBUG); |
48083 | 477 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
478 |
} |