author | mbaesken |
Thu, 28 Nov 2019 13:02:39 +0100 | |
changeset 59323 | ae2eb76c486d |
parent 54579 | 270557b396eb |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
54579
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
52902
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, 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; |
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
30 |
import java.net.http.HttpClient; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.nio.ByteBuffer; |
48083 | 32 |
import java.util.ArrayList; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.util.List; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.util.Map; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.net.InetSocketAddress; |
48083 | 36 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.util.concurrent.Flow; |
49765 | 38 |
import java.util.function.BiPredicate; |
39 |
import java.net.http.HttpHeaders; |
|
40 |
import java.net.http.HttpRequest; |
|
41 |
import jdk.internal.net.http.Http1Exchange.Http1BodySubscriber; |
|
50681 | 42 |
import jdk.internal.net.http.common.HttpHeadersBuilder; |
49765 | 43 |
import jdk.internal.net.http.common.Log; |
44 |
import jdk.internal.net.http.common.Logger; |
|
45 |
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
|
46 |
|
50681 | 47 |
import static java.lang.String.format; |
48083 | 48 |
import static java.nio.charset.StandardCharsets.US_ASCII; |
49 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
/** |
48083 | 51 |
* An HTTP/1.1 request. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
class Http1Request { |
49765 | 54 |
|
55 |
private static final String COOKIE_HEADER = "Cookie"; |
|
50681 | 56 |
private static final BiPredicate<String,String> NOCOOKIES = |
49765 | 57 |
(k,v) -> !COOKIE_HEADER.equalsIgnoreCase(k); |
58 |
||
48083 | 59 |
private final HttpRequestImpl request; |
60 |
private final Http1Exchange<?> http1Exchange; |
|
61 |
private final HttpConnection connection; |
|
62 |
private final HttpRequest.BodyPublisher requestPublisher; |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
63 |
private volatile HttpHeaders userHeaders; |
50681 | 64 |
private final HttpHeadersBuilder systemHeadersBuilder; |
48083 | 65 |
private volatile boolean streaming; |
66 |
private volatile long contentLength; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
|
48083 | 68 |
Http1Request(HttpRequestImpl request, |
69 |
Http1Exchange<?> http1Exchange) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
this.request = request; |
48083 | 73 |
this.http1Exchange = http1Exchange; |
74 |
this.connection = http1Exchange.connection(); |
|
75 |
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
|
76 |
this.userHeaders = request.getUserHeaders(); |
50681 | 77 |
this.systemHeadersBuilder = request.getSystemHeadersBuilder(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
|
48083 | 80 |
private void logHeaders(String completeHeaders) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
81 |
if (Log.headers()) { |
48083 | 82 |
//StringBuilder sb = new StringBuilder(256); |
83 |
//sb.append("REQUEST HEADERS:\n"); |
|
84 |
//Log.dumpHeaders(sb, " ", systemHeaders); |
|
85 |
//Log.dumpHeaders(sb, " ", userHeaders); |
|
86 |
//Log.logHeaders(sb.toString()); |
|
87 |
||
88 |
String s = completeHeaders.replaceAll("\r\n", "\n"); |
|
49765 | 89 |
if (s.endsWith("\n\n")) s = s.substring(0, s.length() - 2); |
90 |
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
|
91 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
|
49765 | 94 |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
95 |
public void collectHeaders0(StringBuilder sb) { |
50681 | 96 |
BiPredicate<String,String> filter = |
49765 | 97 |
connection.headerFilter(request); |
98 |
||
99 |
// Filter out 'Cookie:' headers, we will collect them at the end. |
|
50681 | 100 |
BiPredicate<String,String> nocookies = NOCOOKIES.and(filter); |
101 |
||
102 |
HttpHeaders systemHeaders = systemHeadersBuilder.build(); |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
103 |
HttpClient client = http1Exchange.client(); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
104 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
105 |
// Filter overridable headers from userHeaders |
54579
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
52902
diff
changeset
|
106 |
userHeaders = HttpHeaders.of(userHeaders.map(), |
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
52902
diff
changeset
|
107 |
connection.contextRestricted(request, client)); |
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
108 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
109 |
final HttpHeaders uh = userHeaders; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
110 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
111 |
// Filter any headers from systemHeaders that are set in userHeaders |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
50681
diff
changeset
|
112 |
systemHeaders = HttpHeaders.of(systemHeaders.map(), (k,v) -> uh.firstValue(k).isEmpty()); |
49765 | 113 |
|
114 |
// If we're sending this request through a tunnel, |
|
115 |
// then don't send any preemptive proxy-* headers that |
|
116 |
// the authentication filter may have saved in its |
|
117 |
// cache. |
|
118 |
collectHeaders1(sb, systemHeaders, nocookies); |
|
119 |
||
120 |
// If we're sending this request through a tunnel, |
|
121 |
// don't send any user-supplied proxy-* headers |
|
122 |
// to the target server. |
|
123 |
collectHeaders1(sb, userHeaders, nocookies); |
|
124 |
||
125 |
// Gather all 'Cookie:' headers and concatenate their |
|
126 |
// values in a single line. |
|
50681 | 127 |
collectCookies(sb, systemHeaders, userHeaders); |
49765 | 128 |
|
129 |
// terminate headers |
|
130 |
sb.append('\r').append('\n'); |
|
48083 | 131 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
|
49765 | 133 |
// Concatenate any 'Cookie:' header in a single line, as mandated |
134 |
// by RFC 6265, section 5.4: |
|
135 |
// |
|
136 |
// <<When the user agent generates an HTTP request, the user agent MUST |
|
137 |
// NOT attach more than one Cookie header field.>> |
|
138 |
// |
|
139 |
// This constraint is relaxed for the HTTP/2 protocol, which |
|
140 |
// explicitly allows sending multiple Cookie header fields. |
|
141 |
// RFC 7540 section 8.1.2.5: |
|
142 |
// |
|
143 |
// <<To allow for better compression efficiency, the Cookie header |
|
144 |
// field MAY be split into separate header fields, each with one or |
|
145 |
// more cookie-pairs.>> |
|
146 |
// |
|
147 |
// This method will therefore concatenate multiple Cookie header field |
|
148 |
// values into a single field, in a similar way than was implemented in |
|
149 |
// the legacy HttpURLConnection. |
|
150 |
// |
|
151 |
// Note that at this point this method performs no further validation |
|
152 |
// on the actual field-values, except to check that they do not contain |
|
153 |
// any illegal character for header field values. |
|
154 |
// |
|
155 |
private void collectCookies(StringBuilder sb, |
|
156 |
HttpHeaders system, |
|
50681 | 157 |
HttpHeaders user) { |
158 |
List<String> systemList = system.allValues(COOKIE_HEADER); |
|
159 |
List<String> userList = user.allValues(COOKIE_HEADER); |
|
49765 | 160 |
boolean found = false; |
161 |
if (systemList != null) { |
|
162 |
for (String cookie : systemList) { |
|
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 (userList != null) { |
|
173 |
for (String cookie : userList) { |
|
174 |
if (!found) { |
|
175 |
found = true; |
|
50681 | 176 |
sb.append(COOKIE_HEADER).append(':').append(' '); |
49765 | 177 |
} else { |
178 |
sb.append(';').append(' '); |
|
179 |
} |
|
180 |
sb.append(cookie); |
|
181 |
} |
|
182 |
} |
|
183 |
if (found) sb.append('\r').append('\n'); |
|
184 |
} |
|
185 |
||
50681 | 186 |
private void collectHeaders1(StringBuilder sb, |
187 |
HttpHeaders headers, |
|
188 |
BiPredicate<String,String> filter) { |
|
48083 | 189 |
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
|
190 |
String key = entry.getKey(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
191 |
List<String> values = entry.getValue(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
for (String value : values) { |
50681 | 193 |
if (!filter.test(key, value)) |
194 |
continue; |
|
49765 | 195 |
sb.append(key).append(':').append(' ') |
196 |
.append(value) |
|
197 |
.append('\r').append('\n'); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
} |
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
201 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
202 |
private String getPathAndQuery(URI uri) { |
49765 | 203 |
String path = uri.getRawPath(); |
204 |
String query = uri.getRawQuery(); |
|
52902
e3398b2e1ab0
8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents:
52554
diff
changeset
|
205 |
if (path == null || path.isEmpty()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
path = "/"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
207 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
if (query == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
query = ""; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
210 |
} |
52902
e3398b2e1ab0
8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents:
52554
diff
changeset
|
211 |
if (query.isEmpty()) { |
49944 | 212 |
return Utils.encode(path); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
} else { |
49944 | 214 |
return Utils.encode(path + "?" + query); |
42460
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
private String authorityString(InetSocketAddress addr) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
return addr.getHostString() + ":" + addr.getPort(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
private String hostString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
URI uri = request.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
int port = uri.getPort(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
String host = uri.getHost(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
boolean defaultPort; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
if (port == -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
defaultPort = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
} else if (request.secure()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
defaultPort = port == 443; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
defaultPort = port == 80; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
234 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
if (defaultPort) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
return host; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
return host + ":" + Integer.toString(port); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
private String requestURI() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
URI uri = request.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
String method = request.method(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
|
48083 | 247 |
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
|
248 |
|| request.isWebSocket()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
return getPathAndQuery(uri); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
if (request.secure()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
252 |
if (request.method().equals("CONNECT")) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
// use authority for connect itself |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
254 |
return authorityString(request.authority()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
256 |
// 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
|
257 |
return getPathAndQuery(uri); |
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 |
} |
48083 | 260 |
if (request.method().equals("CONNECT")) { |
261 |
// use authority for connect itself |
|
262 |
return authorityString(request.authority()); |
|
263 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
|
48083 | 265 |
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
|
266 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
267 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
268 |
private boolean finished; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
269 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
synchronized boolean finished() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
271 |
return finished; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
272 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
274 |
synchronized void setFinished() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
275 |
finished = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
276 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
277 |
|
48083 | 278 |
List<ByteBuffer> headers() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
if (Log.requests() && request != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
280 |
Log.logRequest(request.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
String uriString = requestURI(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
283 |
StringBuilder sb = new StringBuilder(64); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
sb.append(request.method()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
.append(' ') |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
.append(uriString) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
.append(" HTTP/1.1\r\n"); |
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 |
URI uri = request.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
290 |
if (uri != null) { |
50681 | 291 |
systemHeadersBuilder.setHeader("Host", hostString()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
292 |
} |
49765 | 293 |
if (requestPublisher == null) { |
48083 | 294 |
// 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
|
295 |
contentLength = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
296 |
} else { |
48083 | 297 |
contentLength = requestPublisher.contentLength(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
298 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
299 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
300 |
if (contentLength == 0) { |
50681 | 301 |
systemHeadersBuilder.setHeader("Content-Length", "0"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
} else if (contentLength > 0) { |
50681 | 303 |
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
|
304 |
streaming = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
streaming = true; |
50681 | 307 |
systemHeadersBuilder.setHeader("Transfer-encoding", "chunked"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
} |
48083 | 309 |
collectHeaders0(sb); |
310 |
String hs = sb.toString(); |
|
311 |
logHeaders(hs); |
|
312 |
ByteBuffer b = ByteBuffer.wrap(hs.getBytes(US_ASCII)); |
|
313 |
return List.of(b); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
|
48083 | 316 |
Http1BodySubscriber continueRequest() { |
317 |
Http1BodySubscriber subscriber; |
|
318 |
if (streaming) { |
|
319 |
subscriber = new StreamSubscriber(); |
|
320 |
requestPublisher.subscribe(subscriber); |
|
321 |
} else { |
|
322 |
if (contentLength == 0) |
|
323 |
return null; |
|
324 |
||
325 |
subscriber = new FixedContentSubscriber(); |
|
326 |
requestPublisher.subscribe(subscriber); |
|
327 |
} |
|
328 |
return subscriber; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
330 |
|
49765 | 331 |
final class StreamSubscriber extends Http1BodySubscriber { |
332 |
||
333 |
StreamSubscriber() { super(debug); } |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
334 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
335 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
public void onSubscribe(Flow.Subscription subscription) { |
49765 | 337 |
if (isSubscribed()) { |
48083 | 338 |
Throwable t = new IllegalStateException("already subscribed"); |
339 |
http1Exchange.appendToOutgoing(t); |
|
340 |
} else { |
|
49765 | 341 |
setSubscription(subscription); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
public void onNext(ByteBuffer item) { |
48083 | 347 |
Objects.requireNonNull(item); |
348 |
if (complete) { |
|
349 |
Throwable t = new IllegalStateException("subscription already completed"); |
|
350 |
http1Exchange.appendToOutgoing(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
} else { |
48083 | 352 |
int chunklen = item.remaining(); |
353 |
ArrayList<ByteBuffer> l = new ArrayList<>(3); |
|
354 |
l.add(getHeader(chunklen)); |
|
355 |
l.add(item); |
|
356 |
l.add(ByteBuffer.wrap(CRLF)); |
|
357 |
http1Exchange.appendToOutgoing(l); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
@Override |
50681 | 362 |
public String currentStateMessage() { |
363 |
return "streaming request body " + (complete ? "complete" : "incomplete"); |
|
364 |
} |
|
365 |
||
366 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
public void onError(Throwable throwable) { |
48083 | 368 |
if (complete) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
return; |
48083 | 370 |
|
49765 | 371 |
cancelSubscription(); |
48083 | 372 |
http1Exchange.appendToOutgoing(throwable); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
373 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
374 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
375 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
public void onComplete() { |
48083 | 377 |
if (complete) { |
378 |
Throwable t = new IllegalStateException("subscription already completed"); |
|
379 |
http1Exchange.appendToOutgoing(t); |
|
380 |
} else { |
|
381 |
ArrayList<ByteBuffer> l = new ArrayList<>(2); |
|
382 |
l.add(ByteBuffer.wrap(EMPTY_CHUNK_BYTES)); |
|
383 |
l.add(ByteBuffer.wrap(CRLF)); |
|
384 |
complete = true; |
|
385 |
//setFinished(); |
|
386 |
http1Exchange.appendToOutgoing(l); |
|
387 |
http1Exchange.appendToOutgoing(COMPLETED); |
|
388 |
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
|
389 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
390 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
393 |
|
49765 | 394 |
final class FixedContentSubscriber extends Http1BodySubscriber { |
48083 | 395 |
|
396 |
private volatile long contentWritten; |
|
49765 | 397 |
FixedContentSubscriber() { super(debug); } |
48083 | 398 |
|
399 |
@Override |
|
400 |
public void onSubscribe(Flow.Subscription subscription) { |
|
49765 | 401 |
if (isSubscribed()) { |
48083 | 402 |
Throwable t = new IllegalStateException("already subscribed"); |
403 |
http1Exchange.appendToOutgoing(t); |
|
404 |
} else { |
|
49765 | 405 |
setSubscription(subscription); |
48083 | 406 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
407 |
} |
48083 | 408 |
|
409 |
@Override |
|
410 |
public void onNext(ByteBuffer item) { |
|
49765 | 411 |
if (debug.on()) debug.log("onNext"); |
48083 | 412 |
Objects.requireNonNull(item); |
413 |
if (complete) { |
|
414 |
Throwable t = new IllegalStateException("subscription already completed"); |
|
415 |
http1Exchange.appendToOutgoing(t); |
|
416 |
} else { |
|
417 |
long writing = item.remaining(); |
|
418 |
long written = (contentWritten += writing); |
|
419 |
||
420 |
if (written > contentLength) { |
|
49765 | 421 |
cancelSubscription(); |
48083 | 422 |
String msg = connection.getConnectionFlow() |
423 |
+ " [" + Thread.currentThread().getName() +"] " |
|
424 |
+ "Too many bytes in request body. Expected: " |
|
425 |
+ contentLength + ", got: " + written; |
|
426 |
http1Exchange.appendToOutgoing(new IOException(msg)); |
|
427 |
} else { |
|
428 |
http1Exchange.appendToOutgoing(List.of(item)); |
|
429 |
} |
|
430 |
} |
|
431 |
} |
|
432 |
||
433 |
@Override |
|
50681 | 434 |
public String currentStateMessage() { |
435 |
return format("fixed content-length: %d, bytes sent: %d", |
|
436 |
contentLength, contentWritten); |
|
437 |
} |
|
438 |
||
439 |
@Override |
|
48083 | 440 |
public void onError(Throwable throwable) { |
49765 | 441 |
if (debug.on()) debug.log("onError"); |
48083 | 442 |
if (complete) // TODO: error? |
443 |
return; |
|
444 |
||
49765 | 445 |
cancelSubscription(); |
48083 | 446 |
http1Exchange.appendToOutgoing(throwable); |
447 |
} |
|
448 |
||
449 |
@Override |
|
450 |
public void onComplete() { |
|
49765 | 451 |
if (debug.on()) debug.log("onComplete"); |
48083 | 452 |
if (complete) { |
453 |
Throwable t = new IllegalStateException("subscription already completed"); |
|
454 |
http1Exchange.appendToOutgoing(t); |
|
455 |
} else { |
|
456 |
complete = true; |
|
457 |
long written = contentWritten; |
|
458 |
if (contentLength > written) { |
|
49765 | 459 |
cancelSubscription(); |
48083 | 460 |
Throwable t = new IOException(connection.getConnectionFlow() |
461 |
+ " [" + Thread.currentThread().getName() +"] " |
|
462 |
+ "Too few bytes returned by the publisher (" |
|
463 |
+ written + "/" |
|
464 |
+ contentLength + ")"); |
|
465 |
http1Exchange.appendToOutgoing(t); |
|
466 |
} else { |
|
467 |
http1Exchange.appendToOutgoing(COMPLETED); |
|
468 |
} |
|
469 |
} |
|
470 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
471 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
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
|
474 |
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
|
475 |
|
48083 | 476 |
/** Returns a header for a particular chunk size */ |
477 |
private static ByteBuffer getHeader(int size) { |
|
478 |
String hexStr = Integer.toHexString(size); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
479 |
byte[] hexBytes = hexStr.getBytes(US_ASCII); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
480 |
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
|
481 |
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
|
482 |
header[hexBytes.length] = CRLF[0]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
483 |
header[hexBytes.length+1] = CRLF[1]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
484 |
return ByteBuffer.wrap(header); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
485 |
} |
48083 | 486 |
|
49765 | 487 |
final Logger debug = Utils.getDebugLogger(this::toString, Utils.DEBUG); |
48083 | 488 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
489 |
} |