summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_http_parse.c
diff options
context:
space:
mode:
authorValentin Bartenev <vbart@nginx.com>2017-12-08 19:18:00 +0300
committerValentin Bartenev <vbart@nginx.com>2017-12-08 19:18:00 +0300
commit20d720dfc57c16d18bc2db5e61955d3d964e0163 (patch)
treea1b53b875690fc4882c2f14645740e16b2c53e68 /src/nxt_http_parse.c
parent4be977454097b154b539b9f82a35e0cae7a59168 (diff)
downloadunit-20d720dfc57c16d18bc2db5e61955d3d964e0163.tar.gz
unit-20d720dfc57c16d18bc2db5e61955d3d964e0163.tar.bz2
HTTP parser: slightly improved readability of code.
As suggested by Igor Sysoev.
Diffstat (limited to 'src/nxt_http_parse.c')
-rw-r--r--src/nxt_http_parse.c204
1 files changed, 101 insertions, 103 deletions
diff --git a/src/nxt_http_parse.c b/src/nxt_http_parse.c
index 946c8da2..d826c536 100644
--- a/src/nxt_http_parse.c
+++ b/src/nxt_http_parse.c
@@ -89,41 +89,36 @@ nxt_http_parse_target(u_char **pos, u_char *end)
p = *pos;
- for ( ;; ) {
- if (nxt_slow_path(end - p < 10)) {
- return NXT_HTTP_TARGET_AGAIN;
- }
+ while (nxt_fast_path(end - p >= 10)) {
-#define nxt_http_parse_target_step \
- { \
- trap = nxt_http_target_chars[*p]; \
+#define nxt_target_test_char(ch) \
\
- if (nxt_slow_path(trap != 0)) { \
- break; \
- } \
+ trap = nxt_http_target_chars[ch]; \
\
- p++; \
+ if (nxt_slow_path(trap != 0)) { \
+ *pos = &(ch); \
+ return trap; \
}
- nxt_http_parse_target_step
- nxt_http_parse_target_step
- nxt_http_parse_target_step
- nxt_http_parse_target_step
+/* enddef */
- nxt_http_parse_target_step
- nxt_http_parse_target_step
- nxt_http_parse_target_step
- nxt_http_parse_target_step
+ nxt_target_test_char(p[0]);
+ nxt_target_test_char(p[1]);
+ nxt_target_test_char(p[2]);
+ nxt_target_test_char(p[3]);
- nxt_http_parse_target_step
- nxt_http_parse_target_step
+ nxt_target_test_char(p[4]);
+ nxt_target_test_char(p[5]);
+ nxt_target_test_char(p[6]);
+ nxt_target_test_char(p[7]);
-#undef nxt_http_parse_target_step
- }
+ nxt_target_test_char(p[8]);
+ nxt_target_test_char(p[9]);
- *pos = p;
+ p += 10;
+ }
- return trap;
+ return NXT_HTTP_TARGET_AGAIN;
}
@@ -160,48 +155,51 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
rp->method.start = p;
- for ( ;; p++) {
+ for ( ;; ) {
- for ( ;; ) {
- if (nxt_slow_path(end - p < 12)) {
- return NXT_AGAIN;
- }
+ while (nxt_fast_path(end - p >= 8)) {
-#define nxt_http_parse_request_line_step \
- { \
- ch = *p; \
- \
- if (nxt_slow_path(ch < 'A' || ch > 'Z')) { \
- break; \
- } \
+#define nxt_method_test_char(ch) \
\
- p++; \
+ if (nxt_slow_path((ch) < 'A' || (ch) > 'Z')) { \
+ p = &(ch); \
+ goto method_unusual_char; \
}
- nxt_http_parse_request_line_step
- nxt_http_parse_request_line_step
- nxt_http_parse_request_line_step
- nxt_http_parse_request_line_step
+/* enddef */
- nxt_http_parse_request_line_step
- nxt_http_parse_request_line_step
- nxt_http_parse_request_line_step
- nxt_http_parse_request_line_step
+ nxt_method_test_char(p[0]);
+ nxt_method_test_char(p[1]);
+ nxt_method_test_char(p[2]);
+ nxt_method_test_char(p[3]);
-#undef nxt_http_parse_request_line_step
+ nxt_method_test_char(p[4]);
+ nxt_method_test_char(p[5]);
+ nxt_method_test_char(p[6]);
+ nxt_method_test_char(p[7]);
+
+ p += 8;
}
+ return NXT_AGAIN;
+
+ method_unusual_char:
+
+ ch = *p;
+
if (nxt_fast_path(ch == ' ')) {
rp->method.length = p - rp->method.start;
break;
}
if (ch == '_' || ch == '-') {
+ p++;
continue;
}
if (rp->method.start == p && (ch == NXT_CR || ch == NXT_LF)) {
rp->method.start++;
+ p++;
continue;
}
@@ -461,8 +459,10 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
size = end - p;
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]; \
c = normal[ch]; \
\
@@ -470,28 +470,25 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
goto name_end; \
} \
\
- rp->field_key.str[i % 32] = c; \
- i++; \
- }
+ rp->field_key.str[i % 32] = c;
- while (nxt_fast_path(size - i >= 8)) {
- 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_http_parse_field_name_step
- nxt_http_parse_field_name_step
- nxt_http_parse_field_name_step
+/* enddef */
+
+ 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++;
+ 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)) {
- 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->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 *
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 \
- { \
- if (nxt_slow_path(*p < 0x10)) { \
- return p; \
- } \
+#define nxt_field_end_test_char(ch) \
\
- 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]);
- for (n = (end - p) / 16; 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
-
- 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
- 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[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) / 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
+ while (nxt_fast_path(end - p >= 4)) {
+
+ 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]);
+
+ p += 4;
}
switch (end - p) {
case 3:
- nxt_http_lookup_field_end_step
+ nxt_field_end_test_char(*p); p++;
/* Fall through. */
case 2:
- nxt_http_lookup_field_end_step
+ nxt_field_end_test_char(*p); p++;
/* Fall through. */
case 1:
- nxt_http_lookup_field_end_step
+ nxt_field_end_test_char(*p); p++;
/* Fall through. */
case 0:
break;
@@ -640,8 +640,6 @@ nxt_http_lookup_field_end(u_char *p, u_char *end)
nxt_unreachable();
}
-#undef nxt_http_lookup_field_end_step
-
return p;
}