Correctly include the stream id when convert from Http2HeadersFrame to HttpMessage

Motivation:

We did not correctly set the stream id in the headers of HttpMessage when converting a Http2HeadersFrame. This is based on https://github.com/netty/netty/pull/7778 so thanks to @jprante.

Modifications:

- Correctly set the id when possible in the header.
- Add test case

Result:

Correctly include stream id.
This commit is contained in:
Norman Maurer 2018-03-16 21:34:22 +01:00 committed by Norman Maurer
parent 0adccfdb50
commit 2c90b6235d
2 changed files with 31 additions and 2 deletions

View File

@ -81,9 +81,10 @@ public class Http2StreamFrameToHttpObjectCodec extends MessageToMessageCodec<Htt
@Override @Override
protected void decode(ChannelHandlerContext ctx, Http2StreamFrame frame, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, Http2StreamFrame frame, List<Object> out) throws Exception {
if (frame instanceof Http2HeadersFrame) { if (frame instanceof Http2HeadersFrame) {
int id = 0; // not really the id
Http2HeadersFrame headersFrame = (Http2HeadersFrame) frame; Http2HeadersFrame headersFrame = (Http2HeadersFrame) frame;
Http2Headers headers = headersFrame.headers(); Http2Headers headers = headersFrame.headers();
Http2FrameStream stream = headersFrame.stream();
int id = stream == null ? 0 : stream.id();
final CharSequence status = headers.status(); final CharSequence status = headers.status();

View File

@ -745,12 +745,36 @@ public class Http2StreamFrameToHttpObjectCodecTest {
@Test @Test
public void testDecodeFullResponseHeaders() throws Exception { public void testDecodeFullResponseHeaders() throws Exception {
testDecodeFullResponseHeaders(false);
}
@Test
public void testDecodeFullResponseHeadersWithStreamID() throws Exception {
testDecodeFullResponseHeaders(true);
}
private void testDecodeFullResponseHeaders(boolean withStreamId) throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false)); EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
Http2Headers headers = new DefaultHttp2Headers(); Http2Headers headers = new DefaultHttp2Headers();
headers.scheme(HttpScheme.HTTP.name()); headers.scheme(HttpScheme.HTTP.name());
headers.status(HttpResponseStatus.OK.codeAsText()); headers.status(HttpResponseStatus.OK.codeAsText());
assertTrue(ch.writeInbound(new DefaultHttp2HeadersFrame(headers, true))); Http2HeadersFrame frame = new DefaultHttp2HeadersFrame(headers, true);
if (withStreamId) {
frame.stream(new Http2FrameStream() {
@Override
public int id() {
return 1;
}
@Override
public Http2Stream.State state() {
return Http2Stream.State.OPEN;
}
});
}
assertTrue(ch.writeInbound(frame));
FullHttpResponse response = ch.readInbound(); FullHttpResponse response = ch.readInbound();
try { try {
@ -759,6 +783,10 @@ public class Http2StreamFrameToHttpObjectCodecTest {
assertThat(response.content().readableBytes(), is(0)); assertThat(response.content().readableBytes(), is(0));
assertTrue(response.trailingHeaders().isEmpty()); assertTrue(response.trailingHeaders().isEmpty());
assertFalse(HttpUtil.isTransferEncodingChunked(response)); assertFalse(HttpUtil.isTransferEncodingChunked(response));
if (withStreamId) {
assertEquals(1,
(int) response.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text()));
}
} finally { } finally {
response.release(); response.release();
} }