Chromium Code Reviews| Index: components/cronet/android/test/src/org/chromium/net/Http2TestHandler.java |
| diff --git a/components/cronet/android/test/src/org/chromium/net/Http2TestHandler.java b/components/cronet/android/test/src/org/chromium/net/Http2TestHandler.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cdb5d2cb3fc9de2dac58bbdcd10da04d33c00340 |
| --- /dev/null |
| +++ b/components/cronet/android/test/src/org/chromium/net/Http2TestHandler.java |
| @@ -0,0 +1,217 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
|
pauljensen
2016/01/19 16:03:41
I see you've added 400+ lines since the patch-set
mef
2016/01/20 15:37:41
Acknowledged. I've added these into the CL to make
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net; |
| + |
| +import java.util.Locale; |
| +import java.util.Map; |
| + |
| +import static io.netty.buffer.Unpooled.copiedBuffer; |
| +import static io.netty.buffer.Unpooled.unreleasableBuffer; |
| +import static io.netty.handler.codec.http.HttpResponseStatus.OK; |
| +import static io.netty.handler.logging.LogLevel.INFO; |
| + |
| +import io.netty.buffer.ByteBuf; |
| +import io.netty.buffer.ByteBufUtil; |
| +import io.netty.channel.ChannelHandlerContext; |
| +import io.netty.handler.codec.http2.AbstractHttp2ConnectionHandlerBuilder; |
| +import io.netty.handler.codec.http2.DefaultHttp2Headers; |
| +import io.netty.handler.codec.http2.Http2ConnectionDecoder; |
| +import io.netty.handler.codec.http2.Http2ConnectionEncoder; |
| +import io.netty.handler.codec.http2.Http2ConnectionHandler; |
| +import io.netty.handler.codec.http2.Http2Exception; |
| +import io.netty.handler.codec.http2.Http2Flags; |
| +import io.netty.handler.codec.http2.Http2FrameListener; |
| +import io.netty.handler.codec.http2.Http2FrameLogger; |
| +import io.netty.handler.codec.http2.Http2Headers; |
| +import io.netty.handler.codec.http2.Http2Settings; |
| +import io.netty.util.CharsetUtil; |
| + |
| +/** |
| + * HTTP/2 test handler for Cronet BidirectionalStream tests. |
| + */ |
| +final class Http2TestHandler extends Http2ConnectionHandler implements Http2FrameListener { |
| + private static final Http2FrameLogger sLogger = |
| + new Http2FrameLogger(INFO, Http2TestHandler.class); |
| + private static final ByteBuf RESPONSE_BYTES = |
| + unreleasableBuffer(copiedBuffer("HTTP/2 Test Server", CharsetUtil.UTF_8)); |
| + private boolean mEchoStream = false; |
| + private Http2Headers mResponseHeaders; |
| + |
| + static final class Builder |
| + extends AbstractHttp2ConnectionHandlerBuilder<Http2TestHandler, Builder> { |
| + public Builder() { |
| + frameLogger(sLogger); |
|
pauljensen
2016/01/19 16:03:41
what does logging to sLogger get us? is this unus
mef
2016/01/20 15:37:41
It is conveniently logging received frames into sy
|
| + } |
| + |
| + @Override |
| + public Http2TestHandler build() { |
| + return super.build(); |
| + } |
| + |
| + @Override |
| + protected Http2TestHandler build(Http2ConnectionDecoder decoder, |
| + Http2ConnectionEncoder encoder, Http2Settings initialSettings) { |
| + Http2TestHandler handler = new Http2TestHandler(decoder, encoder, initialSettings); |
| + frameListener(handler); |
| + return handler; |
| + } |
| + } |
| + |
| + private Http2TestHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, |
| + Http2Settings initialSettings) { |
| + super(decoder, encoder, initialSettings); |
| + } |
| + |
| + @Override |
| + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| + super.exceptionCaught(ctx, cause); |
| + cause.printStackTrace(); |
| + ctx.close(); |
| + } |
| + |
| + private static Http2Headers responseHeadersFromRequestHeaders(Http2Headers requestHeaders) { |
| + // Make response headers by echoing request headers. |
| + Http2Headers responseHeaders = new DefaultHttp2Headers().status(OK.codeAsText()); |
| + for (Map.Entry<CharSequence, CharSequence> header : requestHeaders) { |
| + if (!header.getKey().toString().startsWith(":")) { |
|
pauljensen
2016/01/19 16:03:41
is this attempting to ignore HTTP2-specific header
mef
2016/01/20 15:37:41
Yes, to avoid constructing header name like 'echo-
|
| + responseHeaders.add("echo-" + header.getKey(), header.getValue()); |
| + } |
| + } |
| + |
| + responseHeaders.add("echo-method", requestHeaders.get(":method").toString()); |
| + return responseHeaders; |
| + } |
| + |
| + private void sendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf payload) { |
| + // Send a frame for the response status |
| + encoder().writeHeaders(ctx, streamId, mResponseHeaders, 0, false, ctx.newPromise()); |
| + encoder().writeData(ctx, streamId, payload, 0, true, ctx.newPromise()); |
| + ctx.flush(); |
| + } |
| + |
| + @Override |
| + public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, |
| + boolean endOfStream) throws Http2Exception { |
| + int processed = data.readableBytes() + padding; |
| + if (mEchoStream) { |
| + encoder().writeData(ctx, streamId, data.retain(), 0, endOfStream, ctx.newPromise()); |
| + ctx.flush(); |
| + } else if (endOfStream) { |
| + sendResponse(ctx, streamId, data.retain()); |
| + } |
| + return processed; |
| + } |
| + |
| + private void doEchoStream(ChannelHandlerContext ctx, int streamId, Http2Headers headers, |
| + int padding, boolean endOfStream) { |
| + mEchoStream = true; |
| + // Send a frame for the response headers. |
| + encoder().writeHeaders(ctx, streamId, mResponseHeaders, 0, endOfStream, ctx.newPromise()); |
| + ctx.flush(); |
| + } |
| + |
| + private void doEchoTrailers(ChannelHandlerContext ctx, int streamId, Http2Headers headers, |
| + int padding, boolean endOfStream) { |
| + Http2Headers responseHeaders = new DefaultHttp2Headers().status(OK.codeAsText()); |
| + encoder().writeHeaders(ctx, streamId, responseHeaders, 0, false, ctx.newPromise()); |
| + encoder().writeData(ctx, streamId, RESPONSE_BYTES.duplicate(), 0, false, ctx.newPromise()); |
| + Http2Headers responseTrailers = mResponseHeaders.add("trailer", "value1", "Value2"); |
| + encoder().writeHeaders(ctx, streamId, responseTrailers, 0, true, ctx.newPromise()); |
| + ctx.flush(); |
| + } |
| + |
| + private String getEchoAllHeadersResponse(Http2Headers headers) { |
| + StringBuilder response = new StringBuilder(); |
| + for (Map.Entry<CharSequence, CharSequence> header : headers) { |
| + response.append(header.getKey() + ": " + header.getValue() + "\r\n"); |
| + } |
| + return response.toString(); |
| + } |
| + |
| + private String getEchoHeaderResponse(Http2Headers headers) { |
| + String[] split_path = headers.path().toString().split("\\?"); |
| + if (split_path.length <= 1) return "Header name not found."; |
| + |
| + String header_name = split_path[1].toLowerCase(Locale.US); |
| + if (headers.get(header_name) == null) return "Header not found:" + header_name; |
| + |
| + return headers.get(header_name).toString(); |
| + } |
| + |
| + @Override |
| + public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, |
| + int padding, boolean endOfStream) throws Http2Exception { |
| + mResponseHeaders = responseHeadersFromRequestHeaders(headers); |
| + String path = headers.path().toString(); |
| + if (path.startsWith("/echostream")) { |
| + doEchoStream(ctx, streamId, headers, padding, endOfStream); |
| + } else if (path.startsWith("/echotrailers")) { |
| + doEchoTrailers(ctx, streamId, headers, padding, endOfStream); |
| + } else if (endOfStream) { |
| + mResponseHeaders = new DefaultHttp2Headers().status(OK.codeAsText()); |
| + ByteBuf content = ctx.alloc().buffer(); |
| + try { |
| + if (path.startsWith("/echoallheaders")) { |
| + ByteBufUtil.writeAscii(content, getEchoAllHeadersResponse(headers)); |
| + } else if (path.startsWith("/echoheader")) { |
| + ByteBufUtil.writeAscii(content, getEchoHeaderResponse(headers)); |
| + } else if (path.startsWith("/echomethod")) { |
| + ByteBufUtil.writeAscii(content, headers.method()); |
| + } else { |
| + content.writeBytes(RESPONSE_BYTES.duplicate()); |
| + ByteBufUtil.writeAscii(content, " - via HTTP/2"); |
| + } |
| + } catch (Exception e) { |
| + ByteBufUtil.writeAscii(content, "Exception: " + e.toString()); |
| + } |
| + |
| + sendResponse(ctx, streamId, content); |
| + } |
| + } |
| + |
| + @Override |
| + public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, |
| + int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream) |
| + throws Http2Exception { |
| + onHeadersRead(ctx, streamId, headers, padding, endOfStream); |
| + } |
| + |
| + @Override |
| + public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, |
| + short weight, boolean exclusive) throws Http2Exception {} |
| + |
| + @Override |
| + public void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode) |
| + throws Http2Exception {} |
| + |
| + @Override |
| + public void onSettingsAckRead(ChannelHandlerContext ctx) throws Http2Exception {} |
| + |
| + @Override |
| + public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) |
| + throws Http2Exception {} |
| + |
| + @Override |
| + public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception {} |
| + |
| + @Override |
| + public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception {} |
| + |
| + @Override |
| + public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, |
| + Http2Headers headers, int padding) throws Http2Exception {} |
| + |
| + @Override |
| + public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, |
| + ByteBuf debugData) throws Http2Exception {} |
| + |
| + @Override |
| + public void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement) |
| + throws Http2Exception {} |
| + |
| + @Override |
| + public void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int streamId, |
| + Http2Flags flags, ByteBuf payload) throws Http2Exception {} |
| +} |