-
Notifications
You must be signed in to change notification settings - Fork 629
/
HttpServerOperations.java
973 lines (861 loc) · 29.8 KB
/
HttpServerOperations.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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/*
* Copyright (c) 2011-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty5.http.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import io.netty5.buffer.api.Buffer;
import io.netty5.channel.Channel;
import io.netty5.channel.ChannelFutureListeners;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.channel.ChannelOption;
import io.netty5.handler.codec.DefaultHeaders;
import io.netty5.handler.codec.TooLongFrameException;
import io.netty5.handler.codec.http.DefaultFullHttpResponse;
import io.netty5.handler.codec.http.DefaultHttpHeaders;
import io.netty5.handler.codec.http.DefaultHttpResponse;
import io.netty5.handler.codec.http.DefaultLastHttpContent;
import io.netty5.handler.codec.http.EmptyLastHttpContent;
import io.netty5.handler.codec.http.FullHttpRequest;
import io.netty5.handler.codec.http.FullHttpResponse;
import io.netty5.handler.codec.http.HttpContent;
import io.netty5.handler.codec.http.HttpHeaderNames;
import io.netty5.handler.codec.http.HttpHeaderValues;
import io.netty5.handler.codec.http.HttpHeaders;
import io.netty5.handler.codec.http.HttpMessage;
import io.netty5.handler.codec.http.HttpMethod;
import io.netty5.handler.codec.http.HttpRequest;
import io.netty5.handler.codec.http.HttpResponse;
import io.netty5.handler.codec.http.HttpResponseStatus;
import io.netty5.handler.codec.http.HttpUtil;
import io.netty5.handler.codec.http.HttpVersion;
import io.netty5.handler.codec.http.LastHttpContent;
import io.netty5.handler.codec.http.cookie.Cookie;
import io.netty5.handler.codec.http.cookie.ServerCookieDecoder;
import io.netty5.handler.codec.http.cookie.ServerCookieEncoder;
import io.netty5.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty5.handler.codec.http.websocketx.WebSocketCloseStatus;
import io.netty5.handler.codec.http2.HttpConversionUtil;
import io.netty5.util.AsciiString;
import io.netty5.util.Resource;
import io.netty5.util.concurrent.Future;
import io.netty5.util.concurrent.FutureListener;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty5.Connection;
import reactor.netty5.ConnectionObserver;
import reactor.netty5.NettyOutbound;
import reactor.netty5.NettyPipeline;
import reactor.netty5.channel.AbortedException;
import reactor.netty5.channel.ChannelOperations;
import reactor.netty5.http.HttpOperations;
import reactor.netty5.http.websocket.WebsocketInbound;
import reactor.netty5.http.websocket.WebsocketOutbound;
import reactor.util.Logger;
import reactor.util.Loggers;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
import static io.netty5.buffer.api.DefaultBufferAllocators.preferredAllocator;
import static io.netty5.handler.codec.http.HttpUtil.isTransferEncodingChunked;
import static reactor.netty5.ReactorNetty.format;
import static reactor.netty5.http.server.HttpServerFormDecoderProvider.DEFAULT_FORM_DECODER_SPEC;
import static reactor.netty5.http.server.HttpServerState.REQUEST_DECODING_FAILED;
/**
* Conversion between Netty types and Reactor types ({@link HttpOperations}.
*
* @author Stephane Maldini1
*/
class HttpServerOperations extends HttpOperations<HttpServerRequest, HttpServerResponse>
implements HttpServerRequest, HttpServerResponse {
final BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate;
final ConnectionInfo connectionInfo;
final ServerCookieDecoder cookieDecoder;
final ServerCookieEncoder cookieEncoder;
final ServerCookies cookieHolder;
final HttpServerFormDecoderProvider formDecoderProvider;
final BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle;
final HttpRequest nettyRequest;
final HttpResponse nettyResponse;
final HttpHeaders responseHeaders;
final String scheme;
Function<? super String, Map<String, String>> paramsResolver;
String path;
Consumer<? super HttpHeaders> trailerHeadersConsumer;
volatile Context currentContext;
HttpServerOperations(HttpServerOperations replaced) {
super(replaced);
this.compressionPredicate = replaced.compressionPredicate;
this.connectionInfo = replaced.connectionInfo;
this.cookieDecoder = replaced.cookieDecoder;
this.cookieEncoder = replaced.cookieEncoder;
this.cookieHolder = replaced.cookieHolder;
this.currentContext = replaced.currentContext;
this.formDecoderProvider = replaced.formDecoderProvider;
this.mapHandle = replaced.mapHandle;
this.nettyRequest = replaced.nettyRequest;
this.nettyResponse = replaced.nettyResponse;
this.paramsResolver = replaced.paramsResolver;
this.path = replaced.path;
this.responseHeaders = replaced.responseHeaders;
this.scheme = replaced.scheme;
this.trailerHeadersConsumer = replaced.trailerHeadersConsumer;
}
HttpServerOperations(Connection c, ConnectionObserver listener, HttpRequest nettyRequest,
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate,
@Nullable ConnectionInfo connectionInfo,
ServerCookieDecoder decoder,
ServerCookieEncoder encoder,
HttpServerFormDecoderProvider formDecoderProvider,
@Nullable BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle,
boolean secured) {
this(c, listener, nettyRequest, compressionPredicate, connectionInfo, decoder, encoder, formDecoderProvider,
mapHandle, true, secured);
}
HttpServerOperations(Connection c, ConnectionObserver listener, HttpRequest nettyRequest,
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate,
@Nullable ConnectionInfo connectionInfo,
ServerCookieDecoder decoder,
ServerCookieEncoder encoder,
HttpServerFormDecoderProvider formDecoderProvider,
@Nullable BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle,
boolean resolvePath,
boolean secured) {
super(c, listener);
this.compressionPredicate = compressionPredicate;
this.connectionInfo = connectionInfo;
this.cookieDecoder = decoder;
this.cookieEncoder = encoder;
this.cookieHolder = ServerCookies.newServerRequestHolder(nettyRequest.headers(), decoder);
this.currentContext = Context.empty();
this.formDecoderProvider = formDecoderProvider;
this.mapHandle = mapHandle;
this.nettyRequest = nettyRequest;
this.nettyResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
if (resolvePath) {
this.path = resolvePath(nettyRequest.uri());
}
else {
this.path = null;
}
this.responseHeaders = nettyResponse.headers();
this.responseHeaders.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
this.scheme = secured ? "https" : "http";
}
@Override
public NettyOutbound sendHeaders() {
if (hasSentHeaders()) {
return this;
}
return then(Mono.empty());
}
@Override
public HttpServerOperations withConnection(Consumer<? super Connection> withConnection) {
Objects.requireNonNull(withConnection, "withConnection");
withConnection.accept(this);
return this;
}
@Override
protected HttpMessage newFullBodyMessage(Buffer body) {
HttpResponse res =
new DefaultFullHttpResponse(version(), status(), body);
if (!HttpMethod.HEAD.equals(method())) {
responseHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
if (!HttpResponseStatus.NOT_MODIFIED.equals(status())) {
if (HttpUtil.getContentLength(nettyResponse, -1) == -1) {
responseHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, body.readableBytes());
}
}
}
// For HEAD requests:
// - if there is Transfer-Encoding and Content-Length, Transfer-Encoding will be removed
// - if there is only Transfer-Encoding, it will be kept and not replaced by
// Content-Length: body.readableBytes()
// For HEAD requests, the I/O handler may decide to provide only the headers and complete
// the response. In that case body will be EMPTY_BUFFER and if we set Content-Length: 0,
// this will not be correct
// https://github.com/reactor/reactor-netty/issues/1333
else if (HttpUtil.getContentLength(nettyResponse, -1) != -1) {
responseHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
}
res.headers().set(responseHeaders);
return res;
}
@Override
public HttpServerResponse addCookie(Cookie cookie) {
if (!hasSentHeaders()) {
this.responseHeaders.add(HttpHeaderNames.SET_COOKIE,
cookieEncoder.encode(cookie));
}
else {
throw new IllegalStateException("Status and headers already sent");
}
return this;
}
@Override
public HttpServerResponse addHeader(CharSequence name, CharSequence value) {
if (!hasSentHeaders()) {
this.responseHeaders.add(name, value);
}
else {
throw new IllegalStateException("Status and headers already sent");
}
return this;
}
@Override
public HttpServerOperations chunkedTransfer(boolean chunked) {
if (!hasSentHeaders() && isTransferEncodingChunked(nettyResponse) != chunked) {
responseHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
HttpUtil.setTransferEncodingChunked(nettyResponse, chunked);
}
return this;
}
@Override
public Map<CharSequence, Set<Cookie>> cookies() {
if (cookieHolder != null) {
return cookieHolder.getCachedCookies();
}
throw new IllegalStateException("request not parsed");
}
@Override
public Map<CharSequence, List<Cookie>> allCookies() {
if (cookieHolder != null) {
return cookieHolder.getAllCachedCookies();
}
throw new IllegalStateException("request not parsed");
}
@Override
public Context currentContext() {
return currentContext;
}
@Override
public HttpServerResponse header(CharSequence name, CharSequence value) {
if (!hasSentHeaders()) {
this.responseHeaders.set(name, value);
}
else {
throw new IllegalStateException("Status and headers already sent");
}
return this;
}
@Override
public HttpServerResponse headers(HttpHeaders headers) {
if (!hasSentHeaders()) {
this.responseHeaders.set(headers);
}
else {
throw new IllegalStateException("Status and headers already sent");
}
return this;
}
@Override
public boolean isFormUrlencoded() {
CharSequence mimeType = HttpUtil.getMimeType(nettyRequest);
return mimeType != null &&
HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.contentEqualsIgnoreCase(mimeType.toString().trim());
}
@Override
public boolean isKeepAlive() {
return HttpUtil.isKeepAlive(nettyRequest);
}
@Override
public boolean isMultipart() {
return false; //HttpPostRequestDecoder.isMultipart(nettyRequest);
}
@Override
public boolean isWebsocket() {
return get(channel()) instanceof WebsocketServerOperations;
}
final boolean isHttp2() {
return requestHeaders().contains(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text());
}
@Override
public HttpServerResponse keepAlive(boolean keepAlive) {
HttpUtil.setKeepAlive(nettyResponse, keepAlive);
return this;
}
@Override
public HttpMethod method() {
return nettyRequest.method();
}
@Override
@Nullable
public String param(CharSequence key) {
Objects.requireNonNull(key, "key");
Map<String, String> params = null;
if (paramsResolver != null) {
params = this.paramsResolver.apply(uri());
}
return null != params ? params.get(key.toString()) : null;
}
@Override
@Nullable
public Map<String, String> params() {
return null != paramsResolver ? paramsResolver.apply(uri()) : null;
}
@Override
public HttpServerRequest paramsResolver(Function<? super String, Map<String, String>> paramsResolver) {
this.paramsResolver = paramsResolver;
return this;
}
@Override
public Flux<?> receiveObject() {
// Handle the 'Expect: 100-continue' header if necessary.
// TODO: Respond with 413 Request Entity Too Large
// and discard the traffic or close the connection.
// No need to notify the upstream handlers - just log.
// If decoding a response, just throw an error.
if (HttpUtil.is100ContinueExpected(nettyRequest)) {
return Mono.fromCompletionStage(() -> {
if (!hasSentHeaders()) {
return channel().writeAndFlush(CONTINUE).asStage();
}
return channel().newSucceededFuture().asStage();
})
.thenMany(super.receiveObject());
}
else {
return super.receiveObject();
}
}
@Override
@Nullable
public InetSocketAddress hostAddress() {
if (connectionInfo != null) {
return this.connectionInfo.getHostAddress();
}
else {
return null;
}
}
@Override
@Nullable
public InetSocketAddress remoteAddress() {
if (connectionInfo != null) {
return this.connectionInfo.getRemoteAddress();
}
else {
return null;
}
}
@Override
public HttpHeaders requestHeaders() {
if (nettyRequest != null) {
return nettyRequest.headers();
}
throw new IllegalStateException("request not parsed");
}
@Override
public String scheme() {
if (connectionInfo != null) {
return this.connectionInfo.getScheme();
}
else {
return scheme;
}
}
@Override
public HttpHeaders responseHeaders() {
return responseHeaders;
}
@Override
public Mono<Void> send() {
if (markSentHeaderAndBody()) {
HttpMessage response = newFullBodyMessage(channel().bufferAllocator().allocate(0));
return Mono.fromCompletionStage(() -> channel().writeAndFlush(response).asStage());
}
else {
return Mono.empty();
}
}
@Override
public NettyOutbound sendFile(Path file) {
try {
return sendFile(file, 0L, Files.size(file));
}
catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Path not resolved"), e);
}
return then(sendNotFound());
}
}
@Override
public Mono<Void> sendNotFound() {
return this.status(HttpResponseStatus.NOT_FOUND)
.send();
}
@Override
public Mono<Void> sendRedirect(String location) {
Objects.requireNonNull(location, "location");
return this.status(HttpResponseStatus.FOUND)
.header(HttpHeaderNames.LOCATION, location)
.send();
}
/**
* @return the Transfer setting SSE for this http connection (e.g. event-stream)
*/
@Override
public HttpServerResponse sse() {
header(HttpHeaderNames.CONTENT_TYPE, EVENT_STREAM);
return this;
}
@Override
public HttpResponseStatus status() {
return this.nettyResponse.status();
}
@Override
public HttpServerResponse status(HttpResponseStatus status) {
if (!hasSentHeaders()) {
this.nettyResponse.setStatus(status);
}
else {
throw new IllegalStateException("Status and headers already sent");
}
return this;
}
@Override
public HttpServerResponse trailerHeaders(Consumer<? super HttpHeaders> trailerHeaders) {
this.trailerHeadersConsumer = Objects.requireNonNull(trailerHeaders, "trailerHeaders");
return this;
}
@Override
public Mono<Void> sendWebsocket(
BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends Publisher<Void>> websocketHandler,
WebsocketServerSpec configurer) {
return withWebsocketSupport(uri(), configurer, websocketHandler);
}
@Override
public String uri() {
if (nettyRequest != null) {
return nettyRequest.uri();
}
throw new IllegalStateException("request not parsed");
}
@Override
public String fullPath() {
if (path != null) {
return path;
}
throw new IllegalStateException("request not parsed");
}
@Override
public HttpVersion version() {
if (nettyRequest != null) {
return nettyRequest.protocolVersion();
}
throw new IllegalStateException("request not parsed");
}
@Override
public HttpServerResponse compression(boolean compress) {
if (!compress) {
removeHandler(NettyPipeline.CompressionHandler);
}
else if (channel().pipeline()
.get(NettyPipeline.CompressionHandler) == null) {
SimpleCompressionHandler handler = new SimpleCompressionHandler();
try {
// decodeAndClose(...) is needed only to initialize the acceptEncodingQueue
handler.decodeAndClose(channel().pipeline().context(NettyPipeline.ReactiveBridge), nettyRequest);
addHandlerFirst(NettyPipeline.CompressionHandler, handler);
}
catch (Throwable e) {
log.error(format(channel(), ""), e);
}
}
return this;
}
@Override
protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
try {
listener().onStateChange(this, HttpServerState.REQUEST_RECEIVED);
}
catch (Exception e) {
onInboundError(e);
Resource.dispose(msg);
return;
}
if (msg instanceof FullHttpRequest request) {
if (request.payload().readableBytes() > 0) {
super.onInboundNext(ctx, msg);
}
else {
request.close();
}
if (isHttp2()) {
//force auto read to enable more accurate close selection now inbound is done
channel().setOption(ChannelOption.AUTO_READ, true);
onInboundComplete();
}
}
return;
}
if (msg instanceof HttpContent) {
if (!(msg instanceof EmptyLastHttpContent emptyLastHttpContent)) {
super.onInboundNext(ctx, msg);
}
else {
emptyLastHttpContent.close();
}
if (msg instanceof LastHttpContent) {
//force auto read to enable more accurate close selection now inbound is done
channel().setOption(ChannelOption.AUTO_READ, true);
onInboundComplete();
}
}
else {
super.onInboundNext(ctx, msg);
}
}
@Override
protected void onInboundClose() {
discardWhenNoReceiver();
if (!(isInboundCancelled() || isInboundDisposed())) {
onInboundError(new AbortedException("Connection has been closed"));
}
terminate();
}
@Override
protected void afterMarkSentHeaders() {
if (HttpResponseStatus.NOT_MODIFIED.equals(status())) {
responseHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING)
.remove(HttpHeaderNames.CONTENT_LENGTH);
}
if (compressionPredicate != null && compressionPredicate.test(this, this)) {
compression(true);
}
}
@Override
protected void beforeMarkSentHeaders() {
//noop
}
@Override
protected void onHeadersSent() {
//noop
}
@Override
protected void onOutboundComplete() {
if (isWebsocket()) {
return;
}
final Future<Void> f;
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Last HTTP response frame"));
}
if (markSentHeaderAndBody()) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "No sendHeaders() called before complete, sending " +
"zero-length header"));
}
f = channel().writeAndFlush(newFullBodyMessage(channel().bufferAllocator().allocate(0)));
}
else if (markSentBody()) {
LastHttpContent<?> lastHttpContent = new EmptyLastHttpContent(channel().bufferAllocator());
// https://datatracker.ietf.org/doc/html/rfc7230#section-4.1.2
// A trailer allows the sender to include additional fields at the end
// of a chunked message in order to supply metadata that might be
// dynamically generated while the message body is sent, such as a
// message integrity check, digital signature, or post-processing
// status.
if (trailerHeadersConsumer != null && isTransferEncodingChunked(nettyResponse)) {
// https://datatracker.ietf.org/doc/html/rfc7230#section-4.4
// When a message includes a message body encoded with the chunked
// transfer coding and the sender desires to send metadata in the form
// of trailer fields at the end of the message, the sender SHOULD
// generate a Trailer header field before the message body to indicate
// which fields will be present in the trailers.
String declaredHeaderNames = responseHeaders.get(HttpHeaderNames.TRAILER);
if (declaredHeaderNames != null) {
HttpHeaders trailerHeaders = new TrailerHeaders(declaredHeaderNames);
try {
trailerHeadersConsumer.accept(trailerHeaders);
}
catch (IllegalArgumentException e) {
// A sender MUST NOT generate a trailer when header names are
// HttpServerOperations.TrailerHeaders.DISALLOWED_TRAILER_HEADER_NAMES
log.error(format(channel(), "Cannot apply trailer headers [{0}]"), declaredHeaderNames, e);
}
if (!trailerHeaders.isEmpty()) {
lastHttpContent = new DefaultLastHttpContent(channel().bufferAllocator().allocate(0));
lastHttpContent.trailingHeaders().set(trailerHeaders);
}
}
}
f = channel().writeAndFlush(lastHttpContent);
}
else {
discard();
return;
}
f.addListener(s -> {
discard();
if (!s.isSuccess() && log.isDebugEnabled()) {
log.debug(format(channel(), "Failed flushing last frame"), s.cause());
}
});
}
static void cleanHandlerTerminate(Channel ch) {
ChannelOperations<?, ?> ops = get(ch);
if (ops == null) {
return;
}
ops.discard();
//Try to defer the disposing to leave a chance for any synchronous complete following this callback
if (!ops.isSubscriptionDisposed()) {
ch.executor()
.execute(((HttpServerOperations) ops)::terminate);
}
else {
//if already disposed, we can immediately call terminate
((HttpServerOperations) ops).terminate();
}
}
static long requestsCounter(Channel channel) {
HttpServerOperations ops = Connection.from(channel).as(HttpServerOperations.class);
if (ops == null) {
return -1;
}
return ((AtomicLong) ops.connection()).get();
}
static void sendDecodingFailures(
ChannelHandlerContext ctx,
ConnectionObserver listener,
boolean secure,
Throwable t,
Object msg) {
Throwable cause = t.getCause() != null ? t.getCause() : t;
if (log.isWarnEnabled()) {
log.warn(format(ctx.channel(), "Decoding failed: " + msg + " : "), cause);
}
Resource.dispose(msg);
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
cause instanceof TooLongFrameException ? HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE :
HttpResponseStatus.BAD_REQUEST,
ctx.bufferAllocator().allocate(0));
response.headers()
.setInt(HttpHeaderNames.CONTENT_LENGTH, 0)
.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
Connection ops = ChannelOperations.get(ctx.channel());
if (ops == null) {
Connection conn = Connection.from(ctx.channel());
if (msg instanceof HttpRequest request) {
ops = new FailedHttpServerRequest(conn, listener, request, response, secure);
ops.bind();
}
else {
ops = conn;
}
}
ctx.channel().writeAndFlush(response);
listener.onStateChange(ops, REQUEST_DECODING_FAILED);
}
/**
* There is no need of invoking {@link #discard()}, the inbound will
* be canceled on channel inactive event if there is no subscriber available
*
* @param err the {@link Throwable} cause
*/
@Override
protected void onOutboundError(Throwable err) {
if (!channel().isActive()) {
super.onOutboundError(err);
return;
}
if (markSentHeaders()) {
log.error(format(channel(), "Error starting response. Replying error status"), err);
nettyResponse.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
responseHeaders.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
channel().writeAndFlush(newFullBodyMessage(channel().bufferAllocator().allocate(0)))
.addListener(channel(), ChannelFutureListeners.CLOSE);
return;
}
markSentBody();
log.error(format(channel(), "Error finishing response. Closing connection"), err);
channel().writeAndFlush(channel().bufferAllocator().allocate(0))
.addListener(channel(), ChannelFutureListeners.CLOSE);
}
@Override
protected HttpMessage outboundHttpMessage() {
return nettyResponse;
}
final Mono<Void> withWebsocketSupport(String url,
WebsocketServerSpec websocketServerSpec,
BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends Publisher<Void>> websocketHandler) {
Objects.requireNonNull(websocketServerSpec, "websocketServerSpec");
Objects.requireNonNull(websocketHandler, "websocketHandler");
if (markSentHeaders()) {
WebsocketServerOperations ops = new WebsocketServerOperations(url, websocketServerSpec, this);
return Mono.fromCompletionStage(ops.handshakerResult.asStage())
.doOnEach(signal -> {
if (!signal.hasError() && (websocketServerSpec.protocols() == null || ops.selectedSubprotocol() != null)) {
websocketHandler.apply(ops, ops)
.subscribe(new WebsocketSubscriber(ops, Context.of(signal.getContextView())));
}
});
}
else {
log.error(format(channel(), "Cannot enable websocket if headers have already been sent"));
}
return Mono.error(new IllegalStateException("Failed to upgrade to websocket"));
}
static final class WebsocketSubscriber implements CoreSubscriber<Void>, FutureListener<Void> {
final WebsocketServerOperations ops;
final Context context;
WebsocketSubscriber(WebsocketServerOperations ops, Context context) {
this.ops = ops;
this.context = context;
}
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(Void aVoid) {
}
@Override
public void onError(Throwable t) {
ops.onError(t);
}
@Override
public void operationComplete(Future<? extends Void> future) {
ops.terminate();
}
@Override
public void onComplete() {
if (ops.channel()
.isActive()) {
ops.sendCloseNow(new CloseWebSocketFrame(ops.channel().bufferAllocator(),
WebSocketCloseStatus.NORMAL_CLOSURE), this);
}
}
@Override
public Context currentContext() {
return context;
}
}
static final Logger log = Loggers.getLogger(HttpServerOperations.class);
final static AsciiString EVENT_STREAM = new AsciiString("text/event-stream");
final static FullHttpResponse CONTINUE =
new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.CONTINUE,
preferredAllocator().allocate(0));
static final class FailedHttpServerRequest extends HttpServerOperations {
final HttpResponse customResponse;
FailedHttpServerRequest(
Connection c,
ConnectionObserver listener,
HttpRequest nettyRequest,
HttpResponse nettyResponse,
boolean secure) {
super(c, listener, nettyRequest, null, null, ServerCookieDecoder.STRICT, ServerCookieEncoder.STRICT,
DEFAULT_FORM_DECODER_SPEC, null, false, secure);
this.customResponse = nettyResponse;
String tempPath = "";
try {
tempPath = resolvePath(nettyRequest.uri());
}
catch (RuntimeException e) {
tempPath = "/bad-request";
}
finally {
this.path = tempPath;
}
}
@Override
protected HttpMessage outboundHttpMessage() {
return customResponse;
}
@Override
public HttpResponseStatus status() {
return customResponse.status();
}
}
static final class TrailerHeaders extends DefaultHttpHeaders {
static final Set<String> DISALLOWED_TRAILER_HEADER_NAMES = new HashSet<>(14);
static {
// https://datatracker.ietf.org/doc/html/rfc7230#section-4.1.2
// A sender MUST NOT generate a trailer that contains a field necessary
// for message framing (e.g., Transfer-Encoding and Content-Length),
// routing (e.g., Host), request modifiers (e.g., controls and
// conditionals in Section 5 of [RFC7231]), authentication (e.g., see
// [RFC7235] and [RFC6265]), response control data (e.g., see Section
// 7.1 of [RFC7231]), or determining how to process the payload (e.g.,
// Content-Encoding, Content-Type, Content-Range, and Trailer).
DISALLOWED_TRAILER_HEADER_NAMES.add("age");
DISALLOWED_TRAILER_HEADER_NAMES.add("cache-control");
DISALLOWED_TRAILER_HEADER_NAMES.add("content-encoding");
DISALLOWED_TRAILER_HEADER_NAMES.add("content-length");
DISALLOWED_TRAILER_HEADER_NAMES.add("content-range");
DISALLOWED_TRAILER_HEADER_NAMES.add("content-type");
DISALLOWED_TRAILER_HEADER_NAMES.add("date");
DISALLOWED_TRAILER_HEADER_NAMES.add("expires");
DISALLOWED_TRAILER_HEADER_NAMES.add("location");
DISALLOWED_TRAILER_HEADER_NAMES.add("retry-after");
DISALLOWED_TRAILER_HEADER_NAMES.add("trailer");
DISALLOWED_TRAILER_HEADER_NAMES.add("transfer-encoding");
DISALLOWED_TRAILER_HEADER_NAMES.add("vary");
DISALLOWED_TRAILER_HEADER_NAMES.add("warning");
}
TrailerHeaders(String declaredHeaderNames) {
super(true, new TrailerNameValidator(filterHeaderNames(declaredHeaderNames)));
}
static Set<String> filterHeaderNames(String declaredHeaderNames) {
Objects.requireNonNull(declaredHeaderNames, "declaredHeaderNames");
Set<String> result = new HashSet<>();
String[] names = declaredHeaderNames.split(",", -1);
for (String name : names) {
String trimmedStr = name.trim();
if (trimmedStr.isEmpty() ||
DISALLOWED_TRAILER_HEADER_NAMES.contains(trimmedStr.toLowerCase(Locale.ENGLISH))) {
continue;
}
result.add(trimmedStr);
}
return result;
}
static final class TrailerNameValidator implements DefaultHeaders.NameValidator<CharSequence> {
/**
* Contains the headers names specified with {@link HttpHeaderNames#TRAILER}
*/
final Set<String> declaredHeaderNames;
TrailerNameValidator(Set<String> declaredHeaderNames) {
this.declaredHeaderNames = declaredHeaderNames;
}
@Override
public void validateName(CharSequence name) {
if (!declaredHeaderNames.contains(name.toString())) {
throw new IllegalArgumentException("Trailer header name [" + name +
"] not declared with [Trailer] header, or it is not a valid trailer header name");
}
}
}
}
}