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 — [Refactor] Useful helper method getOrDefault & cleaner abstraction (#… · netty/netty@3e83495

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http3/src
    • main/java/io/netty/handler/codec/http3
      • Http3ClientConnectionHandler.java
      • Http3ConnectionHandler.java
      • Http3ControlStreamInboundHandler.java
      • Http3Settings.java
      • Http3SettingsFrame.java
      • QpackEncoderDynamicTable.java
    • test/java/io/netty/handler/codec/http3
      • DefaultHttp3SettingsFrameTest.java
      • Http3FrameCodecTest.java
      • Http3SettingsTest.java
      • QpackDecoderHandlerTest.java
      • QpackDecoderTest.java
      • QpackEncoderDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ClientConnectionHandler.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ClientConnectionHandler.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public Http3ClientConnectionHandler(@Nullable ChannelHandler inboundControlStrea
9090

9191
@Override
9292
void initBidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel channel) {
93-
// See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-6.1
93+
// See https://datatracker.ietf.org/doc/html/rfc9114#name-bidirectional-streams
94+
// https://datatracker.ietf.org/doc/html/rfc9114#section-6.1-3
9495
Http3CodecUtils.connectionError(ctx, Http3ErrorCode.H3_STREAM_CREATION_ERROR,
9596
"Server initiated bidirectional streams are not allowed", true);
9697
}
Collapse file

‎codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ConnectionHandler.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ConnectionHandler.java
+21-9Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@
2727
import java.util.function.LongFunction;
2828

2929
import static io.netty.handler.codec.http3.Http3RequestStreamCodecState.NO_STATE;
30-
import static io.netty.handler.codec.http3.Http3SettingsFrame.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS;
31-
import static io.netty.handler.codec.http3.Http3SettingsFrame.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY;
3230
import static java.lang.Math.toIntExact;
3331

3432
/**
35-
* Handler that handles <a href="https://tools.ietf.org/html/draft-ietf-quic-http-32">HTTP3</a> connections.
33+
* Handler that handles <a href="https://datatracker.ietf.org/doc/html/rfc9114">HTTP3</a> connections.
3634
*/
3735
public abstract class Http3ConnectionHandler implements ChannelInboundHandler {
3836
final Http3FrameCodecFactory codecFactory;
@@ -76,10 +74,24 @@ public abstract class Http3ConnectionHandler implements ChannelInboundHandler {
7674
} else {
7775
localSettings = DefaultHttp3SettingsFrame.copyOf(localSettings);
7876
}
79-
Long maxFieldSectionSize = localSettings.getOrDefault(Http3SettingsFrame.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE,
80-
Http3CodecUtils.DEFAULT_MAX_FIELD_SECTION_SIZE);
81-
this.maxTableCapacity = localSettings.getOrDefault(HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0);
82-
int maxBlockedStreams = toIntExact(localSettings.getOrDefault(HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS, 0));
77+
Long maxFieldSectionSize = localSettings
78+
.settings()
79+
.getOrDefault(
80+
Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id(),
81+
Http3CodecUtils.DEFAULT_MAX_FIELD_SECTION_SIZE
82+
);
83+
this.maxTableCapacity = localSettings
84+
.settings()
85+
.getOrDefault(
86+
Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(),
87+
0L
88+
);
89+
int maxBlockedStreams = toIntExact(localSettings
90+
.settings()
91+
.getOrDefault(
92+
Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(),
93+
0L)
94+
);
8395
qpackDecoder = new QpackDecoder(maxTableCapacity, maxBlockedStreams);
8496
qpackEncoder = new QpackEncoder();
8597
codecFactory = Http3FrameCodec.newFactory(qpackDecoder, maxFieldSectionSize, qpackEncoder);
@@ -94,9 +106,9 @@ private void createControlStreamIfNeeded(ChannelHandlerContext ctx) {
94106
if (!controlStreamCreationInProgress && Http3.getLocalControlStream(ctx.channel()) == null) {
95107
controlStreamCreationInProgress = true;
96108
QuicChannel channel = (QuicChannel) ctx.channel();
97-
// Once the channel became active we need to create an unidirectional stream and write the
109+
// Once the channel became active we need to create a unidirectional stream and write the
98110
// Http3SettingsFrame to it. This needs to be the first frame on this stream.
99-
// https://tools.ietf.org/html/draft-ietf-quic-http-32#section-6.2.1.
111+
// https://datatracker.ietf.org/doc/html/rfc9114#name-control-streams
100112
channel.createStream(QuicStreamType.UNIDIRECTIONAL, remoteControlStreamHandler)
101113
.addListener(f -> {
102114
if (!f.isSuccess()) {
Collapse file

‎codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ControlStreamInboundHandler.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3ControlStreamInboundHandler.java
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
import static io.netty.handler.codec.http3.Http3ErrorCode.H3_ID_ERROR;
3838
import static io.netty.handler.codec.http3.Http3ErrorCode.H3_MISSING_SETTINGS;
3939
import static io.netty.handler.codec.http3.Http3ErrorCode.QPACK_ENCODER_STREAM_ERROR;
40-
import static io.netty.handler.codec.http3.Http3SettingsFrame.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS;
41-
import static io.netty.handler.codec.http3.Http3SettingsFrame.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY;
4240
import static io.netty.handler.codec.http3.QpackUtil.toIntOrThrow;
4341
import static io.netty.util.internal.ThrowableUtil.unknownStackTrace;
4442

@@ -150,8 +148,20 @@ private boolean handleHttp3SettingsFrame(ChannelHandlerContext ctx, Http3Setting
150148
}
151149
quicChannel.createStream(QuicStreamType.UNIDIRECTIONAL,
152150
new QPackEncoderStreamInitializer(qpackEncoder, qpackAttributes,
153-
settingsFrame.getOrDefault(HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY, 0),
154-
settingsFrame.getOrDefault(HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS, 0)))
151+
settingsFrame
152+
.settings()
153+
.getOrDefault(
154+
Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(),
155+
0L
156+
),
157+
settingsFrame
158+
.settings()
159+
.getOrDefault(
160+
Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(),
161+
0L
162+
)
163+
)
164+
)
155165
.addListener(closeOnFailure);
156166
quicChannel.createStream(QuicStreamType.UNIDIRECTIONAL, new QPackDecoderStreamInitializer(qpackAttributes))
157167
.addListener(closeOnFailure);
Collapse file

‎codec-http3/src/main/java/io/netty/handler/codec/http3/Http3Settings.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3Settings.java
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ public Long get(long key) {
114114
return settings.get(key);
115115
}
116116

117+
/**
118+
* Returns the value associated with the specified setting identifier,
119+
* or {@code defaultValue} if no value is present for the given key.
120+
*
121+
* @param key the numeric setting identifier
122+
* @param defaultValue the value to return if the setting is not present
123+
* @return the configured value for the specified key, or {@code defaultValue},
124+
* if the key is not found
125+
*/
126+
public long getOrDefault(long key, long defaultValue) {
127+
return settings.getOrDefault(key, defaultValue);
128+
}
129+
117130
/**
118131
* Returns the {@code QPACK_MAX_TABLE_CAPACITY} value.
119132
*
Collapse file

‎codec-http3/src/main/java/io/netty/handler/codec/http3/Http3SettingsFrame.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3SettingsFrame.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ default Long get(long key) {
9191
*/
9292
@Deprecated
9393
default Long getOrDefault(long key, long defaultValue) {
94-
final Long val = get(key);
95-
return val == null ? defaultValue : val;
94+
return settings().getOrDefault(key, defaultValue);
9695
}
9796

9897
/**
Collapse file

‎codec-http3/src/main/java/io/netty/handler/codec/http3/QpackEncoderDynamicTable.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/QpackEncoderDynamicTable.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ final class QpackEncoderDynamicTable {
6969
/**
7070
* <a href="https://www.rfc-editor.org/rfc/rfc9204.html#name-maximum-dynamic-table-capac">
7171
* Maximum capacity of the table</a>. This is set once based on the
72-
* {@link Http3SettingsFrame#HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY} received by the remote peer.
72+
* {@link Http3SettingIdentifier#HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY} received by the remote peer.
7373
*/
7474
private long maxTableCapacity = -1;
7575

Collapse file

‎codec-http3/src/test/java/io/netty/handler/codec/http3/DefaultHttp3SettingsFrameTest.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/test/java/io/netty/handler/codec/http3/DefaultHttp3SettingsFrameTest.java
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,25 @@ void testDefaultFrameHasEmptySettings() {
4141

4242
assertNotNull(frame.settings());
4343
assertFalse(frame.iterator().hasNext());
44+
assertNull(frame.settings().qpackMaxTableCapacity());
4445
assertNull(frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id()));
4546
}
4647

4748
@Test
4849
void testPutAndGetDelegatesToSettings() {
4950
DefaultHttp3SettingsFrame frame = new DefaultHttp3SettingsFrame();
5051

51-
frame.put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(), 256L);
52-
frame.put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(), 8L);
53-
52+
frame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(), 256L);
5453
assertEquals(256L, frame.settings().qpackMaxTableCapacity());
54+
55+
frame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(), 8L);
5556
assertEquals(8L, frame.settings().qpackBlockedStreams());
56-
assertEquals(8L, frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id()));
57+
58+
frame.put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(), 512L);
59+
assertEquals(512L, frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id()));
60+
61+
frame.put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(), 16L);
62+
assertEquals(16L, frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id()));
5763
}
5864

