Skip to content

Commit

Permalink
[doc] Add HTTP2/HTTP3 configuration to the HTTP examples (#3578)
Browse files Browse the repository at this point in the history
  • Loading branch information
violetagg authored Jan 2, 2025
1 parent 2eafbc2 commit 6fa947f
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 21 deletions.
4 changes: 2 additions & 2 deletions reactor-netty-examples/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2024 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2020-2025 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.
Expand All @@ -26,12 +26,12 @@ dependencies {
api "io.zipkin.reporter2:zipkin-sender-urlconnection:$zipkinSenderVersion"

api "io.netty:netty-transport-native-epoll:$nettyVersion:linux-x86_64"
api "io.netty.incubator:netty-incubator-codec-http3:$nettyHttp3Version"

runtimeOnly "ch.qos.logback:logback-classic:$logbackVersion"
runtimeOnly "io.netty:netty-tcnative-boringssl-static:$boringSslVersion$os_suffix"
// Needed for proxy testing
runtimeOnly "io.netty:netty-handler-proxy:$nettyVersion"
runtimeOnly "io.netty.incubator:netty-incubator-codec-http3:$nettyHttp3Version"
}

description = "Examples for the Reactor Netty library"
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import io.netty.handler.codec.http.cors.CorsHandler;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import java.security.cert.CertificateException;
import java.time.Duration;

import reactor.netty.Connection;
import reactor.netty.NettyOutbound;
import reactor.netty.NettyPipeline;
import reactor.netty.http.Http11SslContextSpec;
import reactor.netty.http.Http2SslContextSpec;
import reactor.netty.http.Http3SslContextSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.server.HttpServer;
import reactor.netty.http.server.HttpServerRequest;
Expand All @@ -44,6 +46,7 @@ public class HttpCorsServer {
static final boolean WIRETAP = System.getProperty("wiretap") != null;
static final boolean COMPRESS = System.getProperty("compress") != null;
static final boolean HTTP2 = System.getProperty("http2") != null;
static final boolean HTTP3 = System.getProperty("http3") != null;

public static void main(String... args) throws CertificateException {
HttpServer server =
Expand All @@ -59,6 +62,9 @@ public static void main(String... args) throws CertificateException {
if (HTTP2) {
server = server.secure(spec -> spec.sslContext(Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey())));
}
else if (HTTP3) {
server = server.secure(spec -> spec.sslContext(Http3SslContextSpec.forServer(ssc.key(), null, ssc.cert())));
}
else {
server = server.secure(spec -> spec.sslContext(Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey())));
}
Expand All @@ -68,6 +74,16 @@ public static void main(String... args) throws CertificateException {
server = server.protocol(HttpProtocol.H2);
}

if (HTTP3) {
server =
server.protocol(HttpProtocol.HTTP3)
.http3Settings(spec -> spec.idleTimeout(Duration.ofSeconds(5))
.maxData(10000000)
.maxStreamDataBidirectionalLocal(1000000)
.maxStreamDataBidirectionalRemote(1000000)
.maxStreamsBidirectional(100));
}

server.bindNow()
.onDispose()
.block();
Expand All @@ -81,11 +97,14 @@ private static NettyOutbound okResponse(HttpServerRequest request, HttpServerRes

private static void addCorsHandler(Connection connection) {
CorsConfig corsConfig = CorsConfigBuilder.forOrigin("example.com").allowNullOrigin().allowCredentials().allowedRequestHeaders("custom-request-header").build();
if (!HTTP2) {
connection.channel().pipeline().addAfter(NettyPipeline.HttpCodec, "Cors", new CorsHandler(corsConfig));
if (HTTP2) {
connection.channel().pipeline().addAfter(NettyPipeline.H2ToHttp11Codec, "Cors", new CorsHandler(corsConfig));
}
else if (HTTP3) {
connection.channel().pipeline().addAfter(NettyPipeline.H3ToHttp11Codec, "Cors", new CorsHandler(corsConfig));
}
else {
connection.channel().pipeline().addAfter(NettyPipeline.H2ToHttp11Codec, "Cors", new CorsHandler(corsConfig));
connection.channel().pipeline().addAfter(NettyPipeline.HttpCodec, "Cors", new CorsHandler(corsConfig));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2020-2025 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.
Expand All @@ -19,8 +19,13 @@
import reactor.core.publisher.Mono;
import reactor.netty.ByteBufFlux;
import reactor.netty.http.Http11SslContextSpec;
import reactor.netty.http.Http2SslContextSpec;
import reactor.netty.http.Http3SslContextSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;

/**
* An HTTP client that sends POST request to the HTTP server and
* receives as a response the content that was sent as a request.
Expand All @@ -33,6 +38,8 @@ public final class EchoClient {
static final int PORT = Integer.parseInt(System.getProperty("port", SECURE ? "8443" : "8080"));
static final boolean WIRETAP = System.getProperty("wiretap") != null;
static final boolean COMPRESS = System.getProperty("compress") != null;
static final boolean HTTP2 = System.getProperty("http2") != null;
static final boolean HTTP3 = System.getProperty("http3") != null;

public static void main(String[] args) {
HttpClient client =
Expand All @@ -42,10 +49,36 @@ public static void main(String[] args) {
.compress(COMPRESS);

if (SECURE) {
Http11SslContextSpec http11SslContextSpec =
Http11SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
client = client.secure(spec -> spec.sslContext(http11SslContextSpec));
if (HTTP2) {
Http2SslContextSpec http2SslContextSpec =
Http2SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
client = client.secure(spec -> spec.sslContext(http2SslContextSpec));
}
else if (HTTP3) {
Http3SslContextSpec http3SslContextSpec =
Http3SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
client = client.secure(spec -> spec.sslContext(http3SslContextSpec));
}
else {
Http11SslContextSpec http11SslContextSpec =
Http11SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
client = client.secure(spec -> spec.sslContext(http11SslContextSpec));
}
}

if (HTTP2) {
client = client.protocol(HttpProtocol.H2);
}

if (HTTP3) {
client =
client.protocol(HttpProtocol.HTTP3)
.http3Settings(spec -> spec.idleTimeout(Duration.ofSeconds(5))
.maxData(10000000)
.maxStreamDataBidirectionalLocal(1000000));
}

String response =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2023 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2020-2025 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.
Expand All @@ -18,9 +18,12 @@
import io.netty.handler.ssl.util.SelfSignedCertificate;
import reactor.netty.http.Http11SslContextSpec;
import reactor.netty.http.Http2SslContextSpec;
import reactor.netty.http.Http3SslContextSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.server.HttpServer;

import java.time.Duration;

import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN;

Expand All @@ -36,6 +39,7 @@ public final class EchoServer {
static final boolean WIRETAP = System.getProperty("wiretap") != null;
static final boolean COMPRESS = System.getProperty("compress") != null;
static final boolean HTTP2 = System.getProperty("http2") != null;
static final boolean HTTP3 = System.getProperty("http3") != null;

public static void main(String[] args) throws Exception {
HttpServer server =
Expand All @@ -52,6 +56,9 @@ public static void main(String[] args) throws Exception {
if (HTTP2) {
server = server.secure(spec -> spec.sslContext(Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey())));
}
else if (HTTP3) {
server = server.secure(spec -> spec.sslContext(Http3SslContextSpec.forServer(ssc.key(), null, ssc.cert())));
}
else {
server = server.secure(spec -> spec.sslContext(Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey())));
}
Expand All @@ -61,6 +68,16 @@ public static void main(String[] args) throws Exception {
server = server.protocol(HttpProtocol.H2);
}

if (HTTP3) {
server =
server.protocol(HttpProtocol.HTTP3)
.http3Settings(spec -> spec.idleTimeout(Duration.ofSeconds(5))
.maxData(10000000)
.maxStreamDataBidirectionalLocal(1000000)
.maxStreamDataBidirectionalRemote(1000000)
.maxStreamsBidirectional(100));
}

server.bindNow()
.onDispose()
.block();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2020-2025 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.
Expand All @@ -17,8 +17,13 @@

import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import reactor.netty.http.Http11SslContextSpec;
import reactor.netty.http.Http2SslContextSpec;
import reactor.netty.http.Http3SslContextSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;

/**
* An HTTP client that sends GET request to the HTTP server and receives as a response - "Hello World!".
*
Expand All @@ -30,6 +35,8 @@ public final class HelloWorldClient {
static final int PORT = Integer.parseInt(System.getProperty("port", SECURE ? "8443" : "8080"));
static final boolean WIRETAP = System.getProperty("wiretap") != null;
static final boolean COMPRESS = System.getProperty("compress") != null;
static final boolean HTTP2 = System.getProperty("http2") != null;
static final boolean HTTP3 = System.getProperty("http3") != null;

public static void main(String[] args) {
HttpClient client =
Expand All @@ -39,10 +46,36 @@ public static void main(String[] args) {
.compress(COMPRESS);

if (SECURE) {
Http11SslContextSpec http11SslContextSpec =
Http11SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
client = client.secure(spec -> spec.sslContext(http11SslContextSpec));
if (HTTP2) {
Http2SslContextSpec http2SslContextSpec =
Http2SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
client = client.secure(spec -> spec.sslContext(http2SslContextSpec));
}
else if (HTTP3) {
Http3SslContextSpec http3SslContextSpec =
Http3SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
client = client.secure(spec -> spec.sslContext(http3SslContextSpec));
}
else {
Http11SslContextSpec http11SslContextSpec =
Http11SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
client = client.secure(spec -> spec.sslContext(http11SslContextSpec));
}
}

if (HTTP2) {
client = client.protocol(HttpProtocol.H2);
}

if (HTTP3) {
client =
client.protocol(HttpProtocol.HTTP3)
.http3Settings(spec -> spec.idleTimeout(Duration.ofSeconds(5))
.maxData(10000000)
.maxStreamDataBidirectionalLocal(1000000));
}

String response =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2023 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2020-2025 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.
Expand All @@ -19,9 +19,12 @@
import reactor.core.publisher.Mono;
import reactor.netty.http.Http11SslContextSpec;
import reactor.netty.http.Http2SslContextSpec;
import reactor.netty.http.Http3SslContextSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.server.HttpServer;

import java.time.Duration;

import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN;

Expand All @@ -37,6 +40,7 @@ public final class HelloWorldServer {
static final boolean WIRETAP = System.getProperty("wiretap") != null;
static final boolean COMPRESS = System.getProperty("compress") != null;
static final boolean HTTP2 = System.getProperty("http2") != null;
static final boolean HTTP3 = System.getProperty("http3") != null;

public static void main(String[] args) throws Exception {
HttpServer server =
Expand All @@ -53,6 +57,9 @@ public static void main(String[] args) throws Exception {
if (HTTP2) {
server = server.secure(spec -> spec.sslContext(Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey())));
}
else if (HTTP3) {
server = server.secure(spec -> spec.sslContext(Http3SslContextSpec.forServer(ssc.key(), null, ssc.cert())));
}
else {
server = server.secure(spec -> spec.sslContext(Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey())));
}
Expand All @@ -62,6 +69,16 @@ public static void main(String[] args) throws Exception {
server = server.protocol(HttpProtocol.H2);
}

if (HTTP3) {
server =
server.protocol(HttpProtocol.HTTP3)
.http3Settings(spec -> spec.idleTimeout(Duration.ofSeconds(5))
.maxData(10000000)
.maxStreamDataBidirectionalLocal(1000000)
.maxStreamDataBidirectionalRemote(1000000)
.maxStreamsBidirectional(100));
}

server.bindNow()
.onDispose()
.block();
Expand Down
Loading

0 comments on commit 6fa947f

Please sign in to comment.