[PATCH 3 of 4] Updated request line parsing to use if() in IP literals

Maxim Dounin mdounin at mdounin.ru
Mon Aug 18 23:51:35 UTC 2025


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1755535220 -10800
#      Mon Aug 18 19:40:20 2025 +0300
# Node ID fe761d14f0f28e37f1bb81c7cfb3464df68d2f84
# Parent  097c394fa0fd14169bbe054c469886213cbaabad
Updated request line parsing to use if() in IP literals.

Using if() results in more readable code and matches the code used in host
parsing, where it is not convenient to use switch().  Besides, it is slightly
faster than switch() on typical inputs.

diff --git a/src/http/ngx_http_parse.c b/src/http/ngx_http_parse.c
--- a/src/http/ngx_http_parse.c
+++ b/src/http/ngx_http_parse.c
@@ -450,34 +450,23 @@ ngx_http_parse_request_line(ngx_http_req
                 break;
             }
 
+            if (ch == ':') {
+                break;
+            }
+
+            if (ch == '.' || ch == '-' || ch == '_' || ch == '~'
+                || ch == '!' || ch == '$' || ch == '&' || ch == '\''
+                || ch == '(' || ch == ')' || ch == '*' || ch == '+'
+                || ch == ',' || ch == ';' || ch == '=' || ch == '%')
+            {
+                /* unreserved, sub-delims, pct-encoded */
+                break;
+            }
+
             switch (ch) {
-            case ':':
-                break;
             case ']':
                 state = sw_host_end;
                 break;
-            case '-':
-            case '.':
-            case '_':
-            case '~':
-                /* unreserved */
-                break;
-            case '!':
-            case '$':
-            case '&':
-            case '\'':
-            case '(':
-            case ')':
-            case '*':
-            case '+':
-            case ',':
-            case ';':
-            case '=':
-                /* sub-delims */
-                break;
-            case '%':
-                /* pct-encoded */
-                break;
             default:
                 return NGX_HTTP_PARSE_INVALID_REQUEST;
             }



More information about the nginx-devel mailing list