[nginx] Added ngx_http_set_complex_value_size_slot().

Ruslan Ermilov ru at nginx.com
Wed Apr 24 18:47:24 UTC 2019


details:   https://hg.nginx.org/nginx/rev/b82162b8496a
branches:  
changeset: 7503:b82162b8496a
user:      Ruslan Ermilov <ru at nginx.com>
date:      Wed Apr 24 16:38:51 2019 +0300
description:
Added ngx_http_set_complex_value_size_slot().

If a complex value is expected to be of type size_t, and the compiled
value is constant, the constant size_t value is remembered at compile
time.

The value is accessed through ngx_http_complex_value_size() which
either returns the remembered constant or evaluates the expression
and parses it as size_t.

diffstat:

 src/http/ngx_http_script.c     |  61 ++++++++++++++++++++++++++++++++++++++++++
 src/http/ngx_http_script.h     |   8 +++++
 src/stream/ngx_stream_script.c |  61 ++++++++++++++++++++++++++++++++++++++++++
 src/stream/ngx_stream_script.h |   8 +++++
 4 files changed, 138 insertions(+), 0 deletions(-)

diffs (213 lines):

diff -r b7a7c02aea3a -r b82162b8496a src/http/ngx_http_script.c
--- a/src/http/ngx_http_script.c	Tue Apr 09 11:40:20 2019 +0300
+++ b/src/http/ngx_http_script.c	Wed Apr 24 16:38:51 2019 +0300
@@ -104,6 +104,37 @@ ngx_http_complex_value(ngx_http_request_
 }
 
 
