Skip to content

Commit

Permalink
[doc] In case of HTTP/2, ensure the handler is added to the stream's …
Browse files Browse the repository at this point in the history
…pipeline
  • Loading branch information
violetagg committed Jan 2, 2025
1 parent f44d579 commit 2eafbc2
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
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.Connection;
import reactor.netty.NettyOutbound;
import reactor.netty.NettyPipeline;
import reactor.netty.http.Http11SslContextSpec;
Expand Down Expand Up @@ -52,7 +51,7 @@ public static void main(String... args) throws CertificateException {
.port(PORT)
.wiretap(WIRETAP)
.compress(COMPRESS)
.doOnChannelInit(HttpCorsServer::addCorsHandler)
.doOnConnection(HttpCorsServer::addCorsHandler)
.route(routes -> routes.route(r -> true, HttpCorsServer::okResponse));

if (SECURE) {
Expand Down Expand Up @@ -80,8 +79,13 @@ private static NettyOutbound okResponse(HttpServerRequest request, HttpServerRes
return response;
}

private static void addCorsHandler(ConnectionObserver observer, Channel channel, SocketAddress remoteAddress) {
private static void addCorsHandler(Connection connection) {
CorsConfig corsConfig = CorsConfigBuilder.forOrigin("example.com").allowNullOrigin().allowCredentials().allowedRequestHeaders("custom-request-header").build();
channel.pipeline().addAfter(NettyPipeline.HttpCodec, "Cors", new CorsHandler(corsConfig));
if (!HTTP2) {
connection.channel().pipeline().addAfter(NettyPipeline.HttpCodec, "Cors", new CorsHandler(corsConfig));
}
else {
connection.channel().pipeline().addAfter(NettyPipeline.H2ToHttp11Codec, "Cors", new CorsHandler(corsConfig));
}
}
}

0 comments on commit 2eafbc2

Please sign in to comment.