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

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: Address Helen's comments. 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 final class Http2TestServer {
xunjieli 2016/01/22 16:45:21 Suggest adding public modifiers (see a previous co
mef 2016/01/25 18:11:25 Done.
41 private static final ConditionVariable sBlock = new ConditionVariable();
42 private static final String TAG = "Http2TestServer";
43
44 // Server port.
45 private static final int PORT = 8443;
46
47 static boolean startHttp2TestServer(Context context, String certFileName, St ring keyFileName)
48 throws Exception {
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 static boolean shutdownHttp2TestServer() throws Exception {
58 return true;
59 }
60
61 static String getServerUrl() {
62 return "https://127.0.0.1:" + PORT + '/';
63 }
64
65 static String getServerHost() {
66 return "127.0.0.1";
67 }
68
69 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() {
90 return getServerUrl() + "echotrailers";
91 }
92
93 private static class Http2TestServerRunnable implements Runnable {
94 private final SslContext mSslCtx;
95
96 Http2TestServerRunnable(File certFile, File keyFile) throws Exception {
97 ApplicationProtocolConfig applicationProtocolConfig = new Applicatio nProtocolConfig(
98 Protocol.ALPN, SelectorFailureBehavior.NO_ADVERTISE,
99 SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolN ames.HTTP_2);
100
101 mSslCtx = new OpenSslServerContext(certFile, keyFile, null, null,
102 Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTAN CE,
103 applicationProtocolConfig, 0, 0);
104 }
105
106 public void run() {
107 Log.i(TAG, "Hello from Http2TestServerRunnable!");
108 try {
109 // Configure the server.
110 EventLoopGroup group = new NioEventLoopGroup();
111 try {
112 ServerBootstrap b = new ServerBootstrap();
113 b.option(ChannelOption.SO_BACKLOG, 1024);
114 b.group(group)
115 .channel(NioServerSocketChannel.class)
116 .handler(new LoggingHandler(LogLevel.INFO))
117 .childHandler(new Http2ServerInitializer(mSslCtx));
118
119 Channel ch = b.bind(PORT).sync().channel();
120 Log.i(TAG, "Netty HTTP/2 server started on " + getServerUrl( ));
121 sBlock.open();
122 ch.closeFuture().sync();
123 } finally {
124 group.shutdownGracefully();
125 }
126 Log.i(TAG, "Stopped Http2TestServerRunnable!");
127 } catch (Exception e) {
128 Log.e(TAG, e.toString());
129 }
130 }
131 }
132
133 /**
134 * Sets up the Netty pipeline for the test server.
135 */
136 private static class Http2ServerInitializer extends ChannelInitializer<Socke tChannel> {
137 private final SslContext mSslCtx;
138
139 public Http2ServerInitializer(SslContext sslCtx) {
140 this.mSslCtx = sslCtx;
141 }
142
143 @Override
144 public void initChannel(SocketChannel ch) {
145 ch.pipeline().addLast(mSslCtx.newHandler(ch.alloc()), new Http2Negot iationHandler());
146 }
147 }
148
149 private static class Http2NegotiationHandler extends ApplicationProtocolNego tiationHandler {
150 protected Http2NegotiationHandler() {
151 super(ApplicationProtocolNames.HTTP_1_1);
152 }
153
154 @Override
155 protected void configurePipeline(ChannelHandlerContext ctx, String proto col)
156 throws Exception {
157 if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
158 ctx.pipeline().addLast(new Http2TestHandler.Builder().build());
159 return;
160 }
161
162 throw new IllegalStateException("unknown protocol: " + protocol);
163 }
164 }
165
166 private Http2TestServer() {}
167 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698