summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorZhidao HONG <z.hong@f5.com>2022-05-18 21:18:40 +0800
committerZhidao HONG <z.hong@f5.com>2022-05-18 21:18:40 +0800
commit6271479610c95d40daf9ed6ec2c7dead67c74f00 (patch)
tree858290b48bf48393beb8c0c041dbed40d71d07aa /src
parent3e06ae3b34fa1fb1aff15186ddd4588f4f5aa3b3 (diff)
downloadunit-6271479610c95d40daf9ed6ec2c7dead67c74f00.tar.gz
unit-6271479610c95d40daf9ed6ec2c7dead67c74f00.tar.bz2
HTTP: generalized argument and cookie parsing.
No functional changes.
Diffstat (limited to 'src')
-rw-r--r--src/nxt_http.h12
-rw-r--r--src/nxt_http_request.c276
-rw-r--r--src/nxt_http_route.c294
3 files changed, 291 insertions, 291 deletions
diff --git a/src/nxt_http.h b/src/nxt_http.h
index 36bc089d..6b19d7df 100644
--- a/src/nxt_http.h
+++ b/src/nxt_http.h
@@ -198,6 +198,15 @@ struct nxt_http_request_s {
};
+typedef struct {
+ uint16_t hash;
+ uint16_t name_length;
+ uint32_t value_length;
+ u_char *name;
+ u_char *value;
+} nxt_http_name_value_t;
+
+
typedef struct nxt_http_route_s nxt_http_route_t;
typedef struct nxt_http_route_rule_s nxt_http_route_rule_t;
typedef struct nxt_http_route_addr_rule_s nxt_http_route_addr_rule_t;
@@ -311,6 +320,9 @@ nxt_int_t nxt_http_request_field(void *ctx, nxt_http_field_t *field,
nxt_int_t nxt_http_request_content_length(void *ctx, nxt_http_field_t *field,
uintptr_t data);
+nxt_array_t *nxt_http_arguments_parse(nxt_http_request_t *r);
+nxt_array_t *nxt_http_cookies_parse(nxt_http_request_t *r);
+
nxt_http_routes_t *nxt_http_routes_create(nxt_task_t *task,
nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *routes_conf);
nxt_http_action_t *nxt_http_action_create(nxt_task_t *task,
diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c
index ac614df6..04a6f7f3 100644
--- a/src/nxt_http_request.c
+++ b/src/nxt_http_request.c
@@ -24,6 +24,25 @@ static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data);
static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now,
struct tm *tm, size_t size, const char *format);
+static nxt_http_name_value_t *nxt_http_argument(nxt_array_t *array,
+ u_char *name, size_t name_length, uint32_t hash, u_char *start,
+ u_char *end);
+static nxt_int_t nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start,
+ u_char *end);
+static nxt_http_name_value_t *nxt_http_cookie(nxt_array_t *array, u_char *name,
+ size_t name_length, u_char *start, u_char *end);
+
+
+#define NXT_HTTP_COOKIE_HASH \
+ (nxt_http_field_hash_end( \
+ nxt_http_field_hash_char( \
+ nxt_http_field_hash_char( \
+ nxt_http_field_hash_char( \
+ nxt_http_field_hash_char( \
+ nxt_http_field_hash_char( \
+ nxt_http_field_hash_char(NXT_HTTP_FIELD_HASH_INIT, \
+ 'c'), 'o'), 'o'), 'k'), 'i'), 'e')) & 0xFFFF)
+
static const nxt_http_request_state_t nxt_http_request_init_state;
static const nxt_http_request_state_t nxt_http_request_body_state;
@@ -748,3 +767,260 @@ nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm,
{
return nxt_http_date(buf, tm);
}
+
+
+nxt_array_t *
+nxt_http_arguments_parse(nxt_http_request_t *r)
+{
+ size_t name_length;
+ u_char *p, *dst, *dst_start, *start, *end, *name;
+ uint8_t d0, d1;
+ uint32_t hash;
+ nxt_array_t *args;
+ nxt_http_name_value_t *nv;
+
+ if (r->arguments != NULL) {
+ return r->arguments;
+ }
+
+ args = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
+ if (nxt_slow_path(args == NULL)) {
+ return NULL;
+ }
+
+ hash = NXT_HTTP_FIELD_HASH_INIT;
+ name = NULL;
+ name_length = 0;
+
+ dst_start = nxt_mp_nget(r->mem_pool, r->args->length);
+ if (nxt_slow_path(dst_start == NULL)) {
+ return NULL;
+ }
+
+ r->args_decoded.start = dst_start;
+
+ start = r->args->start;
+ end = start + r->args->length;
+
+ for (p = start, dst = dst_start; p < end; p++, dst++) {
+ *dst = *p;
+
+ switch (*p) {
+ case '=':
+ if (name == NULL) {
+ name_length = dst - dst_start;
+ name = dst_start;
+ dst_start = dst + 1;
+ }
+
+ continue;
+
+ case '&':
+ if (name_length != 0 || dst != dst_start) {
+ nv = nxt_http_argument(args, name, name_length, hash, dst_start,
+ dst);
+ if (nxt_slow_path(nv == NULL)) {
+ return NULL;
+ }
+ }
+
+ hash = NXT_HTTP_FIELD_HASH_INIT;
+ name_length = 0;
+ name = NULL;
+ dst_start = dst + 1;
+
+ continue;
+
+ case '+':
+ *dst = ' ';
+
+ break;
+
+ case '%':
+ if (nxt_slow_path(end - p <= 2)) {
+ break;
+ }
+
+ d0 = nxt_hex2int[p[1]];
+ d1 = nxt_hex2int[p[2]];
+
+ if (nxt_slow_path((d0 | d1) >= 16)) {
+ break;
+ }
+
+ p += 2;
+ *dst = (d0 << 4) + d1;
+
+ break;
+ }
+
+ if (name == NULL) {
+ hash = nxt_http_field_hash_char(hash, *dst);
+ }
+ }
+
+ r->args_decoded.length = dst - r->args_decoded.start;
+
+ if (name_length != 0 || dst != dst_start) {
+ nv = nxt_http_argument(args, name, name_length, hash, dst_start, dst);
+ if (nxt_slow_path(nv == NULL)) {
+ return NULL;
+ }
+ }
+
+ r->arguments = args;
+
+ return args;
+}
+
+
+static nxt_http_name_value_t *
+nxt_http_argument(nxt_array_t *array, u_char *name, size_t name_length,
+ uint32_t hash, u_char *start, u_char *end)
+{
+ size_t length;
+ nxt_http_name_value_t *nv;
+
+ nv = nxt_array_add(array);
+ if (nxt_slow_path(nv == NULL)) {
+ return NULL;
+ }
+
+ nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
+
+ length = end - start;
+
+ if (name == NULL) {
+ name_length = length;
+ name = start;
+ length = 0;
+ }
+
+ nv->name_length = name_length;
+ nv->value_length = length;
+ nv->name = name;
+ nv->value = start;
+
+ return nv;
+}
+
+
+nxt_array_t *
+nxt_http_cookies_parse(nxt_http_request_t *r)
+{
+ nxt_int_t ret;
+ nxt_array_t *cookies;
+ nxt_http_field_t *f;
+
+ if (r->cookies != NULL) {
+ return r->cookies;
+ }
+
+ cookies = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
+ if (nxt_slow_path(cookies == NULL)) {
+ return NULL;
+ }
+
+ nxt_list_each(f, r->fields) {
+
+ if (f->hash != NXT_HTTP_COOKIE_HASH
+ || f->name_length != 6
+ || nxt_strncasecmp(f->name, (u_char *) "Cookie", 6) != 0)
+ {
+ continue;
+ }
+
+ ret = nxt_http_cookie_parse(cookies, f->value,
+ f->value + f->value_length);
+ if (ret != NXT_OK) {
+ return NULL;
+ }
+
+ } nxt_list_loop;
+
+ r->cookies = cookies;
+
+ return cookies;
+}
+
+
+static nxt_int_t
+nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, u_char *end)
+{
+ size_t name_length;
+ u_char c, *p, *name;
+ nxt_http_name_value_t *nv;
+
+ name = NULL;
+ name_length = 0;
+
+ for (p = start; p < end; p++) {
+ c = *p;
+
+ if (c == '=') {
+ while (start[0] == ' ') { start++; }
+
+ name_length = p - start;
+
+ if (name_length != 0) {
+ name = start;
+ }
+
+ start = p + 1;
+
+ } else if (c == ';') {
+ if (name != NULL) {
+ nv = nxt_http_cookie(cookies, name, name_length, start, p);
+ if (nxt_slow_path(nv == NULL)) {
+ return NXT_ERROR;
+ }
+ }
+
+ name = NULL;
+ start = p + 1;
+ }
+ }
+
+ if (name != NULL) {
+ nv = nxt_http_cookie(cookies, name, name_length, start, p);
+ if (nxt_slow_path(nv == NULL)) {
+ return NXT_ERROR;
+ }
+ }
+
+ return NXT_OK;
+}
+
+
+static nxt_http_name_value_t *
+nxt_http_cookie(nxt_array_t *array, u_char *name, size_t name_length,
+ u_char *start, u_char *end)
+{
+ u_char c, *p;
+ uint32_t hash;
+ nxt_http_name_value_t *nv;
+
+ nv = nxt_array_add(array);
+ if (nxt_slow_path(nv == NULL)) {
+ return NULL;
+ }
+
+ nv->name_length = name_length;
+ nv->name = name;
+
+ hash = NXT_HTTP_FIELD_HASH_INIT;
+
+ for (p = name; p < name + name_length; p++) {
+ c = *p;
+ hash = nxt_http_field_hash_char(hash, c);
+ }
+
+ nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
+
+ while (start < end && end[-1] == ' ') { end--; }
+
+ nv->value_length = end - start;
+ nv->value = start;
+
+ return nv;
+}
diff --git a/src/nxt_http_route.c b/src/nxt_http_route.c
index af28400d..558dae9d 100644
--- a/src/nxt_http_route.c
+++ b/src/nxt_http_route.c
@@ -92,15 +92,6 @@ typedef struct {
uint32_t value_length;
u_char *name;
u_char *value;
-} nxt_http_name_value_t;
-
-
-typedef struct {
- uint16_t hash;
- uint16_t name_length;
- uint32_t value_length;
- u_char *name;
- u_char *value;
} nxt_http_cookie_t;
@@ -172,17 +163,6 @@ struct nxt_http_routes_s {
};
-#define NXT_COOKIE_HASH \
- (nxt_http_field_hash_end( \
- nxt_http_field_hash_char( \
- nxt_http_field_hash_char( \
- nxt_http_field_hash_char( \
- nxt_http_field_hash_char( \
- nxt_http_field_hash_char( \
- nxt_http_field_hash_char(NXT_HTTP_FIELD_HASH_INIT, \
- 'c'), 'o'), 'o'), 'k'), 'i'), 'e')) & 0xFFFF)
-
-
static nxt_http_route_t *nxt_http_route_create(nxt_task_t *task,
nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *cv);
static nxt_http_route_match_t *nxt_http_route_match_create(nxt_task_t *task,
@@ -241,10 +221,6 @@ static nxt_int_t nxt_http_route_header(nxt_http_request_t *r,
nxt_http_route_rule_t *rule);
static nxt_int_t nxt_http_route_arguments(nxt_http_request_t *r,
nxt_http_route_rule_t *rule);
-static nxt_array_t *nxt_http_route_arguments_parse(nxt_http_request_t *r);
-static nxt_http_name_value_t *nxt_http_route_argument(nxt_array_t *array,
- u_char *name, size_t name_length, uint32_t hash, u_char *start,
- u_char *end);
static nxt_int_t nxt_http_route_test_argument(nxt_http_request_t *r,
nxt_http_route_rule_t *rule, nxt_array_t *array);
static nxt_int_t nxt_http_route_scheme(nxt_http_request_t *r,
@@ -253,11 +229,6 @@ static nxt_int_t nxt_http_route_query(nxt_http_request_t *r,
nxt_http_route_rule_t *rule);
static nxt_int_t nxt_http_route_cookies(nxt_http_request_t *r,
nxt_http_route_rule_t *rule);
-static nxt_array_t *nxt_http_route_cookies_parse(nxt_http_request_t *r);
-static nxt_int_t nxt_http_route_cookie_parse(nxt_array_t *cookies,
- u_char *start, u_char *end);
-static nxt_http_name_value_t *nxt_http_route_cookie(nxt_array_t *array,
- u_char *name, size_t name_length, u_char *start, u_char *end);
static nxt_int_t nxt_http_route_test_cookie(nxt_http_request_t *r,
nxt_http_route_rule_t *rule, nxt_array_t *array);
static nxt_int_t nxt_http_route_pattern(nxt_http_request_t *r,
@@ -1995,7 +1966,7 @@ nxt_http_route_arguments(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
{
nxt_array_t *arguments;
- arguments = nxt_http_route_arguments_parse(r);
+ arguments = nxt_http_arguments_parse(r);
if (nxt_slow_path(arguments == NULL)) {
return -1;
}
@@ -2004,143 +1975,6 @@ nxt_http_route_arguments(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
}
-static nxt_array_t *
-nxt_http_route_arguments_parse(nxt_http_request_t *r)
-{
- size_t name_length;
- u_char *p, *dst, *dst_start, *start, *end, *name;
- uint8_t d0, d1;
- uint32_t hash;
- nxt_array_t *args;
- nxt_http_name_value_t *nv;
-
- if (r->arguments != NULL) {
- return r->arguments;
- }
-
- args = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
- if (nxt_slow_path(args == NULL)) {
- return NULL;
- }
-
- hash = NXT_HTTP_FIELD_HASH_INIT;
- name = NULL;
- name_length = 0;
-
- dst_start = nxt_mp_nget(r->mem_pool, r->args->length);
- if (nxt_slow_path(dst_start == NULL)) {
- return NULL;
- }
-
- r->args_decoded.start = dst_start;
-
- start = r->args->start;
- end = start + r->args->length;
-
- for (p = start, dst = dst_start; p < end; p++, dst++) {
- *dst = *p;
-
- switch (*p) {
- case '=':
- if (name == NULL) {
- name_length = dst - dst_start;
- name = dst_start;
- dst_start = dst + 1;
- }
-
- continue;
-
- case '&':
- if (name_length != 0 || dst != dst_start) {
- nv = nxt_http_route_argument(args, name, name_length, hash,
- dst_start, dst);
- if (nxt_slow_path(nv == NULL)) {
- return NULL;
- }
- }
-
- hash = NXT_HTTP_FIELD_HASH_INIT;
- name_length = 0;
- name = NULL;
- dst_start = dst + 1;
-
- continue;
-
- case '+':
- *dst = ' ';
-
- break;
-
- case '%':
- if (nxt_slow_path(end - p <= 2)) {
- break;
- }
-
- d0 = nxt_hex2int[p[1]];
- d1 = nxt_hex2int[p[2]];
-
- if (nxt_slow_path((d0 | d1) >= 16)) {
- break;
- }
-
- p += 2;
- *dst = (d0 << 4) + d1;
-
- break;
- }
-
- if (name == NULL) {
- hash = nxt_http_field_hash_char(hash, *dst);
- }
- }
-
- r->args_decoded.length = dst - r->args_decoded.start;
-
- if (name_length != 0 || dst != dst_start) {
- nv = nxt_http_route_argument(args, name, name_length, hash, dst_start,
- dst);
- if (nxt_slow_path(nv == NULL)) {
- return NULL;
- }
- }
-
- r->arguments = args;
-
- return args;
-}
-
-
-static nxt_http_name_value_t *
-nxt_http_route_argument(nxt_array_t *array, u_char *name, size_t name_length,
- uint32_t hash, u_char *start, u_char *end)
-{
- size_t length;
- nxt_http_name_value_t *nv;
-
- nv = nxt_array_add(array);
- if (nxt_slow_path(nv == NULL)) {
- return NULL;
- }
-
- nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
-
- length = end - start;
-
- if (name == NULL) {
- name_length = length;
- name = start;
- length = 0;
- }
-
- nv->name_length = name_length;
- nv->value_length = length;
- nv->name = name;
- nv->value = start;
-
- return nv;
-}
-
-
static nxt_int_t
nxt_http_route_test_argument(nxt_http_request_t *r,
nxt_http_route_rule_t *rule, nxt_array_t *array)
@@ -2196,7 +2030,7 @@ nxt_http_route_query(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
{
nxt_array_t *arguments;
- arguments = nxt_http_route_arguments_parse(r);
+ arguments = nxt_http_arguments_parse(r);
if (nxt_slow_path(arguments == NULL)) {
return -1;
}
@@ -2211,7 +2045,7 @@ nxt_http_route_cookies(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
{
nxt_array_t *cookies;
- cookies = nxt_http_route_cookies_parse(r);
+ cookies = nxt_http_cookies_parse(r);
if (nxt_slow_path(cookies == NULL)) {
return -1;
}
@@ -2220,128 +2054,6 @@ nxt_http_route_cookies(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
}
-static nxt_array_t *
-nxt_http_route_cookies_parse(nxt_http_request_t *r)
-{
- nxt_int_t ret;
- nxt_array_t *cookies;
- nxt_http_field_t *f;
-
- if (r->cookies != NULL) {
- return r->cookies;
- }
-
- cookies = nxt_array_create(r->mem_pool, 2, sizeof(nxt_http_name_value_t));
- if (nxt_slow_path(cookies == NULL)) {
- return NULL;
- }
-
- nxt_list_each(f, r->fields) {
-
- if (f->hash != NXT_COOKIE_HASH
- || f->name_length != 6
- || nxt_strncasecmp(f->name, (u_char *) "Cookie", 6) != 0)
- {
- continue;
- }
-
- ret = nxt_http_route_cookie_parse(cookies, f->value,
- f->value + f->value_length);
- if (ret != NXT_OK) {
- return NULL;
- }
-
- } nxt_list_loop;
-
- r->cookies = cookies;
-
- return cookies;
-}
-
-
-static nxt_int_t
-nxt_http_route_cookie_parse(nxt_array_t *cookies, u_char *start, u_char *end)
-{
- size_t name_length;
- u_char c, *p, *name;
- nxt_http_name_value_t *nv;
-
- name = NULL;
- name_length = 0;
-
- for (p = start; p < end; p++) {
- c = *p;
-
- if (c == '=') {
- while (start[0] == ' ') { start++; }
-
- name_length = p - start;
-
- if (name_length != 0) {
- name = start;
- }
-
- start = p + 1;
-
- } else if (c == ';') {
- if (name != NULL) {
- nv = nxt_http_route_cookie(cookies, name, name_length,
- start, p);
- if (nxt_slow_path(nv == NULL)) {
- return NXT_ERROR;
- }
- }
-
- name = NULL;
- start = p + 1;
- }
- }
-
- if (name != NULL) {
- nv = nxt_http_route_cookie(cookies, name, name_length, start, p);
- if (nxt_slow_path(nv == NULL)) {
- return NXT_ERROR;
- }
- }
-
- return NXT_OK;
-}
-
-
-static nxt_http_name_value_t *
-nxt_http_route_cookie(nxt_array_t *array, u_char *name, size_t name_length,
- u_char *start, u_char *end)
-{
- u_char c, *p;
- uint32_t hash;
- nxt_http_name_value_t *nv;
-
- nv = nxt_array_add(array);
- if (nxt_slow_path(nv == NULL)) {
- return NULL;
- }
-
- nv->name_length = name_length;
- nv->name = name;
-
- hash = NXT_HTTP_FIELD_HASH_INIT;
-
- for (p = name; p < name + name_length; p++) {
- c = *p;
- hash = nxt_http_field_hash_char(hash, c);
- }
-
- nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF;
-
- while (start < end && end[-1] == ' ') { end--; }
-
- nv->value_length = end - start;
- nv->value = start;
-
- return nv;
-}
-
-
static nxt_int_t
nxt_http_route_test_cookie(nxt_http_request_t *r,
nxt_http_route_rule_t *rule, nxt_array_t *array)