-
Notifications
You must be signed in to change notification settings - Fork 630
/
HttpServer.java
1013 lines (966 loc) · 39.3 KB
/
HttpServer.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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.netty.http.server;
import java.net.SocketAddress;
import java.time.Duration;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.cookie.ServerCookieDecoder;
import io.netty.handler.codec.http.cookie.ServerCookieEncoder;
import io.netty.handler.ssl.JdkSslContext;
import io.netty.handler.ssl.OpenSsl;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import reactor.netty.Connection;
import reactor.netty.ConnectionObserver;
import reactor.netty.channel.ChannelMetricsRecorder;
import reactor.netty.http.Http2SettingsSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.logging.HttpMessageLogFactory;
import reactor.netty.http.logging.ReactorNettyHttpMessageLogFactory;
import reactor.netty.http.server.logging.AccessLog;
import reactor.netty.http.server.logging.AccessLogArgProvider;
import reactor.netty.http.server.logging.AccessLogFactory;
import reactor.netty.tcp.SslProvider;
import reactor.netty.tcp.TcpServer;
import reactor.netty.transport.ServerTransport;
import reactor.util.Logger;
import reactor.util.Loggers;
import reactor.util.Metrics;
import reactor.util.context.Context;
import static reactor.netty.ReactorNetty.format;
/**
* An HttpServer allows building in a safe immutable way an HTTP server that is
* materialized and connecting when {@link #bind()} is ultimately called.
* <p>
* <p>Examples:
* <pre>
* {@code
* HttpServer.create()
* .host("0.0.0.0")
* .handle((req, res) -> res.sendString(Flux.just("hello")))
* .bind()
* .block();
* }
* </pre>
*
* @author Stephane Maldini
* @author Violeta Georgieva
*/
public abstract class HttpServer extends ServerTransport<HttpServer, HttpServerConfig> {
/**
* Prepare an {@link HttpServer}
*
* @return a new {@link HttpServer}
*/
public static HttpServer create() {
return HttpServerBind.INSTANCE;
}
/**
* Prepare an {@link HttpServer}
* <p>
* <strong>Note:</strong>
* There isn't only one method that replaces this deprecated method.
* The configuration that can be done with this deprecated method,
* can also be done with the other methods exposed by {@link HttpServer}.
* </p>
* <p>Examples:</p>
* <p>Configuration via the deprecated '.from(...)' method</p>
* <pre>
* {@code
* HttpServer.from(
* TcpServer.attr(...) // configures the channel attributes
* .bindAddress(...) // configures the bind (local) address
* .childAttr(...) // configures the child channel attributes
* .childObserve() // configures the child channel connection observer
* .childOption(...) // configures the child channel options
* .channelGroup(...) // configures the channel group
* .doOnBound(...) // configures the doOnBound callback
* .doOnChannelInit(...) // configures the channel handler
* .doOnConnection(...) // configures the doOnConnection callback
* .doOnUnbound(...) // configures the doOnUnbound callback
* .metrics(...) // configures the metrics
* .observe() // configures the connection observer
* .option(...) // configures the channel options
* .runOn(...) // configures the event loop group
* .secure() // configures the SSL
* .wiretap()) // configures the wire logging
* }
* </pre>
*
* <p>Configuration via the other methods exposed by {@link HttpServer}</p>
* <pre>
* {@code
* HttpServer.attr(...) // configures the channel attributes
* .bindAddress(...) // configures the bind (local) address
* .childAttr(...) // configures the child channel attributes
* .childObserve() // configures the child channel connection observer
* .childOption(...) // configures the child channel options
* .channelGroup(...) // configures the channel group
* .doOnBound(...) // configures the doOnBound callback
* .doOnChannelInit(...) // configures the channel handler
* .doOnConnection(...) // configures the doOnConnection callback
* .doOnUnbound(...) // configures the doOnUnbound callback
* .metrics(...) // configures the metrics
* .observe() // configures the connection observer
* .option(...) // configures the channel options
* .runOn(...) // configures the event loop group
* .secure() // configures the SSL
* .wiretap() // configures the wire logging
* }
* </pre>
*
* <p>Wire logging in plain text</p>
* <pre>
* {@code
* HttpServer.wiretap("logger", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL)
* }
* </pre>
*
* @return a new {@link HttpServer}
* @deprecated Use the other methods exposed by {@link HttpServer} to achieve the same configurations.
* This method will be removed in version 1.1.0.
*/
@Deprecated
public static HttpServer from(TcpServer tcpServer) {
Objects.requireNonNull(tcpServer, "tcpServer");
return HttpServerBind.applyTcpServerConfig(tcpServer.configuration());
}
/**
* Enable or disable the access log. If enabled, the default log system will be used.
* <p>
* Example:
* <pre>
* {@code
* HttpServer.create()
* .port(8080)
* .route(r -> r.get("/hello",
* (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
* .sendString(Mono.just("Hello World!"))))
* .accessLog(true)
* .bindNow()
* .onDispose()
* .block();
* }
* </pre>
* <p>
*
* Note that this method takes precedence over the {@value reactor.netty.ReactorNetty#ACCESS_LOG_ENABLED} system property.
*
* @param enable enable or disable the access log
* @return a new {@link HttpServer}
* @since 1.0.3
*/
public final HttpServer accessLog(boolean enable) {
HttpServer dup = duplicate();
dup.configuration().accessLog = null;
dup.configuration().accessLogEnabled = enable;
return dup;
}
/**
* Enable or disable the access log and customize it through an {@link AccessLogFactory}.
* <p>
* Example:
* <pre>
* {@code
* HttpServer.create()
* .port(8080)
* .route(r -> r.get("/hello",
* (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
* .sendString(Mono.just("Hello World!"))))
* .accessLog(true, AccessLogFactory.createFilter(
* args -> String.valueOf(args.uri()).startsWith("/health"),
* args -> AccessLog.create("user-agent={}", args.requestHeader("user-agent"))
* )
* .bindNow()
* .onDispose()
* .block();
* }
* </pre>
* <p>
* The {@link AccessLogFactory} class offers several helper methods to generate such a function,
* notably if one wants to {@link AccessLogFactory#createFilter(Predicate) filter} some requests out of the access log.
*
* Note that this method takes precedence over the {@value reactor.netty.ReactorNetty#ACCESS_LOG_ENABLED} system property.
*
* @param enable enable or disable the access log
* @param accessLogFactory the {@link AccessLogFactory} that creates an {@link AccessLog} given an {@link AccessLogArgProvider}
* @return a new {@link HttpServer}
* @since 1.0.3
*/
public final HttpServer accessLog(boolean enable, AccessLogFactory accessLogFactory) {
Objects.requireNonNull(accessLogFactory);
HttpServer dup = duplicate();
dup.configuration().accessLog = enable ? accessLogFactory : null;
dup.configuration().accessLogEnabled = enable;
return dup;
}
/**
* Customize the access log, provided access logging has been enabled through the
* {@value reactor.netty.ReactorNetty#ACCESS_LOG_ENABLED} system property.
* <p>
* Example:
* <pre>
* {@code
* HttpServer.create()
* .port(8080)
* .route(r -> r.get("/hello",
* (req, res) -> res.header(CONTENT_TYPE, TEXT_PLAIN)
* .sendString(Mono.just("Hello World!"))))
* .accessLog(argProvider ->
* AccessLog.create("user-agent={}", argProvider.requestHeader("user-agent")))
* .bindNow()
* .onDispose()
* .block();
* }
* </pre>
* <p>
*
* @param accessLogFactory the {@link Function} that creates an {@link AccessLog} given an {@link AccessLogArgProvider}
* @return a new {@link HttpServer}
* @since 1.0.1
* @deprecated as of 1.0.3. Prefer the {@link #accessLog(boolean, AccessLogFactory) variant}
* with the {@link AccessLogFactory} interface instead. This method will be removed in version 1.2.0.
*/
@Deprecated
public final HttpServer accessLog(Function<AccessLogArgProvider, AccessLog> accessLogFactory) {
Objects.requireNonNull(accessLogFactory, "accessLogFactory");
HttpServer dup = duplicate();
dup.configuration().accessLog = accessLogFactory;
return dup;
}
@Override
public final HttpServer bindAddress(Supplier<? extends SocketAddress> bindAddressSupplier) {
return super.bindAddress(bindAddressSupplier);
}
@Override
public final HttpServer channelGroup(ChannelGroup channelGroup) {
return super.channelGroup(channelGroup);
}
/**
* Enable GZip response compression if the client request presents accept encoding
* headers and the provided {@link java.util.function.Predicate} matches.
* <p>
* Note: the passed {@link HttpServerRequest} and {@link HttpServerResponse}
* should be considered read-only and the implement SHOULD NOT consume or
* write the request/response in this predicate.
* </p>
*
* @param predicate that returns true to compress the response.
*
* @return a new {@link HttpServer}
*/
public final HttpServer compress(BiPredicate<HttpServerRequest, HttpServerResponse> predicate) {
Objects.requireNonNull(predicate, "compressionPredicate");
HttpServer dup = duplicate();
dup.configuration().compressPredicate = predicate;
return dup;
}
/**
* Specifies whether GZip response compression is enabled if the client request
* presents accept encoding.
*
* @param compressionEnabled if true GZip response compression
* is enabled if the client request presents accept encoding, otherwise disabled.
* @return a new {@link HttpServer}
*/
public final HttpServer compress(boolean compressionEnabled) {
HttpServer dup = duplicate();
if (compressionEnabled) {
dup.configuration().minCompressionSize = 0;
}
else {
dup.configuration().minCompressionSize = -1;
dup.configuration().compressPredicate = null;
}
return dup;
}
/**
* Enable GZip response compression if the client request presents accept encoding
* headers AND the response reaches a minimum threshold
*
* @param minResponseSize compression is performed once response size exceeds the given
* value in bytes
*
* @return a new {@link HttpServer}
*/
public final HttpServer compress(int minResponseSize) {
if (minResponseSize < 0) {
throw new IllegalArgumentException("minResponseSize must be positive");
}
HttpServer dup = duplicate();
dup.configuration().minCompressionSize = minResponseSize;
return dup;
}
/**
* Configure the
* {@link ServerCookieEncoder}; {@link ServerCookieDecoder} will be
* chosen based on the encoder
*
* @param encoder the preferred ServerCookieEncoder
*
* @return a new {@link HttpServer}
*/
public final HttpServer cookieCodec(ServerCookieEncoder encoder) {
Objects.requireNonNull(encoder, "encoder");
ServerCookieDecoder decoder = encoder == ServerCookieEncoder.LAX ?
ServerCookieDecoder.LAX : ServerCookieDecoder.STRICT;
HttpServer dup = duplicate();
dup.configuration().cookieEncoder = encoder;
dup.configuration().cookieDecoder = decoder;
return dup;
}
/**
* Configure the
* {@link ServerCookieEncoder} and {@link ServerCookieDecoder}
*
* @param encoder the preferred ServerCookieEncoder
* @param decoder the preferred ServerCookieDecoder
*
* @return a new {@link HttpServer}
*/
public final HttpServer cookieCodec(ServerCookieEncoder encoder, ServerCookieDecoder decoder) {
Objects.requireNonNull(encoder, "encoder");
Objects.requireNonNull(decoder, "decoder");
HttpServer dup = duplicate();
dup.configuration().cookieEncoder = encoder;
dup.configuration().cookieDecoder = decoder;
return dup;
}
/**
* Specifies a custom request handler for deriving information about the connection.
*
* @param handler the forwarded header handler
* @return a new {@link HttpServer}
* @since 0.9.12
*/
public final HttpServer forwarded(BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> handler) {
Objects.requireNonNull(handler, "handler");
HttpServer dup = duplicate();
dup.configuration().forwardedHeaderHandler = handler;
return dup;
}
/**
* Specifies whether support for the {@code "Forwarded"} and {@code "X-Forwarded-*"}
* HTTP request headers for deriving information about the connection is enabled.
*
* @param forwardedEnabled if true support for the {@code "Forwarded"} and {@code "X-Forwarded-*"}
* HTTP request headers for deriving information about the connection is enabled,
* otherwise disabled.
* @return a new {@link HttpServer}
* @since 0.9.7
*/
public final HttpServer forwarded(boolean forwardedEnabled) {
if (forwardedEnabled) {
if (configuration().forwardedHeaderHandler == DefaultHttpForwardedHeaderHandler.INSTANCE) {
return this;
}
HttpServer dup = duplicate();
dup.configuration().forwardedHeaderHandler = DefaultHttpForwardedHeaderHandler.INSTANCE;
return dup;
}
else if (configuration().forwardedHeaderHandler != null) {
HttpServer dup = duplicate();
dup.configuration().forwardedHeaderHandler = null;
return dup;
}
return this;
}
/**
* Attach an I/O handler to react on a connected client
*
* @param handler an I/O handler that can dispose underlying connection when {@link
* Publisher} terminates. Only the first registered handler will subscribe to the
* returned {@link Publisher} while other will immediately cancel given a same
* {@link Connection}
*
* @return a new {@link HttpServer}
*/
public final HttpServer handle(
BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> handler) {
Objects.requireNonNull(handler, "handler");
return childObserve(new HttpServerHandle(handler));
}
@Override
public final HttpServer host(String host) {
return super.host(host);
}
/**
* Apply HTTP/2 configuration
*
* @param http2Settings configures {@link Http2SettingsSpec} before requesting
* @return a new {@link HttpServer}
*/
public final HttpServer http2Settings(Consumer<Http2SettingsSpec.Builder> http2Settings) {
Objects.requireNonNull(http2Settings, "http2Settings");
Http2SettingsSpec.Builder builder = Http2SettingsSpec.builder();
http2Settings.accept(builder);
Http2SettingsSpec settings = builder.build();
if (settings.equals(configuration().http2Settings)) {
return this;
}
HttpServer dup = duplicate();
dup.configuration().http2Settings = settings;
return dup;
}
/**
* Apply HTTP form decoder configuration.
* The configuration is used when {@link HttpServerRequest#receiveForm()} is invoked.
* When a specific configuration per request is needed {@link HttpServerRequest#receiveForm(Consumer)}
* should be used.
*
* @param formDecoderBuilder {@link HttpServerFormDecoderProvider.Builder} for HTTP form decoder configuration
* @return a new {@link HttpServer}
* @since 1.0.11
*/
public final HttpServer httpFormDecoder(Consumer<HttpServerFormDecoderProvider.Builder> formDecoderBuilder) {
Objects.requireNonNull(formDecoderBuilder, "formDecoderBuilder");
HttpServerFormDecoderProvider.Build builder = new HttpServerFormDecoderProvider.Build();
formDecoderBuilder.accept(builder);
HttpServerFormDecoderProvider formDecoderProvider = builder.build();
if (formDecoderProvider.equals(configuration().formDecoderProvider)) {
return this;
}
HttpServer dup = duplicate();
dup.configuration().formDecoderProvider = formDecoderProvider;
return dup;
}
/**
* When {@link HttpMessage} is about to be logged the configured factory will be used for
* generating a sanitized log message.
* <p>
* Default to {@link ReactorNettyHttpMessageLogFactory}:
* <ul>
* <li>hides the query from the uri</li>
* <li>hides the headers values</li>
* <li>only {@link DecoderException} message is presented</li>
* </ul>
*
* @param httpMessageLogFactory the factory for generating the log message
* @return a new {@link HttpServer}
* @since 1.0.24
*/
public final HttpServer httpMessageLogFactory(HttpMessageLogFactory httpMessageLogFactory) {
Objects.requireNonNull(httpMessageLogFactory, "httpMessageLogFactory");
HttpServer dup = duplicate();
dup.configuration().httpMessageLogFactory = httpMessageLogFactory;
return dup;
}
/**
* Configure the {@link io.netty.handler.codec.http.HttpServerCodec}'s request decoding options.
*
* @param requestDecoderOptions a function to mutate the provided Http request decoder options
* @return a new {@link HttpServer}
*/
public final HttpServer httpRequestDecoder(Function<HttpRequestDecoderSpec, HttpRequestDecoderSpec> requestDecoderOptions) {
Objects.requireNonNull(requestDecoderOptions, "requestDecoderOptions");
HttpRequestDecoderSpec decoder = requestDecoderOptions.apply(new HttpRequestDecoderSpec()).build();
if (decoder.equals(configuration().decoder)) {
return this;
}
HttpServer dup = duplicate();
dup.configuration().decoder = decoder;
return dup;
}
/**
* Specifies an idle timeout on the connection when it is waiting for an HTTP request (resolution: ms).
* Once the timeout is reached the connection will be closed.
* <p>If an {@code idleTimeout} is not specified, this indicates no timeout (i.e. infinite),
* which means the connection will be closed only if one of the peers decides to close it.
* <p>If the {@code idleTimeout} is less than {@code 1ms}, then {@code 1ms} will be the idle timeout.
* <p>By default {@code idleTimeout} is not specified.
*
* @param idleTimeout an idle timeout on the connection when it is waiting for an HTTP request (resolution: ms)
* @return a new {@link HttpServer}
* @since 0.9.15
*/
public final HttpServer idleTimeout(Duration idleTimeout) {
Objects.requireNonNull(idleTimeout, "idleTimeout");
HttpServer dup = duplicate();
dup.configuration().idleTimeout = idleTimeout;
return dup;
}
/**
* Decorate the configured I/O handler.
* See {@link #handle(BiFunction)}.
*
* @param mapHandle A {@link BiFunction} to decorate the configured I/O handler
* @return a new {@link HttpServer}
*/
public final HttpServer mapHandle(BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle) {
Objects.requireNonNull(mapHandle, "mapHandle");
HttpServer dup = duplicate();
dup.configuration().mapHandle = mapHandle;
return dup;
}
/**
* The maximum number of HTTP/1.1 requests which can be served until the connection is closed by the server.
* Setting this attribute to:
* <ul>
* <li><strong>-1</strong>: The connection serves unlimited number of requests. It is up to the I/O handler to decide
* to close the connection. This is the default behaviour.</li>
* <li><strong>1</strong>: The connection is marked as non persistent and serves just one request.</li>
* <li><strong>>1</strong>: The connection serves a number of requests up to the specified maximum number
* then the connection is closed by the server.</li>
* </ul>
* @param maxKeepAliveRequests the maximum number of HTTP/1.1 requests which can be served until
* the connection is closed by the server
* @return a new {@link HttpServer}
* @since 1.0.13
*/
public final HttpServer maxKeepAliveRequests(int maxKeepAliveRequests) {
if (maxKeepAliveRequests < -1 || maxKeepAliveRequests == 0) {
throw new IllegalArgumentException("maxKeepAliveRequests must be positive or -1");
}
HttpServer dup = duplicate();
dup.configuration().maxKeepAliveRequests = maxKeepAliveRequests;
return dup;
}
/**
* Whether to enable metrics to be collected and registered in Micrometer's
* {@link io.micrometer.core.instrument.Metrics#globalRegistry globalRegistry}
* under the name {@link reactor.netty.Metrics#HTTP_SERVER_PREFIX}.
* <p>{@code uriTagValue} function receives the actual uri and returns the uri tag value
* that will be used for the metrics with {@link reactor.netty.Metrics#URI} tag.
* For example instead of using the actual uri {@code "/users/1"} as uri tag value, templated uri
* {@code "/users/{id}"} can be used.
* <p><strong>Note:</strong>
* It is strongly recommended to provide template-like form for the URIs. Without a conversion to a template-like form,
* each distinct URI leads to the creation of a distinct tag, which takes a lot of memory for the metrics.
* <p><strong>Note:</strong>
* It is strongly recommended applications to configure an upper limit for the number of the URI tags.
* For example:
* <pre class="code">
* Metrics.globalRegistry
* .config()
* .meterFilter(MeterFilter.maximumAllowableTags(HTTP_SERVER_PREFIX, URI, 100, MeterFilter.deny()));
* </pre>
* <p>By default metrics are not enabled.
*
* @param enable true enables metrics collection; false disables it
* @param uriTagValue a function that receives the actual uri and returns the uri tag value
* that will be used for the metrics with {@link reactor.netty.Metrics#URI} tag
* @return a new {@link HttpServer}
* @since 0.9.7
*/
public final HttpServer metrics(boolean enable, Function<String, String> uriTagValue) {
if (enable) {
if (!Metrics.isInstrumentationAvailable()) {
throw new UnsupportedOperationException(
"To enable metrics, you must add the dependency `io.micrometer:micrometer-core`" +
" to the class path first");
}
if (uriTagValue == Function.<String>identity()) {
log.debug("Metrics are enabled with [uriTagValue=Function#identity]. " +
"It is strongly recommended to provide template-like form for the URIs. " +
"Without a conversion to a template-like form, each distinct URI leads " +
"to the creation of a distinct tag, which takes a lot of memory for the metrics.");
}
HttpServer dup = duplicate();
dup.configuration().metricsRecorder(() -> configuration().defaultMetricsRecorder());
dup.configuration().uriTagValue = uriTagValue;
return dup;
}
else if (configuration().metricsRecorder() != null) {
HttpServer dup = duplicate();
dup.configuration().metricsRecorder(null);
dup.configuration().uriTagValue = null;
return dup;
}
else {
return this;
}
}
@Override
public final HttpServer metrics(boolean enable, Supplier<? extends ChannelMetricsRecorder> recorder) {
return super.metrics(enable, recorder);
}
/**
* Specifies whether the metrics are enabled on the {@link HttpServer}.
* All generated metrics are provided to the specified recorder which is only
* instantiated if metrics are being enabled (the instantiation is not lazy,
* but happens immediately, while configuring the {@link HttpServer}).
* <p>{@code uriValue} function receives the actual uri and returns the uri value
* that will be used when the metrics are propagated to the recorder.
* For example instead of using the actual uri {@code "/users/1"} as uri value, templated uri
* {@code "/users/{id}"} can be used.
*
* @param enable true enables metrics collection; false disables it
* @param recorder a supplier for the metrics recorder that receives the collected metrics
* @param uriValue a function that receives the actual uri and returns the uri value
* that will be used when the metrics are propagated to the recorder.
* @return a new {@link HttpServer}
*/
public final HttpServer metrics(boolean enable, Supplier<? extends ChannelMetricsRecorder> recorder, Function<String, String> uriValue) {
if (enable) {
HttpServer dup = duplicate();
dup.configuration().metricsRecorder(recorder);
dup.configuration().uriTagValue = uriValue;
return dup;
}
else if (configuration().metricsRecorder() != null) {
HttpServer dup = duplicate();
dup.configuration().metricsRecorder(null);
dup.configuration().uriTagValue = null;
return dup;
}
else {
return this;
}
}
/**
* Removes any previously applied SSL configuration customization
*
* @return a new {@link HttpServer}
*/
public final HttpServer noSSL() {
if (configuration().isSecure()) {
HttpServer dup = duplicate();
dup.configuration().sslProvider = null;
return dup;
}
return this;
}
@Override
public final HttpServer port(int port) {
return super.port(port);
}
/**
* The HTTP protocol to support. Default is {@link HttpProtocol#HTTP11}.
*
* @param supportedProtocols The various {@link HttpProtocol} this server will support
*
* @return a new {@link HttpServer}
*/
public final HttpServer protocol(HttpProtocol... supportedProtocols) {
Objects.requireNonNull(supportedProtocols, "supportedProtocols");
HttpServer dup = duplicate();
dup.configuration().protocols(supportedProtocols);
return dup;
}
/**
* Specifies whether support for the {@code "HAProxy proxy protocol"}
* for deriving information about the address of the remote peer is enabled.
*
* @param proxyProtocolSupportType
* <ul>
* <li>
* choose {@link ProxyProtocolSupportType#ON}
* to enable support for the {@code "HAProxy proxy protocol"}
* for deriving information about the address of the remote peer.
* </li>
* <li>choose {@link ProxyProtocolSupportType#OFF} to disable the proxy protocol support.</li>
* <li>
* choose {@link ProxyProtocolSupportType#AUTO}
* then each connection of the same {@link HttpServer} will auto detect whether there is proxy protocol,
* so {@link HttpServer} can accept requests with or without proxy protocol at the same time.
* </li>
* </ul>
*
* @return a new {@link HttpServer}
*/
public final HttpServer proxyProtocol(ProxyProtocolSupportType proxyProtocolSupportType) {
Objects.requireNonNull(proxyProtocolSupportType, "The parameter: proxyProtocolSupportType must not be null.");
if (proxyProtocolSupportType == configuration().proxyProtocolSupportType) {
return this;
}
if (proxyProtocolSupportType == ProxyProtocolSupportType.ON ||
proxyProtocolSupportType == ProxyProtocolSupportType.AUTO) {
if (!HAProxyMessageReader.isProxyProtocolAvailable()) {
throw new UnsupportedOperationException(
"To enable proxyProtocol, you must add the dependency `io.netty:netty-codec-haproxy`" +
" to the class path first");
}
}
HttpServer dup = duplicate();
dup.configuration().proxyProtocolSupportType = proxyProtocolSupportType;
return dup;
}
/**
* Define routes for the server through the provided {@link HttpServerRoutes} builder.
*
* @param routesBuilder provides a route builder to be mutated in order to define routes.
* @return a new {@link HttpServer} starting the router on subscribe
*/
public final HttpServer route(Consumer<? super HttpServerRoutes> routesBuilder) {
Objects.requireNonNull(routesBuilder, "routeBuilder");
HttpServerRoutes routes = HttpServerRoutes.newRoutes();
routesBuilder.accept(routes);
return handle(routes);
}
/**
* Apply an SSL configuration customization via the passed builder. The builder
* will produce the {@link SslContext} to be passed to with a default value of
* {@code 10} seconds handshake timeout unless the environment property {@code
* reactor.netty.tcp.sslHandshakeTimeout} is set.
*
* If {@link SelfSignedCertificate} needs to be used, the sample below can be
* used. Note that {@link SelfSignedCertificate} should not be used in production.
* <pre>
* {@code
* SelfSignedCertificate cert = new SelfSignedCertificate();
* Http11SslContextSpec http11SslContextSpec =
* Http11SslContextSpec.forServer(cert.certificate(), cert.privateKey());
* secure(sslContextSpec -> sslContextSpec.sslContext(http11SslContextSpec));
* }
* </pre>
*
* @param sslProviderBuilder builder callback for further customization of SslContext.
* @return a new {@link HttpServer}
*/
public final HttpServer secure(Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder) {
return secure(sslProviderBuilder, false);
}
/**
* Apply an SSL configuration customization via the passed builder. The builder
* will produce the {@link SslContext} to be passed to with a default value of
* {@code 10} seconds handshake timeout unless the environment property {@code
* reactor.netty.tcp.sslHandshakeTimeout} is set.
* <p>
* If {@link SelfSignedCertificate} needs to be used, the sample below can be
* used. Note that {@link SelfSignedCertificate} should not be used in production.
* <pre>
* {@code
* SelfSignedCertificate cert = new SelfSignedCertificate();
* Http11SslContextSpec http11SslContextSpec =
* Http11SslContextSpec.forServer(cert.certificate(), cert.privateKey());
* secure(sslContextSpec -> sslContextSpec.sslContext(http11SslContextSpec), true);
* }
* </pre>
*
* @param sslProviderBuilder builder callback for further customization of SslContext.
* @param redirectHttpToHttps true enables redirecting HTTP to HTTPS by changing the
* scheme only but otherwise leaving the port the same.
* This configuration is applicable only for HTTP 1.x.
* @return a new {@link HttpServer}
* @since 1.0.5
*/
public final HttpServer secure(Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder, boolean redirectHttpToHttps) {
Objects.requireNonNull(sslProviderBuilder, "sslProviderBuilder");
HttpServer dup = duplicate();
SslProvider.SslContextSpec builder = SslProvider.builder();
sslProviderBuilder.accept(builder);
dup.configuration().sslProvider = ((SslProvider.Builder) builder).build();
dup.configuration().redirectHttpToHttps = redirectHttpToHttps;
return dup;
}
/**
* Applies an SSL configuration via the passed {@link SslProvider}.
*
* If {@link SelfSignedCertificate} needs to be used, the sample below can be
* used. Note that {@link SelfSignedCertificate} should not be used in production.
* <pre>
* {@code
* SelfSignedCertificate cert = new SelfSignedCertificate();
* Http11SslContextSpec http11SslContextSpec =
* Http11SslContextSpec.forServer(cert.certificate(), cert.privateKey());
* secure(sslContextSpec -> sslContextSpec.sslContext(http11SslContextSpec));
* }
* </pre>
*
* @param sslProvider The provider to set when configuring SSL
*
* @return a new {@link HttpServer}
*/
public final HttpServer secure(SslProvider sslProvider) {
return secure(sslProvider, false);
}
/**
* Applies an SSL configuration via the passed {@link SslProvider}.
* <p>
* If {@link SelfSignedCertificate} needs to be used, the sample below can be
* used. Note that {@link SelfSignedCertificate} should not be used in production.
* <pre>
* {@code
* SelfSignedCertificate cert = new SelfSignedCertificate();
* Http11SslContextSpec http11SslContextSpec =
* Http11SslContextSpec.forServer(cert.certificate(), cert.privateKey());
* secure(sslContextSpec -> sslContextSpec.sslContext(http11SslContextSpec), true);
* }
* </pre>
*
* @param sslProvider The provider to set when configuring SSL
* @param redirectHttpToHttps true enables redirecting HTTP to HTTPS by changing the
* scheme only but otherwise leaving the port the same.
* This configuration is applicable only for HTTP 1.x.
* @return a new {@link HttpServer}
* @since 1.0.5
*/
public final HttpServer secure(SslProvider sslProvider, boolean redirectHttpToHttps) {
Objects.requireNonNull(sslProvider, "sslProvider");
HttpServer dup = duplicate();
dup.configuration().sslProvider = sslProvider;
dup.configuration().redirectHttpToHttps = redirectHttpToHttps;
return dup;
}
/**
* Apply a {@link TcpServer} mapping function to update TCP configuration and
* return an enriched {@link HttpServer} to use.
* <p>
* <strong>Note:</strong>
* There isn't only one method that replaces this deprecated method.
* The configuration that can be done with this deprecated method,
* can also be done with the other methods exposed by {@link HttpServer}.
* </p>
* <p>Examples:</p>
* <p>Configuration via the deprecated '.tcpConfiguration(...)' method</p>
* <pre>
* {@code
* HttpServer.tcpConfiguration(tcpServer ->
* tcpServer.attr(...) // configures the channel attributes
* .bindAddress(...) // configures the bind (local) address
* .channelGroup(...) // configures the channel group
* .childAttr(...) // configures the child channel attributes
* .childObserve(...) // configures the child channel connection observer
* .childOption(...) // configures the child channel options
* .doOnBound(...) // configures the doOnBound callback
* .doOnChannelInit(...) // configures the channel handler
* .doOnConnection(...) // configures the doOnConnection callback
* .doOnUnbound(...) // configures the doOnUnbound callback
* .handle(...) // configures the I/O handler
* .host(...) // configures the host name
* .metrics(...) // configures the metrics
* .noSSL() // removes SSL configuration
* .observe() // configures the connection observer
* .option(...) // configures the channel options
* .port(...) // configures the port
* .runOn(...) // configures the event loop group
* .secure() // configures the SSL
* .wiretap()) // configures the wire logging
* }
* </pre>
*
* <p>Configuration via the other methods exposed by {@link HttpServer}</p>
* <pre>
* {@code
* HttpServer.attr(...) // configures the channel attributes
* .bindAddress(...) // configures the bind (local) address
* .channelGroup(...) // configures the channel group
* .childAttr(...) // configures the child channel attributes
* .childObserve(...) // configures the child channel connection observer
* .childOption(...) // configures the child channel options
* .doOnBound(...) // configures the doOnBound callback
* .doOnChannelInit(...) // configures the channel handler
* .doOnConnection(...) // configures the doOnConnection callback
* .doOnUnbound(...) // configures the doOnUnbound callback
* .handle(...) // configures the I/O handler
* .host(...) // configures the host name
* .metrics(...) // configures the metrics
* .noSSL() // removes SSL configuration
* .observe() // configures the connection observer
* .option(...) // configures the channel options
* .port(...) // configures the port
* .runOn(...) // configures the event loop group
* .secure() // configures the SSL
* .wiretap() // configures the wire logging
* }
* </pre>
*
* <p>Wire logging in plain text</p>
* <pre>
* {@code
* HttpServer.wiretap("logger", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL)
* }
* </pre>
*
* @param tcpMapper A {@link TcpServer} mapping function to update TCP configuration and
* return an enriched {@link HttpServer} to use.
* @return a new {@link HttpServer}
* @deprecated Use the other methods exposed by {@link HttpServer} to achieve the same configurations.
* This method will be removed in version 1.1.0.
*/
@Deprecated
@SuppressWarnings("ReturnValueIgnored")
public final HttpServer tcpConfiguration(Function<? super TcpServer, ? extends TcpServer> tcpMapper) {
Objects.requireNonNull(tcpMapper, "tcpMapper");
HttpServerTcpConfig tcpServer = new HttpServerTcpConfig(this);
// ReturnValueIgnored is deliberate
tcpMapper.apply(tcpServer);
return tcpServer.httpServer;
}
/**
* Based on the actual configuration, returns a {@link Mono} that triggers:
* <ul>
* <li>an initialization of the event loop groups</li>
* <li>loads the necessary native libraries for the transport</li>
* <li>loads the necessary native libraries for the security if there is such</li>
* </ul>
* By default, when method is not used, the {@code bind operation} absorbs the extra time needed to load resources.
*
* @return a {@link Mono} representing the completion of the warmup
* @since 1.0.3
*/
@Override
public Mono<Void> warmup() {
return Mono.when(
super.warmup(),
Mono.fromRunnable(() -> {
SslProvider provider = configuration().sslProvider();
if (provider != null && !(provider.getSslContext() instanceof JdkSslContext)) {
OpenSsl.version();
}
}));
}
@Override
public final HttpServer wiretap(boolean enable) {
return super.wiretap(enable);
}
static final Logger log = Loggers.getLogger(HttpServer.class);
static final class HttpServerHandle implements ConnectionObserver {
final BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> handler;
HttpServerHandle(BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> handler) {
this.handler = handler;
}
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void onStateChange(Connection connection, State newState) {
if (newState == HttpServerState.REQUEST_RECEIVED) {
try {
if (log.isDebugEnabled()) {
log.debug(format(connection.channel(), "Handler is being applied: {}"), handler);
}
HttpServerOperations ops = (HttpServerOperations) connection;
Publisher<Void> publisher = handler.apply(ops, ops);
Mono<Void> mono = Mono.deferContextual(ctx -> {
ops.currentContext = Context.of(ctx);
return Mono.fromDirect(publisher);
});
if (ops.mapHandle != null) {
mono = ops.mapHandle.apply(mono, connection);