+size_t
+ngx_http_complex_value_size(ngx_http_request_t *r,
+    ngx_http_complex_value_t *val, size_t default_value)
+{
+    size_t     size;
+    ngx_str_t  value;
+
+    if (val == NULL) {
+        return default_value;
+    }
+
+    if (val->lengths == NULL) {
+        return val->u.size;
+    }
+
+    if (ngx_http_complex_value(r, val, &value) != NGX_OK) {
+        return default_value;
+    }
+
+    size = ngx_parse_size(&value);
+
+    if (size == (size_t) NGX_ERROR) {
+        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                      "invalid size \"%V\"", &value);
+        return default_value;
+    }
+
+    return size;
+}
+
+
 ngx_int_t
 ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)
 {
@@ -244,6 +275,36 @@ ngx_http_set_complex_value_slot(ngx_conf
 }
 
 
+char *
+ngx_http_set_complex_value_size_slot(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf)
+{
+    char  *p = conf;
+
+    char                      *rv;
+    ngx_http_complex_value_t  *cv;
+
+    rv = ngx_http_set_complex_value_slot(cf, cmd, conf);
+
+    if (rv != NGX_CONF_OK) {
+        return rv;
+    }
+
+    cv = *(ngx_http_complex_value_t **) (p + cmd->offset);
+
+    if (cv->lengths) {
+        return NGX_CONF_OK;
+    }
+
+    cv->u.size = ngx_parse_size(&cv->value);
+    if (cv->u.size == (size_t) NGX_ERROR) {
+        return "invalid value";
+    }
+
+    return NGX_CONF_OK;
+}
+
+
 ngx_int_t
 ngx_http_test_predicates(ngx_http_request_t *r, ngx_array_t *predicates)
 {
diff -r b7a7c02aea3a -r b82162b8496a src/http/ngx_http_script.h
--- a/src/http/ngx_http_script.h	Tue Apr 09 11:40:20 2019 +0300
+++ b/src/http/ngx_http_script.h	Wed Apr 24 16:38:51 2019 +0300
@@ -68,6 +68,10 @@ typedef struct {
     ngx_uint_t                 *flushes;
     void                       *lengths;
     void                       *values;
+
+    union {
+        size_t                  size;
+    } u;
 } ngx_http_complex_value_t;
 
 
@@ -207,9 +211,13 @@ void ngx_http_script_flush_complex_value
     ngx_http_complex_value_t *val);
 ngx_int_t ngx_http_complex_value(ngx_http_request_t *r,
     ngx_http_complex_value_t *val, ngx_str_t *value);
+size_t ngx_http_complex_value_size(ngx_http_request_t *r,
+    ngx_http_complex_value_t *val, size_t default_value);
 ngx_int_t ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv);
 char *ngx_http_set_complex_value_slot(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
+char *ngx_http_set_complex_value_size_slot(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
 
 
 ngx_int_t ngx_http_test_predicates(ngx_http_request_t *r,
diff -r b7a7c02aea3a -r b82162b8496a src/stream/ngx_stream_script.c
--- a/src/stream/ngx_stream_script.c	Tue Apr 09 11:40:20 2019 +0300
+++ b/src/stream/ngx_stream_script.c	Wed Apr 24 16:38:51 2019 +0300
@@ -105,6 +105,37 @@ ngx_stream_complex_value(ngx_stream_sess
 }
 
 
+size_t
+ngx_stream_complex_value_size(ngx_stream_session_t *s,
+    ngx_stream_complex_value_t *val, size_t default_value)
+{
+    size_t     size;
+    ngx_str_t  value;
+
+    if (val == NULL) {
+        return default_value;
+    }
+
+    if (val->lengths == NULL) {
+        return val->u.size;
+    }
+
+    if (ngx_stream_complex_value(s, val, &value) != NGX_OK) {
+        return default_value;
+    }
+
+    size = ngx_parse_size(&value);
+
+    if (size == (size_t) NGX_ERROR) {
+        ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
+                      "invalid size \"%V\"", &value);
+        return default_value;
+    }
+
+    return size;
+}
+
+
 ngx_int_t
 ngx_stream_compile_complex_value(ngx_stream_compile_complex_value_t *ccv)
 {
@@ -246,6 +277,36 @@ ngx_stream_set_complex_value_slot(ngx_co
 }
 
 
+char *
+ngx_stream_set_complex_value_size_slot(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf)
+{
+    char  *p = conf;
+
+    char                        *rv;
+    ngx_stream_complex_value_t  *cv;
+
+    rv = ngx_stream_set_complex_value_slot(cf, cmd, conf);
+
+    if (rv != NGX_CONF_OK) {
+        return rv;
+    }
+
+    cv = *(ngx_stream_complex_value_t **) (p + cmd->offset);
+
+    if (cv->lengths) {
+        return NGX_CONF_OK;
+    }
+
+    cv->u.size = ngx_parse_size(&cv->value);
+    if (cv->u.size == (size_t) NGX_ERROR) {
+        return "invalid value";
+    }
+
+    return NGX_CONF_OK;
+}
+
+
 ngx_uint_t
 ngx_stream_script_variables_count(ngx_str_t *value)
 {
diff -r b7a7c02aea3a -r b82162b8496a src/stream/ngx_stream_script.h
--- a/src/stream/ngx_stream_script.h	Tue Apr 09 11:40:20 2019 +0300
+++ b/src/stream/ngx_stream_script.h	Wed Apr 24 16:38:51 2019 +0300
@@ -56,6 +56,10 @@ typedef struct {
     ngx_uint_t                   *flushes;
     void                         *lengths;
     void                         *values;
+
+    union {
+        size_t                    size;
+    } u;
 } ngx_stream_complex_value_t;
 
 
@@ -102,10 +106,14 @@ void ngx_stream_script_flush_complex_val
     ngx_stream_complex_value_t *val);
 ngx_int_t ngx_stream_complex_value(ngx_stream_session_t *s,
     ngx_stream_complex_value_t *val, ngx_str_t *value);
+size_t ngx_stream_complex_value_size(ngx_stream_session_t *s,
+    ngx_stream_complex_value_t *val, size_t default_value);
 ngx_int_t ngx_stream_compile_complex_value(
     ngx_stream_compile_complex_value_t *ccv);
 char *ngx_stream_set_complex_value_slot(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
+char *ngx_stream_set_complex_value_size_slot(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
 
 
 ngx_uint_t ngx_stream_script_variables_count(ngx_str_t *value);


More information about the nginx-devel mailing list