36131
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2016, 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 |
*/
|
|
24 |
package java.net.http;
|
|
25 |
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.net.URI;
|
|
28 |
import java.nio.ByteBuffer;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.Map;
|
|
31 |
import java.util.Set;
|
|
32 |
import java.util.function.LongConsumer;
|
|
33 |
import static java.net.http.HttpClient.Version.HTTP_1_1;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Handles a HTTP/1.1 response in two blocking calls. readHeaders() and
|
|
37 |
* readBody(). There can be more than one of these per Http exchange.
|
|
38 |
*/
|
|
39 |
class Http1Response {
|
|
40 |
|
|
41 |
private ResponseContent content;
|
|
42 |
private final HttpRequestImpl request;
|
|
43 |
HttpResponseImpl response;
|
|
44 |
private final HttpConnection connection;
|
|
45 |
private ResponseHeaders headers;
|
|
46 |
private int responseCode;
|
|
47 |
private ByteBuffer buffer; // same buffer used for reading status line and headers
|
|
48 |
private final Http1Exchange exchange;
|
|
49 |
private final boolean redirecting; // redirecting
|
|
50 |
private boolean return2Cache; // return connection to cache when finished
|
|
51 |
|
|
52 |
Http1Response(HttpConnection conn, Http1Exchange exchange) {
|
|
53 |
this.request = exchange.request();
|
|
54 |
this.exchange = exchange;
|
|
55 |
this.connection = conn;
|
|
56 |
this.redirecting = false;
|
|
57 |
buffer = connection.getRemaining();
|
|
58 |
}
|
|
59 |
|
|
60 |
// called when the initial read should come from a buffer left
|
|
61 |
// over from a previous response.
|
|
62 |
void setBuffer(ByteBuffer buffer) {
|
|
63 |
this.buffer = buffer;
|
|
64 |
}
|
|
65 |
|
|
66 |
@SuppressWarnings("unchecked")
|
|
67 |
public void readHeaders() throws IOException {
|
|
68 |
String statusline = readStatusLine();
|
|
69 |
if (statusline == null) {
|
|
70 |
if (Log.errors()) {
|
|
71 |
Log.logError("Connection closed. Retry");
|
|
72 |
}
|
|
73 |
connection.close();
|
|
74 |
// connection was closed
|
|
75 |
throw new IOException("Connection closed");
|
|
76 |
}
|
|
77 |
if (!statusline.startsWith("HTTP/1.")) {
|
|
78 |
throw new IOException("Invalid status line: " + statusline);
|
|
79 |
}
|
|
80 |
char c = statusline.charAt(7);
|
|
81 |
responseCode = Integer.parseInt(statusline.substring(9, 12));
|
|
82 |
|
|
83 |
headers = new ResponseHeaders(connection, buffer);
|
|
84 |
headers.initHeaders();
|
|
85 |
if (Log.headers()) {
|
|
86 |
logHeaders(headers);
|
|
87 |
}
|
|
88 |
response = new HttpResponseImpl(responseCode,
|
|
89 |
exchange.exchange,
|
|
90 |
headers,
|
|
91 |
null,
|
|
92 |
connection.sslParameters(),
|
|
93 |
HTTP_1_1,
|
|
94 |
connection);
|
|
95 |
}
|
|
96 |
|
|
97 |
private boolean finished;
|
|
98 |
|
|
99 |
synchronized void completed() {
|
|
100 |
finished = true;
|
|
101 |
}
|
|
102 |
|
|
103 |
synchronized boolean finished() {
|
|
104 |
return finished;
|
|
105 |
}
|
|
106 |
|
|
107 |
// Blocking flow controller implementation. Only works when a
|
|
108 |
// thread is dedicated to reading response body
|
|
109 |
|
|
110 |
static class FlowController implements LongConsumer {
|
|
111 |
long window ;
|
|
112 |
|
|
113 |
@Override
|
|
114 |
public synchronized void accept(long value) {
|
|
115 |
window += value;
|
|
116 |
notifyAll();
|
|
117 |
}
|
|
118 |
|
|
119 |
public synchronized void request(long value) throws InterruptedException {
|
|
120 |
while (window < value) {
|
|
121 |
wait();
|
|
122 |
}
|
|
123 |
window -= value;
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
FlowController flowController;
|
|
128 |
|
|
129 |
int fixupContentLen(int clen) {
|
|
130 |
if (request.method().equalsIgnoreCase("HEAD")) {
|
|
131 |
return 0;
|
|
132 |
}
|
|
133 |
if (clen == -1) {
|
|
134 |
if (headers.firstValue("Transfer-encoding").orElse("")
|
|
135 |
.equalsIgnoreCase("chunked")) {
|
|
136 |
return -1;
|
|
137 |
}
|
|
138 |
return 0;
|
|
139 |
}
|
|
140 |
return clen;
|
|
141 |
}
|
|
142 |
|
|
143 |
private void returnBuffer(ByteBuffer buf) {
|
|
144 |
// not currently used, but will be when we change SSL to use fixed
|
|
145 |
// sized buffers and a single buffer pool for HttpClientImpl
|
|
146 |
}
|
|
147 |
|
|
148 |
@SuppressWarnings("unchecked")
|
|
149 |
public <T> T readBody(java.net.http.HttpResponse.BodyProcessor<T> p,
|
|
150 |
boolean return2Cache)
|
|
151 |
throws IOException
|
|
152 |
{
|
|
153 |
T body = null; // TODO: check null case below
|
|
154 |
this.return2Cache = return2Cache;
|
|
155 |
final java.net.http.HttpResponse.BodyProcessor<T> pusher = p;
|
|
156 |
|
|
157 |
int clen0 = headers.getContentLength();
|
|
158 |
final int clen = fixupContentLen(clen0);
|
|
159 |
|
|
160 |
flowController = new FlowController();
|
|
161 |
|
|
162 |
body = pusher.onResponseBodyStart(clen, headers, flowController);
|
|
163 |
|
|
164 |
ExecutorWrapper executor;
|
|
165 |
if (body == null) {
|
|
166 |
executor = ExecutorWrapper.callingThread();
|
|
167 |
} else {
|
|
168 |
executor = request.client().executorWrapper();
|
|
169 |
}
|
|
170 |
|
|
171 |
final ResponseHeaders h = headers;
|
|
172 |
if (body == null) {
|
|
173 |
content = new ResponseContent(connection,
|
|
174 |
clen,
|
|
175 |
h,
|
|
176 |
pusher,
|
|
177 |
flowController);
|
|
178 |
content.pushBody(headers.getResidue());
|
|
179 |
body = pusher.onResponseComplete();
|
|
180 |
completed();
|
|
181 |
onFinished();
|
|
182 |
return body;
|
|
183 |
} else {
|
|
184 |
executor.execute(() -> {
|
|
185 |
try {
|
|
186 |
content = new ResponseContent(connection,
|
|
187 |
clen,
|
|
188 |
h,
|
|
189 |
pusher,
|
|
190 |
flowController);
|
|
191 |
content.pushBody(headers.getResidue());
|
|
192 |
pusher.onResponseComplete();
|
|
193 |
completed();
|
|
194 |
onFinished();
|
|
195 |
} catch (Throwable e) {
|
|
196 |
pusher.onResponseError(e);
|
|
197 |
}
|
|
198 |
},
|
|
199 |
() -> response.getAccessControlContext());
|
|
200 |
}
|
|
201 |
return body;
|
|
202 |
}
|
|
203 |
|
|
204 |
private void onFinished() {
|
|
205 |
connection.buffer = content.getResidue();
|
|
206 |
if (return2Cache) {
|
|
207 |
connection.returnToCache(headers);
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
private void logHeaders(ResponseHeaders headers) {
|
|
212 |
Map<String, List<String>> h = headers.mapInternal();
|
|
213 |
Set<String> keys = h.keySet();
|
|
214 |
Set<Map.Entry<String, List<String>>> entries = h.entrySet();
|
|
215 |
for (Map.Entry<String, List<String>> entry : entries) {
|
|
216 |
String key = entry.getKey();
|
|
217 |
StringBuilder sb = new StringBuilder();
|
|
218 |
sb.append(key).append(": ");
|
|
219 |
List<String> values = entry.getValue();
|
|
220 |
if (values != null) {
|
|
221 |
for (String value : values) {
|
|
222 |
sb.append(value).append(' ');
|
|
223 |
}
|
|
224 |
}
|
|
225 |
Log.logHeaders(sb.toString());
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
HttpResponseImpl response() {
|
|
230 |
return response;
|
|
231 |
}
|
|
232 |
|
|
233 |
boolean redirecting() {
|
|
234 |
return redirecting;
|
|
235 |
}
|
|
236 |
|
|
237 |
HttpHeaders responseHeaders() {
|
|
238 |
return headers;
|
|
239 |
}
|
|
240 |
|
|
241 |
int responseCode() {
|
|
242 |
return responseCode;
|
|
243 |
}
|
|
244 |
|
|
245 |
static final char CR = '\r';
|
|
246 |
static final char LF = '\n';
|
|
247 |
|
|
248 |
private ByteBuffer getBuffer() throws IOException {
|
|
249 |
if (buffer == null || !buffer.hasRemaining()) {
|
|
250 |
buffer = connection.read();
|
|
251 |
}
|
|
252 |
return buffer;
|
|
253 |
}
|
|
254 |
|
|
255 |
ByteBuffer buffer() {
|
|
256 |
return buffer;
|
|
257 |
}
|
|
258 |
|
|
259 |
String readStatusLine() throws IOException {
|
|
260 |
boolean cr = false;
|
|
261 |
StringBuilder statusLine = new StringBuilder(128);
|
|
262 |
ByteBuffer b;
|
|
263 |
while ((b = getBuffer()) != null) {
|
|
264 |
byte[] buf = b.array();
|
|
265 |
int offset = b.position();
|
|
266 |
int len = b.limit() - offset;
|
|
267 |
|
|
268 |
for (int i = 0; i < len; i++) {
|
|
269 |
char c = (char) buf[i+offset];
|
|
270 |
|
|
271 |
if (cr) {
|
|
272 |
if (c == LF) {
|
|
273 |
b.position(i + 1 + offset);
|
|
274 |
return statusLine.toString();
|
|
275 |
} else {
|
|
276 |
throw new IOException("invalid status line");
|
|
277 |
}
|
|
278 |
}
|
|
279 |
if (c == CR) {
|
|
280 |
cr = true;
|
|
281 |
} else {
|
|
282 |
statusLine.append(c);
|
|
283 |
}
|
|
284 |
}
|
|
285 |
// unlikely, but possible, that multiple reads required
|
|
286 |
b.position(b.limit());
|
|
287 |
}
|
|
288 |
return null;
|
|
289 |
}
|
|
290 |
}
|