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

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

Issue 1412243012: Initial implementation of CronetBidirectionalStream. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Destroy the native adapter instead of cancel if can't post task to executor. Created 4 years, 11 months 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(
pauljensen 2016/01/19 16:03:41 I don't think the extra parenthesis around "new Th
mef 2016/01/20 15:37:41 Done.
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 boolean shutdownHttp2TestServer() throws Exception {
58 return true;
59 }
60
61 public static String getBaseUrl() {
62 return "https://127.0.0.1:" + PORT + '/';
63 }
64
65 public static String getServerHost() {
66 return "127.0.0.1";
67 }
68
69 public static int getServerPort() {
70 return PORT;
71 }
72
73 static String getEchoAllHeadersUrl() {
74 return getBaseUrl() + "echoallheaders";
75 }
76
77 static String getEchoHeaderUrl(String headerName) {
78 return getBaseUrl() + "echoheader?" + headerName;
79 }
80
81 static String getEchoMethodUrl() {
82 return getBaseUrl() + "echomethod";
pauljensen 2016/01/19 16:03:41 these strings (e.g. "echomethod") seem duplicated
mef 2016/01/20 15:37:41 They sure can. I think it would make sense to unif
pauljensen 2016/01/22 03:55:31 The more unification the better :) It may be hard
83 }
84
85 static String getEchoStreamUrl() {
86 return getBaseUrl() + "echostream";
87 }
88
89 static String getEchoTrailersUrl() {
90 return getBaseUrl() + "echotrailers";
91 }
92
93 private static void onServerStarted() {
94 Log.i(TAG, "Http2 server started.");
pauljensen 2016/01/19 16:03:41 any reason onServerStarted is in a seperate functi
mef 2016/01/20 15:37:41 Done.
95 sBlock.open();
96 }
97
98 static class Http2TestServerRunnable implements Runnable {
99 File mCertFile;
pauljensen 2016/01/19 16:03:41 private final?
mef 2016/01/20 15:37:41 Done.
100 File mKeyFile;
pauljensen 2016/01/19 16:03:41 ditto
mef 2016/01/20 15:37:41 Done.
101
102 Http2TestServerRunnable(File certFile, File keyFile) {
103 mCertFile = certFile;
104 mKeyFile = keyFile;
105 }
106
107 public void run() {
108 Log.i(TAG, "Hello from Http2TestServerRunnable!");
109 try {
110 runHttp2TestServer(mCertFile, mKeyFile);
111 } catch (Exception e) {
112 Log.e(TAG, e.toString());
113 }
114 }
115 }
116
117 private static void runHttp2TestServer(File certFile, File keyFile) throws E xception {
118 ApplicationProtocolConfig applicationProtocolConfig =
119 new ApplicationProtocolConfig(Protocol.ALPN, SelectorFailureBeha vior.NO_ADVERTISE,
120 SelectedListenerFailureBehavior.ACCEPT, ApplicationProto colNames.HTTP_2);
121
122 final SslContext sslCtx =
123 new OpenSslServerContext(certFile, keyFile, null, null, Http2Sec urityUtil.CIPHERS,
124 SupportedCipherSuiteFilter.INSTANCE, applicationProtocol Config, 0, 0);
125
126 // Configure the server.
127 EventLoopGroup group = new NioEventLoopGroup();
128 try {
129 ServerBootstrap b = new ServerBootstrap();
130 b.option(ChannelOption.SO_BACKLOG, 1024);
131 b.group(group)
132 .channel(NioServerSocketChannel.class)
133 .handler(new LoggingHandler(LogLevel.INFO))
134 .childHandler(new Http2ServerInitializer(sslCtx));
135
136 Channel ch = b.bind(PORT).sync().channel();
137 Log.i(TAG, "Netty HTTP/2 server started on " + getBaseUrl());
138 onServerStarted();
139 ch.closeFuture().sync();
140 } finally {
141 group.shutdownGracefully();
142 }
143 Log.i(TAG, "Stopped Http2TestServerRunnable!");
144 }
145
146 /**
147 * Sets up the Netty pipeline for the test server.
148 */
149 static class Http2ServerInitializer extends ChannelInitializer<SocketChannel > {
150 private final SslContext mSslCtx;
151
152 public Http2ServerInitializer(SslContext sslCtx) {
153 this.mSslCtx = sslCtx;
154 }
155
156 @Override
157 public void initChannel(SocketChannel ch) {
158 ch.pipeline().addLast(mSslCtx.newHandler(ch.alloc()), new Http2Negot iationHandler());
159 }
160 }
161
162 static class Http2NegotiationHandler extends ApplicationProtocolNegotiationH andler {
163 protected Http2NegotiationHandler() {
164 super(ApplicationProtocolNames.HTTP_1_1);
165 }
166
167 @Override
168 protected void configurePipeline(ChannelHandlerContext ctx, String proto col)
169 throws Exception {
170 if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
171 ctx.pipeline().addLast(new Http2TestHandler.Builder().build());
172 return;
173 }
174
175 throw new IllegalStateException("unknown protocol: " + protocol);
176 }
177 }
178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698