HTTP parser: slightly improved readability of code.

As suggested by Igor Sysoev.
This commit is contained in:
Valentin Bartenev
2017-12-08 19:18:00 +03:00
parent 4be9774540
commit 20d720dfc5

View File

@@ -89,41 +89,36 @@ nxt_http_parse_target(u_char **pos, u_char *end)
p = *pos; p = *pos;
for ( ;; ) { while (nxt_fast_path(end - p >= 10)) {
if (nxt_slow_path(end - p < 10)) {
return NXT_HTTP_TARGET_AGAIN; #define nxt_target_test_char(ch) \
\
trap = nxt_http_target_chars[ch]; \
\
if (nxt_slow_path(trap != 0)) { \
*pos = &(ch); \
return trap; \
} }
#define nxt_http_parse_target_step \ /* enddef */
{ \
trap = nxt_http_target_chars[*p]; \
\
if (nxt_slow_path(trap != 0)) { \
break; \
} \
\
p++; \
}
nxt_http_parse_target_step nxt_target_test_char(p[0]);
nxt_http_parse_target_step nxt_target_test_char(p[1]);
nxt_http_parse_target_step nxt_target_test_char(p[2]);
nxt_http_parse_target_step nxt_target_test_char(p[3]);
nxt_http_parse_target_step nxt_target_test_char(p[4]);
nxt_http_parse_target_step nxt_target_test_char(p[5]);
nxt_http_parse_target_step nxt_target_test_char(p[6]);
nxt_http_parse_target_step nxt_target_test_char(p[7]);
nxt_http_parse_target_step nxt_target_test_char(p[8]);
nxt_http_parse_target_step nxt_target_test_char(p[9]);
#undef nxt_http_parse_target_step p += 10;
} }
*pos = p; return NXT_HTTP_TARGET_AGAIN;
return trap;
} }
@@ -160,48 +155,51 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
rp->method.start = p; rp->method.start = p;
for ( ;; p++) { for ( ;; ) {
for ( ;; ) { while (nxt_fast_path(end - p >= 8)) {
if (nxt_slow_path(end - p < 12)) {
return NXT_AGAIN; #define nxt_method_test_char(ch) \
\
if (nxt_slow_path((ch) < 'A' || (ch) > 'Z')) { \
p = &(ch); \
goto method_unusual_char; \
} }
#define nxt_http_parse_request_line_step \ /* enddef */
{ \
ch = *p; \
\
if (nxt_slow_path(ch < 'A' || ch > 'Z')) { \
break; \
} \
\
p++; \
}
nxt_http_parse_request_line_step nxt_method_test_char(p[0]);
nxt_http_parse_request_line_step nxt_method_test_char(p[1]);
nxt_http_parse_request_line_step nxt_method_test_char(p[2]);
nxt_http_parse_request_line_step nxt_method_test_char(p[3]);
nxt_http_parse_request_line_step nxt_method_test_char(p[4]);
nxt_http_parse_request_line_step nxt_method_test_char(p[5]);
nxt_http_parse_request_line_step nxt_method_test_char(p[6]);
nxt_http_parse_request_line_step nxt_method_test_char(p[7]);
#undef nxt_http_parse_request_line_step p += 8;
} }
return NXT_AGAIN;
method_unusual_char:
ch = *p;
if (nxt_fast_path(ch == ' ')) { if (nxt_fast_path(ch == ' ')) {
rp->method.length = p - rp->method.start; rp->method.length = p - rp->method.start;
break; break;
} }
if (ch == '_' || ch == '-') { if (ch == '_' || ch == '-') {
p++;
continue; continue;
} }
if (rp->method.start == p && (ch == NXT_CR || ch == NXT_LF)) { if (rp->method.start == p && (ch == NXT_CR || ch == NXT_LF)) {
rp->method.start++; rp->method.start++;
p++;
continue; continue;
} }
@@ -461,8 +459,10 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
size = end - p; size = end - p;
i = rp->field_name.length; i = rp->field_name.length;
#define nxt_http_parse_field_name_step \ while (nxt_fast_path(size - i >= 8)) {
{ \
#define nxt_field_name_test_char(i) \
\
ch = p[i]; \ ch = p[i]; \
c = normal[ch]; \ c = normal[ch]; \
\ \
@@ -470,28 +470,25 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
goto name_end; \ goto name_end; \
} \ } \
\ \
rp->field_key.str[i % 32] = c; \ rp->field_key.str[i % 32] = c;
i++; \
}
while (nxt_fast_path(size - i >= 8)) { /* enddef */
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step
nxt_http_parse_field_name_step nxt_field_name_test_char(i); i++;
nxt_http_parse_field_name_step nxt_field_name_test_char(i); i++;
nxt_http_parse_field_name_step nxt_field_name_test_char(i); i++;
nxt_http_parse_field_name_step nxt_field_name_test_char(i); i++;
nxt_field_name_test_char(i); i++;
nxt_field_name_test_char(i); i++;
nxt_field_name_test_char(i); i++;
nxt_field_name_test_char(i); i++;
} }
while (nxt_fast_path(i != size)) { while (nxt_fast_path(i != size)) {
nxt_http_parse_field_name_step nxt_field_name_test_char(i); i++;
} }
#undef nxt_http_parse_field_name_step
rp->field_name.length = i; rp->field_name.length = i;
rp->handler = &nxt_http_parse_field_name; rp->handler = &nxt_http_parse_field_name;
@@ -584,55 +581,58 @@ nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos,
static u_char * static u_char *
nxt_http_lookup_field_end(u_char *p, u_char *end) nxt_http_lookup_field_end(u_char *p, u_char *end)
{ {
nxt_uint_t n; while (nxt_fast_path(end - p >= 16)) {
#define nxt_http_lookup_field_end_step \ #define nxt_field_end_test_char(ch) \
{ \
if (nxt_slow_path(*p < 0x10)) { \
return p; \
} \
\ \
p++; \ if (nxt_slow_path((ch) < 0x10)) { \
return &(ch); \
}
/* enddef */
nxt_field_end_test_char(p[0]);
nxt_field_end_test_char(p[1]);
nxt_field_end_test_char(p[2]);
nxt_field_end_test_char(p[3]);
nxt_field_end_test_char(p[4]);
nxt_field_end_test_char(p[5]);
nxt_field_end_test_char(p[6]);
nxt_field_end_test_char(p[7]);
nxt_field_end_test_char(p[8]);
nxt_field_end_test_char(p[9]);
nxt_field_end_test_char(p[10]);
nxt_field_end_test_char(p[11]);
nxt_field_end_test_char(p[12]);
nxt_field_end_test_char(p[13]);
nxt_field_end_test_char(p[14]);
nxt_field_end_test_char(p[15]);
p += 16;
} }
for (n = (end - p) / 16; nxt_fast_path(n != 0); n--) { while (nxt_fast_path(end - p >= 4)) {
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step nxt_field_end_test_char(p[0]);
nxt_http_lookup_field_end_step nxt_field_end_test_char(p[1]);
nxt_http_lookup_field_end_step nxt_field_end_test_char(p[2]);
nxt_http_lookup_field_end_step nxt_field_end_test_char(p[3]);
nxt_http_lookup_field_end_step p += 4;
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
}
for (n = (end - p) / 4; nxt_fast_path(n != 0); n--) {
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
nxt_http_lookup_field_end_step
} }
switch (end - p) { switch (end - p) {
case 3: case 3:
nxt_http_lookup_field_end_step nxt_field_end_test_char(*p); p++;
/* Fall through. */ /* Fall through. */
case 2: case 2:
nxt_http_lookup_field_end_step nxt_field_end_test_char(*p); p++;
/* Fall through. */ /* Fall through. */
case 1: case 1:
nxt_http_lookup_field_end_step nxt_field_end_test_char(*p); p++;
/* Fall through. */ /* Fall through. */
case 0: case 0:
break; break;
@@ -640,8 +640,6 @@ nxt_http_lookup_field_end(u_char *p, u_char *end)
nxt_unreachable(); nxt_unreachable();
} }
#undef nxt_http_lookup_field_end_step
return p; return p;
} }