@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 addHttpServerCodec(@NotNull ChannelPipeline pipeline) { pipeline.addLast("httpRequestEncoder", new HttpResponseEncoder()); // https://jetbrains.zendesk.com/agent/tickets/68315 pipeline.addLast("httpRequestDecoder", new HttpRequestDecoder(16 * 1024, 16 * 1024, 8192)); pipeline.addLast("httpObjectAggregator", new HttpObjectAggregator(MAX_CONTENT_LENGTH)); // could be added earlier if HTTPS if (pipeline.get(ChunkedWriteHandler.class) == null) { pipeline.addLast("chunkedWriteHandler", new ChunkedWriteHandler()); } pipeline.addLast("corsHandler", new CorsHandlerDoNotUseOwnLogger(CorsConfig .withAnyOrigin() .allowCredentials() .allowNullOrigin() .allowedRequestMethods(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, HttpMethod.PATCH) .allowedRequestHeaders("origin", "accept", "authorization", "content-type") .build())); }
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 WebAppServer(int port) { this.port = port; String hubHomeDirectory = System.getProperty("app.home"); if (hubHomeDirectory.isEmpty()) { this.hubSiteDirectory = "src/main/webapp"; } else { this.hubSiteDirectory = hubHomeDirectory + "/site"; } corsConfig = CorsConfig.withAnyOrigin() .allowCredentials() // required for custom headers .allowedRequestMethods( HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.OPTIONS) .maxAge(1 * 60 * 60) // 1 hour .allowedRequestHeaders( HttpHeaders.Names.CONTENT_TYPE, RESTCodec.HEADER_REQUEST_ID, // header for tracking request ID HttpHeaders.Names.AUTHORIZATION) // header for OAuth2 authentication .exposeHeaders(RESTCodec.HEADER_REQUEST_ID) .build(); LOGGER.debug("Website content served from '{}'", hubSiteDirectory); }
@Inject public ApiProtocolSwitcher(ObjectMapper objectMapper) { super(); this.objectMapper = objectMapper; corsConfig = CorsConfig.withAnyOrigin() .allowCredentials() // required for custom headers .allowedRequestMethods( HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.OPTIONS) .maxAge(1 * 60 * 60) // 1 hour .allowedRequestHeaders( HttpHeaderNames.CONTENT_TYPE.toString(), RESTCodec.HEADER_REQUEST_ID, // header for tracking request ID HttpHeaderNames.AUTHORIZATION.toString()) // header for OAuth2 authentication .exposeHeaders(RESTCodec.HEADER_REQUEST_ID) .build(); }
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()); }
LaputaServerInitializer(SslContext sslContext, CorsConfig corsConfig, String webSocketPath, LaputaRequestProcessor requestProcessor) { this.sslContext = sslContext; this.corsConfig = corsConfig; this.webSocketPath = webSocketPath; this.requestProcessor = requestProcessor; }
private CorsConfig getCorsConfig(Config config) { if (!config.hasPath(CFG_SERVER_CORS_ALLOWS_ORIGINS)) { return null; } List<String> origins = config.getStringList(CFG_SERVER_CORS_ALLOWS_ORIGINS); if (null == origins || origins.isEmpty()) { return null; } CorsConfigBuilder builder = null; for (String origin : origins) { if (SIGN_STAR.equals(origin)) { builder = CorsConfigBuilder.forAnyOrigin(); break; } } if (null == builder) { builder = CorsConfigBuilder.forOrigins(origins.toArray(new String[origins.size()])); } if (config.hasPath(CFG_SERVER_CORS_ALLOWS_CREDENTIALS)) { boolean allowCredentials = config.getBoolean(CFG_SERVER_CORS_ALLOWS_CREDENTIALS); if (allowCredentials) { builder.allowCredentials(); } } return builder.build(); }
@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()); } }; }
public CorsHandlerDoNotUseOwnLogger(@NotNull CorsConfig config) { super(config); }
public CorsConfig cors() { return cors; }
public CorsHandlerDoNotUseOwnLogger(@Nonnull CorsConfig config) { super(config); }