From d445d2e4c8512bc64f23f70c880bd1776c5a9722 Mon Sep 17 00:00:00 2001 From: Jack Cheng Date: Tue, 17 Dec 2024 19:30:43 -0800 Subject: [PATCH 1/2] Provide a corresponding Reactor Netty version of Cors Http Server example as found in Netty project. --- .../examples/http/cors/HttpCorsServer.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java diff --git a/reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java b/reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java new file mode 100644 index 0000000000..ab4b5e02b6 --- /dev/null +++ b/reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2024 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.examples.http.cors; + +import io.netty.channel.Channel; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.cors.CorsConfig; +import io.netty.handler.codec.http.cors.CorsConfigBuilder; +import io.netty.handler.codec.http.cors.CorsHandler; +import io.netty.handler.ssl.util.SelfSignedCertificate; +import java.net.SocketAddress; +import java.security.cert.CertificateException; +import reactor.netty.ConnectionObserver; +import reactor.netty.NettyOutbound; +import reactor.netty.NettyPipeline; +import reactor.netty.http.Http11SslContextSpec; +import reactor.netty.http.Http2SslContextSpec; +import reactor.netty.http.HttpProtocol; +import reactor.netty.http.server.HttpServer; +import reactor.netty.http.server.HttpServerRequest; +import reactor.netty.http.server.HttpServerResponse; + +/** + * An HTTP server that handles CORS (Cross-Origin Resource Sharing) requests. + * + * @author Jack Cheng + **/ +public class HttpCorsServer { + + static final boolean SECURE = System.getProperty("secure") != null; + 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; + + public static void main(String... args) throws CertificateException { + + + HttpServer server = + HttpServer.create() + .port(PORT) + .wiretap(WIRETAP) + .compress(COMPRESS) + .doOnChannelInit(HttpCorsServer::addCorsHandler) + .route(routes -> routes.route(r -> true, + HttpCorsServer::okResponse)); + + if (SECURE) { + + SelfSignedCertificate ssc = new SelfSignedCertificate("localhost"); + if (HTTP2) { + server = server.secure(spec -> spec.sslContext(Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey()))); + } + else { + server = server.secure(spec -> spec.sslContext(Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey()))); + } + } + + if (HTTP2) { + server = server.protocol(HttpProtocol.H2); + } + + server.bindNow() + .onDispose() + .block(); + } + + private static NettyOutbound okResponse(HttpServerRequest request, HttpServerResponse response) { + response.status(HttpResponseStatus.OK); + response.header("custom-response-header", "Some value"); + return response; + } + + + private static void addCorsHandler(ConnectionObserver observer, Channel channel, SocketAddress remoteAddress) { + CorsConfig corsConfig = CorsConfigBuilder.forOrigin("example.com").allowNullOrigin().allowCredentials().allowedRequestHeaders("custom-request-header").build(); + channel.pipeline().addAfter(NettyPipeline.HttpCodec, "Cors", new CorsHandler(corsConfig)); + } +} From a0585dba7dcefb5dcbbd502c69831bbda96eb067 Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Thu, 2 Jan 2025 13:15:20 +0200 Subject: [PATCH 2/2] Update copyright year --- .../java/reactor/netty/examples/http/cors/HttpCorsServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java b/reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java index ab4b5e02b6..efa2fbb297 100644 --- a/reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java +++ b/reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 VMware, Inc. or its affiliates, All Rights Reserved. + * Copyright (c) 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.