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 public static String getEchoStreamUrl() { | |
xunjieli
2016/01/27 16:16:38
nit: need docmentation.
mef
2016/01/27 19:06:47
Done.
| |
83 return getServerUrl() + Http2TestHandler.ECHO_STREAM_PATH; | |
84 } | |
85 | |
86 public static String getEchoTrailersUrl() { | |
xunjieli
2016/01/27 16:16:38
nit: need documentation to explain what this does.
mef
2016/01/27 19:06:47
Done.
| |
87 return getServerUrl() + Http2TestHandler.ECHO_TRAILERS_PATH; | |
88 } | |
89 | |
90 public static boolean startHttp2TestServer( | |
91 Context context, String certFileName, String keyFileName) throws Exc eption { | |
92 new Thread( | |
93 new Http2TestServerRunnable(new File(CertTestUtil.CERTS_DIRECTOR Y + certFileName), | |
94 new File(CertTestUtil.CERTS_DIRECTORY + keyFileName))) | |
95 .start(); | |
96 sBlock.block(); | |
97 return true; | |
98 } | |
99 | |
100 private Http2TestServer() {} | |
101 | |
102 private static class Http2TestServerRunnable implements Runnable { | |
103 private final SslContext mSslCtx; | |
104 | |
105 Http2TestServerRunnable(File certFile, File keyFile) throws Exception { | |
106 ApplicationProtocolConfig applicationProtocolConfig = new Applicatio nProtocolConfig( | |
107 Protocol.ALPN, SelectorFailureBehavior.NO_ADVERTISE, | |
108 SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolN ames.HTTP_2); | |
109 | |
110 mSslCtx = new OpenSslServerContext(certFile, keyFile, null, null, | |
111 Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTAN CE, | |
112 applicationProtocolConfig, 0, 0); | |
113 } | |
114 | |
115 public void run() { | |
116 try { | |
117 // Configure the server. | |
118 EventLoopGroup group = new NioEventLoopGroup(); | |
119 try { | |
120 ServerBootstrap b = new ServerBootstrap(); | |
121 b.option(ChannelOption.SO_BACKLOG, 1024); | |
122 b.group(group) | |
123 .channel(NioServerSocketChannel.class) | |
124 .handler(new LoggingHandler(LogLevel.INFO)) | |
125 .childHandler(new Http2ServerInitializer(mSslCtx)); | |
126 | |
127 sServerChannel = b.bind(PORT).sync().channel(); | |
128 Log.i(TAG, "Netty HTTP/2 server started on " + getServerUrl( )); | |
129 sBlock.open(); | |
130 sServerChannel.closeFuture().sync(); | |
131 } finally { | |
132 group.shutdownGracefully(); | |
133 } | |
134 Log.i(TAG, "Stopped Http2TestServerRunnable!"); | |
135 } catch (Exception e) { | |
136 Log.e(TAG, e.toString()); | |
137 } | |
138 } | |
139 } | |
140 | |
141 /** | |
142 * Sets up the Netty pipeline for the test server. | |
143 */ | |
144 private static class Http2ServerInitializer extends ChannelInitializer<Socke tChannel> { | |
145 private final SslContext mSslCtx; | |
146 | |
147 public Http2ServerInitializer(SslContext sslCtx) { | |
148 this.mSslCtx = sslCtx; | |
149 } | |
150 | |
151 @Override | |
152 public void initChannel(SocketChannel ch) { | |
153 ch.pipeline().addLast(mSslCtx.newHandler(ch.alloc()), new Http2Negot iationHandler()); | |
154 } | |
155 } | |
156 | |
157 private static class Http2NegotiationHandler extends ApplicationProtocolNego tiationHandler { | |
158 protected Http2NegotiationHandler() { | |
159 super(ApplicationProtocolNames.HTTP_1_1); | |
160 } | |
161 | |
162 @Override | |
163 protected void configurePipeline(ChannelHandlerContext ctx, String proto col) | |
164 throws Exception { | |
165 if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { | |
166 ctx.pipeline().addLast(new Http2TestHandler.Builder().build()); | |
167 return; | |
168 } | |
169 | |
170 throw new IllegalStateException("unknown protocol: " + protocol); | |
171 } | |
172 } | |
173 } | |
OLD | NEW |