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

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: Self review. 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 {
xunjieli 2016/01/21 19:13:08 maybe add a private constructor?
mef 2016/01/22 14:33:44 Done.
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;
xunjieli 2016/01/21 19:13:08 add private?
mef 2016/01/22 14:33:44 Done.
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_DIRECTOR Y + 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 getServerUrl() {
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 getServerUrl() + "echoallheaders";
75 }
76
77 static String getEchoHeaderUrl(String headerName) {
78 return getServerUrl() + "echoheader?" + headerName;
79 }
80
81 static String getEchoMethodUrl() {
82 return getServerUrl() + "echomethod";
83 }
84
85 static String getEchoStreamUrl() {
86 return getServerUrl() + "echostream";
87 }
88
89 static String getEchoTrailersUrl() {
xunjieli 2016/01/21 19:13:08 Why are these methods some public and some package
mef 2016/01/22 14:33:44 Done.
90 return getServerUrl() + "echotrailers";
91 }
92
93 private static class Http2TestServerRunnable implements Runnable {
94 private final File mCertFile;
95 private final File mKeyFile;
96
97 Http2TestServerRunnable(File certFile, File keyFile) {
98 mCertFile = certFile;
99 mKeyFile = keyFile;
100 }
101
102 public void run() {
103 Log.i(TAG, "Hello from Http2TestServerRunnable!");
104 try {
105 runHttp2TestServer(mCertFile, mKeyFile);
106 } catch (Exception e) {
107 Log.e(TAG, e.toString());
108 }
109 }
110 }
111
112 private static void runHttp2TestServer(File certFile, File keyFile) throws E xception {
xunjieli 2016/01/21 19:13:08 maybe move this method to Http2TestServerRunnable?
mef 2016/01/22 14:33:44 Done.
113 ApplicationProtocolConfig applicationProtocolConfig =
114 new ApplicationProtocolConfig(Protocol.ALPN, SelectorFailureBeha vior.NO_ADVERTISE,
115 SelectedListenerFailureBehavior.ACCEPT, ApplicationProto colNames.HTTP_2);
116
117 final SslContext sslCtx =
118 new OpenSslServerContext(certFile, keyFile, null, null, Http2Sec urityUtil.CIPHERS,
119 SupportedCipherSuiteFilter.INSTANCE, applicationProtocol Config, 0, 0);
120
121 // Configure the server.
122 EventLoopGroup group = new NioEventLoopGroup();
123 try {
124 ServerBootstrap b = new ServerBootstrap();
125 b.option(ChannelOption.SO_BACKLOG, 1024);
126 b.group(group)
127 .channel(NioServerSocketChannel.class)
128 .handler(new LoggingHandler(LogLevel.INFO))
129 .childHandler(new Http2ServerInitializer(sslCtx));
130
131 Channel ch = b.bind(PORT).sync().channel();
132 Log.i(TAG, "Netty HTTP/2 server started on " + getServerUrl());
133 sBlock.open();
134 ch.closeFuture().sync();
135 } finally {
136 group.shutdownGracefully();
137 }
138 Log.i(TAG, "Stopped Http2TestServerRunnable!");
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698