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 — Replace chunk extension Match[] state machine with flat transition table by franz1981 · Pull Request #16542 · netty/netty

Replace chunk extension Match[] state machine with flat transition table #16542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
franz1981 wants to merge 4 commits into netty:4.2
base: 4.2
Loading
Loading
from franz1981:4.2_16540

Uh oh!

There was an error while loading. Please reload this page.

Open

Replace chunk extension Match[] state machine with flat transition table #16542

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
97a0b79
Replace chunk extension Match[] state machine with flat transition table
franz1981 Apr 21, 2026
dc6516e
Add isolated benchmark for chunk extension validation loop
franz1981 Apr 21, 2026
b49729a
Merge branch '4.2' into 4.2_16540
franz1981 May 12, 2026
70e5cb6
Merge branch '4.2' into 4.2_16540
normanmaurer Jun 15, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

77 changes: 41 additions & 36 deletions 77 ...-http/src/main/java/io/netty/handler/codec/http/HttpChunkLineValidatingByteProcessor.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.netty.util.ByteProcessor;

import java.util.Arrays;
import java.util.BitSet;

/**
Expand Down Expand Up @@ -53,13 +54,16 @@
* }</pre>
*/
final class HttpChunkLineValidatingByteProcessor implements ByteProcessor {
// Valid-at-finish states first; SIZE = 0 so default field value is the initial state
private static final int SIZE = 0;
private static final int CHUNK_EXT_NAME = 1;
private static final int CHUNK_EXT_VAL_START = 2;
private static final int CHUNK_EXT_VAL_QUOTED = 3;
private static final int CHUNK_EXT_VAL_QUOTED_ESCAPE = 4;
private static final int CHUNK_EXT_VAL_QUOTED_END = 5;
private static final int CHUNK_EXT_VAL_TOKEN = 6;
private static final int CHUNK_EXT_VAL_QUOTED_END = 2;
private static final int CHUNK_EXT_VAL_TOKEN = 3;
private static final int FIRST_INVALID_FINISH_STATE = 4;
// Invalid-at-finish states grouped at the end so finish() is a single range check
private static final int CHUNK_EXT_VAL_START = 4;
private static final int CHUNK_EXT_VAL_QUOTED = 5;
private static final int CHUNK_EXT_VAL_QUOTED_ESCAPE = 6;

static final class Match extends BitSet {
private static final long serialVersionUID = 49522994383099834L;
Expand Down Expand Up @@ -103,6 +107,14 @@ private enum State {
.chars(" \t")
.chars("(),/:<=>?@[\\]{}", false),
new Match(CHUNK_EXT_VAL_START).chars("=")),
ChunkExtValQuotedEnd(
new Match(CHUNK_EXT_VAL_QUOTED_END).chars("\t "),
new Match(CHUNK_EXT_NAME).chars(";")),
ChunkExtValToken(
new Match(CHUNK_EXT_VAL_TOKEN)
.range(0x21, 0x7E, true)
.chars("(),/:<=>?@[\\]{};", false),
new Match(CHUNK_EXT_NAME).chars(";")),
ChunkExtValStart(
new Match(CHUNK_EXT_VAL_START).chars(" \t"),
new Match(CHUNK_EXT_VAL_QUOTED).chars("\""),
Expand All @@ -122,57 +134,50 @@ private enum State {
.chars("\t ")
.range(0x21, 0x7E)
.range(0x80, 0xFF)),
ChunkExtValQuotedEnd(
new Match(CHUNK_EXT_VAL_QUOTED_END).chars("\t "),
new Match(CHUNK_EXT_NAME).chars(";")),
ChunkExtValToken(
new Match(CHUNK_EXT_VAL_TOKEN)
.range(0x21, 0x7E, true)
.chars("(),/:<=>?@[\\]{};", false),
new Match(CHUNK_EXT_NAME).chars(";")),
;

private final Match[] matches;

State(Match... matches) {
this.matches = matches;
}
}

State match(byte value) {
for (Match match : matches) {
if (match.get(value)) {
return STATES_BY_ORDINAL[match.then];
private static final byte[] TRANSITIONS = buildTable();

private static byte[] buildTable() {
Comment thread

@normanmaurer normanmaurer Jun 15, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@franz1981 can you add some comments to better explain how all this works ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
State[] states = State.values();
byte[] table = new byte[states.length * 256];
Arrays.fill(table, (byte) -1);
for (State state : states) {
int base = state.ordinal() << 8;
for (Match m : state.matches) {
for (int i = m.nextSetBit(0); i >= 0; i = m.nextSetBit(i + 1)) {
table[base | i] = (byte) m.then;
}
}
if (this == Size) {
throw new NumberFormatException("Invalid chunk size");
} else {
throw new InvalidChunkExtensionException("Invalid chunk extension");
}
}
return table;
}

private static final State[] STATES_BY_ORDINAL = State.values();

private State state = State.Size;
int state;

@Override
public boolean process(byte value) {
state = state.match(value);
int next = TRANSITIONS[state << 8 | (value & 0xFF)];
Comment thread

@normanmaurer normanmaurer Jun 15, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some comments related to the bitshifting etc ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
if (next < 0) {
if (state == SIZE) {
throw new NumberFormatException("Invalid chunk size");
}
throw new InvalidChunkExtensionException("Invalid chunk extension");
}
state = next;
return true;
}

public void finish() {
switch (state) {
case ChunkExtValQuoted:
case ChunkExtValQuotedEscape:
case ChunkExtValStart:
throw new InvalidChunkExtensionException("Invalid chunk extension");
if (state >= FIRST_INVALID_FINISH_STATE) {
throw new InvalidChunkExtensionException("Invalid chunk extension");
}
// Exhaustiveness check
assert state == State.Size ||
state == State.ChunkExtName ||
state == State.ChunkExtValQuotedEnd ||
state == State.ChunkExtValToken;
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.