forked from robaho/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExchangeImpl.java
More file actions
496 lines (433 loc) · 15.1 KB
/
ExchangeImpl.java
File metadata and controls
496 lines (433 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package robaho.net.httpserver;
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import java.util.*;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import com.sun.net.httpserver.*;
import robaho.net.httpserver.websockets.WebSocketHandler;
class ExchangeImpl {
Headers reqHdrs, rspHdrs;
Request req;
String method;
URI uri;
HttpConnection connection;
long reqContentLen;
long rspContentLen;
/* raw streams which access the socket directly */
InputStream ris;
OutputStream ros;
/* close the underlying connection when this exchange finished */
boolean close;
boolean closed;
boolean http10 = false;
/* for formatting the Date: header */
private static final DateTimeFormatter FORMATTER;
static {
String pattern = "EEE, dd MMM yyyy HH:mm:ss zzz";
FORMATTER = DateTimeFormatter.ofPattern(pattern, Locale.US)
.withZone(ZoneId.of("GMT"));
}
private static final String HEAD = "HEAD";
private static final String CONNECT = "CONNECT";
/*
* streams which take care of the HTTP protocol framing
* and are passed up to higher layers
*/
InputStream uis;
OutputStream uos;
LeftOverInputStream uis_orig; // uis may have be a user supplied wrapper
PlaceholderOutputStream uos_orig;
/* true after response headers sent */
volatile boolean sentHeaders;
Map<String, Object> attributes;
int rcode = -1;
HttpPrincipal principal;
final boolean websocket;
ExchangeImpl(
String m, URI u, Request req, long len, HttpConnection connection) throws IOException {
this.req = req;
// make a mutable copy to allow HttpHandler to modify in chain
this.reqHdrs = new Headers();
reqHdrs.putAll(req.headers());
this.rspHdrs = new Headers();
this.method = m;
this.uri = u;
this.connection = connection;
this.websocket = WebSocketHandler.isWebsocketRequested(this.reqHdrs);
if (this.websocket) {
// length is indeterminate
len = -1;
}
this.reqContentLen = len;
/* ros only used for headers, body written directly to stream */
this.ros = req.outputStream();
this.ris = req.inputStream();
}
public Headers getRequestHeaders() {
return reqHdrs;
}
public Headers getResponseHeaders() {
return rspHdrs;
}
public URI getRequestURI() {
return uri;
}
public String getRequestMethod() {
return method;
}
public HttpContextImpl getHttpContext() {
return connection.getHttpContext();
}
private boolean isHeadRequest() {
return HEAD.equals(getRequestMethod());
}
private boolean isConnectRequest() {
return CONNECT.equals(getRequestMethod());
}
public void close() {
if (closed) {
return;
}
closed = true;
/*
* close the underlying connection if,
* a) the streams not set up yet, no response can be sent, or
* b) if the wrapper output stream is not set up, or
* c) if the close of the input/outpu stream fails
*/
try {
if (uis_orig == null || uos == null) {
connection.close();
return;
}
if (!uos_orig.isWrapped()) {
connection.close();
return;
}
if (!uis_orig.isClosed()) {
uis_orig.close();
}
uos.close();
} catch (IOException e) {
connection.close();
}
}
public InputStream getRequestBody() {
if (uis != null) {
return uis;
}
if (websocket || isConnectRequest()) {
// connection cannot be re-used
uis = ris;
} else if (reqContentLen == -1L) {
uis_orig = new ChunkedInputStream(this, ris);
uis = uis_orig;
} else {
uis_orig = new FixedLengthInputStream(this, ris, reqContentLen);
uis = uis_orig;
}
return uis;
}
LeftOverInputStream getOriginalInputStream() {
return uis_orig;
}
public int getResponseCode() {
return rcode;
}
public OutputStream getResponseBody() {
/*
* TODO. Change spec to remove restriction below. Filters
* cannot work with this restriction
*
* if (!sentHeaders) {
* throw new IllegalStateException ("headers not sent");
* }
*/
if (uos == null) {
uos_orig = new PlaceholderOutputStream(null);
uos = uos_orig;
}
return uos;
}
/*
* returns the place holder stream, which is the stream
* returned from the 1st call to getResponseBody()
* The "real" ouputstream is then placed inside this
*/
PlaceholderOutputStream getPlaceholderResponseBody() {
getResponseBody();
return uos_orig;
}
// hard-coded formatting for Date header, rather than using slower DateFormatter
public void sendResponseHeaders(int rCode, long contentLen)
throws IOException {
final Logger logger = getServerImpl().getLogger();
if (sentHeaders) {
throw new IOException("headers already sent");
}
this.rcode = rCode;
String statusLine = rCode == 101 ? "HTTP/1.1 101 Switching Protocols\r\n"
: "HTTP/1.1 " + rCode + Code.msg(rCode) + "\r\n";
PlaceholderOutputStream o = getPlaceholderResponseBody();
ros.write(statusLine.getBytes(ISO_CHARSET));
boolean noContentToSend = false; // assume there is content
boolean noContentLengthHeader = false; // must not send Content-length is set
rspHdrs.set("Date", ActivityTimer.dateAndTime());
Integer bufferSize = (Integer)this.getAttribute(Attributes.SOCKET_WRITE_BUFFER);
if(bufferSize!=null) {
getConnection().getSocket().setOption(StandardSocketOptions.SO_SNDBUF, bufferSize);
}
/* check for response type that is not allowed to send a body */
if (rCode == 101) {
logger.log(Level.DEBUG, () -> "switching protocols");
if (contentLen != 0) {
String msg = "sendResponseHeaders: rCode = " + rCode
+ ": forcing contentLen = 0";
logger.log(Level.WARNING, msg);
}
contentLen = 0;
} else if ((rCode >= 100 && rCode < 200) /* informational */
|| (rCode == 204) /* no content */
|| (rCode == 304)) /* not modified */
{
if (contentLen != -1) {
String msg = "sendResponseHeaders: rCode = " + rCode
+ ": forcing contentLen = -1";
logger.log(Level.WARNING, msg);
}
contentLen = -1;
noContentLengthHeader = (rCode != 304);
}
if (isHeadRequest() || rCode == 304) {
/*
* HEAD requests or 304 responses should not set a content length by passing it
* through this API, but should instead manually set the required
* headers.
*/
if (contentLen >= 0) {
logger.log(Level.WARNING, "sendResponseHeaders: being invoked with a content length for a HEAD request");
}
noContentToSend = true;
contentLen = 0;
o.setWrappedStream(new FixedLengthOutputStream(this, ros, contentLen));
} else { /* not a HEAD request or 304 response */
if (contentLen == 0) {
if (websocket || isConnectRequest()) {
o.setWrappedStream(ros);
close = true;
}
else if (http10) {
o.setWrappedStream(new UndefLengthOutputStream(this, ros));
close = true;
} else {
rspHdrs.set("Transfer-encoding", "chunked");
o.setWrappedStream(new ChunkedOutputStream(this, ros));
}
} else {
if (contentLen == -1) {
noContentToSend = true;
contentLen = 0;
}
if (!noContentLengthHeader) {
rspHdrs.set("Content-length", Long.toString(contentLen));
}
o.setWrappedStream(new FixedLengthOutputStream(this, ros, contentLen));
}
}
// A custom handler can request that the connection be
// closed after the exchange by supplying Connection: close
// to the response header. Nothing to do if the exchange is
// already set up to be closed.
if (!close) {
List<String> values = rspHdrs.get("Connection");
if(values!=null) {
for(var val : values) {
if(val.equalsIgnoreCase("close")) {
logger.log(Level.DEBUG, "Connection: close requested by handler");
close=true;
break;
}
}
}
}
writeHeaders(rspHdrs, ros);
this.rspContentLen = contentLen;
sentHeaders = true;
if(logger.isLoggable(Level.TRACE)) {
logger.log(Level.TRACE, "Sent headers: noContentToSend=" + noContentToSend);
}
if(contentLen==0) {
ros.flush();
}
if (noContentToSend) {
close();
}
getServerImpl().logReply(rCode, req.requestLine(), null);
}
static final Charset ISO_CHARSET = StandardCharsets.ISO_8859_1;
static final String colonSpace = ": ";
static final String CRNL = "\r\n";
private static void outputAscii(String s,OutputStream os) throws IOException {
os.write(s.getBytes(ISO_CHARSET));
}
void writeHeaders(Headers map, OutputStream os) throws IOException {
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
for (String val : entry.getValue()) {
outputAscii(key,os);
outputAscii(colonSpace,os);
outputAscii(val,os);
outputAscii(CRNL,os);
}
}
outputAscii(CRNL,os);
}
public InetSocketAddress getRemoteAddress() {
Socket s = connection.getSocket();
InetAddress ia = s.getInetAddress();
int port = s.getPort();
return new InetSocketAddress(ia, port);
}
public InetSocketAddress getLocalAddress() {
Socket s = connection.getSocket();
InetAddress ia = s.getLocalAddress();
int port = s.getLocalPort();
return new InetSocketAddress(ia, port);
}
public String getProtocol() {
String reqline = req.requestLine();
int index = reqline.lastIndexOf(" ");
return reqline.substring(index + 1);
}
public SSLSession getSSLSession() {
return connection.getSSLSession();
}
public Object getAttribute(String name) {
if (name == null) {
throw new NullPointerException("null name parameter");
}
if (attributes == null) {
attributes = getHttpContext().getAttributes();
}
return attributes.get(name);
}
public void setAttribute(String name, Object value) {
if (name == null) {
throw new NullPointerException("null name parameter");
}
if (attributes == null) {
attributes = getHttpContext().getAttributes();
}
if (value != null) {
attributes.put(name, value);
} else {
attributes.remove(name);
}
}
public void setStreams(InputStream i, OutputStream o) {
assert uis != null;
if (i != null) {
uis = i;
}
if (o != null) {
uos = o;
}
}
/**
* PP
*/
HttpConnection getConnection() {
return connection;
}
ServerImpl getServerImpl() {
return getHttpContext().getServerImpl();
}
public HttpPrincipal getPrincipal() {
return principal;
}
void setPrincipal(HttpPrincipal principal) {
this.principal = principal;
}
static ExchangeImpl get(HttpExchange t) {
if (t instanceof HttpExchangeImpl) {
return ((HttpExchangeImpl) t).getExchangeImpl();
} else {
assert t instanceof HttpsExchangeImpl;
return ((HttpsExchangeImpl) t).getExchangeImpl();
}
}
}
/**
* An OutputStream which wraps another stream
* which is supplied either at creation time, or sometime later.
* If a caller/user tries to write to this stream before
* the wrapped stream has been provided, then an IOException will
* be thrown.
*/
class PlaceholderOutputStream extends java.io.OutputStream {
OutputStream wrapped;
PlaceholderOutputStream(OutputStream os) {
wrapped = os;
}
void setWrappedStream(OutputStream os) {
wrapped = os;
}
boolean isWrapped() {
return wrapped != null;
}
private void checkWrap() throws IOException {
if (wrapped == null) {
throw new IOException("response headers not sent yet");
}
}
public void write(int b) throws IOException {
checkWrap();
wrapped.write(b);
}
public void write(byte b[]) throws IOException {
checkWrap();
wrapped.write(b);
}
public void write(byte b[], int off, int len) throws IOException {
checkWrap();
wrapped.write(b, off, len);
}
public void flush() throws IOException {
checkWrap();
wrapped.flush();
}
public void close() throws IOException {
checkWrap();
wrapped.close();
}
}