/
Http2ServerResponseImpl.java
671 lines (592 loc) · 16.8 KB
/
Http2ServerResponseImpl.java
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
* Copyright (c) 2011-2017 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.http.impl;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.handler.codec.http2.Http2Headers;
import io.vertx.codegen.annotations.Nullable;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.http.StreamResetException;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.net.impl.ConnectionBase;
import io.vertx.core.spi.metrics.HttpServerMetrics;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import static io.vertx.core.spi.metrics.Metrics.METRICS_ENABLED;
/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public class Http2ServerResponseImpl implements HttpServerResponse {
private static final Logger log = LoggerFactory.getLogger(Http2ServerResponseImpl.class);
private final VertxHttp2Stream stream;
private final ChannelHandlerContext ctx;
private final Http2ServerConnection conn;
private final boolean push;
private final Object metric;
private final String host;
private Http2Headers headers = new DefaultHttp2Headers();
private Http2HeadersAdaptor headersMap;
private Http2Headers trailers;
private Http2HeadersAdaptor trailedMap;
private boolean chunked;
private boolean headWritten;
private boolean ended;
private int statusCode = 200;
private String statusMessage; // Not really used but we keep the message for the getStatusMessage()
private Handler<Void> drainHandler;
private Handler<Throwable> exceptionHandler;
private Handler<Void> headersEndHandler;
private Handler<Void> bodyEndHandler;
private Handler<Void> closeHandler;
private Handler<Void> endHandler;
private long bytesWritten;
private int numPush;
private boolean inHandler;
public Http2ServerResponseImpl(Http2ServerConnection conn, VertxHttp2Stream stream, Object metric, boolean push, String contentEncoding, String host) {
this.metric = metric;
this.stream = stream;
this.ctx = conn.handlerContext;
this.conn = conn;
this.push = push;
this.host = host;
if (contentEncoding != null) {
putHeader(HttpHeaderNames.CONTENT_ENCODING, contentEncoding);
}
}
public Http2ServerResponseImpl(
Http2ServerConnection conn,
VertxHttp2Stream stream,
HttpMethod method,
String path,
boolean push,
String contentEncoding) {
this.stream = stream;
this.ctx = conn.handlerContext;
this.conn = conn;
this.push = push;
this.host = null;
if (contentEncoding != null) {
putHeader(HttpHeaderNames.CONTENT_ENCODING, contentEncoding);
}
HttpServerMetrics metrics = conn.metrics();
this.metric = (METRICS_ENABLED && metrics != null) ? metrics.responsePushed(conn.metric(), method, path, this) : null;
}
synchronized void beginRequest() {
inHandler = true;
}
synchronized boolean endRequest() {
inHandler = false;
return numPush > 0;
}
void callReset(long code) {
handleEnded(true);
handleError(new StreamResetException(code));
}
void handleError(Throwable cause) {
if (exceptionHandler != null) {
exceptionHandler.handle(cause);
}
}
void handleClose() {
handleEnded(true);
}
private void checkHeadWritten() {
if (headWritten) {
throw new IllegalStateException("Header already sent");
}
}
@Override
public HttpServerResponse exceptionHandler(Handler<Throwable> handler) {
synchronized (conn) {
if (handler != null) {
checkEnded();
}
exceptionHandler = handler;
return this;
}
}
@Override
public int getStatusCode() {
synchronized (conn) {
return statusCode;
}
}
@Override
public HttpServerResponse setStatusCode(int statusCode) {
if (statusCode < 0) {
throw new IllegalArgumentException("code: " + statusCode + " (expected: 0+)");
}
synchronized (conn) {
checkHeadWritten();
this.statusCode = statusCode;
return this;
}
}
@Override
public String getStatusMessage() {
synchronized (conn) {
if (statusMessage == null) {
return HttpResponseStatus.valueOf(statusCode).reasonPhrase();
}
return statusMessage;
}
}
@Override
public HttpServerResponse setStatusMessage(String statusMessage) {
synchronized (conn) {
checkHeadWritten();
this.statusMessage = statusMessage;
return this;
}
}
@Override
public HttpServerResponse setChunked(boolean chunked) {
synchronized (conn) {
checkHeadWritten();
this.chunked = true;
return this;
}
}
@Override
public boolean isChunked() {
synchronized (conn) {
return chunked;
}
}
@Override
public MultiMap headers() {
synchronized (conn) {
if (headersMap == null) {
headersMap = new Http2HeadersAdaptor(headers);
}
return headersMap;
}
}
@Override
public HttpServerResponse putHeader(String name, String value) {
synchronized (conn) {
checkHeadWritten();
headers().set(name, value);
return this;
}
}
@Override
public HttpServerResponse putHeader(CharSequence name, CharSequence value) {
synchronized (conn) {
checkHeadWritten();
headers().set(name, value);
return this;
}
}
@Override
public HttpServerResponse putHeader(String name, Iterable<String> values) {
synchronized (conn) {
checkHeadWritten();
headers().set(name, values);
return this;
}
}
@Override
public HttpServerResponse putHeader(CharSequence name, Iterable<CharSequence> values) {
synchronized (conn) {
checkHeadWritten();
headers().set(name, values);
return this;
}
}
@Override
public MultiMap trailers() {
synchronized (conn) {
if (trailedMap == null) {
trailedMap = new Http2HeadersAdaptor(trailers = new DefaultHttp2Headers());
}
return trailedMap;
}
}
@Override
public HttpServerResponse putTrailer(String name, String value) {
synchronized (conn) {
checkEnded();
trailers().set(name, value);
return this;
}
}
@Override
public HttpServerResponse putTrailer(CharSequence name, CharSequence value) {
synchronized (conn) {
checkEnded();
trailers().set(name, value);
return this;
}
}
@Override
public HttpServerResponse putTrailer(String name, Iterable<String> values) {
synchronized (conn) {
checkEnded();
trailers().set(name, values);
return this;
}
}
@Override
public HttpServerResponse putTrailer(CharSequence name, Iterable<CharSequence> value) {
synchronized (conn) {
checkEnded();
trailers().set(name, value);
return this;
}
}
@Override
public HttpServerResponse closeHandler(Handler<Void> handler) {
synchronized (conn) {
if (handler != null) {
checkEnded();
}
closeHandler = handler;
return this;
}
}
@Override
public HttpServerResponse endHandler(@Nullable Handler<Void> handler) {
synchronized (conn) {
if (handler != null) {
checkEnded();
}
endHandler = handler;
return this;
}
}
@Override
public HttpServerResponse writeContinue() {
synchronized (conn) {
checkHeadWritten();
stream.writeHeaders(new DefaultHttp2Headers().status("100"), false);
ctx.flush();
return this;
}
}
@Override
public HttpServerResponse write(Buffer chunk) {
ByteBuf buf = chunk.getByteBuf();
return write(buf);
}
@Override
public HttpServerResponse write(String chunk, String enc) {
return write(Buffer.buffer(chunk, enc).getByteBuf());
}
@Override
public HttpServerResponse write(String chunk) {
return write(Buffer.buffer(chunk).getByteBuf());
}
private Http2ServerResponseImpl write(ByteBuf chunk) {
write(chunk, false);
return this;
}
@Override
public void end(String chunk) {
end(Buffer.buffer(chunk));
}
@Override
public void end(String chunk, String enc) {
end(Buffer.buffer(chunk, enc));
}
@Override
public void end(Buffer chunk) {
end(chunk.getByteBuf());
}
@Override
public void end() {
end((ByteBuf) null);
}
void toNetSocket() {
checkEnded();
checkSendHeaders(false);
handleEnded(false);
}
private void end(ByteBuf chunk) {
synchronized (conn) {
write(chunk, true);
}
}
private boolean checkSendHeaders(boolean end) {
if (!headWritten) {
if (headersEndHandler != null) {
headersEndHandler.handle(null);
}
headWritten = true;
headers.status(Integer.toString(statusCode));
stream.writeHeaders(headers, end);
if (end) {
ctx.flush();
}
return true;
} else {
return false;
}
}
void write(ByteBuf chunk, boolean end) {
synchronized (conn) {
checkEnded();
boolean hasBody = false;
if (chunk != null) {
hasBody = true;
bytesWritten += chunk.readableBytes();
} else {
chunk = Unpooled.EMPTY_BUFFER;
}
if (end) {
if (!headWritten && !headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
headers().set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(chunk.readableBytes()));
}
handleEnded(false);
}
boolean sent = checkSendHeaders(end && !hasBody && trailers == null);
if (hasBody || (!sent && end)) {
stream.writeData(chunk, end && trailers == null);
}
if (end && trailers != null) {
stream.writeHeaders(trailers, true);
}
if (end && bodyEndHandler != null) {
bodyEndHandler.handle(null);
}
}
}
@Override
public HttpServerResponse writeCustomFrame(int type, int flags, Buffer payload) {
synchronized (conn) {
checkEnded();
checkSendHeaders(false);
stream.writeFrame(type, flags, payload.getByteBuf());
ctx.flush();
return this;
}
}
private void checkEnded() {
if (ended) {
throw new IllegalStateException("Response has already been written");
}
}
private void handleEnded(boolean failed) {
if (!ended) {
ended = true;
if (METRICS_ENABLED && metric != null) {
// Null in case of push response : handle this case
if (failed) {
conn.metrics().requestReset(metric);
} else {
conn.reportBytesWritten(bytesWritten);
conn.metrics().responseEnd(metric, this);
}
}
if (exceptionHandler != null) {
conn.getContext().runOnContext(v -> exceptionHandler.handle(ConnectionBase.CLOSED_EXCEPTION));
}
if (endHandler != null) {
conn.getContext().runOnContext(endHandler);
}
if (closeHandler != null) {
conn.getContext().runOnContext(closeHandler);
}
}
}
void writabilityChanged() {
if (!ended && !writeQueueFull() && drainHandler != null) {
drainHandler.handle(null);
}
}
@Override
public boolean writeQueueFull() {
synchronized (conn) {
checkEnded();
return stream.isNotWritable();
}
}
@Override
public HttpServerResponse setWriteQueueMaxSize(int maxSize) {
synchronized (conn) {
checkEnded();
// It does not seem to be possible to configure this at the moment
}
return this;
}
@Override
public HttpServerResponse drainHandler(Handler<Void> handler) {
synchronized (conn) {
if (handler != null) {
checkEnded();
}
drainHandler = handler;
return this;
}
}
@Override
public HttpServerResponse sendFile(String filename, long offset, long length) {
return sendFile(filename, offset, length, null);
}
@Override
public HttpServerResponse sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
synchronized (conn) {
checkEnded();
Context resultCtx = resultHandler != null ? stream.vertx.getOrCreateContext() : null;
File file = stream.vertx.resolveFile(filename);
if (!file.exists()) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(new FileNotFoundException())));
} else {
log.error("File not found: " + filename);
}
return this;
}
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (IOException e) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(e)));
} else {
log.error("Failed to send file", e);
}
return this;
}
long contentLength = Math.min(length, file.length() - offset);
if (headers.get(HttpHeaderNames.CONTENT_LENGTH) == null) {
putHeader(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(contentLength));
}
if (headers.get(HttpHeaderNames.CONTENT_TYPE) == null) {
String contentType = MimeMapping.getMimeTypeForFilename(filename);
if (contentType != null) {
putHeader(HttpHeaderNames.CONTENT_TYPE, contentType);
}
}
checkSendHeaders(false);
Future<Long> result = Future.future();
result.setHandler(ar -> {
if (ar.succeeded()) {
bytesWritten += ar.result();
end();
}
if (resultHandler != null) {
resultCtx.runOnContext(v -> {
resultHandler.handle(Future.succeededFuture());
});
}
});
FileStreamChannel fileChannel = new FileStreamChannel(result, stream, offset, contentLength);
drainHandler(fileChannel.drainHandler);
ctx.channel()
.eventLoop()
.register(fileChannel)
.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
fileChannel.pipeline().fireUserEventTriggered(raf);
} else {
result.tryFail(future.cause());
}
});
}
return this;
}
@Override
public void close() {
conn.close();
}
@Override
public boolean ended() {
synchronized (conn) {
return ended;
}
}
@Override
public boolean closed() {
return conn.isClosed();
}
@Override
public boolean headWritten() {
synchronized (conn) {
return headWritten;
}
}
@Override
public HttpServerResponse headersEndHandler(@Nullable Handler<Void> handler) {
synchronized (conn) {
headersEndHandler = handler;
return this;
}
}
@Override
public HttpServerResponse bodyEndHandler(@Nullable Handler<Void> handler) {
synchronized (conn) {
bodyEndHandler = handler;
return this;
}
}
@Override
public long bytesWritten() {
synchronized (conn) {
return bytesWritten;
}
}
@Override
public int streamId() {
return stream.id();
}
@Override
public void reset(long code) {
synchronized (conn) {
checkEnded();
handleEnded(true);
stream.writeReset(code);
ctx.flush();
}
}
@Override
public HttpServerResponse push(HttpMethod method, String host, String path, Handler<AsyncResult<HttpServerResponse>> handler) {
return push(method, host, path, null, handler);
}
@Override
public HttpServerResponse push(HttpMethod method, String path, MultiMap headers, Handler<AsyncResult<HttpServerResponse>> handler) {
return push(method, null, path, headers, handler);
}
@Override
public HttpServerResponse push(HttpMethod method, String host, String path, MultiMap headers, Handler<AsyncResult<HttpServerResponse>> handler) {
synchronized (conn) {
if (push) {
throw new IllegalStateException("A push response cannot promise another push");
}
checkEnded();
conn.sendPush(stream.id(), host, method, headers, path, handler);
if (!inHandler) {
ctx.flush();
}
numPush++;
return this;
}
}
@Override
public HttpServerResponse push(HttpMethod method, String path, Handler<AsyncResult<HttpServerResponse>> handler) {
return push(method, host, path, handler);
}
}