GeezerVison is showing github.com — Implement Quic stream reset frame application protocol error code pro… · netty/netty@2eadf3e
File treeExpand file treeCollapse file tree Open diff view settings codec-classes-quic/src/main/java/io/netty/handler/codec/quic test/java/io/netty/handler/codec/quic
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicException.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | /** | 21 | 21 | * Exception produced while processing {@code QUIC}. | 22 | 22 | */ | 23 | | -public final class QuicException extends Exception { | | 23 | +public class QuicException extends Exception { | 24 | 24 |
| 25 | 25 | private final QuicTransportError error; | 26 | 26 |
| |
Collapse file Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * Copyright 2025 The Netty Project | | 3 | + * | | 4 | + * The Netty Project licenses this file to you under the Apache License, | | 5 | + * version 2.0 (the "License"); you may not use this file except in compliance | | 6 | + * with the License. You may obtain a copy of the License at: | | 7 | + * | | 8 | + * https://www.apache.org/licenses/LICENSE-2.0 | | 9 | + * | | 10 | + * Unless required by applicable law or agreed to in writing, software | | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | | 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | | 13 | + * License for the specific language governing permissions and limitations | | 14 | + * under the License. | | 15 | + */ | | 16 | +package io.netty.handler.codec.quic; | | 17 | + | | 18 | +/** | | 19 | + * Signals a stream reset. | | 20 | + */ | | 21 | +public final class QuicStreamResetException extends QuicException { | | 22 | + | | 23 | + private final long applicationProtocolCode; | | 24 | + | | 25 | + public QuicStreamResetException(String message, long applicationProtocolCode) { | | 26 | + super(message); | | 27 | + | | 28 | + this.applicationProtocolCode = applicationProtocolCode; | | 29 | + } | | 30 | + | | 31 | + /** | | 32 | + * Returns the optional application protocol error code set on {@code RESET_STREAM} frames. | | 33 | + * | | 34 | + * Note: this is not yet implemented for {@code STOP_SENDING} frames. | | 35 | + * | | 36 | + * @return the optional application protocol error code or {@code -1} when no such code is provided. | | 37 | + */ | | 38 | + public long applicationProtocolCode() { | | 39 | + return applicationProtocolCode; | | 40 | + } | | 41 | +} |
Collapse file Copy file name to clipboardExpand all lines: codec-classes-quic/src/main/java/io/netty/handler/codec/quic/Quiche.java +16-3Lines changed: 16 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 30 | 30 | import java.nio.ByteBuffer; | 31 | 31 | import java.nio.ByteOrder; | 32 | 32 |
| | 33 | +import static io.netty.handler.codec.quic.QuicheError.STREAM_RESET; | | 34 | + | 33 | 35 | final class Quiche { | 34 | 36 | private static final InternalLogger logger = InternalLoggerFactory.getInstance(Quiche.class); | 35 | 37 | private static final boolean TRACE_LOGGING_ENABLED = logger.isTraceEnabled(); | @@ -401,9 +403,11 @@ static native int quiche_conn_stream_priority( | 401 | 403 | static native byte[] quiche_conn_destination_id(long connAddr); | 402 | 404 |
| 403 | 405 | /** | 404 | | - * See <a href="https://github.com/cloudflare/quiche/blob/0.6.0/include/quiche.h#L258">quiche_conn_stream_recv</a>. | | 406 | + * See <a href="https://github.com/cloudflare/quiche/blob/0.24.5/quiche/include/quiche.h#L397"> | | 407 | + * quiche_conn_stream_recv</a>. | 405 | 408 | */ | 406 | | - static native int quiche_conn_stream_recv(long connAddr, long streamId, long outAddr, int bufLen, long finAddr); | | 409 | + static native int quiche_conn_stream_recv(long connAddr, long streamId, long outAddr, int bufLen, long finAddr, | | 410 | + long outErrorCodeAddr); | 407 | 411 |
| 408 | 412 | /** | 409 | 413 | * See <a href="https://github.com/cloudflare/quiche/blob/0.6.0/include/quiche.h#L262">quiche_conn_stream_send</a>. | @@ -895,10 +899,19 @@ static long getPrimitiveValue(ByteBuffer memory, int offset, int valueType) { | 895 | 899 | } | 896 | 900 |
| 897 | 901 | static Exception convertToException(int result) { | | 902 | + return convertToException(result, -1); | | 903 | + } | | 904 | + | | 905 | + static Exception convertToException(int result, long code) { | 898 | 906 | QuicTransportErrorHolder holder = ERROR_MAPPINGS.get(result); | 899 | 907 | if (holder == null) { | 900 | 908 | // There is no mapping to a transport error, it's something internal so throw it directly. | 901 | | - return new QuicException(QuicheError.valueOf(result).message()); | | 909 | + QuicheError error = QuicheError.valueOf(result); | | 910 | + if (error == STREAM_RESET) { | | 911 | + return new QuicStreamResetException(error.message(), code); | | 912 | + } else { | | 913 | + return new QuicException(error.message()); | | 914 | + } | 902 | 915 | } | 903 | 916 | Exception exception = new QuicException(holder.error + ": " + holder.quicheErrorName, holder.error); | 904 | 917 | if (result == QUICHE_ERR_TLS_FAIL) { | |
Collapse file Copy file name to clipboardExpand all lines: codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicheQuicChannel.java +13-2Lines changed: 13 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -142,6 +142,7 @@ public void operationComplete(ChannelFuture future) { | 142 | 142 | private boolean inFireChannelReadCompleteQueue; | 143 | 143 | private boolean fireChannelReadCompletePending; | 144 | 144 | private ByteBuf finBuffer; | | 145 | + private ByteBuf outErrorCodeBuffer; | 145 | 146 | private ChannelPromise connectPromise; | 146 | 147 | private ScheduledFuture<?> connectTimeoutFuture; | 147 | 148 | private QuicConnectionAddress connectAddress; | @@ -624,6 +625,10 @@ protected void doClose() throws Exception { | 624 | 625 | finBuffer.release(); | 625 | 626 | finBuffer = null; | 626 | 627 | } | | 628 | + if (outErrorCodeBuffer != null) { | | 629 | + outErrorCodeBuffer.release(); | | 630 | + outErrorCodeBuffer = null; | | 631 | + } | 627 | 632 | } finally { | 628 | 633 | if (sendResult == SendResult.SOME) { | 629 | 634 | // As this is the close let us flush it asap. | @@ -940,13 +945,19 @@ StreamRecvResult streamRecv(long streamId, ByteBuf buffer) throws Exception { | 940 | 945 | if (finBuffer == null) { | 941 | 946 | finBuffer = alloc().directBuffer(1); | 942 | 947 | } | | 948 | + if (outErrorCodeBuffer == null) { | | 949 | + outErrorCodeBuffer = alloc().directBuffer(8); | | 950 | + } | | 951 | + outErrorCodeBuffer.setLongLE(0, -1L); | 943 | 952 | int writerIndex = buffer.writerIndex(); | 944 | 953 | int recvLen = Quiche.quiche_conn_stream_recv(connAddr, streamId, | 945 | | - Quiche.writerMemoryAddress(buffer), buffer.writableBytes(), Quiche.writerMemoryAddress(finBuffer)); | | 954 | + Quiche.writerMemoryAddress(buffer), buffer.writableBytes(), Quiche.writerMemoryAddress(finBuffer), | | 955 | + Quiche.writerMemoryAddress(outErrorCodeBuffer)); | | 956 | + long errorCode = outErrorCodeBuffer.getLongLE(0); | 946 | 957 | if (recvLen == Quiche.QUICHE_ERR_DONE) { | 947 | 958 | return StreamRecvResult.DONE; | 948 | 959 | } else if (recvLen < 0) { | 949 | | - throw Quiche.convertToException(recvLen); | | 960 | + throw Quiche.convertToException(recvLen, errorCode); | 950 | 961 | } | 951 | 962 | buffer.writerIndex(writerIndex + recvLen); | 952 | 963 | return finBuffer.getBoolean(0) ? StreamRecvResult.FIN : StreamRecvResult.OK; | |
Collapse file Copy file name to clipboardExpand all lines: codec-native-quic/src/main/c/netty_quic_quiche.c +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -490,9 +490,8 @@ static jint netty_quiche_conn_stream_priority(JNIEnv* env, jclass clazz, jlong c | 490 | 490 | return (jint) quiche_conn_stream_priority((quiche_conn *) conn, (uint64_t) stream_id, (uint8_t) urgency, incremental == JNI_TRUE ? true : false); | 491 | 491 | } | 492 | 492 |
| 493 | | -static jint netty_quiche_conn_stream_recv(JNIEnv* env, jclass clazz, jlong conn, jlong stream_id, jlong out, int buf_len, jlong finAddr) { | 494 | | - uint64_t error_code; | 495 | | - return (jint) quiche_conn_stream_recv((quiche_conn *) conn, (uint64_t) stream_id, (uint8_t *) out, (size_t) buf_len, (bool *) finAddr, &error_code); | | 493 | +static jint netty_quiche_conn_stream_recv(JNIEnv* env, jclass clazz, jlong conn, jlong stream_id, jlong out, int buf_len, jlong finAddr, jlong error_codeAddr) { | | 494 | + return (jint) quiche_conn_stream_recv((quiche_conn *) conn, (uint64_t) stream_id, (uint8_t *) out, (size_t) buf_len, (bool *) finAddr, (uint64_t *) error_codeAddr); | 496 | 495 | } | 497 | 496 |
| 498 | 497 | static jint netty_quiche_conn_stream_send(JNIEnv* env, jclass clazz, jlong conn, jlong stream_id, jlong buf, int buf_len, jboolean fin) { | @@ -1181,7 +1180,7 @@ static const JNINativeMethod fixed_method_table[] = { | 1181 | 1180 | { "quiche_conn_peer_streams_left_bidi", "(J)J", (void *) netty_quiche_conn_peer_streams_left_bidi }, | 1182 | 1181 | { "quiche_conn_peer_streams_left_uni", "(J)J", (void *) netty_quiche_conn_peer_streams_left_uni }, | 1183 | 1182 | { "quiche_conn_stream_priority", "(JJBZ)I", (void *) netty_quiche_conn_stream_priority }, | 1184 | | - { "quiche_conn_stream_recv", "(JJJIJ)I", (void *) netty_quiche_conn_stream_recv }, | | 1183 | + { "quiche_conn_stream_recv", "(JJJIJJ)I", (void *) netty_quiche_conn_stream_recv }, | 1185 | 1184 | { "quiche_conn_stream_send", "(JJJIZ)I", (void *) netty_quiche_conn_stream_send }, | 1186 | 1185 | { "quiche_conn_stream_shutdown", "(JJIJ)I", (void *) netty_quiche_conn_stream_shutdown }, | 1187 | 1186 | { "quiche_conn_stream_capacity", "(JJ)J", (void *) netty_quiche_conn_stream_capacity }, | |
Collapse file Copy file name to clipboardExpand all lines: codec-native-quic/src/test/java/io/netty/handler/codec/quic/QuicStreamShutdownTest.java +71-6Lines changed: 71 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 31 | 31 | import java.util.concurrent.CountDownLatch; | 32 | 32 | import java.util.concurrent.Executor; | 33 | 33 | import java.util.concurrent.TimeUnit; | | 34 | +import java.util.concurrent.atomic.AtomicLong; | 34 | 35 | import java.util.concurrent.atomic.AtomicReference; | 35 | 36 |
| 36 | | -import static org.junit.jupiter.api.Assertions.assertTrue; | 37 | 37 | import static org.junit.jupiter.api.Assertions.fail; | | 38 | +import static org.junit.jupiter.api.Assertions.assertTrue; | | 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 38 | 40 |
| 39 | 41 | public class QuicStreamShutdownTest extends AbstractQuicTest { | 40 | 42 |
| @@ -96,11 +98,6 @@ public void testShutdownInputClosureByServerCausesStreamStopped(Executor executo | 96 | 98 | try { | 97 | 99 | server = QuicTestUtils.newServer(executor, new ChannelInboundHandlerAdapter(), | 98 | 100 | new ChannelInboundHandlerAdapter() { | 99 | | - @Override | 100 | | - public void channelRegistered(ChannelHandlerContext ctx) { | 101 | | - ctx.fireChannelRegistered(); | 102 | | - } | 103 | | - | 104 | 101 | @Override | 105 | 102 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | 106 | 103 | QuicStreamChannel streamChannel = (QuicStreamChannel) ctx.channel(); | @@ -163,4 +160,72 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) | 163 | 160 | shutdown(executor); | 164 | 161 | } | 165 | 162 | } | | 163 | + | | 164 | + @ParameterizedTest | | 165 | + @MethodSource("newSslTaskExecutors") | | 166 | + public void testShutdownOutputClosureByServerCausesStreamReset(Executor executor) throws Throwable { | | 167 | + Channel server = null; | | 168 | + Channel channel = null; | | 169 | + CountDownLatch latch = new CountDownLatch(2); | | 170 | + AtomicReference<Throwable> errorRef = new AtomicReference<>(); | | 171 | + AtomicLong applicationErrorCode = new AtomicLong(-1); | | 172 | + try { | | 173 | + server = QuicTestUtils.newServer(executor, new ChannelInboundHandlerAdapter(), | | 174 | + new ChannelInboundHandlerAdapter() { | | 175 | + @Override | | 176 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { | | 177 | + QuicStreamChannel streamChannel = (QuicStreamChannel) ctx.channel(); | | 178 | + ByteBuf buffer = (ByteBuf) msg; | | 179 | + buffer.release(); | | 180 | + streamChannel.shutdownOutput(100).addListener(new ChannelFutureListener() { | | 181 | + @Override | | 182 | + public void operationComplete(ChannelFuture f) { | | 183 | + if (!f.isSuccess()) { | | 184 | + errorRef.compareAndSet(null, f.cause()); | | 185 | + } | | 186 | + latch.countDown(); | | 187 | + } | | 188 | + }); | | 189 | + } | | 190 | + }); | | 191 | + | | 192 | + channel = QuicTestUtils.newClient(executor); | | 193 | + QuicChannel quicChannel = QuicTestUtils.newQuicChannelBootstrap(channel) | | 194 | + .handler(new ChannelInboundHandlerAdapter()) | | 195 | + .streamHandler(new ChannelInboundHandlerAdapter()) | | 196 | + .remoteAddress(server.localAddress()) | | 197 | + .connect() | | 198 | + .get(); | | 199 | + | | 200 | + QuicStreamChannel streamChannel = quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, | | 201 | + new ChannelInboundHandlerAdapter() { | | 202 | + @Override | | 203 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | | 204 | + if (cause instanceof QuicStreamResetException) { | | 205 | + QuicStreamResetException reset = (QuicStreamResetException) cause; | | 206 | + applicationErrorCode.set(reset.applicationProtocolCode()); | | 207 | + } else { | | 208 | + errorRef.set(cause); | | 209 | + } | | 210 | + latch.countDown(); | | 211 | + } | | 212 | + }).sync().getNow(); | | 213 | + | | 214 | + streamChannel.writeAndFlush(Unpooled.buffer().writeInt(4)).sync(); | | 215 | + | | 216 | + if (!latch.await(5, TimeUnit.SECONDS)) { | | 217 | + fail("Timeout while waiting for completion", errorRef.get()); | | 218 | + } | | 219 | + Throwable error = errorRef.get(); | | 220 | + if (error != null) { | | 221 | + fail("Failure during execution", error); | | 222 | + } | | 223 | + assertEquals(100, applicationErrorCode.get()); | | 224 | + } finally { | | 225 | + QuicTestUtils.closeIfNotNull(channel); | | 226 | + QuicTestUtils.closeIfNotNull(server); | | 227 | + | | 228 | + shutdown(executor); | | 229 | + } | | 230 | + } | 166 | 231 | } |
|