|
44 | 44 | import static io.netty.buffer.Unpooled.wrappedBuffer; |
45 | 45 | import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT; |
46 | 46 | import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR; |
| 47 | +import static io.netty.handler.codec.http2.Http2Error.STREAM_CLOSED; |
47 | 48 | import static io.netty.handler.codec.http2.Http2PromisedRequestVerifier.ALWAYS_VERIFY; |
48 | 49 | import static io.netty.handler.codec.http2.Http2Stream.State.IDLE; |
49 | 50 | import static io.netty.handler.codec.http2.Http2Stream.State.OPEN; |
@@ -855,6 +856,64 @@ public void pushPromiseReadShouldSucceed() throws Exception { |
855 | 856 | eq(EmptyHttp2Headers.INSTANCE), eq(0)); |
856 | 857 | } |
857 | 858 |
|
| 859 | + // The promised stream must still be reserved (i.e. its existence tracked by the connection) even when the |
| 860 | + // Http2PromisedRequestVerifier subsequently rejects the promised request, so that the stream ID cannot be |
| 861 | + // silently reused or its book keeping lost. A follow-up frame for that stream must then be rejected with a |
| 862 | + // stream error rather than a connection error, since the connection is now aware of the stream. |
| 863 | + @Test |
| 864 | + public void pushPromiseReadRejectedByVerifierShouldStillReserveStream() throws Exception { |
| 865 | + Http2PromisedRequestVerifier verifier = new Http2PromisedRequestVerifier() { |
| 866 | + @Override |
| 867 | + public boolean isAuthoritative(ChannelHandlerContext ctx, Http2Headers headers) { |
| 868 | + return false; |
| 869 | + } |
| 870 | + |
| 871 | + @Override |
| 872 | + public boolean isCacheable(Http2Headers headers) { |
| 873 | + return true; |
| 874 | + } |
| 875 | + |
| 876 | + @Override |
| 877 | + public boolean isSafe(Http2Headers headers) { |
| 878 | + return true; |
| 879 | + } |
| 880 | + }; |
| 881 | + DefaultHttp2ConnectionDecoder rejecting = |
| 882 | + new DefaultHttp2ConnectionDecoder(connection, encoder, reader, verifier); |
| 883 | + rejecting.lifecycleManager(lifecycleManager); |
| 884 | + rejecting.frameListener(listener); |
| 885 | + // Prime the decoder past the connection preface so PUSH_PROMISE is not treated as an unexpected |
| 886 | + // first frame. |
| 887 | + decode(rejecting).onSettingsRead(ctx, new Http2Settings()); |
| 888 | + final Http2FrameListener dec = decode(rejecting); |
| 889 | + |
| 890 | + Http2Exception ex = assertThrows(Http2Exception.class, new Executable() { |
| 891 | + @Override |
| 892 | + public void execute() throws Throwable { |
| 893 | + dec.onPushPromiseRead(ctx, STREAM_ID, PUSH_STREAM_ID, EmptyHttp2Headers.INSTANCE, 0); |
| 894 | + } |
| 895 | + }); |
| 896 | + assertEquals(PROTOCOL_ERROR, ex.error()); |
| 897 | + assertEquals(PUSH_STREAM_ID, Http2Exception.streamId(ex)); |
| 898 | + |
| 899 | + verify(remote).reservePushStream(eq(PUSH_STREAM_ID), eq(stream)); |
| 900 | + verify(listener, never()).onPushPromiseRead(eq(ctx), anyInt(), anyInt(), any(Http2Headers.class), anyInt()); |
| 901 | + |
| 902 | + // Because the promised stream was reserved despite the rejection, the connection now knows about its |
| 903 | + // existence. A follow-up HEADERS frame for that stream (e.g. once it has been reset and closed) must |
| 904 | + // therefore be rejected with a stream error scoped to that stream, rather than a connection error. |
| 905 | + when(connection.stream(PUSH_STREAM_ID)).thenReturn(pushStream); |
| 906 | + when(pushStream.state()).thenReturn(Http2Stream.State.CLOSED); |
| 907 | + Http2Exception followUpEx = assertThrows(Http2Exception.class, new Executable() { |
| 908 | + @Override |
| 909 | + public void execute() throws Throwable { |
| 910 | + dec.onHeadersRead(ctx, PUSH_STREAM_ID, EmptyHttp2Headers.INSTANCE, 0, true); |
| 911 | + } |
| 912 | + }); |
| 913 | + assertEquals(STREAM_CLOSED, followUpEx.error()); |
| 914 | + assertEquals(PUSH_STREAM_ID, Http2Exception.streamId(followUpEx)); |
| 915 | + } |
| 916 | + |
858 | 917 | @Test |
859 | 918 | public void priorityReadAfterGoAwaySentShouldAllowFramesForStreamCreatedByLocalEndpoint() throws Exception { |
860 | 919 | mockGoAwaySentShouldAllowFramesForStreamCreatedByLocalEndpoint(); |
|