5965
@Test
@@ -73,6 +79,7 @@ void testSettingsReferenceIsRetained() {
7379
// Modify settings externally and verify frame sees update
7480
settings.qpackBlockedStreams(3);
7581
assertEquals(3L, frame.get(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id()));
82+
assertEquals(3L, frame.settings().qpackBlockedStreams());
7683
}
7784

7885
@Test
Collapse file

‎codec-http3/src/test/java/io/netty/handler/codec/http3/Http3FrameCodecTest.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/test/java/io/netty/handler/codec/http3/Http3FrameCodecTest.java
+18-14Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ private void setUp(int maxBlockedStreams, boolean delayQpackStreams) throws Exce
9797
parent = new EmbeddedQuicChannel(true);
9898
qpackAttributes = new QpackAttributes(parent, false);
9999
Http3.setQpackAttributes(parent, qpackAttributes);
100-
final Http3SettingsFrame settings = new DefaultHttp3SettingsFrame();
100+
final Http3SettingsFrame http3SettingsFrame = new DefaultHttp3SettingsFrame();
101101
maxTableCapacity = 1024L;
102-
settings.put(Http3SettingsFrame.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY, maxTableCapacity);
103-
settings.put(Http3SettingsFrame.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS, (long) maxBlockedStreams);
102+
http3SettingsFrame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(),
103+
maxTableCapacity);
104+
http3SettingsFrame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(),
105+
(long) maxBlockedStreams);
104106
decoder = new QpackDecoder(maxTableCapacity, maxBlockedStreams);
105107
decoder.setDynamicTableCapacity(maxTableCapacity);
106108
qpackEncoderHandler = new QpackEncoderHandler(maxTableCapacity, decoder);
@@ -272,17 +274,17 @@ public void testHttp3SettingsFrame(
272274
boolean fragmented, int maxBlockedStreams, boolean delayQpackStreams) throws Exception {
273275
setUp(maxBlockedStreams, delayQpackStreams);
274276
Http3SettingsFrame settingsFrame = new DefaultHttp3SettingsFrame();
275-
settingsFrame.put(Http3SettingsFrame.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY, 100L);
276-
settingsFrame.put(Http3SettingsFrame.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS, 1L);
277-
settingsFrame.put(Http3SettingsFrame.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE, 128L);
278-
settingsFrame.put(Http3SettingsFrame.HTTP3_SETTINGS_ENABLE_CONNECT_PROTOCOL, 0L);
279-
settingsFrame.put(Http3SettingIdentifier.HTTP3_SETTINGS_H3_DATAGRAM.id(), 1L);
277+
settingsFrame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(), 100L);
278+
settingsFrame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_BLOCKED_STREAMS.id(), 1L);
279+
settingsFrame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id(), 128L);
280+
settingsFrame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_ENABLE_CONNECT_PROTOCOL.id(), 0L);
281+
settingsFrame.settings().put(Http3SettingIdentifier.HTTP3_SETTINGS_H3_DATAGRAM.id(), 1L);
280282
// Ensure we can encode and decode all sizes correctly.
281283
// unknown settings id/key will be ignored
282-
settingsFrame.put(63, 63L);
283-
settingsFrame.put(16383, 16383L);
284-
settingsFrame.put(1073741823, 1073741823L);
285-
settingsFrame.put(4611686018427387903L, 4611686018427387903L);
284+
settingsFrame.settings().put(63, 63L);
285+
settingsFrame.settings().put(16383, 16383L);
286+
settingsFrame.settings().put(1073741823, 1073741823L);
287+
settingsFrame.settings().put(4611686018427387903L, 4611686018427387903L);
286288
testFrameEncodedAndDecoded(
287289
fragmented, maxBlockedStreams, delayQpackStreams, settingsFrame);
288290
}
@@ -530,9 +532,11 @@ public void testDecodeSettingsWithSameKey(int maxBlockedStreams, boolean delayQp
530532
Http3CodecUtils.writeVariableLengthInteger(buffer, Http3CodecUtils.HTTP3_SETTINGS_FRAME_TYPE);
531533
Http3CodecUtils.writeVariableLengthInteger(buffer, 4);
532534
// Write the key and some random value... Both should be only 1 byte long each.
533-
Http3CodecUtils.writeVariableLengthInteger(buffer, Http3SettingsFrame.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE);
535+
Http3CodecUtils.writeVariableLengthInteger(buffer,
536+
Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id());
534537
Http3CodecUtils.writeVariableLengthInteger(buffer, 1);
535-
Http3CodecUtils.writeVariableLengthInteger(buffer, Http3SettingsFrame.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE);
538+
Http3CodecUtils.writeVariableLengthInteger(buffer,
539+
Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id());
536540
Http3CodecUtils.writeVariableLengthInteger(buffer, 1);
537541

538542
testDecodeInvalidSettings(delayQpackStreams, buffer);
Collapse file

‎codec-http3/src/test/java/io/netty/handler/codec/http3/Http3SettingsTest.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/test/java/io/netty/handler/codec/http3/Http3SettingsTest.java
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ void testIteratorProvidesEntries() {
106106
assertTrue(count.get() == 2);
107107
}
108108

109+
@Test
110+
void testDefaultGet() {
111+
Http3Settings settings = new Http3Settings();
112+
113+
assertNull(settings.get(Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id()));
114+
115+
assertEquals(Long.MAX_VALUE, settings
116+
.getOrDefault(
117+
Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id(),
118+
Long.MAX_VALUE)
119+
);
120+
assertEquals(Long.MIN_VALUE, settings
121+
.getOrDefault(
122+
Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id(),
123+
Long.MIN_VALUE)
124+
);
125+
126+
assertNull(settings.put(Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id(), 1000L));
127+
128+
assertEquals(1000L, settings.get(Http3SettingIdentifier.HTTP3_SETTINGS_MAX_FIELD_SECTION_SIZE.id()));
129+
}
130+
109131
@Test
110132
void testForEachConsumer() {
111133
Http3Settings settings = new Http3Settings()
Collapse file

‎codec-http3/src/test/java/io/netty/handler/codec/http3/QpackDecoderHandlerTest.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/test/java/io/netty/handler/codec/http3/QpackDecoderHandlerTest.java
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import static io.netty.handler.codec.http3.Http3.setQpackAttributes;
2626
import static io.netty.handler.codec.http3.Http3ErrorCode.QPACK_DECODER_STREAM_ERROR;
27-
import static io.netty.handler.codec.http3.Http3SettingsFrame.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY;
27+
import static io.netty.handler.codec.http3.Http3SettingIdentifier.HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY;
2828
import static io.netty.handler.codec.http3.QpackUtil.encodePrefixedInteger;
2929
import static org.hamcrest.MatcherAssert.assertThat;
3030
import static org.hamcrest.Matchers.instanceOf;
@@ -327,8 +327,8 @@ private void setup(long maxTableCapacity) throws Exception {
327327
parent = new EmbeddedQuicChannel(true);
328328
attributes = new QpackAttributes(parent, false);
329329
setQpackAttributes(parent, attributes);
330-
Http3SettingsFrame settings = new DefaultHttp3SettingsFrame();
331-
settings.put(HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY, maxTableCapacity);
330+
Http3SettingsFrame settingsFrame = new DefaultHttp3SettingsFrame();
331+
settingsFrame.settings().put(HTTP3_SETTINGS_QPACK_MAX_TABLE_CAPACITY.id(), maxTableCapacity);
332332
QpackDecoder decoder = new QpackDecoder(maxTableCapacity, 0);
333333
encoderStream = (EmbeddedQuicStreamChannel) parent.createStream(QuicStreamType.UNIDIRECTIONAL,
334334
new QpackEncoderHandler(maxTableCapacity, decoder)).get();

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.