OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.net; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.os.ConditionVariable; |
| 9 |
| 10 import org.chromium.base.Log; |
| 11 import org.chromium.net.test.util.CertTestUtil; |
| 12 |
| 13 import java.io.File; |
| 14 |
| 15 import io.netty.bootstrap.ServerBootstrap; |
| 16 import io.netty.channel.Channel; |
| 17 import io.netty.channel.ChannelHandlerContext; |
| 18 import io.netty.channel.ChannelInitializer; |
| 19 import io.netty.channel.ChannelOption; |
| 20 import io.netty.channel.EventLoopGroup; |
| 21 import io.netty.channel.nio.NioEventLoopGroup; |
| 22 import io.netty.channel.socket.SocketChannel; |
| 23 import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 24 import io.netty.handler.codec.http2.Http2SecurityUtil; |
| 25 import io.netty.handler.logging.LogLevel; |
| 26 import io.netty.handler.logging.LoggingHandler; |
| 27 import io.netty.handler.ssl.ApplicationProtocolConfig; |
| 28 import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol; |
| 29 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBeh
avior; |
| 30 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior; |
| 31 import io.netty.handler.ssl.ApplicationProtocolNames; |
| 32 import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler; |
| 33 import io.netty.handler.ssl.OpenSslServerContext; |
| 34 import io.netty.handler.ssl.SslContext; |
| 35 import io.netty.handler.ssl.SupportedCipherSuiteFilter; |
| 36 |
| 37 /** |
| 38 * Wrapper class to start a HTTP/2 test server. |
| 39 */ |
| 40 public final class Http2TestServer { |
| 41 private static final ConditionVariable sBlock = new ConditionVariable(); |
| 42 private static Channel sServerChannel; |
| 43 private static final String TAG = "Http2TestServer"; |
| 44 |
| 45 private static final String HOST = "127.0.0.1"; |
| 46 // Server port. |
| 47 private static final int PORT = 8443; |
| 48 |
| 49 public static boolean shutdownHttp2TestServer() throws Exception { |
| 50 if (sServerChannel != null) { |
| 51 sServerChannel.close(); |
| 52 sServerChannel = null; |
| 53 return true; |
| 54 } |
| 55 return false; |
| 56 } |
| 57 |
| 58 public static String getServerHost() { |
| 59 return HOST; |
| 60 } |
| 61 |
| 62 public static int getServerPort() { |
| 63 return PORT; |
| 64 } |
| 65 |
| 66 public static String getServerUrl() { |
| 67 return "https://" + HOST + ":" + PORT; |
| 68 } |
| 69 |
| 70 public static String getEchoAllHeadersUrl() { |
| 71 return getServerUrl() + Http2TestHandler.ECHO_ALL_HEADERS_PATH; |
| 72 } |
| 73 |
| 74 public static String getEchoHeaderUrl(String headerName) { |
| 75 return getServerUrl() + Http2TestHandler.ECHO_HEADER_PATH + "?" + header
Name; |
| 76 } |
| 77 |
| 78 public static String getEchoMethodUrl() { |
| 79 return getServerUrl() + Http2TestHandler.ECHO_METHOD_PATH; |
| 80 } |
| 81 |
| 82 /** |
| 83 * @return url of the server resource which will echo every received stream
data frame. |
| 84 */ |
| 85 public static String getEchoStreamUrl() { |
| 86 return getServerUrl() + Http2TestHandler.ECHO_STREAM_PATH; |
| 87 } |
| 88 |
| 89 /** |
| 90 * @return url of the server resource which will echo request headers as res
ponse trailers. |
| 91 */ |
| 92 public static String getEchoTrailersUrl() { |
| 93 return getServerUrl() + Http2TestHandler.ECHO_TRAILERS_PATH; |
| 94 } |
| 95 |
| 96 public static boolean startHttp2TestServer( |
| 97 Context context, String certFileName, String keyFileName) throws Exc
eption { |
| 98 new Thread( |
| 99 new Http2TestServerRunnable(new File(CertTestUtil.CERTS_DIRECTOR
Y + certFileName), |
| 100 new File(CertTestUtil.CERTS_DIRECTORY + keyFileName))) |
| 101 .start(); |
| 102 sBlock.block(); |
| 103 return true; |
| 104 } |
| 105 |
| 106 private Http2TestServer() {} |
| 107 |
| 108 private static class Http2TestServerRunnable implements Runnable { |
| 109 private final SslContext mSslCtx; |
| 110 |
| 111 Http2TestServerRunnable(File certFile, File keyFile) throws Exception { |
| 112 ApplicationProtocolConfig applicationProtocolConfig = new Applicatio
nProtocolConfig( |
| 113 Protocol.ALPN, SelectorFailureBehavior.NO_ADVERTISE, |
| 114 SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolN
ames.HTTP_2); |
| 115 |
| 116 mSslCtx = new OpenSslServerContext(certFile, keyFile, null, null, |
| 117 Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTAN
CE, |
| 118 applicationProtocolConfig, 0, 0); |
| 119 } |
| 120 |
| 121 public void run() { |
| 122 try { |
| 123 // Configure the server. |
| 124 EventLoopGroup group = new NioEventLoopGroup(); |
| 125 try { |
| 126 ServerBootstrap b = new ServerBootstrap(); |
| 127 b.option(ChannelOption.SO_BACKLOG, 1024); |
| 128 b.group(group) |
| 129 .channel(NioServerSocketChannel.class) |
| 130 .handler(new LoggingHandler(LogLevel.INFO)) |
| 131 .childHandler(new Http2ServerInitializer(mSslCtx)); |
| 132 |
| 133 sServerChannel = b.bind(PORT).sync().channel(); |
| 134 Log.i(TAG, "Netty HTTP/2 server started on " + getServerUrl(
)); |
| 135 sBlock.open(); |
| 136 sServerChannel.closeFuture().sync(); |
| 137 } finally { |
| 138 group.shutdownGracefully(); |
| 139 } |
| 140 Log.i(TAG, "Stopped Http2TestServerRunnable!"); |
| 141 } catch (Exception e) { |
| 142 Log.e(TAG, e.toString()); |
| 143 } |
| 144 } |
| 145 } |
| 146 |
| 147 /** |
| 148 * Sets up the Netty pipeline for the test server. |
| 149 */ |
| 150 private static class Http2ServerInitializer extends ChannelInitializer<Socke
tChannel> { |
| 151 private final SslContext mSslCtx; |
| 152 |
| 153 public Http2ServerInitializer(SslContext sslCtx) { |
| 154 this.mSslCtx = sslCtx; |
| 155 } |
| 156 |
| 157 @Override |
| 158 public void initChannel(SocketChannel ch) { |
| 159 ch.pipeline().addLast(mSslCtx.newHandler(ch.alloc()), new Http2Negot
iationHandler()); |
| 160 } |
| 161 } |
| 162 |
| 163 private static class Http2NegotiationHandler extends ApplicationProtocolNego
tiationHandler { |
| 164 protected Http2NegotiationHandler() { |
| 165 super(ApplicationProtocolNames.HTTP_1_1); |
| 166 } |
| 167 |
| 168 @Override |
| 169 protected void configurePipeline(ChannelHandlerContext ctx, String proto
col) |
| 170 throws Exception { |
| 171 if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { |
| 172 ctx.pipeline().addLast(new Http2TestHandler.Builder().build()); |
| 173 return; |
| 174 } |
| 175 |
| 176 throw new IllegalStateException("unknown protocol: " + protocol); |
| 177 } |
| 178 } |
| 179 } |
OLD | NEW |