/** * Process incoming data. */ private void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception { isList.add(new ByteBufInputStream(data.content())); if (data.isEndStream()) { isList.add(NettyInputStream.END_OF_INPUT); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof Http2StreamFrame) { // Handle HTTP/2 clear text upgrade request if (msg instanceof Http2HeadersFrame) { Http2HeadersFrame frame = (Http2HeadersFrame) msg; connection.onHeadersRead(ctx, 1, frame.headers(), frame.padding(), frame.isEndStream()); } else if (msg instanceof Http2DataFrame) { Http2DataFrame frame = (Http2DataFrame) msg; connection.onDataRead(ctx, 1, frame.content(), frame.padding(), frame.isEndStream()); } } else { super.channelRead(ctx, msg); } }
if (msg instanceof Http2DataFrame) { Http2DataFrame dataFrame = (Http2DataFrame) msg; encoder().writeData(ctx, dataFrame.stream().id(), dataFrame.content(), dataFrame.padding(), dataFrame.isEndStream(), promise); } else if (msg instanceof Http2HeadersFrame) {
if (msg instanceof Http2DataFrame) { Http2DataFrame dataFrame = (Http2DataFrame) msg; encoder().writeData(ctx, dataFrame.stream().id(), dataFrame.content(), dataFrame.padding(), dataFrame.isEndStream(), promise); } else if (msg instanceof Http2HeadersFrame) {
if (msg instanceof Http2DataFrame) { Http2DataFrame dataFrame = (Http2DataFrame) msg; encoder().writeData(ctx, dataFrame.stream().id(), dataFrame.content(), dataFrame.padding(), dataFrame.isEndStream(), promise); } else if (msg instanceof Http2HeadersFrame) {
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof Http2StreamFrame) { // Handle HTTP/2 clear text upgrade request if (msg instanceof Http2HeadersFrame) { Http2HeadersFrame frame = (Http2HeadersFrame) msg; connection.onHeadersRead(ctx, 1, frame.headers(), frame.padding(), frame.isEndStream()); } else if (msg instanceof Http2DataFrame) { Http2DataFrame frame = (Http2DataFrame) msg; connection.onDataRead(ctx, 1, frame.content(), frame.padding(), frame.isEndStream()); } } else { super.channelRead(ctx, msg); } }
private void onDataRead(Http2DataFrame dataFrame, ChannelHandlerContext ctx) throws Http2Exception { ByteBuf data = dataFrame.content(); data.retain(); if (!dataFrame.isEndStream()) { ctx.fireChannelRead(new DefaultHttpContent(data)); } else { ctx.fireChannelRead(new DefaultLastHttpContent(data)); } }
private void onDataRead(Http2DataFrame dataFrame, ChannelHandlerContext ctx) throws Http2Exception { ByteBuf data = dataFrame.content(); data.retain(); if (!dataFrame.isEndStream()) { ctx.fireChannelRead(new DefaultHttpContent(data)); } else { ctx.fireChannelRead(new DefaultLastHttpContent(data)); } }
private void writeData( ChannelHandlerContext ctx, Http2DataFrame data, ChannelPromise promise, int currentStreamId) throws Exception { encoder().writeData(ctx, currentStreamId, data.content(), 0, data.isEndStream(), promise); }
private void writeData( ChannelHandlerContext ctx, Http2DataFrame data, ChannelPromise promise, int currentStreamId) throws Exception { encoder().writeData(ctx, currentStreamId, data.content(), 0, data.isEndStream(), promise); }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { boolean lastContent = false; if (msg instanceof Http2HeadersFrame) { final Http2HeadersFrame responseHeaders = (Http2HeadersFrame) msg; final Http2Headers headers = responseHeaders.headers(); lastContent = responseHeaders.isEndStream(); accessLog.status(headers.status()) .chunked(true); } if (msg instanceof Http2DataFrame) { final Http2DataFrame data = (Http2DataFrame) msg; lastContent = data.isEndStream(); accessLog.increaseContentLength(data.content().readableBytes()); } if (lastContent) { ctx.write(msg, promise) .addListener(future -> { if (future.isSuccess()) { accessLog.log(); } }); return; } ctx.write(msg, promise); } }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { boolean lastContent = false; if (msg instanceof Http2HeadersFrame) { final Http2HeadersFrame responseHeaders = (Http2HeadersFrame) msg; final Http2Headers headers = responseHeaders.headers(); lastContent = responseHeaders.isEndStream(); accessLog.status(headers.status()) .chunked(true); } if (msg instanceof Http2DataFrame) { final Http2DataFrame data = (Http2DataFrame) msg; lastContent = data.isEndStream(); accessLog.increaseContentLength(data.content().readableBytes()); } if (lastContent) { ctx.write(msg, promise) .addListener(future -> { if (future.isSuccess()) { accessLog.log(); } }); return; } ctx.write(msg, promise); } }
@Override protected void onInboundNext(ChannelHandlerContext ctx, Object msg) { if (msg instanceof Http2DataFrame) { Http2DataFrame data = (Http2DataFrame) msg; super.onInboundNext(ctx, data.content()); if (data.isEndStream()) { onInboundComplete(); } return; } else if(msg instanceof Http2HeadersFrame) { listener().onStateChange(this, HttpServerState.REQUEST_RECEIVED); if (((Http2HeadersFrame) msg).isEndStream()) { super.onInboundNext(ctx, msg); } return; } super.onInboundNext(ctx, msg); } }
@Override protected void onInboundNext(ChannelHandlerContext ctx, Object msg) { if (msg instanceof Http2DataFrame) { Http2DataFrame data = (Http2DataFrame) msg; super.onInboundNext(ctx, data.content()); if (data.isEndStream()) { onInboundComplete(); } return; } else if(msg instanceof Http2HeadersFrame) { listener().onStateChange(this, HttpServerState.REQUEST_RECEIVED); if (((Http2HeadersFrame) msg).isEndStream()) { super.onInboundNext(ctx, msg); } return; } super.onInboundNext(ctx, msg); } }
@Test public void testFullRequestWithBody() throws Exception { outputReceived = new CountDownLatch(1); ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "body"); FullRequest requestIn = RequestBuilders.newPost("/").host("localhost").body(body).build(); channel.writeOutbound(requestIn); channel.runPendingTasks(); // blocks Uninterruptibles.awaitUninterruptibly(outputReceived); Http2Request requestOut = (Http2Request) requests.remove(0); assertTrue(requestOut != null); assertTrue(requestOut.payload instanceof Http2Headers); assertEquals("POST", ((Http2Headers) requestOut.payload).method().toString()); assertEquals("/", ((Http2Headers) requestOut.payload).path()); assertFalse(requestOut.eos); Http2Request contentOut = (Http2Request) requests.remove(0); assertTrue(contentOut != null); assertTrue(contentOut.payload instanceof Http2DataFrame); assertEquals(body, ((Http2DataFrame) contentOut.payload).content()); assertTrue(contentOut.eos); }
SegmentedRequestData data = new SegmentedRequestData( initialRequest, new Http2SegmentedData(frame.content(), msg.eos, msg.streamId)); messageSession.onInboundRequestData(data); return data;
assertFalse(((Http2DataFrame) bodyOut1.payload).content() == null); assertEquals(body1, ((Http2DataFrame) bodyOut1.payload).content()); assertFalse(bodyOut1.eos); assertFalse(((Http2DataFrame) bodyOut2.payload).content() == null); assertEquals(body2, ((Http2DataFrame) bodyOut2.payload).content()); assertFalse(bodyOut2.eos);
@Test public void testFullResponseWithBody() throws Exception { outputReceived = new CountDownLatch(2); ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "response"); Http2Headers headersIn = new DefaultHttp2Headers().method("GET").path("/"); Http2Request requestIn = Http2Request.build(1, headersIn, true); FullResponse responseIn = ResponseBuilders.newOk().streamId(1).body(body).build(); channel.writeInbound(requestIn); channel.runPendingTasks(); // blocks channel.writeOutbound(responseIn); channel.runPendingTasks(); // blocks Uninterruptibles.awaitUninterruptibly(outputReceived); Http2Response responseOut = responses.remove(0); assertNotNull(responseOut); assertTrue(responseOut.payload instanceof Http2Headers); assertEquals("200", ((Http2Headers) responseOut.payload).status().toString()); assertFalse(responseOut.eos); assertEquals(1, responseOut.streamId); Http2Response bodyOut1 = responses.remove(0); assertNotNull(bodyOut1); assertTrue(bodyOut1.payload instanceof Http2DataFrame); assertEquals(body, ((Http2DataFrame) bodyOut1.payload).content()); assertTrue(bodyOut1.eos); assertEquals(1, bodyOut1.streamId); }