[nginx] svn commit: r4463 - trunk/src/http/modules

vbart at nginx.com vbart at nginx.com
Mon Feb 13 10:56:09 UTC 2012


Author: vbart
Date: 2012-02-13 10:56:09 +0000 (Mon, 13 Feb 2012)
New Revision: 4463

Modified:
   trunk/src/http/modules/ngx_http_proxy_module.c
Log:
Proxy: generic rewrite code from the "proxy_redirect" handlers moved to a
separate function with trivial optimization.

No functional changes.


Modified: trunk/src/http/modules/ngx_http_proxy_module.c
===================================================================
--- trunk/src/http/modules/ngx_http_proxy_module.c	2012-02-13 10:42:44 UTC (rev 4462)
+++ trunk/src/http/modules/ngx_http_proxy_module.c	2012-02-13 10:56:09 UTC (rev 4463)
@@ -13,7 +13,8 @@
 typedef struct ngx_http_proxy_rewrite_s  ngx_http_proxy_rewrite_t;
 
 typedef ngx_int_t (*ngx_http_proxy_rewrite_pt)(ngx_http_request_t *r,
-    ngx_table_elt_t *h, size_t prefix, ngx_http_proxy_rewrite_t *pr);
+    ngx_table_elt_t *h, size_t prefix, size_t len,
+    ngx_http_proxy_rewrite_t *pr);
 
 struct ngx_http_proxy_rewrite_s {
     ngx_http_proxy_rewrite_pt      handler;
@@ -123,6 +124,8 @@
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
     ngx_table_elt_t *h, size_t prefix);
+static ngx_int_t ngx_http_proxy_rewrite(ngx_http_request_t *r,
+    ngx_table_elt_t *h, size_t prefix, size_t len, ngx_str_t *replacement);
 
 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
@@ -2272,6 +2275,7 @@
 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
     size_t prefix)
 {
+    size_t                      len;
     ngx_int_t                   rc;
     ngx_uint_t                  i;
     ngx_http_proxy_rewrite_t   *pr;
@@ -2285,8 +2289,10 @@
         return NGX_DECLINED;
     }
 
+    len = h->value.len - prefix;
+
     for (i = 0; i < plcf->redirects->nelts; i++) {
-        rc = pr[i].handler(r, h, prefix, &pr[i]);
+        rc = pr[i].handler(r, h, prefix, len, &pr[i]);
 
         if (rc != NGX_DECLINED) {
             return rc;
@@ -2299,17 +2305,15 @@
 
 static ngx_int_t
 ngx_http_proxy_rewrite_complex_handler(ngx_http_request_t *r,
-    ngx_table_elt_t *h, size_t prefix, ngx_http_proxy_rewrite_t *pr)
+    ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
 {
-    size_t      len;
-    u_char     *data, *p;
-    ngx_str_t   pattern, replacement;
+    ngx_str_t  pattern, replacement;
 
     if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
         return NGX_ERROR;
     }
 
-    if (pattern.len > h->value.len - prefix
+    if (pattern.len > len
         || ngx_rstrncmp(h->value.data + prefix, pattern.data,
                         pattern.len) != 0)
     {
@@ -2320,26 +2324,7 @@
         return NGX_ERROR;
     }
 
-    len = replacement.len + h->value.len - pattern.len;
-
-    data = ngx_pnalloc(r->pool, len);
-    if (data == NULL) {
-        return NGX_ERROR;
-    }
-
-    p = ngx_copy(data, h->value.data, prefix);
-
-    if (replacement.len) {
-        p = ngx_copy(p, replacement.data, replacement.len);
-    }
-
-    ngx_memcpy(p, h->value.data + prefix + pattern.len,
-               h->value.len - pattern.len - prefix);
-
-    h->value.len = len;
-    h->value.data = data;
-
-    return NGX_OK;
+    return ngx_http_proxy_rewrite(r, h, prefix, pattern.len, &replacement);
 }
 
 
@@ -2347,13 +2332,11 @@
 
 static ngx_int_t
 ngx_http_proxy_rewrite_regex_handler(ngx_http_request_t *r, ngx_table_elt_t *h,
-    size_t prefix, ngx_http_proxy_rewrite_t *pr)
+    size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
 {
-    size_t      len;
-    u_char     *data;
-    ngx_str_t   pattern, replacement;
+    ngx_str_t  pattern, replacement;
 
-    pattern.len = h->value.len - prefix;
+    pattern.len = len;
     pattern.data = h->value.data + prefix;
 
     if (ngx_http_regex_exec(r, pr->pattern.regex, &pattern) != NGX_OK) {
@@ -2364,30 +2347,55 @@
         return NGX_ERROR;
     }
 
-    if (!prefix) {
+    if (prefix == 0 && h->value.len == len) {
         h->value = replacement;
         return NGX_OK;
     }
 
-    len = prefix + replacement.len;
+    return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement);
+}
 
-    data = ngx_pnalloc(r->pool, len);
-    if (data == NULL) {
-        return NGX_ERROR;
+#endif
+
+
+static ngx_int_t
+ngx_http_proxy_rewrite(ngx_http_request_t *r, ngx_table_elt_t *h, size_t prefix,
+    size_t len, ngx_str_t *replacement)
+{
+    u_char  *p, *data;
+    size_t   new_len;
+
+    new_len = replacement->len + h->value.len - len;
+
+    if (replacement->len > len) {
+
+        data = ngx_pnalloc(r->pool, new_len);
+        if (data == NULL) {
+            return NGX_ERROR;
+        }
+
+        p = ngx_copy(data, h->value.data, prefix);
+        p = ngx_copy(p, replacement->data, replacement->len);
+
+        ngx_memcpy(p, h->value.data + prefix + len,
+                   h->value.len - len - prefix);
+
+        h->value.data = data;
+
+    } else {
+        p = ngx_copy(h->value.data + prefix, replacement->data,
+                     replacement->len);
+
+        ngx_memmove(p, h->value.data + prefix + len,
+                    h->value.len - len - prefix);
     }
 
-    ngx_memcpy(data, h->value.data, prefix);
-    ngx_memcpy(data + prefix, replacement.data, replacement.len);
+    h->value.len = new_len;
 
-    h->value.len = len;
-    h->value.data = data;
-
     return NGX_OK;
 }
 
-#endif
 
-
 static ngx_int_t
 ngx_http_proxy_add_variables(ngx_conf_t *cf)
 {



More information about the nginx-devel mailing list