Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: components/cronet/android/test/src/org/chromium/net/Http2TestServer.java

Issue 1529643002: [Cronet] Use prebuilt Netty Http/2 server for Cronet BidirectionalStream testing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bidi_impl_helen8
Patch Set: Sync Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 final String TAG = "Http2TestServer";
43
44 // Host-based server.
45 static final int PORT = 8443;
46
47 public static boolean startHttp2TestServer(
48 Context context, String certFileName, String keyFileName) throws Exc eption {
49 (new Thread(
50 new Http2TestServerRunnable(new File(CertTestUtil.CERTS_DIRECTO RY + certFileName),
51 new File(CertTestUtil.CERTS_DIRECTORY + keyFileName))))
52 .start();
53 sBlock.block();
54 return true;
55 }
56
57 public static void shutdownHttp2TestServer() throws Exception {}
58
59 public static String getBaseUrl() {
60 return "https://127.0.0.1:" + PORT + '/';
61 }
62
63 public static String getServerHost() {
64 return "127.0.0.1";
65 }
66
67 public static int getServerPort() {
68 return PORT;
69 }
70
71 static String getEchoAllHeadersUrl() {
72 return getBaseUrl() + "echoallheaders";
73 }
74
75 static String getEchoHeaderUrl(String headerName) {
76 return getBaseUrl() + "echoheader?" + headerName;
77 }
78
79 static String getEchoMethodUrl() {
80 return getBaseUrl() + "echomethod";
81 }
82
83 static String getEchoStreamUrl() {
84 return getBaseUrl() + "echostream";
85 }
86
87 static String getEchoTrailersUrl() {
88 return getBaseUrl() + "echotrailers";
89 }
90
91 private static void onServerStarted() {
92 Log.i(TAG, "Http2 server started.");
93 sBlock.open();
94 }
95
96 static class Http2TestServerRunnable implements Runnable {
97 File mCertFile;
98 File mKeyFile;
99
100 Http2TestServerRunnable(File certFile, File keyFile) {
101 mCertFile = certFile;
102 mKeyFile = keyFile;
103 }
104
105 public void run() {
106 Log.i(TAG, "Hello from Http2TestServerRunnable!");
107 try {
108 runHttp2TestServer(mCertFile, mKeyFile);
109 } catch (Exception e) {
110 Log.e(TAG, e.toString());
111 }
112 }
113 }
114
115 private static void runHttp2TestServer(File certFile, File keyFile) throws E xception {
116 ApplicationProtocolConfig applicationProtocolConfig =
117 new ApplicationProtocolConfig(Protocol.ALPN, SelectorFailureBeha vior.NO_ADVERTISE,
118 SelectedListenerFailureBehavior.ACCEPT, ApplicationProto colNames.HTTP_2);
119
120 final SslContext sslCtx =
121 new OpenSslServerContext(certFile, keyFile, null, null, Http2Sec urityUtil.CIPHERS,
122 SupportedCipherSuiteFilter.INSTANCE, applicationProtocol Config, 0, 0);
123
124 // Configure the server.
125 EventLoopGroup group = new NioEventLoopGroup();
126 try {
127 ServerBootstrap b = new ServerBootstrap();
128 b.option(ChannelOption.SO_BACKLOG, 1024);
129 b.group(group)
130 .channel(NioServerSocketChannel.class)
131 .handler(new LoggingHandler(LogLevel.INFO))
132 .childHandler(new Http2ServerInitializer(sslCtx));
133
134 Channel ch = b.bind(PORT).sync().channel();
135 Log.i(TAG, "Netty HTTP/2 server started on " + getBaseUrl());
136 onServerStarted();
137 ch.closeFuture().sync();
138 } finally {
139 group.shutdownGracefully();
140 }
141 Log.i(TAG, "Stopped Http2TestServerRunnable!");
142 }
143
144 /**
145 * Sets up the Netty pipeline for the test server.
146 */
147 static class Http2ServerInitializer extends ChannelInitializer<SocketChannel > {
148 private final SslContext mSslCtx;
149
150 public Http2ServerInitializer(SslContext sslCtx) {
151 this.mSslCtx = sslCtx;
152 }
153
154 @Override
155 public void initChannel(SocketChannel ch) {
156 ch.pipeline().addLast(mSslCtx.newHandler(ch.alloc()), new Http2Negot iationHandler());
157 }
158 }
159
160 static class Http2NegotiationHandler extends ApplicationProtocolNegotiationH andler {
161 protected Http2NegotiationHandler() {
162 super(ApplicationProtocolNames.HTTP_1_1);
163 }
164
165 @Override
166 protected void configurePipeline(ChannelHandlerContext ctx, String proto col)
167 throws Exception {
168 if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
169 ctx.pipeline().addLast(new Http2TestHandler.Builder().build());
170 return;
171 }
172
173 throw new IllegalStateException("unknown protocol: " + protocol);
174 }
175 }
176 }
OLDNEW
« no previous file with comments | « components/cronet/android/test/src/org/chromium/net/Http2TestHandler.java ('k') | third_party/netty/README.chromium » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698