nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.39k stars 327 forks source link

Improved http router cookie. #344

Open hongzhidao opened 4 years ago

hongzhidao commented 4 years ago

Hi, I did a bit rework.

  1. Removed unused nxt_http_cookie_t.
  2. Added r->cookie check-in nxt_http_route_cookies.
  3. Simplify nxt_http_route_arguments and nxt_http_route_cookies.
# HG changeset patch
# User hongzhidao <hongzhidao@gmail.com>
# Date 1573168772 -28800
# Node ID 991ab50a9dbdcb58394fa5f7cb0520144c3ff276
# Parent  944efece387a06ea563d63f18c3d8d408766b295
Improved http router cookie.

diff -r 944efece387a -r 991ab50a9dbd src/nxt_http_route.c
--- a/src/nxt_http_route.c  Thu Nov 07 19:53:25 2019 +0300
+++ b/src/nxt_http_route.c  Fri Nov 08 07:19:32 2019 +0800
@@ -70,15 +70,6 @@

 typedef struct {
-    uint16_t                       hash;
-    uint16_t                       name_length;
-    uint32_t                       value_length;
-    u_char                         *name;
-    u_char                         *value;
-} nxt_http_cookie_t;
-
-
-typedef struct {
     /* The object must be the first field. */
     nxt_http_route_object_t        object:8;
     uint32_t                       items;
@@ -197,10 +188,6 @@
 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,
-    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);
@@ -208,8 +195,8 @@
     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_scheme(nxt_http_request_t *r,
+    nxt_http_route_rule_t *rule);
 static nxt_int_t nxt_http_route_test_rule(nxt_http_request_t *r,
     nxt_http_route_rule_t *rule, u_char *start, size_t length);
 static nxt_int_t nxt_http_route_pattern(nxt_http_request_t *r,
@@ -1220,7 +1207,9 @@
 static nxt_int_t
 nxt_http_route_arguments(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
 {
-    nxt_array_t  *arguments;
+    nxt_bool_t             ret;
+    nxt_array_t            *arguments;
+    nxt_http_name_value_t  *nv, *end;

     if (r->args == NULL) {
         return 0;
@@ -1231,7 +1220,28 @@
         return -1;
     }

-    return nxt_http_route_test_argument(r, rule, arguments);
+    ret = 0;
+
+    nv = arguments->elts;
+    end = nv + arguments->nelts;
+
+    while (nv < end) {
+
+        if (rule->u.name.hash == nv->hash
+            && rule->u.name.length == nv->name_length
+            && nxt_memcmp(rule->u.name.start, nv->name, nv->name_length) == 0)
+        {
+            ret = nxt_http_route_test_rule(r, rule, nv->value,
+                                           nv->value_length);
+            if (ret == 0) {
+                break;
+            }
+        }
+
+        nv++;
+    }
+
+    return ret;
 }

@@ -1335,16 +1345,25 @@

 static nxt_int_t
-nxt_http_route_test_argument(nxt_http_request_t *r,
-    nxt_http_route_rule_t *rule, nxt_array_t *array)
+nxt_http_route_cookies(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
 {
     nxt_bool_t             ret;
+    nxt_array_t            *cookies;
     nxt_http_name_value_t  *nv, *end;

+    if (r->cookie == NULL) {
+        return 0;
+    }
+
+    cookies = nxt_http_route_cookies_parse(r);
+    if (nxt_slow_path(cookies == NULL)) {
+        return -1;
+    }
+
     ret = 0;

-    nv = array->elts;
-    end = nv + array->nelts;
+    nv = cookies->elts;
+    end = nv + cookies->nelts;

     while (nv < end) {

@@ -1366,32 +1385,6 @@
 }

-static nxt_int_t
-nxt_http_route_scheme(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
-{
-    nxt_bool_t  tls, https;
-
-    https = (rule->pattern[0].length1 == nxt_length("https"));
-    tls = (r->tls != NULL);
-
-    return (tls == https);
-}
-
-
-static nxt_int_t
-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);
-    if (nxt_slow_path(cookies == NULL)) {
-        return -1;
-    }
-
-    return nxt_http_route_test_cookie(r, rule, cookies);
-}
-
-
 static nxt_array_t *
 nxt_http_route_cookies_parse(nxt_http_request_t *r)
 {
@@ -1515,34 +1508,14 @@

 static nxt_int_t
-nxt_http_route_test_cookie(nxt_http_request_t *r,
-    nxt_http_route_rule_t *rule, nxt_array_t *array)
+nxt_http_route_scheme(nxt_http_request_t *r, nxt_http_route_rule_t *rule)
 {
-    nxt_bool_t             ret;
-    nxt_http_name_value_t  *nv, *end;
+    nxt_bool_t  tls, https;

-    ret = 0;
+    https = (rule->pattern[0].length1 == nxt_length("https"));
+    tls = (r->tls != NULL);

-    nv = array->elts;
-    end = nv + array->nelts;
-
-    while (nv < end) {
-
-        if (rule->u.name.hash == nv->hash
-            && rule->u.name.length == nv->name_length
-            && nxt_memcmp(rule->u.name.start, nv->name, nv->name_length) == 0)
-        {
-            ret = nxt_http_route_test_rule(r, rule, nv->value,
-                                           nv->value_length);
-            if (ret == 0) {
-                break;
-            }
-        }
-
-        nv++;
-    }
-
-    return ret;
+    return (tls == https);
 }
hongzhidao commented 4 years ago

@VBart

diff -r 991ab50a9dbd src/nxt_http_route.c
--- a/src/nxt_http_route.c  Fri Nov 08 07:19:32 2019 +0800
+++ b/src/nxt_http_route.c  Sat Nov 09 11:11:49 2019 +0800
@@ -369,7 +369,7 @@
     match_conf = nxt_conf_get_path(cv, &match_path);

     n = (match_conf != NULL) ? nxt_conf_object_members_count(match_conf) : 0;
-    size = sizeof(nxt_http_route_match_t) + n * sizeof(nxt_http_route_rule_t *);
+    size = sizeof(nxt_http_route_match_t) + n * sizeof(nxt_http_route_test_t *);

     mp = tmcf->router_conf->mem_pool;

Though the size of nxt_http_route_rule_t is equal to the size of nxt_http_route_test_t. But the type of test field is nxt_http_route_test_t[].

typedef union {
    nxt_http_route_rule_t          *rule;
    nxt_http_route_table_t         *table;
} nxt_http_route_test_t;

typedef struct {
    uint32_t                       items;
    nxt_http_pass_t                pass;
    nxt_http_route_test_t          test[0];
} nxt_http_route_match_t;