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

Side by Side Diff: components/cronet/android/test/src/org/chromium/net/Http2TestHandler.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 4 years, 12 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 java.util.Locale;
8 import java.util.Map;
9
10 import static io.netty.buffer.Unpooled.copiedBuffer;
11 import static io.netty.buffer.Unpooled.unreleasableBuffer;
12 import static io.netty.handler.codec.http.HttpResponseStatus.OK;
13 import static io.netty.handler.logging.LogLevel.INFO;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.ByteBufUtil;
17 import io.netty.channel.ChannelHandlerContext;
18 import io.netty.handler.codec.http2.DefaultHttp2Headers;
19 import io.netty.handler.codec.http2.Http2ConnectionDecoder;
20 import io.netty.handler.codec.http2.Http2ConnectionEncoder;
21 import io.netty.handler.codec.http2.Http2ConnectionHandler;
22 import io.netty.handler.codec.http2.Http2Exception;
23 import io.netty.handler.codec.http2.Http2Flags;
24 import io.netty.handler.codec.http2.Http2FrameListener;
25 import io.netty.handler.codec.http2.Http2FrameLogger;
26 import io.netty.handler.codec.http2.Http2Headers;
27 import io.netty.handler.codec.http2.Http2Settings;
28 import io.netty.util.CharsetUtil;
29
30 /**
31 * HTTP/2 test handler for Cronet BidirectionalStream tests.
32 */
33 final class Http2TestHandler extends Http2ConnectionHandler implements Http2Fram eListener {
34 private static final Http2FrameLogger sLogger =
35 new Http2FrameLogger(INFO, Http2TestHandler.class);
36 private static final ByteBuf RESPONSE_BYTES =
37 unreleasableBuffer(copiedBuffer("HTTP/2 Test Server", CharsetUtil.UT F_8));
38 private boolean mEchoStream = false;
39
40 static final class Builder extends BuilderBase<Http2TestHandler, Builder> {
41 public Builder() {
42 frameLogger(sLogger);
43 }
44
45 @Override
46 public Http2TestHandler build0(
47 Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder) {
48 Http2TestHandler handler = new Http2TestHandler(decoder, encoder, in itialSettings());
49 frameListener(handler);
50 return handler;
51 }
52 }
53
54 private Http2TestHandler(Http2ConnectionDecoder decoder, Http2ConnectionEnco der encoder,
55 Http2Settings initialSettings) {
56 super(decoder, encoder, initialSettings);
57 }
58
59 @Override
60 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) thro ws Exception {
61 super.exceptionCaught(ctx, cause);
62 cause.printStackTrace();
63 ctx.close();
64 }
65
66 private void sendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf p ayload) {
67 // Send a frame for the response status
68 Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText()) ;
69 encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise( ));
70 encoder().writeData(ctx, streamId, payload, 0, true, ctx.newPromise());
71 ctx.flush();
72 }
73
74 @Override
75 public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding,
76 boolean endOfStream) throws Http2Exception {
77 int processed = data.readableBytes() + padding;
78 if (mEchoStream) {
79 encoder().writeData(ctx, streamId, data.retain(), 0, endOfStream, ct x.newPromise());
80 ctx.flush();
81 } else if (endOfStream) {
82 sendResponse(ctx, streamId, data.retain());
83 }
84 return processed;
85 }
86
87 private void doEchoStream(ChannelHandlerContext ctx, int streamId, Http2Head ers headers,
88 int padding, boolean endOfStream) {
89 mEchoStream = true;
90 // Send a frame for the response status
91 Http2Headers responseHeaders = new DefaultHttp2Headers().status(OK.codeA sText());
92 responseHeaders.add(headers);
93 encoder().writeHeaders(ctx, streamId, responseHeaders, 0, endOfStream, c tx.newPromise());
94 ctx.flush();
95 }
96
97 private void doEchoTrailers(ChannelHandlerContext ctx, int streamId, Http2He aders headers,
98 int padding, boolean endOfStream) {
99 Http2Headers responseHeaders = new DefaultHttp2Headers().status(OK.codeA sText());
100 encoder().writeHeaders(ctx, streamId, responseHeaders, 0, false, ctx.new Promise());
101 encoder().writeData(ctx, streamId, RESPONSE_BYTES.duplicate(), 0, false, ctx.newPromise());
102 Http2Headers responseTrailers =
103 new DefaultHttp2Headers().add("trailer", "value1", "Value2");
104 responseTrailers.add(headers);
105 encoder().writeHeaders(ctx, streamId, responseTrailers, 0, true, ctx.new Promise());
106 ctx.flush();
107 }
108
109 private String getEchoAllHeadersResponse(Http2Headers headers) {
110 StringBuilder response = new StringBuilder();
111 for (Map.Entry<CharSequence, CharSequence> header : headers) {
112 response.append(header.getKey() + ": " + header.getValue() + "\r\n") ;
113 }
114 return response.toString();
115 }
116
117 private String getEchoHeaderResponse(Http2Headers headers) {
118 String[] split_path = headers.path().toString().split("\\?");
119 if (split_path.length <= 1) return "Header name not found.";
120
121 String header_name = split_path[1].toLowerCase(Locale.US);
122 if (headers.get(header_name) == null) return "Header not found:" + heade r_name;
123
124 return headers.get(header_name).toString();
125 }
126
127 @Override
128 public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Head ers headers,
129 int padding, boolean endOfStream) throws Http2Exception {
130 String path = headers.path().toString();
131 if (path.startsWith("/echostream")) {
132 doEchoStream(ctx, streamId, headers, padding, endOfStream);
133 } else if (path.startsWith("/echotrailers")) {
134 doEchoTrailers(ctx, streamId, headers, padding, endOfStream);
135 } else if (endOfStream) {
136 ByteBuf content = ctx.alloc().buffer();
137 try {
138 if (path.startsWith("/echoallheaders")) {
139 ByteBufUtil.writeAscii(content, getEchoAllHeadersResponse(he aders));
140 } else if (path.startsWith("/echoheader")) {
141 ByteBufUtil.writeAscii(content, getEchoHeaderResponse(header s));
142 } else if (path.startsWith("/echomethod")) {
143 ByteBufUtil.writeAscii(content, headers.method());
144 } else {
145 content.writeBytes(RESPONSE_BYTES.duplicate());
146 ByteBufUtil.writeAscii(content, " - via HTTP/2");
147 }
148 } catch (Exception e) {
149 ByteBufUtil.writeAscii(content, "Exception: " + e.toString());
150 }
151
152 sendResponse(ctx, streamId, content);
153 }
154 }
155
156 @Override
157 public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Head ers headers,
158 int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream)
159 throws Http2Exception {
160 onHeadersRead(ctx, streamId, headers, padding, endOfStream);
161 }
162
163 @Override
164 public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int stre amDependency,
165 short weight, boolean exclusive) throws Http2Exception {}
166
167 @Override
168 public void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long er rorCode)
169 throws Http2Exception {}
170
171 @Override
172 public void onSettingsAckRead(ChannelHandlerContext ctx) throws Http2Excepti on {}
173
174 @Override
175 public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings )
176 throws Http2Exception {}
177
178 @Override
179 public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2 Exception {}
180
181 @Override
182 public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Ht tp2Exception {}
183
184 @Override
185 public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int p romisedStreamId,
186 Http2Headers headers, int padding) throws Http2Exception {}
187
188 @Override
189 public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long e rrorCode,
190 ByteBuf debugData) throws Http2Exception {}
191
192 @Override
193 public void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement)
194 throws Http2Exception {}
195
196 @Override
197 public void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int st reamId,
198 Http2Flags flags, ByteBuf payload) throws Http2Exception {}
199 }
OLDNEW
« no previous file with comments | « components/cronet.gypi ('k') | components/cronet/android/test/src/org/chromium/net/Http2TestServer.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698