GeezerVison is showing github.com — Correctly encoder / decode and handle unknown HTTP/3 frames (#30) · chrisvest/netty@37153b4
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/incubator/codec/http3 test/java/io/netty/incubator/codec/http3
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +105Lines changed: 105 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * Copyright 2020 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.incubator.codec.http3; | | 17 | + | | 18 | +import io.netty.buffer.ByteBuf; | | 19 | +import io.netty.buffer.DefaultByteBufHolder; | | 20 | +import io.netty.util.internal.StringUtil; | | 21 | + | | 22 | +import java.util.Objects; | | 23 | + | | 24 | +public final class DefaultHttp3UnknownFrame extends DefaultByteBufHolder implements Http3UnknownFrame { | | 25 | + private final long type; | | 26 | + | | 27 | + public DefaultHttp3UnknownFrame(long type, ByteBuf payload) { | | 28 | + super(payload); | | 29 | + this.type = Http3CodecUtils.checkIsReservedFrameType(type); | | 30 | + } | | 31 | + | | 32 | + @Override | | 33 | + public long type() { | | 34 | + return type; | | 35 | + } | | 36 | + | | 37 | + @Override | | 38 | + public Http3UnknownFrame copy() { | | 39 | + return new DefaultHttp3UnknownFrame(type, content().copy()); | | 40 | + } | | 41 | + | | 42 | + @Override | | 43 | + public Http3UnknownFrame duplicate() { | | 44 | + return new DefaultHttp3UnknownFrame(type, content().duplicate()); | | 45 | + } | | 46 | + | | 47 | + @Override | | 48 | + public Http3UnknownFrame retainedDuplicate() { | | 49 | + return new DefaultHttp3UnknownFrame(type, content().retainedDuplicate()); | | 50 | + } | | 51 | + | | 52 | + @Override | | 53 | + public Http3UnknownFrame replace(ByteBuf content) { | | 54 | + return new DefaultHttp3UnknownFrame(type, content); | | 55 | + } | | 56 | + | | 57 | + @Override | | 58 | + public Http3UnknownFrame retain() { | | 59 | + super.retain(); | | 60 | + return this; | | 61 | + } | | 62 | + | | 63 | + @Override | | 64 | + public Http3UnknownFrame retain(int increment) { | | 65 | + super.retain(increment); | | 66 | + return this; | | 67 | + } | | 68 | + | | 69 | + @Override | | 70 | + public Http3UnknownFrame touch() { | | 71 | + super.touch(); | | 72 | + return this; | | 73 | + } | | 74 | + | | 75 | + @Override | | 76 | + public Http3UnknownFrame touch(Object hint) { | | 77 | + super.touch(hint); | | 78 | + return this; | | 79 | + } | | 80 | + | | 81 | + @Override | | 82 | + public String toString() { | | 83 | + return StringUtil.simpleClassName(this) + "(type=" + type() + ", content=" + content() + ')'; | | 84 | + } | | 85 | + | | 86 | + @Override | | 87 | + public boolean equals(Object o) { | | 88 | + if (this == o) { | | 89 | + return true; | | 90 | + } | | 91 | + if (o == null || getClass() != o.getClass()) { | | 92 | + return false; | | 93 | + } | | 94 | + DefaultHttp3UnknownFrame that = (DefaultHttp3UnknownFrame) o; | | 95 | + if (type != that.type) { | | 96 | + return false; | | 97 | + } | | 98 | + return super.equals(o); | | 99 | + } | | 100 | + | | 101 | + @Override | | 102 | + public int hashCode() { | | 103 | + return Objects.hash(super.hashCode(), type); | | 104 | + } | | 105 | +} |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/io/netty/incubator/codec/http3/Http3CodecUtils.java +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | import io.netty.channel.ChannelHandlerContext; | 21 | 21 | import io.netty.incubator.codec.quic.QuicChannel; | 22 | 22 | import io.netty.util.CharsetUtil; | | 23 | +import io.netty.util.internal.ObjectUtil; | 23 | 24 |
| 24 | 25 | final class Http3CodecUtils { | 25 | 26 | static final long DEFAULT_MAX_HEADER_LIST_SIZE = 0xffffffffL; | 26 | 27 |
| | 28 | + // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-7.2.8 | | 29 | + static final long MIN_RESERVED_FRAME_TYPE = 0x1f * 1 + 0x21; | | 30 | + static final long MAX_RESERVED_FRAME_TYPE = 0x1f * (long) Integer.MAX_VALUE + 0x21; | | 31 | + | 27 | 32 | private Http3CodecUtils() { } | 28 | 33 |
| | 34 | + static long checkIsReservedFrameType(long type) { | | 35 | + return ObjectUtil.checkInRange(type, MIN_RESERVED_FRAME_TYPE, MAX_RESERVED_FRAME_TYPE, "type"); | | 36 | + } | | 37 | + | | 38 | + static boolean isReservedFrameType(long type) { | | 39 | + return type >= MIN_RESERVED_FRAME_TYPE && type <= MAX_RESERVED_FRAME_TYPE; | | 40 | + } | | 41 | + | | 42 | + static boolean isReservedHttp2(long type) { | | 43 | + switch ((int) type) { | | 44 | + // Reserved types that were used in HTTP/2 | | 45 | + case 0x2: | | 46 | + case 0x6: | | 47 | + case 0x8: | | 48 | + case 0x9: | | 49 | + return true; | | 50 | + default: | | 51 | + return false; | | 52 | + } | | 53 | + } | | 54 | + | 29 | 55 | /** | 30 | 56 | * Returns the number of bytes needed to encode the variable length integer. | 31 | 57 | * | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/io/netty/incubator/codec/http3/Http3ControlStreamFrameDispatcher.java +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,9 @@ public boolean isSharable() { | 45 | 45 | @Override | 46 | 46 | public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { | 47 | 47 | // Check if we are already on the local control frame or not. | 48 | | - if (ctx.channel() != localControlStream && msg instanceof Http3ControlStreamFrame) { | | 48 | + if (ctx.channel() != localControlStream && msg instanceof Http3ControlStreamFrame && | | 49 | + // We can not make any smart decision for unknown frames as there are allowed on all stream types. | | 50 | + !(msg instanceof Http3UnknownFrame)) { | 49 | 51 | // write and flush as otherwise we may never flush the control stream for the write. | 50 | 52 | localControlStream.writeAndFlush(msg).addListener(new ChannelPromiseNotifier(promise)); | 51 | 53 | return; | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/io/netty/incubator/codec/http3/Http3ControlStreamInboundHandler.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -85,10 +85,10 @@ public void channelRead(ChannelHandlerContext ctx, Http3ControlStreamFrame frame | 85 | 85 | } | 86 | 86 | } else if (frame instanceof Http3CancelPushFrame) { | 87 | 87 | // TODO: implement me | 88 | | - } else { | 89 | | - // TODO: Do we need to do something to handle unknown frames | 90 | 88 | } | 91 | 89 |
| | 90 | + // We don't need to do any special handling for Http3UnknownFrames as we either pass these to the next handler | | 91 | + // or release these directly. | 92 | 92 | if (forwardControlFrames) { | 93 | 93 | // The user did specify ChannelHandler that should be notified about control stream frames. | 94 | 94 | // Let's forward the frame so the user can do something with it. | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/io/netty/incubator/codec/http3/Http3FrameDecoder.java +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,7 +65,14 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { | 65 | 65 | if (in.readableBytes() < typeLen) { | 66 | 66 | return; | 67 | 67 | } | 68 | | - type = readVariableLengthInteger(in, typeLen); | | 68 | + long type = readVariableLengthInteger(in, typeLen); | | 69 | + if (Http3CodecUtils.isReservedHttp2(type)) { | | 70 | + // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-7.2.8 | | 71 | + Http3CodecUtils.connectionError(ctx, Http3ErrorCode.H3_FRAME_UNEXPECTED, | | 72 | + "Reserved type for HTTP/2 received.", true); | | 73 | + return; | | 74 | + } | | 75 | + this.type = type; | 69 | 76 | if (!in.isReadable()) { | 70 | 77 | return; | 71 | 78 | } | @@ -88,6 +95,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { | 88 | 95 | this.payLoadLength = -1; | 89 | 96 | try { | 90 | 97 | if (type <= Integer.MAX_VALUE) { | | 98 | + // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-11.2.1 | 91 | 99 | switch ((int) type) { | 92 | 100 | case 0x0: | 93 | 101 | // DATA | @@ -136,8 +144,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { | 136 | 144 | out.add(new DefaultHttp3MaxPushIdFrame(readVariableLengthInteger(in, pidLen))); | 137 | 145 | break; | 138 | 146 | default: | | 147 | + // TODO: Should we do validation here ? | 139 | 148 | // Handling reserved frame types | 140 | 149 | // https://tools.ietf.org/html/draft-ietf-quic-http-32#section-7.2.8 | | 150 | + out.add(new DefaultHttp3UnknownFrame(type, in.readRetainedSlice(payLoadLength))); | 141 | 151 | break; | 142 | 152 | } | 143 | 153 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/io/netty/incubator/codec/http3/Http3FrameEncoder.java +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,8 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) | 53 | 53 | writeGoAwayFrame(ctx, (Http3GoAwayFrame) msg, promise); | 54 | 54 | } else if (msg instanceof Http3MaxPushIdFrame) { | 55 | 55 | writeMaxPushIdFrame(ctx, (Http3MaxPushIdFrame) msg, promise); | | 56 | + } else if (msg instanceof Http3UnknownFrame) { | | 57 | + writeUnknownFrame(ctx, (Http3UnknownFrame) msg, promise); | 56 | 58 | } else { | 57 | 59 | unsupported(promise); | 58 | 60 | } | @@ -145,6 +147,30 @@ private static void writeFrameWithId(ChannelHandlerContext ctx, long type, long | 145 | 147 | ctx.write(out, promise); | 146 | 148 | } | 147 | 149 |
| | 150 | + private static void writeUnknownFrame( | | 151 | + ChannelHandlerContext ctx, Http3UnknownFrame frame, ChannelPromise promise) { | | 152 | + long type = frame.type(); | | 153 | + if (Http3CodecUtils.isReservedHttp2(type)) { | | 154 | + Http3Exception exception = new Http3Exception(Http3ErrorCode.H3_FRAME_UNEXPECTED, | | 155 | + "Reserved type for HTTP/2 send."); | | 156 | + promise.setFailure(exception); | | 157 | + // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-7.2.8 | | 158 | + Http3CodecUtils.connectionError(ctx, exception, false); | | 159 | + return; | | 160 | + } | | 161 | + if (!Http3CodecUtils.isReservedFrameType(type)) { | | 162 | + Http3Exception exception = new Http3Exception(Http3ErrorCode.H3_FRAME_UNEXPECTED, | | 163 | + "Non reserved type for HTTP/3 send."); | | 164 | + promise.setFailure(exception); | | 165 | + return; | | 166 | + } | | 167 | + ByteBuf out = ctx.alloc().directBuffer(); | | 168 | + writeVariableLengthInteger(out, type); | | 169 | + writeVariableLengthInteger(out, frame.content().readableBytes()); | | 170 | + ByteBuf content = frame.content().retain(); | | 171 | + ctx.write(Unpooled.wrappedUnmodifiableBuffer(out, content), promise); | | 172 | + } | | 173 | + | 148 | 174 | private static void unsupported(ChannelPromise promise) { | 149 | 175 | promise.setFailure(new UnsupportedOperationException()); | 150 | 176 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/io/netty/incubator/codec/http3/Http3FrameTypeValidationHandler.java +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,9 +34,13 @@ private T cast(Object msg) { | 34 | 34 | return (T) msg; | 35 | 35 | } | 36 | 36 |
| | 37 | + private boolean isValid(Object msg) { | | 38 | + return frameType.isInstance(msg); | | 39 | + } | | 40 | + | 37 | 41 | @Override | 38 | 42 | public final void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { | 39 | | - if (frameType.isInstance(msg)) { | | 43 | + if (isValid(msg)) { | 40 | 44 | write(ctx, cast(msg), promise); | 41 | 45 | } else { | 42 | 46 | frameTypeUnexpected(promise, msg); | @@ -49,7 +53,7 @@ public void write(ChannelHandlerContext ctx, T msg, ChannelPromise promise) thro | 49 | 53 |
| 50 | 54 | @Override | 51 | 55 | public final void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | 52 | | - if (frameType.isInstance(msg)) { | | 56 | + if (isValid(msg)) { | 53 | 57 | channelRead(ctx, cast(msg)); | 54 | 58 | } else { | 55 | 59 | frameTypeUnexpected(ctx, msg); | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/io/netty/incubator/codec/http3/Http3RequestStreamInboundHandler.java +49-10Lines changed: 49 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,31 +42,70 @@ public final boolean isSharable() { | 42 | 42 | public final void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | 43 | 43 | firstFrameReceived = true; | 44 | 44 | boolean inputShutdown = ((QuicStreamChannel) ctx.channel()).isInputShutdown(); | 45 | | - if (inputShutdown) { | 46 | | - lastFrameDetected = true; | | 45 | + if (msg instanceof Http3UnknownFrame) { | | 46 | + channelRead(ctx, (Http3UnknownFrame) msg); | | 47 | + if (inputShutdown) { | | 48 | + notifyLast(ctx); | | 49 | + } | | 50 | + } else { | | 51 | + if (inputShutdown) { | | 52 | + lastFrameDetected = true; | | 53 | + } | | 54 | + if (msg instanceof Http3HeadersFrame) { | | 55 | + channelRead(ctx, (Http3HeadersFrame) msg, inputShutdown); | | 56 | + } | | 57 | + if (msg instanceof Http3DataFrame) { | | 58 | + channelRead(ctx, (Http3DataFrame) msg, inputShutdown); | | 59 | + } | 47 | 60 | } | 48 | | - channelRead(ctx, (Http3RequestStreamFrame) msg, inputShutdown); | 49 | 61 | } | 50 | 62 |
| 51 | 63 | @Override | 52 | 64 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { | 53 | 65 | if (evt == ChannelInputShutdownEvent.INSTANCE) { | 54 | | - if (!lastFrameDetected && firstFrameReceived) { | 55 | | - lastFrameDetected = true; | 56 | | - channelRead(ctx, EMPTY, true); | 57 | | - } | | 66 | + notifyLast(ctx); | 58 | 67 | } | 59 | 68 | ctx.fireUserEventTriggered(evt); | 60 | 69 | } | 61 | 70 |
| | 71 | + private void notifyLast(ChannelHandlerContext ctx) throws Exception { | | 72 | + if (!lastFrameDetected && firstFrameReceived) { | | 73 | + lastFrameDetected = true; | | 74 | + channelRead(ctx, EMPTY, true); | | 75 | + } | | 76 | + } | | 77 | + | | 78 | + /** | | 79 | + * Called once a {@link Http3HeadersFrame} is ready for this stream to process. | | 80 | + * | | 81 | + * @param ctx the {@link ChannelHandlerContext} of this handler. | | 82 | + * @param frame the {@link Http3HeadersFrame} that was read | | 83 | + * @param isLast {@code true} if this is the last frame that will be read for this stream. | | 84 | + * @throws Exception thrown if an error happens during processing. | | 85 | + */ | | 86 | + protected abstract void channelRead(ChannelHandlerContext ctx, Http3HeadersFrame frame, boolean isLast) | | 87 | + throws Exception; | | 88 | + | 62 | 89 | /** | 63 | | - * Called once a {@link Http3RequestStreamFrame} is ready for this stream to process. | | 90 | + * Called once a {@link Http3DataFrame} is ready for this stream to process. | 64 | 91 | * | 65 | 92 | * @param ctx the {@link ChannelHandlerContext} of this handler. | 66 | | - * @param frame the {@link Http3RequestStreamFrame} that was read | | 93 | + * @param frame the {@link Http3DataFrame} that was read | 67 | 94 | * @param isLast {@code true} if this is the last frame that will be read for this stream. | 68 | 95 | * @throws Exception thrown if an error happens during processing. | 69 | 96 | */ | 70 | | - public abstract void channelRead(ChannelHandlerContext ctx, Http3RequestStreamFrame frame, boolean isLast) | | 97 | + protected abstract void channelRead(ChannelHandlerContext ctx, Http3DataFrame frame, boolean isLast) | 71 | 98 | throws Exception; | | 99 | + | | 100 | + /** | | 101 | + * Called once a {@link Http3UnknownFrame} is ready for this stream to process. By default these frames are just | | 102 | + * released and so dropped on the floor as stated in the RFC. That said you may want to override this method if | | 103 | + * you use some custom frames which are not part of the main spec. | | 104 | + * | | 105 | + * @param ctx the {@link ChannelHandlerContext} of this handler. | | 106 | + * @param frame the {@link Http3UnknownFrame} that was read | | 107 | + */ | | 108 | + protected void channelRead(@SuppressWarnings("unused") ChannelHandlerContext ctx, Http3UnknownFrame frame) { | | 109 | + frame.release(); | | 110 | + } | 72 | 111 | } |
|