GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Correctly encoder / decode and handle unknown HTTP/3 frames (#30) · chrisvest/netty@37153b4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • main/java/io/netty/incubator/codec/http3
      • DefaultHttp3UnknownFrame.java
      • Http3CodecUtils.java
      • Http3ControlStreamFrameDispatcher.java
      • Http3ControlStreamInboundHandler.java
      • Http3FrameDecoder.java
      • Http3FrameEncoder.java
      • Http3FrameTypeValidationHandler.java
      • Http3RequestStreamInboundHandler.java
      • Http3UnknownFrame.java
    • test/java/io/netty/incubator/codec/http3
      • Http3FrameEncoderDecoderTest.java
      • Http3RequestStreamInboundHandlerTest.java
      • Http3RequestStreamValidationHandlerTest.java
      • Http3ServerExample.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/main/java/io/netty/incubator/codec/http3/DefaultHttp3UnknownFrame.java‎

Copy file name to clipboard
+105Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

‎src/main/java/io/netty/incubator/codec/http3/Http3CodecUtils.java‎

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 numberDiff line numberDiff line change
@@ -20,12 +20,38 @@
2020
import io.netty.channel.ChannelHandlerContext;
2121
import io.netty.incubator.codec.quic.QuicChannel;
2222
import io.netty.util.CharsetUtil;
23+
import io.netty.util.internal.ObjectUtil;
2324

2425
final class Http3CodecUtils {
2526
static final long DEFAULT_MAX_HEADER_LIST_SIZE = 0xffffffffL;
2627

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+
2732
private Http3CodecUtils() { }
2833

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+
2955
/**
3056
* Returns the number of bytes needed to encode the variable length integer.
3157
*
Collapse file

‎src/main/java/io/netty/incubator/codec/http3/Http3ControlStreamFrameDispatcher.java‎

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 numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public boolean isSharable() {
4545
@Override
4646
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
4747
// 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)) {
4951
// write and flush as otherwise we may never flush the control stream for the write.
5052
localControlStream.writeAndFlush(msg).addListener(new ChannelPromiseNotifier(promise));
5153
return;
Collapse file

‎src/main/java/io/netty/incubator/codec/http3/Http3ControlStreamInboundHandler.java‎

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 numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public void channelRead(ChannelHandlerContext ctx, Http3ControlStreamFrame frame
8585
}
8686
} else if (frame instanceof Http3CancelPushFrame) {
8787
// TODO: implement me
88-
} else {
89-
// TODO: Do we need to do something to handle unknown frames
9088
}
9189

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.
9292
if (forwardControlFrames) {
9393
// The user did specify ChannelHandler that should be notified about control stream frames.
9494
// Let's forward the frame so the user can do something with it.
Collapse file

‎src/main/java/io/netty/incubator/codec/http3/Http3FrameDecoder.java‎

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 numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
6565
if (in.readableBytes() < typeLen) {
6666
return;
6767
}
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;
6976
if (!in.isReadable()) {
7077
return;
7178
}
@@ -88,6 +95,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
8895
this.payLoadLength = -1;
8996
try {
9097
if (type <= Integer.MAX_VALUE) {
98+
// See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-11.2.1
9199
switch ((int) type) {
92100
case 0x0:
93101
// DATA
@@ -136,8 +144,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
136144
out.add(new DefaultHttp3MaxPushIdFrame(readVariableLengthInteger(in, pidLen)));
137145
break;
138146
default:
147+
// TODO: Should we do validation here ?
139148
// Handling reserved frame types
140149
// https://tools.ietf.org/html/draft-ietf-quic-http-32#section-7.2.8
150+
out.add(new DefaultHttp3UnknownFrame(type, in.readRetainedSlice(payLoadLength)));
141151
break;
142152
}
143153
}
Collapse file

‎src/main/java/io/netty/incubator/codec/http3/Http3FrameEncoder.java‎

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 numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
5353
writeGoAwayFrame(ctx, (Http3GoAwayFrame) msg, promise);
5454
} else if (msg instanceof Http3MaxPushIdFrame) {
5555
writeMaxPushIdFrame(ctx, (Http3MaxPushIdFrame) msg, promise);
56+
} else if (msg instanceof Http3UnknownFrame) {
57+
writeUnknownFrame(ctx, (Http3UnknownFrame) msg, promise);
5658
} else {
5759
unsupported(promise);
5860
}
@@ -145,6 +147,30 @@ private static void writeFrameWithId(ChannelHandlerContext ctx, long type, long
145147
ctx.write(out, promise);
146148
}
147149

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+
148174
private static void unsupported(ChannelPromise promise) {
149175
promise.setFailure(new UnsupportedOperationException());
150176
}
Collapse file

‎src/main/java/io/netty/incubator/codec/http3/Http3FrameTypeValidationHandler.java‎

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 numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ private T cast(Object msg) {
3434
return (T) msg;
3535
}
3636

37+
private boolean isValid(Object msg) {
38+
return frameType.isInstance(msg);
39+
}
40+
3741
@Override
3842
public final void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
39-
if (frameType.isInstance(msg)) {
43+
if (isValid(msg)) {
4044
write(ctx, cast(msg), promise);
4145
} else {
4246
frameTypeUnexpected(promise, msg);
@@ -49,7 +53,7 @@ public void write(ChannelHandlerContext ctx, T msg, ChannelPromise promise) thro
4953

5054
@Override
5155
public final void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
52-
if (frameType.isInstance(msg)) {
56+
if (isValid(msg)) {
5357
channelRead(ctx, cast(msg));
5458
} else {
5559
frameTypeUnexpected(ctx, msg);
Collapse file

‎src/main/java/io/netty/incubator/codec/http3/Http3RequestStreamInboundHandler.java‎

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 numberDiff line numberDiff line change
@@ -42,31 +42,70 @@ public final boolean isSharable() {
4242
public final void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
4343
firstFrameReceived = true;
4444
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+
}
4760
}
48-
channelRead(ctx, (Http3RequestStreamFrame) msg, inputShutdown);
4961
}
5062

5163
@Override
5264
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
5365
if (evt == ChannelInputShutdownEvent.INSTANCE) {
54-
if (!lastFrameDetected && firstFrameReceived) {
55-
lastFrameDetected = true;
56-
channelRead(ctx, EMPTY, true);
57-
}
66+
notifyLast(ctx);
5867
}
5968
ctx.fireUserEventTriggered(evt);
6069
}
6170

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+
6289
/**
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.
6491
*
6592
* @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
6794
* @param isLast {@code true} if this is the last frame that will be read for this stream.
6895
* @throws Exception thrown if an error happens during processing.
6996
*/
70-
public abstract void channelRead(ChannelHandlerContext ctx, Http3RequestStreamFrame frame, boolean isLast)
97+
protected abstract void channelRead(ChannelHandlerContext ctx, Http3DataFrame frame, boolean isLast)
7198
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+
}
72111
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.