-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a corresponding Reactor Netty version of Cors Http Server exa…
…mple as found in Netty project.
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
reactor-netty-examples/src/main/java/reactor/netty/examples/http/cors/HttpCorsServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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)); | ||
} | ||
} |