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