1 /* 2 * Copyright 2012 The Netty Project 3 * 4 * The Netty Project licenses this file to you under the Apache License, 5 * version 2.0 (the "License"); you may not use this file except in compliance 6 * with the License. You may obtain a copy of the License at: 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations 14 * under the License. 15 */ 16 package io.netty.example.http.cors; 17 18 import io.netty.bootstrap.ServerBootstrap; 19 import io.netty.channel.EventLoopGroup; 20 import io.netty.channel.MultiThreadIoEventLoopGroup; 21 import io.netty.channel.nio.NioIoHandler; 22 import io.netty.channel.socket.nio.NioServerSocketChannel; 23 import io.netty.example.util.ServerUtil; 24 import io.netty.handler.logging.LogLevel; 25 import io.netty.handler.logging.LoggingHandler; 26 import io.netty.handler.ssl.SslContext; 27 28 /** 29 * This example server aims to demonstrate 30 * <a href="https://www.w3.org/TR/cors/">Cross Origin Resource Sharing</a> (CORS) in Netty. 31 * It does not have a client like most of the other examples, but instead has 32 * a html page that is loaded to try out CORS support in a web browser. 33 * <p> 34 * 35 * CORS is configured in {@link HttpCorsServerInitializer} and by updating the config you can 36 * try out various combinations, like using a specific origin instead of a 37 * wildcard origin ('*'). 38 * <p> 39 * 40 * The file {@code src/main/resources/cors/cors.html} contains a very basic example client 41 * which can be used to try out different configurations. For example, you can add 42 * custom headers to force a CORS preflight request to make the request fail. Then 43 * to enable a successful request, configure the CorsHandler to allow that/those 44 * request headers. 45 * 46 * <h2>Testing CORS</h2> 47 * You can either load the file {@code src/main/resources/cors/cors.html} using a web server 48 * or load it from the file system using a web browser. 49 * 50 * <h3>Using a web server</h3> 51 * To test CORS support you can serve the file {@code src/main/resources/cors/cors.html} 52 * using a web server. You can then add a new host name to your systems hosts file, for 53 * example if you are on Linux you may update /etc/hosts to add an additional name 54 * for you local system: 55 * <pre> 56 * 127.0.0.1 localhost domain1.com 57 * </pre> 58 * Now, you should be able to access {@code http://domain1.com/cors.html} depending on how you 59 * have configured you local web server the exact url may differ. 60 * 61 * <h3>Using a web browser</h3> 62 * Open the file {@code src/main/resources/cors/cors.html} in a web browser. You should see 63 * loaded page and in the text area the following message: 64 * <pre> 65 * 'CORS is not working' 66 * </pre> 67 * 68 * If you inspect the headers being sent using your browser you'll see that the 'Origin' 69 * request header is {@code 'null'}. This is expected and happens when you load a file from the 70 * local file system. Netty can handle this by configuring the CorsHandler which is done 71 * in the {@link HttpCorsServerInitializer}. 72 * 73 */ 74 public final class HttpCorsServer { 75 76 static final boolean SSL = System.getProperty("ssl") != null; 77 static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080")); 78 79 public static void main(String[] args) throws Exception { 80 // Configure SSL. 81 final SslContext sslCtx = ServerUtil.buildSslContext(); 82 83 EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory()); 84 EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory()); 85 try { 86 ServerBootstrap b = new ServerBootstrap(); 87 b.group(bossGroup, workerGroup) 88 .channel(NioServerSocketChannel.class) 89 .handler(new LoggingHandler(LogLevel.INFO)) 90 .childHandler(new HttpCorsServerInitializer(sslCtx)); 91 92 b.bind(PORT).sync().channel().closeFuture().sync(); 93 } finally { 94 bossGroup.shutdownGracefully(); 95 workerGroup.shutdownGracefully(); 96 } 97 } 98 }