summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorValentin Bartenev <vbart@nginx.com>2017-03-01 15:29:18 +0300
committerValentin Bartenev <vbart@nginx.com>2017-03-01 15:29:18 +0300
commit4df646a25888609b1cceab3cdcda14710f9498e4 (patch)
tree4a26b9eb0f2947ccdcd4db13b3d3f43ba2b8841a
parentfde4d18e3a66bb71e122e27fe11b152547ca1b2b (diff)
downloadunit-4df646a25888609b1cceab3cdcda14710f9498e4.tar.gz
unit-4df646a25888609b1cceab3cdcda14710f9498e4.tar.bz2
HTTP parser.
Diffstat (limited to '')
-rw-r--r--auto/sources3
-rw-r--r--src/nxt_http_parse.c889
-rw-r--r--src/nxt_http_parse.h74
-rw-r--r--src/nxt_main.h1
-rw-r--r--test/nxt_http_parse_unit_test.c457
-rw-r--r--test/nxt_lib_unit_test.c4
-rw-r--r--test/nxt_lib_unit_test.h1
7 files changed, 1429 insertions, 0 deletions
diff --git a/auto/sources b/auto/sources
index 7d4f9a99..af9a21f4 100644
--- a/auto/sources
+++ b/auto/sources
@@ -65,6 +65,7 @@ NXT_LIB_DEPS=" \
src/nxt_sockaddr.h \
src/nxt_job_resolve.h \
src/nxt_listen_socket.h \
+ src/nxt_http_parse.h \
"
NXT_LIB_SRCS=" \
@@ -127,6 +128,7 @@ NXT_LIB_SRCS=" \
src/nxt_sockaddr.c \
src/nxt_listen_socket.c \
src/nxt_upstream_round_robin.c \
+ src/nxt_http_parse.c \
"
NXT_LIB_SRC0=" \
@@ -198,6 +200,7 @@ NXT_LIB_UNIT_TEST_SRCS=" \
test/nxt_malloc_unit_test.c \
test/nxt_utf8_unit_test.c \
test/nxt_rbtree1_unit_test.c \
+ test/nxt_http_parse_unit_test.c \
"
NXT_LIB_UTF8_FILE_NAME_TEST_SRCS=" \
diff --git a/src/nxt_http_parse.c b/src/nxt_http_parse.c
new file mode 100644
index 00000000..d91dd959
--- /dev/null
+++ b/src/nxt_http_parse.c
@@ -0,0 +1,889 @@
+
+/*
+ * Copyright (C) NGINX, Inc.
+ * Copyright (C) Valentin V. Bartenev
+ */
+
+#include <nxt_main.h>
+
+#ifdef __SSE4_2__
+#include <x86intrin.h>
+#endif
+
+
+typedef struct {
+ nxt_http_field_handler_t handler;
+ uintptr_t data;
+ union {
+ uint8_t str[8];
+ uint64_t ui64;
+ } key[];
+} nxt_http_fields_hash_entry_t;
+
+
+#define nxt_http_fields_hash_next_entry(entry, n) \
+ ((nxt_http_fields_hash_entry_t *) ((u_char *) (entry) \
+ + sizeof(nxt_http_fields_hash_entry_t) \
+ + n * 8))
+
+
+struct nxt_http_fields_hash_s {
+ size_t min_length;
+ size_t max_length;
+ void *long_fields;
+ nxt_http_fields_hash_entry_t *entries[];
+};
+
+
+static nxt_int_t nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp,
+ u_char **pos, u_char *end);
+static nxt_int_t nxt_http_parse_request_line(nxt_http_request_parse_t *rp,
+ u_char **pos, u_char *end);
+static nxt_int_t nxt_http_parse_field_name(nxt_http_request_parse_t *rp,
+ u_char **pos, u_char *end);
+static nxt_int_t nxt_http_parse_field_value(nxt_http_request_parse_t *rp,
+ u_char **pos, u_char *end);
+static u_char *nxt_http_lookup_field_end(u_char *p, u_char *end);
+static nxt_int_t nxt_http_parse_field_end(nxt_http_request_parse_t *rp,
+ u_char **pos, u_char *end);
+
+static nxt_http_fields_hash_entry_t *nxt_http_fields_hash_lookup(
+ nxt_http_fields_hash_t *hash, uint64_t *key, nxt_str_t *value);
+static nxt_http_fields_hash_entry_t *nxt_http_header_fields_hash_lookup_long(
+ nxt_http_fields_hash_t *hash, nxt_str_t *value);
+
+
+typedef enum {
+ NXT_HTTP_TARGET_SPACE = 1, /* \s */
+ NXT_HTTP_TARGET_HASH, /* # */
+ NXT_HTTP_TARGET_AGAIN,
+ NXT_HTTP_TARGET_BAD, /* \0\r\n */
+
+ /* traps below are used for extended check only */
+
+ NXT_HTTP_TARGET_SLASH = 5, /* / */
+ NXT_HTTP_TARGET_DOT, /* . */
+ NXT_HTTP_TARGET_ARGS_MARK, /* ? */
+ NXT_HTTP_TARGET_QUOTE_MARK, /* % */
+ NXT_HTTP_TARGET_PLUS, /* + */
+} nxt_http_target_traps_e;
+
+
+static const uint8_t nxt_http_target_chars[256] nxt_aligned(64) = {
+ /* \0 \n \r */
+ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ /*
+ * \s ! " # $ % & ' ( ) * + , - . /
+ * 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
+ */
+ 1, 0, 0, 2, 0, 8, 0, 0, 0, 0, 0, 9, 0, 0, 6, 5,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+};
+
+
+nxt_inline nxt_http_target_traps_e
+nxt_http_parse_target(u_char **pos, u_char *end)
+{
+ u_char *p;
+ nxt_uint_t trap;
+
+ p = *pos;
+
+ for ( ;; ) {
+ if (nxt_slow_path(end - p < 10)) {
+ return NXT_HTTP_TARGET_AGAIN;
+ }
+
+#define nxt_http_parse_target_step \
+ { \
+ trap = nxt_http_target_chars[*p]; \
+ \
+ if (nxt_slow_path(trap != 0)) { \
+ break; \
+ } \
+ \
+ p++; \
+ }
+
+ nxt_http_parse_target_step
+ nxt_http_parse_target_step
+ nxt_http_parse_target_step
+ nxt_http_parse_target_step
+
+ nxt_http_parse_target_step
+ nxt_http_parse_target_step
+ nxt_http_parse_target_step
+ nxt_http_parse_target_step
+
+ nxt_http_parse_target_step
+ nxt_http_parse_target_step
+
+#undef nxt_http_parse_target_step
+ }
+
+ *pos = p;
+
+ return trap;
+}
+
+
+#ifdef __SSE4_2__
+
+nxt_inline nxt_http_target_traps_e
+nxt_http_parse_target_rest(u_char **pos, u_char *end)
+{
+ int n;
+ u_char *p;
+ nxt_uint_t i;
+
+ static const u_char stop_chars[16] nxt_aligned(16) = " #\r\n";
+
+ __m128i pattern = _mm_load_si128((__m128i *) stop_chars);
+
+ p = *pos;
+
+ for (n = (end - p) / 16; nxt_fast_path(n != 0); n--) {
+
+ __m128i test = _mm_loadu_si128((__m128i *) p);
+
+ i = _mm_cmpistri(pattern, test, _SIDD_LEAST_SIGNIFICANT
+ | _SIDD_CMP_EQUAL_ANY
+ | _SIDD_UBYTE_OPS);
+
+ p += i;
+
+ if (i != 16) {
+ *pos = p;
+ return nxt_http_target_chars[*p];
+ }
+ }
+
+ *pos = p;
+
+ return nxt_http_parse_target(pos, end);
+}
+
+#else
+#define nxt_http_parse_target_rest nxt_http_parse_target
+#endif
+
+
+nxt_int_t
+nxt_http_parse_request(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b)
+{
+ nxt_int_t rc;
+
+ if (rp->handler == NULL) {
+ rp->handler = &nxt_http_parse_request_line;
+ }
+
+ do {
+ rc = rp->handler(rp, &b->pos, b->free);
+ } while (rc == NXT_OK);
+
+ return rc;
+}
+
+
+static nxt_int_t
+nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
+ u_char *end)
+{
+ u_char *p, ch, *after_slash;
+ nxt_int_t rc;
+ nxt_http_ver_t version;
+ nxt_http_target_traps_e trap;
+
+ static const nxt_http_ver_t http11 = { "HTTP/1.1" };
+ static const nxt_http_ver_t http10 = { "HTTP/1.0" };
+
+ p = *pos;
+
+ rp->method.start = p;
+
+ for ( ;; p++) {
+
+ for ( ;; ) {
+ if (nxt_slow_path(end - p < 12)) {
+ return NXT_AGAIN;
+ }
+
+#define nxt_http_parse_request_line_step \
+ { \
+ ch = *p; \
+ \
+ if (nxt_slow_path(ch < 'A' || ch > 'Z')) { \
+ break; \
+ } \
+ \
+ p++; \
+ }
+
+ 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_http_parse_request_line_step
+ nxt_http_parse_request_line_step
+ nxt_http_parse_request_line_step
+ nxt_http_parse_request_line_step
+
+#undef nxt_http_parse_request_line_step
+ }
+
+ if (nxt_fast_path(ch == ' ')) {
+ rp->method.length = p - rp->method.start;
+ break;
+ }
+
+ if (ch == '_' || ch == '-') {
+ continue;
+ }
+
+ if (rp->method.start == p && (ch == NXT_CR || ch == NXT_LF)) {
+ rp->method.start++;
+ continue;
+ }
+
+ return NXT_ERROR;
+ }
+
+ p++;
+
+ if (nxt_slow_path(p == end)) {
+ return NXT_AGAIN;
+ }
+
+ /* target */
+
+ nxt_prefetch(&nxt_http_target_chars[' ']);
+ nxt_prefetch(&nxt_http_target_chars['@']);
+ nxt_prefetch(&nxt_http_target_chars['`']);
+
+ ch = *p;
+
+ if (nxt_slow_path(ch != '/')) {
+ rc = nxt_http_parse_unusual_target(rp, &p, end);
+
+ if (nxt_slow_path(rc != NXT_OK)) {
+ return rc;
+ }
+ }
+
+ rp->target_start = p;
+
+ after_slash = p + 1;
+
+ for ( ;; ) {
+ p++;
+
+ trap = nxt_http_parse_target(&p, end);
+
+ switch (trap) {
+ case NXT_HTTP_TARGET_SLASH:
+ if (nxt_slow_path(after_slash == p)) {
+ rp->complex_target = 1;
+ goto rest_of_target;
+ }
+
+ after_slash = p + 1;
+
+ rp->exten_start = NULL;
+ continue;
+
+ case NXT_HTTP_TARGET_DOT:
+ if (nxt_slow_path(after_slash == p)) {
+ rp->complex_target = 1;
+ goto rest_of_target;
+ }
+
+ rp->exten_start = p + 1;
+ continue;
+
+ case NXT_HTTP_TARGET_ARGS_MARK:
+ rp->args_start = p + 1;
+ goto rest_of_target;
+
+ case NXT_HTTP_TARGET_SPACE:
+ rp->target_end = p;
+ goto space_after_target;
+
+ case NXT_HTTP_TARGET_QUOTE_MARK:
+ rp->quoted_target = 1;
+ goto rest_of_target;
+
+ case NXT_HTTP_TARGET_PLUS:
+ rp->plus_in_target = 1;
+ continue;
+
+ case NXT_HTTP_TARGET_HASH:
+ rp->complex_target = 1;
+ goto rest_of_target;
+
+ case NXT_HTTP_TARGET_AGAIN:
+ return NXT_AGAIN;
+
+ case NXT_HTTP_TARGET_BAD:
+ return NXT_ERROR;
+ }
+
+ nxt_unreachable();
+ }
+
+rest_of_target:
+
+ for ( ;; ) {
+ p++;
+
+ trap = nxt_http_parse_target_rest(&p, end);
+
+ switch (trap) {
+ case NXT_HTTP_TARGET_SPACE:
+ rp->target_end = p;
+ goto space_after_target;
+
+ case NXT_HTTP_TARGET_HASH:
+ rp->complex_target = 1;
+ continue;
+
+ case NXT_HTTP_TARGET_AGAIN:
+ return NXT_AGAIN;
+
+ case NXT_HTTP_TARGET_BAD:
+ return NXT_ERROR;
+
+ default:
+ continue;
+ }
+
+ nxt_unreachable();
+ }
+
+space_after_target:
+
+ if (nxt_slow_path(end - p < 10)) {
+ return NXT_AGAIN;
+ }
+
+ /* " HTTP/1.1\r\n" or " HTTP/1.1\n" */
+
+ nxt_memcpy(version.str, &p[1], 8);
+
+ if (nxt_fast_path((version.ui64 == http11.ui64
+ || version.ui64 == http10.ui64
+ || (p[1] == 'H'
+ && p[2] == 'T'
+ && p[3] == 'T'
+ && p[4] == 'P'
+ && p[5] == '/'
+ && p[6] >= '0' && p[6] <= '9'
+ && p[7] == '.'
+ && p[8] >= '0' && p[8] <= '9'))
+ && (p[9] == '\r' || p[9] == '\n')))
+ {
+ rp->version.ui64 = version.ui64;
+
+ if (nxt_fast_path(p[9] == '\r')) {
+ p += 10;
+
+ if (nxt_slow_path(p == end)) {
+ return NXT_AGAIN;
+ }
+
+ if (nxt_slow_path(*p != '\n')) {
+ return NXT_ERROR;
+ }
+
+ *pos = p + 1;
+ return nxt_http_parse_field_name(rp, pos, end);
+ }
+
+ *pos = p + 10;
+ return nxt_http_parse_field_name(rp, pos, end);
+ }
+
+ if (p[1] == ' ') {
+ /* surplus space after tartet */
+ p++;
+ goto space_after_target;
+ }
+
+ rp->space_in_target = 1;
+ goto rest_of_target;
+}
+
+
+static nxt_int_t
+nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, u_char **pos,
+ u_char *end)
+{
+ u_char *p, ch;
+
+ p = *pos;
+
+ ch = *p;
+
+ if (ch == ' ') {
+ /* skip surplus spaces before target */
+
+ do {
+ p++;
+
+ if (nxt_slow_path(p == end)) {
+ return NXT_AGAIN;
+ }
+
+ ch = *p;
+
+ } while (ch == ' ');
+
+ if (ch == '/') {
+ *pos = p;
+ return NXT_OK;
+ }
+ }
+
+ /* absolute path or '*' */
+
+ /* TODO */
+
+ return NXT_ERROR;
+}
+
+
+static nxt_int_t
+nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
+ u_char *end)
+{
+ u_char *p, ch, c;
+ size_t i, size;
+
+ static const u_char normal[256] nxt_aligned(64) =
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0"
+
+ /* These 64 bytes should reside in one cache line. */
+ "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0"
+ "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0"
+
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
+ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
+
+ p = *pos;
+
+ size = end - p;
+
+ for (i = rp->offset; i != size; i++) {
+
+ ch = p[i];
+
+ c = normal[ch];
+
+ if (nxt_fast_path(c != '\0')) {
+ rp->field_name_key.str[i % 32] = c;
+ continue;
+ }
+
+ if (nxt_fast_path(ch == ':')) {
+ if (nxt_slow_path(i == 0)) {
+ return NXT_ERROR;
+ }
+
+ *pos = &p[i] + 1;
+
+ rp->field_name.start = p;
+ rp->field_name.length = i;
+
+ rp->offset = 0;
+
+ return nxt_http_parse_field_value(rp, pos, end);
+ }
+
+ *pos = &p[i];
+
+ rp->field_name.length = 0;
+
+ return nxt_http_parse_field_end(rp, pos, end);
+ }
+
+ rp->offset = i;
+ rp->handler = &nxt_http_parse_field_name;
+
+ return NXT_AGAIN;
+}
+
+
+static nxt_int_t
+nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos,
+ u_char *end)
+{
+ u_char *p, ch;
+
+ p = *pos;
+
+ for ( ;; ) {
+ if (nxt_slow_path(p == end)) {
+ *pos = p;
+ rp->handler = &nxt_http_parse_field_value;
+ return NXT_AGAIN;
+ }
+
+ if (*p != ' ') {
+ break;
+ }
+
+ p++;
+ }
+
+ *pos = p;
+
+ p += rp->offset;
+
+ for ( ;; ) {
+ p = nxt_http_lookup_field_end(p, end);
+
+ if (nxt_slow_path(p == end)) {
+ rp->offset = p - *pos;
+ rp->handler = &nxt_http_parse_field_value;
+ return NXT_AGAIN;
+ }
+
+ ch = *p;
+
+ if (nxt_fast_path(ch == '\r' || ch == '\n')) {
+ break;
+ }
+
+ if (ch == '\0') {
+ return NXT_ERROR;
+ }
+ }
+
+ if (nxt_fast_path(p != *pos)) {
+ while (p[-1] == ' ') {
+ p--;
+ }
+ }
+
+ rp->offset = 0;
+
+ rp->field_value.start = *pos;
+ rp->field_value.length = p - *pos;
+
+ *pos = p;
+
+ return nxt_http_parse_field_end(rp, pos, end);
+}
+
+
+static u_char *
+nxt_http_lookup_field_end(u_char *p, u_char *end)
+{
+ nxt_uint_t n;
+#ifdef __SSE4_2__
+ nxt_uint_t i;
+
+ static const u_char end_chars[16] nxt_aligned(16) = "\r\n";
+
+ __m128i pattern = _mm_load_si128((__m128i *) end_chars);
+
+ for (n = (end - p) / 16; nxt_fast_path(n != 0); n--) {
+
+ __m128i test = _mm_loadu_si128((__m128i *) p);
+
+ i = _mm_cmpistri(pattern, test, _SIDD_LEAST_SIGNIFICANT
+ | _SIDD_CMP_EQUAL_ANY
+ | _SIDD_UBYTE_OPS);
+
+ p += i;
+
+ if (i != 16) {
+ return p;
+ }
+ }
+#endif
+
+#define nxt_http_lookup_field_end_step \
+ { \
+ if (nxt_slow_path(*p <= '\r')) { \
+ return p; \
+ } \
+ \
+ p++; \
+ }
+
+ for (n = (end - p) / 8; 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
+ }
+
+ switch (end - p) {
+ case 7:
+ nxt_http_lookup_field_end_step
+ case 6:
+ nxt_http_lookup_field_end_step
+ case 5:
+ nxt_http_lookup_field_end_step
+ case 4:
+ nxt_http_lookup_field_end_step
+ case 3:
+ nxt_http_lookup_field_end_step
+ case 2:
+ nxt_http_lookup_field_end_step
+ case 1:
+ nxt_http_lookup_field_end_step
+ case 0:
+ break;
+ default:
+ nxt_unreachable();
+ }
+
+#undef nxt_http_lookup_field_end_step
+
+ return p;
+}
+
+
+static nxt_int_t
+nxt_http_parse_field_end(nxt_http_request_parse_t *rp, u_char **pos,
+ u_char *end)
+{
+ u_char *p;
+ nxt_int_t rc;
+ nxt_http_fields_hash_entry_t *entry;
+
+ p = *pos;
+
+ if (nxt_fast_path(*p == '\r')) {
+ p++;
+
+ if (nxt_slow_path(p == end)) {
+ rp->handler = &nxt_http_parse_field_end;
+ return NXT_AGAIN;
+ }
+ }
+
+ if (nxt_fast_path(*p == '\n')) {
+ *pos = p + 1;
+
+ if (rp->field_name.length != 0) {
+ entry = nxt_http_fields_hash_lookup(rp->hash,
+ rp->field_name_key.ui64,
+ &rp->field_name);
+
+ if (entry != NULL) {
+ rc = entry->handler(rp->ctx, &rp->field_name, &rp->field_value,
+ entry->data);
+
+ if (nxt_slow_path(rc != NXT_OK)) {
+ return NXT_ERROR;
+ }
+ }
+
+ nxt_memzero(rp->field_name_key.str, 32);
+
+ rp->handler = &nxt_http_parse_field_name;
+ return NXT_OK;
+ }
+
+ return NXT_DONE;
+ }
+
+ return NXT_ERROR;
+}
+
+
+static nxt_http_fields_hash_entry_t *
+nxt_http_fields_hash_lookup(nxt_http_fields_hash_t *hash, uint64_t *key,
+ nxt_str_t *value)
+{
+ nxt_http_fields_hash_entry_t *entry;
+
+ if (hash == NULL || value->length < hash->min_length) {
+ return NULL;
+ }
+
+ if (value->length > hash->max_length) {
+ if (value->length > 32 && hash->long_fields != NULL) {
+ return nxt_http_header_fields_hash_lookup_long(hash, value);
+ }
+
+ return NULL;
+ }
+
+ entry = hash->entries[value->length - hash->min_length];
+
+ if (entry == NULL) {
+ return NULL;
+ }
+
+ switch ((value->length + 7) / 8) {
+ case 1:
+ do {
+ if (entry->key[0].ui64 == key[0]) {
+ return entry;
+ }
+
+ entry = nxt_http_fields_hash_next_entry(entry, 1);
+
+ } while (entry->handler != NULL);
+
+ break;
+
+ case 2:
+ do {
+ if (entry->key[0].ui64 == key[0]
+ && entry->key[1].ui64 == key[1])
+ {
+ return entry;
+ }
+
+ entry = nxt_http_fields_hash_next_entry(entry, 2);
+
+ } while (entry->handler != NULL);
+
+ break;
+
+ case 3:
+ do {
+ if (entry->key[0].ui64 == key[0]
+ && entry->key[1].ui64 == key[1]
+ && entry->key[2].ui64 == key[2])
+ {
+ return entry;
+ }
+
+ entry = nxt_http_fields_hash_next_entry(entry, 3);
+
+ } while (entry->handler != NULL);
+
+ break;
+
+ case 4:
+ do {
+ if (entry->key[0].ui64 == key[0]
+ && entry->key[1].ui64 == key[1]
+ && entry->key[2].ui64 == key[2]
+ && entry->key[3].ui64 == key[3])
+ {
+ return entry;
+ }
+
+ entry = nxt_http_fields_hash_next_entry(entry, 4);
+
+ } while (entry->handler != NULL);
+
+ break;
+
+ default:
+ nxt_unreachable();
+ }
+
+ return NULL;
+}
+
+
+static nxt_http_fields_hash_entry_t *
+nxt_http_header_fields_hash_lookup_long(nxt_http_fields_hash_t *hash,
+ nxt_str_t *value)
+{
+ /* TODO */
+ return NULL;
+}
+
+
+nxt_http_fields_hash_t *
+nxt_http_fields_hash(nxt_http_fields_t *fields, nxt_mem_pool_t *mp)
+{
+ size_t min_length, max_length, length, size;
+ nxt_uint_t i, j, n;
+ nxt_http_fields_hash_t *hash;
+ nxt_http_fields_hash_entry_t *entry;
+
+ min_length = 0;
+ max_length = 0;
+
+ for (i = 0; fields[i].handler != NULL; i++) {
+ length = fields[i].name.length;
+
+ if (length > 32) {
+ /* TODO */
+ return NULL;
+ }
+
+ min_length = nxt_min(length, min_length);
+ max_length = nxt_max(length, max_length);
+ }
+
+ size = (max_length - min_length + 1)
+ * sizeof(nxt_http_fields_hash_entry_t *);
+
+ hash = nxt_mem_zalloc(mp, sizeof(nxt_http_fields_hash_t) + size);
+
+ if (nxt_slow_path(hash == NULL)) {
+ return NULL;
+ }
+
+ hash->min_length = min_length;
+ hash->max_length = max_length;
+
+ for (i = 0; fields[i].handler != NULL; i++) {
+ length = fields[i].name.length;
+ entry = hash->entries[length - min_length];
+
+ if (entry != NULL) {
+ continue;
+ }
+
+ n = 1;
+
+ for (j = i + 1; fields[j].handler != NULL; j++) {
+ if (length == fields[j].name.length) {
+ n++;
+ }
+ }
+
+ size = sizeof(nxt_http_fields_hash_entry_t) + nxt_align_size(length, 8);
+
+ entry = nxt_mem_zalloc(mp, n * size
+ + sizeof(nxt_http_fields_hash_entry_t));
+
+ if (nxt_slow_path(entry == NULL)) {
+ return NULL;
+ }
+
+ hash->entries[length - min_length] = entry;
+
+ for (j = i; fields[j].handler != NULL; j++) {
+ if (length != fields[j].name.length) {
+ continue;
+ }
+
+ entry->handler = fields[j].handler;
+ entry->data = fields[j].data;
+
+ nxt_memcpy_lowcase(entry->key->str, fields[j].name.start, length);
+
+ n--;
+
+ if (n == 0) {
+ break;
+ }
+
+ entry = (nxt_http_fields_hash_entry_t *) ((u_char *) entry + size);
+ }
+ }
+
+ return hash;
+}
diff --git a/src/nxt_http_parse.h b/src/nxt_http_parse.h
new file mode 100644
index 00000000..2376855e
--- /dev/null
+++ b/src/nxt_http_parse.h
@@ -0,0 +1,74 @@
+
+/*
+ * Copyright (C) NGINX, Inc.
+ * Copyright (C) Valentin V. Bartenev
+ */
+
+#ifndef _NXT_HTTP_PARSER_H_INCLUDED_
+#define _NXT_HTTP_PARSER_H_INCLUDED_
+
+
+typedef struct nxt_http_request_parse_s nxt_http_request_parse_t;
+typedef struct nxt_http_fields_hash_s nxt_http_fields_hash_t;
+
+typedef nxt_int_t (*nxt_http_field_handler_t)(void *ctx, nxt_str_t *name,
+ nxt_str_t *value, uintptr_t data);
+
+
+typedef union {
+ u_char str[8];
+ uint64_t ui64;
+} nxt_http_ver_t;
+
+
+struct nxt_http_request_parse_s {
+ nxt_int_t (*handler)(nxt_http_request_parse_t *rp,
+ u_char **pos, u_char *end);
+
+ size_t offset;
+
+ nxt_str_t method;
+
+ u_char *target_start;
+ u_char *target_end;
+ u_char *exten_start;
+ u_char *args_start;
+
+ nxt_http_ver_t version;
+
+ union {
+ uint8_t str[32];
+ uint64_t ui64[4];
+ } field_name_key;
+
+ nxt_str_t field_name;
+ nxt_str_t field_value;
+
+ nxt_http_fields_hash_t *hash;
+ void *ctx;
+
+ /* target with "/." */
+ unsigned complex_target:1;
+ /* target with "%" */
+ unsigned quoted_target:1;
+ /* target with " " */
+ unsigned space_in_target:1;
+ /* target with "+" */
+ unsigned plus_in_target:1;
+};
+
+
+typedef struct {
+ nxt_str_t name;
+ nxt_http_field_handler_t handler;
+ uintptr_t data;
+} nxt_http_fields_t;
+
+
+nxt_int_t nxt_http_parse_request(nxt_http_request_parse_t *rp,
+ nxt_buf_mem_t *b);
+nxt_http_fields_hash_t *nxt_http_fields_hash(nxt_http_fields_t *fields,
+ nxt_mem_pool_t *mp);
+
+
+#endif /* _NXT_HTTP_PARSER_H_INCLUDED_ */
diff --git a/src/nxt_main.h b/src/nxt_main.h
index bd5d4662..8db1f833 100644
--- a/src/nxt_main.h
+++ b/src/nxt_main.h
@@ -148,6 +148,7 @@ typedef void (*nxt_event_conn_handler_t)(nxt_thread_t *thr,
#include <nxt_source.h>
typedef struct nxt_upstream_source_s nxt_upstream_source_t;
+#include <nxt_http_parse.h>
#include <nxt_stream_source.h>
#include <nxt_upstream.h>
#include <nxt_upstream_source.h>
diff --git a/test/nxt_http_parse_unit_test.c b/test/nxt_http_parse_unit_test.c
new file mode 100644
index 00000000..abe7093d
--- /dev/null
+++ b/test/nxt_http_parse_unit_test.c
@@ -0,0 +1,457 @@
+
+/*
+ * Copyright (C) NGINX, Inc.
+ * Copyright (C) Valentin V. Bartenev
+ */
+
+#include <nxt_main.h>
+
+
+typedef struct {
+ nxt_str_t method;
+ nxt_str_t target;
+ nxt_str_t exten;
+ nxt_str_t args;
+ u_char version[8];
+
+ /* target with "/." */
+ unsigned complex_target:1;
+ /* target with "%" */
+ unsigned quoted_target:1;
+ /* target with " " */
+ unsigned space_in_target:1;
+ /* target with "+" */
+ unsigned plus_in_target:1;
+} nxt_http_parse_unit_test_request_line_t;
+
+
+typedef union {
+ void *pointer;
+ nxt_http_parse_unit_test_request_line_t request_line;
+} nxt_http_parse_unit_test_data_t;
+
+
+typedef struct {
+ nxt_str_t request;
+ nxt_int_t result;
+ nxt_int_t (*handler)(nxt_http_request_parse_t *rp,
+ nxt_http_parse_unit_test_data_t *data,
+ nxt_str_t *request, nxt_log_t *log);
+
+ nxt_http_parse_unit_test_data_t data;
+} nxt_http_parse_unit_test_case_t;
+
+
+static nxt_int_t nxt_http_parse_unit_test_run(nxt_http_request_parse_t *rp,
+ nxt_str_t *request);
+
+static nxt_int_t nxt_http_parse_unit_test_request_line(
+ nxt_http_request_parse_t *rp, nxt_http_parse_unit_test_data_t *data,
+ nxt_str_t *request, nxt_log_t *log);
+
+static nxt_int_t nxt_http_unit_test_header_return(void *ctx, nxt_str_t *name,
+ nxt_str_t *value, uintptr_t data);
+
+
+static nxt_http_parse_unit_test_case_t nxt_http_unit_test_cases[] = {
+ {
+ nxt_string("GET / HTTP/1.0\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("/"),
+ nxt_null_string,
+ nxt_null_string,
+ "HTTP/1.0",
+ 0, 0, 0, 0
+ }}
+ },
+ {
+ nxt_string("XXX-METHOD /d.ir/fi+le.ext?key=val HTTP/1.2\n\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("XXX-METHOD"),
+ nxt_string("/d.ir/fi+le.ext?key=val"),
+ nxt_string("ext?key=val"),
+ nxt_string("key=val"),
+ "HTTP/1.2",
+ 0, 0, 0, 1
+ }}
+ },
+ {
+ nxt_string("GET /di.r/? HTTP/1.0\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("/di.r/?"),
+ nxt_null_string,
+ nxt_string(""),
+ "HTTP/1.0",
+ 0, 0, 0, 0
+ }}
+ },
+ {
+ nxt_string("GEt / HTTP/1.0\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET /\0 HTTP/1.0\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET /\r HTTP/1.0\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET /\n HTTP/1.0\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET / HTTP/1.0\r\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET /. HTTP/1.0\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("/."),
+ nxt_null_string,
+ nxt_null_string,
+ "HTTP/1.0",
+ 1, 0, 0, 0
+ }}
+ },
+ {
+ nxt_string("GET /# HTTP/1.0\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("/#"),
+ nxt_null_string,
+ nxt_null_string,
+ "HTTP/1.0",
+ 1, 0, 0, 0
+ }}
+ },
+ {
+ nxt_string("GET /?# HTTP/1.0\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("/?#"),
+ nxt_null_string,
+ nxt_string("#"),
+ "HTTP/1.0",
+ 1, 0, 0, 0
+ }}
+ },
+ {
+ nxt_string("GET // HTTP/1.0\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("//"),
+ nxt_null_string,
+ nxt_null_string,
+ "HTTP/1.0",
+ 1, 0, 0, 0
+ }}
+ },
+ {
+ nxt_string("GET /%20 HTTP/1.0\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("/%20"),
+ nxt_null_string,
+ nxt_null_string,
+ "HTTP/1.0",
+ 0, 1, 0, 0
+ }}
+ },
+ {
+ nxt_string("GET / a HTTP/1.0\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("/ a"),
+ nxt_null_string,
+ nxt_null_string,
+ "HTTP/1.0",
+ 0, 0, 1, 0
+ }}
+ },
+ {
+ nxt_string("GET / HTTP/1.0 HTTP/1.1\r\n\r\n"),
+ NXT_DONE,
+ &nxt_http_parse_unit_test_request_line,
+ { .request_line = {
+ nxt_string("GET"),
+ nxt_string("/ HTTP/1.0"),
+ nxt_null_string,
+ nxt_null_string,
+ "HTTP/1.1",
+ 0, 0, 1, 0
+ }}
+ },
+ {
+ nxt_string("GET / HTTP/1.1\r\n"
+ "Host: example.com\r\n\r\n"),
+ NXT_DONE,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET / HTTP/1.1\r\n"
+ ":Host: example.com\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET / HTTP/1.1\r\n"
+ "Ho_st: example.com\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET / HTTP/1.1\r\n"
+ "Ho\0st: example.com\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET / HTTP/1.1\r\n"
+ "Ho\rst: example.com\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET / HTTP/1.1\r\n"
+ "Host: exa\0mple.com\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET / HTTP/1.1\r\n"
+ "Host: exa\rmple.com\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+ {
+ nxt_string("GET / HTTP/1.1\r\n"
+ "X-Bad-Header: value\r\n\r\n"),
+ NXT_ERROR,
+ NULL, { NULL }
+ },
+};
+
+
+static nxt_http_fields_t nxt_http_unit_test_headers[] = {
+ { nxt_string("X-Bad-Header"),
+ &nxt_http_unit_test_header_return,
+ (uintptr_t) NXT_ERROR },
+
+ { nxt_null_string, NULL, 0 }
+};
+
+
+nxt_int_t
+nxt_http_parse_unit_test(nxt_thread_t *thr)
+{
+ nxt_int_t rc;
+ nxt_uint_t i;
+ nxt_mem_pool_t *pool;
+ nxt_http_fields_hash_t *hash;
+ nxt_http_request_parse_t rp;
+ nxt_http_parse_unit_test_case_t *test;
+
+ nxt_thread_time_update(thr);
+
+ pool = nxt_mem_pool_create(512);
+ if (pool == NULL) {
+ return NXT_ERROR;
+ }
+
+ hash = nxt_http_fields_hash(nxt_http_unit_test_headers, pool);
+
+ if (hash == NULL) {
+ return NXT_ERROR;
+ }
+
+ for (i = 0; i < nxt_nitems(nxt_http_unit_test_cases); i++) {
+ test = &nxt_http_unit_test_cases[i];
+
+ nxt_memzero(&rp, sizeof(nxt_http_request_parse_t));
+
+ rp.hash = hash;
+
+ rc = nxt_http_parse_unit_test_run(&rp, &test->request);
+
+ if (rc != test->result) {
+ nxt_log_alert(thr->log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - result: %i (expected: %i)",
+ &test->request, rc, test->result);
+ return NXT_ERROR;
+ }
+
+ if (test->handler != NULL
+ && test->handler(&rp, &test->data, &test->request, thr->log)
+ != NXT_OK)
+ {
+ return NXT_ERROR;
+ }
+ }
+
+ nxt_mem_pool_destroy(pool);
+
+ nxt_log_error(NXT_LOG_NOTICE, thr->log, "http parse unit test passed");
+
+ return NXT_OK;
+}
+
+
+static nxt_int_t
+nxt_http_parse_unit_test_run(nxt_http_request_parse_t *rp, nxt_str_t *request)
+{
+ nxt_int_t rc;
+ nxt_buf_mem_t buf;
+
+ buf.start = request->start;
+ buf.end = request->start + request->length;
+
+ buf.pos = buf.start;
+ buf.free = buf.pos + 1;
+
+ do {
+ buf.free++;
+ rc = nxt_http_parse_request(rp, &buf);
+ } while (buf.free < buf.end && rc == NXT_AGAIN);
+
+ return rc;
+}
+
+
+static nxt_int_t
+nxt_http_parse_unit_test_request_line(nxt_http_request_parse_t *rp,
+ nxt_http_parse_unit_test_data_t *data, nxt_str_t *request, nxt_log_t *log)
+{
+ nxt_str_t str;
+
+ nxt_http_parse_unit_test_request_line_t *test = &data->request_line;
+
+ if (rp->method.start != test->method.start
+ && !nxt_strstr_eq(&rp->method, &test->method))
+ {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - method: \"%V\" (expected: \"%V\")",
+ request, &rp->method, &test->method);
+ return NXT_ERROR;
+ }
+
+ str.length = rp->target_end - rp->target_start;
+ str.start = rp->target_start;
+
+ if (str.start != test->target.start
+ && !nxt_strstr_eq(&str, &test->target))
+ {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - target: \"%V\" (expected: \"%V\")",
+ request, &str, &test->target);
+ return NXT_ERROR;
+ }
+
+ str.length = (rp->exten_start != NULL) ? rp->target_end - rp->exten_start
+ : 0;
+ str.start = rp->exten_start;
+
+ if (str.start != test->exten.start
+ && !nxt_strstr_eq(&str, &test->exten))
+ {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - exten: \"%V\" (expected: \"%V\")",
+ request, &str, &test->exten);
+ return NXT_ERROR;
+ }
+
+ str.length = (rp->args_start != NULL) ? rp->target_end - rp->args_start
+ : 0;
+ str.start = rp->args_start;
+
+ if (str.start != test->args.start
+ && !nxt_strstr_eq(&str, &test->args))
+ {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - args: \"%V\" (expected: \"%V\")",
+ request, &str, &test->args);
+ return NXT_ERROR;
+ }
+
+ if (nxt_memcmp(rp->version.str, test->version, 8) != 0) {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - version: \"%*s\" (expected: \"%*s\")",
+ request, 8, rp->version.str, 8, test->version);
+ return NXT_ERROR;
+ }
+
+ if (rp->complex_target != test->complex_target) {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - complex_target: %d (expected: %d)",
+ request, rp->complex_target, test->complex_target);
+ return NXT_ERROR;
+ }
+
+ if (rp->quoted_target != test->quoted_target) {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - quoted_target: %d (expected: %d)",
+ request, rp->quoted_target, test->quoted_target);
+ return NXT_ERROR;
+ }
+
+ if (rp->space_in_target != test->space_in_target) {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - space_in_target: %d (expected: %d)",
+ request, rp->space_in_target, test->space_in_target);
+ return NXT_ERROR;
+ }
+
+ if (rp->plus_in_target != test->plus_in_target) {
+ nxt_log_alert(log, "http parse unit test case failed:\n"
+ " - request:\n\"%V\"\n"
+ " - plus_in_target: %d (expected: %d)",
+ request, rp->plus_in_target, test->plus_in_target);
+ return NXT_ERROR;
+ }
+
+ return NXT_OK;
+}
+
+
+static nxt_int_t
+nxt_http_unit_test_header_return(void *ctx, nxt_str_t *name, nxt_str_t *value,
+ uintptr_t data)
+{
+ return (nxt_int_t) data;
+}
diff --git a/test/nxt_lib_unit_test.c b/test/nxt_lib_unit_test.c
index 876416e5..fce4c963 100644
--- a/test/nxt_lib_unit_test.c
+++ b/test/nxt_lib_unit_test.c
@@ -149,5 +149,9 @@ main(int argc, char **argv)
return 1;
}
+ if (nxt_http_parse_unit_test(thr) != NXT_OK) {
+ return 1;
+ }
+
return 0;
}
diff --git a/test/nxt_lib_unit_test.h b/test/nxt_lib_unit_test.h
index a6a24855..a39c7c03 100644
--- a/test/nxt_lib_unit_test.h
+++ b/test/nxt_lib_unit_test.h
@@ -65,6 +65,7 @@ nxt_int_t nxt_gmtime_unit_test(nxt_thread_t *thr);
nxt_int_t nxt_sprintf_unit_test(nxt_thread_t *thr);
nxt_int_t nxt_malloc_unit_test(nxt_thread_t *thr);
nxt_int_t nxt_utf8_unit_test(nxt_thread_t *thr);
+nxt_int_t nxt_http_parse_unit_test(nxt_thread_t *thr);
#endif /* _NXT_LIB_UNIT_TEST_H_INCLUDED_ */