@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if(sslCtx!=null) { p.addLast(new SslHandler(sslCtx.newEngine(ch.alloc()))); } p.addLast(new HttpResponseEncoder());//必须放在最前面,如果decoder途中需要回复消息,则decoder前面需要encoder p.addLast(new HttpRequestDecoder()); p.addLast(new HttpObjectAggregator(65536));//限制contentLength //大文件传输处理 // p.addLast(new ChunkedWriteHandler()); // p.addLast(new HttpContentCompressor()); //跨域配置 CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build(); p.addLast(new CorsHandler(corsConfig)); p.addLast(new DefaultListenerHandler<HttpRequest>(listener)); }
public static void main(String[] args) { String ip = "127.0.0.1"; int port = 8080; ChannelInitializer<SocketChannel> channelInit = new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); CorsConfig corsConfig = CorsConfig.withAnyOrigin() .allowedRequestHeaders("content-type","accept","MyCustomHeader") .allowedRequestMethods(PUT,POST,GET,DELETE) .build(); p.addLast(new HttpResponseEncoder()); p.addLast(new HttpRequestDecoder()); p.addLast(new HttpObjectAggregator(65536)); p.addLast(new ChunkedWriteHandler()); p.addLast(new CorsHandler(corsConfig)); p.addLast(new SimpleCORSHandler()); } }; NettyServerUtil.newHttpServerBootstrap(ip, port, channelInit); }
@Override protected void initChannel(SocketChannel ch) throws Exception { // Create a default pipeline implementation. CorsConfig corsConfig = CorsConfig.withAnyOrigin().build(); ChannelPipeline pipeline = ch.pipeline(); // Uncomment the following line if you want HTTPS //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine(); //engine.setUseClientMode(false); //pipeline.addLast("ssl", new SslHandler(engine)); pipeline.addLast("encoder", new HttpResponseEncoder()); pipeline.addLast("decoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(8388608)); // 8MB //pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("cors", new CorsHandler(corsConfig)); pipeline.addLast("handler", new HttpStaticFileServerHandler()); }
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } if (enableGzip) { p.addLast(new HttpContentCompressor()); } p.addLast(new HttpServerCodec(36192 * 2, 36192 * 8, 36192 * 16, false)); p.addLast(new HttpServerExpectContinueHandler()); p.addLast(new HttpObjectAggregator(Integer.MAX_VALUE)); p.addLast(new ChunkedWriteHandler()); if (enableCors) { CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build(); p.addLast(new CorsHandler(corsConfig)); } if (null != blade.webSocketPath()) { p.addLast(new WebSocketServerProtocolHandler(blade.webSocketPath(), null, true)); p.addLast(new WebSockerHandler(blade)); } service.scheduleWithFixedDelay(() -> date = new AsciiString(DateKit.gmtDate(LocalDateTime.now())), 1000, 1000, TimeUnit.MILLISECONDS); p.addLast(new HttpServerHandler()); }
public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); SslContext sslContext = server.sslContext(); if (sslContext != null) { p.addLast(sslContext.newHandler(ch.alloc())); } p.addLast(new HttpRequestDecoder()); p.addLast(new HttpObjectAggregator(server.maxContentLength())); p.addLast(new HttpResponseEncoder()); if (server.cors() != null) { p.addLast(new CorsHandler(server.cors())); } p.addLast(routerHandler); p.addLast(badClientSilencer); }
@Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); p.addLast(new ReadTimeoutHandler(60, TimeUnit.SECONDS)); if (sslContext != null) { p.addLast(sslContext.newHandler(ch.alloc())); } p.addLast(new HttpContentCompressor(5)); p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(1048576)); p.addLast(new ChunkedWriteHandler()); if (null != corsConfig) { p.addLast(new CorsHandler(corsConfig)); } p.addLast(new WebSocketServerCompressionHandler()); p.addLast(new WebSocketServerProtocolHandler(webSocketPath, null, true)); p.addLast(new LaputaServerHandler(null != sslContext, requestProcessor)); }
@Override protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception { ChannelPipeline pipeline = ctx.pipeline(); if (msg.uri().equals("/websocket")) { LOGGER.debug("Switching to WebSockets pipeline..."); pipeline.addAfter(ctx.name(), "ws-protocol-updater", webSocketsServerProtocolUpdater); pipeline.addAfter("ws-protocol-updater", "api-response-encoder-ws", webSocketsApiResponseEncoder); pipeline.addAfter("api-response-encoder-ws", "api-request-decoder-ws", new WebSocketsApiRequestDecoder(objectMapper)); pipeline.remove(this); } else { LOGGER.debug("Switching to REST pipeline..."); pipeline.addAfter(ctx.name(), "cors", new CorsHandler(corsConfig)); pipeline.addAfter("cors", "rest-codec", restCodec); pipeline.remove(this); } out.add(ReferenceCountUtil.retain(msg)); }
public void start() throws InterruptedException { ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_LINGER, socketLinger); b.option(ChannelOption.SO_REUSEADDR, reuseAddress); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpRequestDecoder()); p.addLast(new HttpObjectAggregator(1024 * 1024 * 5)); p.addLast(new HttpResponseEncoder()); p.addLast(new HttpContentCompressor()); if (corsConfiguration.hasHeader()) { p.addLast(new CorsHandler( CorsConfig .withOrigin(corsConfiguration.getHeader()) .allowedRequestHeaders(HttpHeaders.Names.CONTENT_TYPE) .allowedRequestMethods(HttpMethod.POST) .build()) ); } p.addLast(jsonRpcWeb3FilterHandler); p.addLast(jsonRpcWeb3ServerHandler); } }); b.bind(host, port).sync(); }
protected ChannelHandler setupHttpChannel(Configuration config, SslContext sslCtx) { return new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("ssl", new NonSslRedirectHandler(config, sslCtx)); ch.pipeline().addLast("encoder", new HttpResponseEncoder()); ch.pipeline().addLast("decoder", new HttpRequestDecoder()); ch.pipeline().addLast("compressor", new HttpContentCompressor()); ch.pipeline().addLast("decompressor", new HttpContentDecompressor()); ch.pipeline().addLast("aggregator", new HttpObjectAggregator(8192)); ch.pipeline().addLast("chunker", new ChunkedWriteHandler()); final Configuration.Cors corsCfg = config.getHttp().getCors(); final CorsConfig.Builder ccb; if (corsCfg.isAllowAnyOrigin()) { ccb = new CorsConfig.Builder(); } else { ccb = new CorsConfig.Builder(corsCfg.getAllowedOrigins().stream().toArray(String[]::new)); } if (corsCfg.isAllowNullOrigin()) { ccb.allowNullOrigin(); } if (corsCfg.isAllowCredentials()) { ccb.allowCredentials(); } corsCfg.getAllowedMethods().stream().map(HttpMethod::valueOf).forEach(ccb::allowedRequestMethods); corsCfg.getAllowedHeaders().forEach(ccb::allowedRequestHeaders); CorsConfig cors = ccb.build(); LOG.trace("Cors configuration: {}", cors); ch.pipeline().addLast("cors", new CorsHandler(cors)); ch.pipeline().addLast("queryDecoder", new qonduit.netty.http.HttpRequestDecoder(config)); ch.pipeline().addLast("strict", new StrictTransportHandler(config)); ch.pipeline().addLast("login", new X509LoginRequestHandler(config)); ch.pipeline().addLast("doLogin", new BasicAuthLoginRequestHandler(config)); ch.pipeline().addLast("error", new HttpExceptionHandler()); } }; }
@Override public void initChannel(SocketChannel ch) { CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build(); ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); } pipeline.addLast(new HttpResponseEncoder()); pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new CorsHandler(corsConfig)); pipeline.addLast(new OkResponseHandler()); }
@Override public void initChannel(SocketChannel ch) { CorsConfig corsConfig = CorsConfig.withAnyOrigin().build(); ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); } pipeline.addLast(new HttpResponseEncoder()); pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new CorsHandler(corsConfig)); pipeline.addLast(new OkResponseHandler()); }
public void start() { apiBootstrap = new ServerBootstrap(); ThreadFactory threadFactory = new NamedThreadFactory("kha-webapp"); EventLoopGroup commonGroup = new OioEventLoopGroup(0, threadFactory); try { // the hub will only have a few connections, so OIO is likely to be faster than NIO in this case! apiBootstrap.group(commonGroup, commonGroup) .channel(OioServerSocketChannel.class) .localAddress(port) .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("http-request-decoder", new HttpRequestDecoder()); pipeline.addLast("http-object-aggregator", new HttpObjectAggregator(1048576)); pipeline.addLast("http-response-encoder", new HttpResponseEncoder()); // pipeline.addLast("deflater", new HttpContentDecompressor()); // pipeline.addLast("inflater", new HttpContentCompressor()); pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("cors", new CorsHandler(corsConfig)); pipeline.addLast("file-handler", new HttpStaticFileServerHandler(hubSiteDirectory, true)); } }); ChannelFuture f = apiBootstrap.bind().sync(); LOGGER.info("WebApp available on http://{}:{}", InetAddress.getLocalHost().getCanonicalHostName(), port); f.channel().closeFuture().sync(); } catch (Exception e) { LOGGER.error("Can't start WebApp server", e); } }
@Override public void initChannel(SocketChannel ch) { CorsConfig corsConfig = CorsConfig.withAnyOrigin().build(); ChannelPipeline p = ch.pipeline(); p.addLast(new HttpResponseEncoder()); p.addLast(new HttpRequestDecoder()); p.addLast(new HttpObjectAggregator(65536)); p.addLast(new ChunkedWriteHandler()); p.addLast(new CorsHandler(corsConfig)); p.addLast(new HttpHandler()); }
protected ChannelHandler setupHttpChannel(Configuration config, SslContext sslCtx) { return new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("ssl", new NonSslRedirectHandler(config, sslCtx)); ch.pipeline().addLast("encoder", new HttpResponseEncoder()); ch.pipeline().addLast("decoder", new HttpRequestDecoder()); ch.pipeline().addLast("compressor", new HttpContentCompressor()); ch.pipeline().addLast("decompressor", new HttpContentDecompressor()); ch.pipeline().addLast("aggregator", new HttpObjectAggregator(8192)); ch.pipeline().addLast("chunker", new ChunkedWriteHandler()); final Configuration.Cors corsCfg = config.getHttp().getCors(); final CorsConfig.Builder ccb; if (corsCfg.isAllowAnyOrigin()) { ccb = new CorsConfig.Builder(); } else { ccb = new CorsConfig.Builder(corsCfg.getAllowedOrigins().stream().toArray(String[]::new)); } if (corsCfg.isAllowNullOrigin()) { ccb.allowNullOrigin(); } if (corsCfg.isAllowCredentials()) { ccb.allowCredentials(); } corsCfg.getAllowedMethods().stream().map(HttpMethod::valueOf).forEach(ccb::allowedRequestMethods); corsCfg.getAllowedHeaders().forEach(ccb::allowedRequestHeaders); CorsConfig cors = ccb.build(); LOG.trace("Cors configuration: {}", cors); ch.pipeline().addLast("cors", new CorsHandler(cors)); ch.pipeline().addLast("queryDecoder", new timely.netty.http.HttpRequestDecoder(config)); ch.pipeline().addLast("fileServer", new HttpStaticFileServerHandler()); ch.pipeline().addLast("strict", new StrictTransportHandler(config)); ch.pipeline().addLast("login", new X509LoginRequestHandler(config)); ch.pipeline().addLast("doLogin", new BasicAuthLoginRequestHandler(config)); ch.pipeline().addLast("aggregators", new HttpAggregatorsRequestHandler()); ch.pipeline().addLast("metrics", new HttpMetricsRequestHandler(config)); ch.pipeline().addLast("query", new HttpQueryRequestHandler(dataStore)); ch.pipeline().addLast("search", new HttpSearchLookupRequestHandler(dataStore)); ch.pipeline().addLast("suggest", new HttpSuggestRequestHandler(dataStore)); ch.pipeline().addLast("version", new HttpVersionRequestHandler()); ch.pipeline().addLast("put", new HttpMetricPutHandler(dataStore)); ch.pipeline().addLast("error", new TimelyExceptionHandler()); } }; }