OLD | NEW |
(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.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 public final class Http2TestHandler extends Http2ConnectionHandler implements Ht
tp2FrameListener { |
| 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; |
| 40 private Http2Headers mResponseHeaders; |
| 41 |
| 42 static final class Builder |
| 43 extends AbstractHttp2ConnectionHandlerBuilder<Http2TestHandler, Buil
der> { |
| 44 public Builder() { |
| 45 frameLogger(sLogger); |
| 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(":")) { |
| 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 static String getEchoAllHeadersResponse(Http2Headers headers) { |
| 88 StringBuilder response = new StringBuilder(); |
| 89 for (Map.Entry<CharSequence, CharSequence> header : headers) { |
| 90 response.append(header.getKey() + ": " + header.getValue() + "\r\n")
; |
| 91 } |
| 92 return response.toString(); |
| 93 } |
| 94 |
| 95 private static String getEchoHeaderResponse(Http2Headers headers) { |
| 96 String[] split_path = headers.path().toString().split("\\?"); |
| 97 if (split_path.length <= 1) return "Header name not found."; |
| 98 |
| 99 String header_name = split_path[1].toLowerCase(Locale.US); |
| 100 if (headers.get(header_name) == null) return "Header not found:" + heade
r_name; |
| 101 |
| 102 return headers.get(header_name).toString(); |
| 103 } |
| 104 |
| 105 private void doSendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf
payload) { |
| 106 // Send a frame for the response status |
| 107 encoder().writeHeaders(ctx, streamId, mResponseHeaders, 0, false, ctx.ne
wPromise()); |
| 108 encoder().writeData(ctx, streamId, payload, 0, true, ctx.newPromise()); |
| 109 ctx.flush(); |
| 110 } |
| 111 |
| 112 private void doEchoStream(ChannelHandlerContext ctx, int streamId, Http2Head
ers headers, |
| 113 int padding, boolean endOfStream) { |
| 114 mEchoStream = true; |
| 115 // Send a frame for the response headers. |
| 116 encoder().writeHeaders(ctx, streamId, mResponseHeaders, 0, endOfStream,
ctx.newPromise()); |
| 117 ctx.flush(); |
| 118 } |
| 119 |
| 120 private void doEchoTrailers(ChannelHandlerContext ctx, int streamId, Http2He
aders headers, |
| 121 int padding, boolean endOfStream) { |
| 122 Http2Headers responseHeaders = new DefaultHttp2Headers().status(OK.codeA
sText()); |
| 123 encoder().writeHeaders(ctx, streamId, responseHeaders, 0, false, ctx.new
Promise()); |
| 124 encoder().writeData(ctx, streamId, RESPONSE_BYTES.duplicate(), 0, false,
ctx.newPromise()); |
| 125 Http2Headers responseTrailers = mResponseHeaders.add("trailer", "value1"
, "Value2"); |
| 126 encoder().writeHeaders(ctx, streamId, responseTrailers, 0, true, ctx.new
Promise()); |
| 127 ctx.flush(); |
| 128 } |
| 129 |
| 130 @Override |
| 131 public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data,
int padding, |
| 132 boolean endOfStream) throws Http2Exception { |
| 133 int processed = data.readableBytes() + padding; |
| 134 if (mEchoStream) { |
| 135 encoder().writeData(ctx, streamId, data.retain(), 0, endOfStream, ct
x.newPromise()); |
| 136 ctx.flush(); |
| 137 } else if (endOfStream) { |
| 138 doSendResponse(ctx, streamId, data.retain()); |
| 139 } |
| 140 return processed; |
| 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 doSendResponse(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 |