Skip to content

Commit

Permalink
Cache Accept headers on HTTP 1.1 requests (netty#13824)
Browse files Browse the repository at this point in the history
Motivation:

The Accept header is frequently used on HTTP 1.1 requests: there's no
point to allocate a fresh new AsciiString while decoding HTTP requests
on them

Modification:

Add Accept caching while decoding HTTP 1.1 requests

Result:

Less garbage and faster HTTP request decoding
  • Loading branch information
franz1981 committed Feb 9, 2024
1 parent e82dc3a commit d5c72e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
*/
public class HttpRequestDecoder extends HttpObjectDecoder {

private static final AsciiString Accept = AsciiString.cached("Accept");
private static final AsciiString Host = AsciiString.cached("Host");
private static final AsciiString Connection = AsciiString.cached("Connection");
private static final AsciiString ContentType = AsciiString.cached("Content-Type");
Expand Down Expand Up @@ -118,6 +119,9 @@ public class HttpRequestDecoder extends HttpObjectDecoder {
private static final long LENGTH_AS_LONG = 'L' | 'e' << 8 | 'n' << 16 | 'g' << 24 |
(long) 't' << 32 | (long) 'h' << 40;

private static final long ACCEPT_AS_LONG = 'A' | 'c' << 8 | 'c' << 16 | 'e' << 24 |
(long) 'p' << 32 | (long) 't' << 40;

/**
* Creates a new instance with the default
* {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and
Expand Down Expand Up @@ -200,10 +204,14 @@ protected HttpMessage createMessage(String[] initialLine) throws Exception {
@Override
protected AsciiString splitHeaderName(final byte[] sb, final int start, final int length) {
final byte firstChar = sb[start];
if (firstChar == 'H' && length == 4) {
if (isHost(sb, start)) {
if (firstChar == 'H') {
if (length == 4 && isHost(sb, start)) {
return Host;
}
} else if (firstChar == 'A') {
if (length == 6 && isAccept(sb, start)) {
return Accept;
}
} else if (firstChar == 'C') {
if (length == 10) {
if (isConnection(sb, start)) {
Expand All @@ -222,6 +230,16 @@ protected AsciiString splitHeaderName(final byte[] sb, final int start, final in
return super.splitHeaderName(sb, start, length);
}

private static boolean isAccept(byte[] sb, int start) {
final long maybeAccept = sb[start] |
sb[start + 1] << 8 |
sb[start + 2] << 16 |
sb[start + 3] << 24 |
(long) sb[start + 4] << 32 |
(long) sb[start + 5] << 40;
return maybeAccept == ACCEPT_AS_LONG;
}

private static boolean isHost(byte[] sb, int start) {
final int maybeHost = sb[start] |
sb[start + 1] << 8 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private static byte[] createContent(String... lineDelimiters) {
"Upgrade: WebSocket" + lineDelimiter2 +
"Connection: Upgrade" + lineDelimiter +
"Host: localhost" + lineDelimiter2 +
"Accept: */*" + lineDelimiter +
"Origin: http://localhost:8080" + lineDelimiter +
"Sec-WebSocket-Key1: 10 28 8V7 8 48 0" + lineDelimiter2 +
"Sec-WebSocket-Key2: 8 Xt754O3Q3QW 0 _60" + lineDelimiter +
Expand Down Expand Up @@ -114,10 +115,11 @@ private static void testDecodeWholeRequestAtOnce(byte[] content, int maxHeaderSi
}

private static void checkHeaders(HttpHeaders headers) {
assertEquals(7, headers.names().size());
assertEquals(8, headers.names().size());
checkHeader(headers, "Upgrade", "WebSocket");
checkHeader(headers, "Connection", "Upgrade");
checkHeader(headers, "Host", "localhost");
checkHeader(headers, "Accept", "*/*");
checkHeader(headers, "Origin", "http://localhost:8080");
checkHeader(headers, "Sec-WebSocket-Key1", "10 28 8V7 8 48 0");
checkHeader(headers, "Sec-WebSocket-Key2", "8 Xt754O3Q3QW 0 _60");
Expand Down

0 comments on commit d5c72e2

Please sign in to comment.