From d48ca6a408d5db088be6663c2f50d7ae7e76a038 Mon Sep 17 00:00:00 2001 From: detailyang Date: Sun, 14 Aug 2016 23:31:52 +0800 Subject: [PATCH 01/26] add objs/* and autoconf to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 39c82fa..982b959 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,6 @@ nginx *.plist a.patch Makefile +autoconf.err +autotest +objs/* From 4b5f07981b279334e64e1440a909fd943481eace Mon Sep 17 00:00:00 2001 From: detailyang Date: Sun, 14 Aug 2016 23:34:24 +0800 Subject: [PATCH 02/26] add -i option (if not set) to more_set_input_headers --- src/ngx_http_headers_more_filter_module.h | 1 + src/ngx_http_headers_more_headers_in.c | 31 ++++++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/ngx_http_headers_more_filter_module.h b/src/ngx_http_headers_more_filter_module.h index 72a5317..df02728 100644 --- a/src/ngx_http_headers_more_filter_module.h +++ b/src/ngx_http_headers_more_filter_module.h @@ -61,6 +61,7 @@ struct ngx_http_headers_more_header_val_s { ngx_http_headers_more_set_header_pt handler; ngx_uint_t offset; ngx_flag_t replace; + ngx_flag_t ifnotset; ngx_flag_t wildcard; }; diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index f903908..b220403 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -358,6 +358,11 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, return ngx_http_set_header_helper(r, hv, value, old); } + if (hv->ifnotset) { + dd("skip because of it does set"); + return NGX_OK; + } + h = *old; if (value->len == 0) { @@ -366,7 +371,7 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, return ngx_http_set_header_helper(r, hv, value, old); } - + h->hash = hv->hash; h->value = *value; @@ -496,19 +501,20 @@ static char * ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, void *conf, ngx_http_headers_more_opcode_t opcode) { + ngx_flag_t replace = 0; + ngx_flag_t ifnotset = 0; ngx_http_headers_more_loc_conf_t *hlcf = conf; - ngx_uint_t i; - ngx_http_headers_more_cmd_t *cmd; ngx_str_t *arg; - ngx_flag_t ignore_next_arg; ngx_str_t *cmd_name; ngx_int_t rc; - ngx_flag_t replace = 0; + ngx_uint_t i; + ngx_flag_t ignore_next_arg; + ngx_http_headers_more_cmd_t *cmd; ngx_http_headers_more_header_val_t *h; - ngx_http_headers_more_main_conf_t *hmcf; - + + if (hlcf->cmds == NULL) { hlcf->cmds = ngx_array_create(cf->pool, 1, sizeof(ngx_http_headers_more_cmd_t)); @@ -595,6 +601,11 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, replace = 1; continue; } + if (arg[i].data[1] == 'i') { + dd("Found if not set flag"); + ifnotset = 1; + continue; + } } ngx_log_error(NGX_LOG_ERR, cf->log, 0, @@ -615,6 +626,7 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, h = cmd->headers->elts; for (i = 0; i < cmd->headers->nelts; i++) { h[i].replace = replace; + h[i].ifnotset = ifnotset; } } @@ -741,6 +753,11 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r, headers = (ngx_array_t *) ((char *) &r->headers_in + hv->offset); if (headers->nelts > 0) { + if (hv->ifnotset) { + dd("skip multi-value headers because of it does set"); + return NGX_OK; + } + ngx_array_destroy(headers); if (ngx_array_init(headers, r->pool, 2, From 834453119bcdb5c2d39d306822b4e2a0058d1a2c Mon Sep 17 00:00:00 2001 From: detailyang Date: Mon, 15 Aug 2016 00:07:15 +0800 Subject: [PATCH 03/26] add -i option (if not set) to more_set_headers --- src/ngx_http_headers_more_headers_out.c | 39 ++++++++++++++++++++----- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/ngx_http_headers_more_headers_out.c b/src/ngx_http_headers_more_headers_out.c index 0f9bc87..36ed065 100644 --- a/src/ngx_http_headers_more_headers_out.c +++ b/src/ngx_http_headers_more_headers_out.c @@ -223,6 +223,10 @@ ngx_http_set_header_helper(ngx_http_request_t *r, continue; matched: + if (hv->ifnotset) { + dd("skip because of it does set"); + return NGX_OK; + } if (value->len == 0 || matched) { dd("clearing normal header for %.*s", (int) hv->key.len, @@ -303,6 +307,11 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, if (old == NULL || *old == NULL) { return ngx_http_set_header_helper(r, hv, value, old, 0); } + + if (hv->ifnotset) { + dd("skip because of it does set"); + return NGX_OK; + } h = *old; @@ -344,6 +353,11 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r, /* override old values (if any) */ if (pa->nelts > 0) { + if (hv->ifnotset) { + dd("skip because of it does set"); + return NGX_OK; + } + ph = pa->elts; for (i = 1; i < pa->nelts; i++) { ph[i]->hash = 0; @@ -565,16 +579,17 @@ static char * ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, void *conf, ngx_http_headers_more_opcode_t opcode) { + ngx_flag_t ifnotset = 0; ngx_http_headers_more_loc_conf_t *hlcf = conf; - ngx_uint_t i; - ngx_http_headers_more_cmd_t *cmd; - ngx_str_t *arg; - ngx_flag_t ignore_next_arg; - ngx_str_t *cmd_name; - ngx_int_t rc; - - ngx_http_headers_more_main_conf_t *hmcf; + ngx_str_t *arg; + ngx_str_t *cmd_name; + ngx_int_t rc; + ngx_flag_t ignore_next_arg; + ngx_uint_t i; + ngx_http_headers_more_cmd_t *cmd; + ngx_http_headers_more_main_conf_t *hmcf; + ngx_http_headers_more_header_val_t *h; if (hlcf->cmds == NULL) { hlcf->cmds = ngx_array_create(cf->pool, 1, @@ -679,6 +694,9 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, ignore_next_arg = 1; + continue; + } else if (arg[i].data[1] == 'i') { + ifnotset = 1; continue; } } @@ -695,6 +713,11 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, if (cmd->headers->nelts == 0) { cmd->headers = NULL; + } else { + h = cmd->headers->elts; + for (i = 0; i < cmd->headers->nelts; i++) { + h[i].ifnotset = ifnotset; + } } if (cmd->types->nelts == 0) { From 3b64ac787a138d3c260b5c5b64fd6291af20b42c Mon Sep 17 00:00:00 2001 From: detailyang Date: Mon, 15 Aug 2016 21:34:25 +0800 Subject: [PATCH 04/26] allow if no set on non builtin header on more_set_headers --- src/ngx_http_headers_more_headers_in.c | 11 ++++++----- src/ngx_http_headers_more_headers_out.c | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index b220403..0d75669 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -268,12 +268,13 @@ ngx_http_set_header_helper(ngx_http_request_t *r, return NGX_ERROR; } + if (hv->ifnotset == 0) { + h[i].value = *value; - h[i].value = *value; - - if (output_header) { - *output_header = &h[i]; - dd("setting existing builtin input header"); + if (output_header) { + *output_header = &h[i]; + dd("setting existing builtin input header"); + } } if (matched == NULL) { diff --git a/src/ngx_http_headers_more_headers_out.c b/src/ngx_http_headers_more_headers_out.c index 36ed065..3eb67a0 100644 --- a/src/ngx_http_headers_more_headers_out.c +++ b/src/ngx_http_headers_more_headers_out.c @@ -225,7 +225,8 @@ ngx_http_set_header_helper(ngx_http_request_t *r, matched: if (hv->ifnotset) { dd("skip because of it does set"); - return NGX_OK; + matched = 1; + continue; } if (value->len == 0 || matched) { From 6d45d4fa0d2255837363f55c074eed3c5c4648aa Mon Sep 17 00:00:00 2001 From: detailyang Date: Mon, 15 Aug 2016 21:57:33 +0800 Subject: [PATCH 05/26] add test for -i option --- t/input.t | 29 ++++++++++++++++++++++++++++- t/sanity.t | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/t/input.t b/t/input.t index 0b8989f..fa4c283 100644 --- a/t/input.t +++ b/t/input.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; # 'no_plan'; repeat_each(2); -plan tests => repeat_each() * 124; +plan tests => repeat_each() * 128; no_long_string(); #no_diff; @@ -1289,3 +1289,30 @@ X-Forwarded-For: 8.8.8.8 Foo: 127.0.0.1 --- no_error_log [error] + + +=== TEST 50: set request header if not set the header with -i option +--- config + location /foo { + more_set_input_headers -i 'X-Foo: howdy'; + echo "input_header: $http_x_foo"; + } +--- request + GET /foo +--- response_body +input_header: howdy + + +=== TEST 51: do not set request header if set the header with -i option +--- config + location /foo { + more_set_input_headers -i 'X-Foo: howdy'; + echo "input_header: $http_x_foo"; + } +--- request + GET /foo +--- more_headers +X-Foo: blah + +--- response_body +input_header: blah diff --git a/t/sanity.t b/t/sanity.t index e316cac..6f59785 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; repeat_each(2); -plan tests => repeat_each() * 113; +plan tests => repeat_each() * 119; #master_on(); #workers(2); @@ -566,3 +566,36 @@ hi ok --- http09 + +=== TEST 34: do not set response header if set the header with -i option +--- config + location /foo { + more_set_headers -i 'X-Foo: bar'; + echo hi; + } +--- request + GET /foo +--- response_headers +X-Foo: bar +--- response_body +hi + + +=== TEST 35: set the response header if set the header with -i option +--- config + location = /backend { + add_header X-Foo baz; + echo hi; + } + + location /foo { + more_set_headers -i 'X-Foo: bar'; + proxy_pass http://127.0.0.1:$server_port/backend; + } +--- request + GET /foo +--- response_headers +X-Foo: baz +--- response_body +hi + From 5c730811686075d75aa046f275875a099caad49c Mon Sep 17 00:00:00 2001 From: detailyang Date: Mon, 15 Aug 2016 22:03:58 +0800 Subject: [PATCH 06/26] better prompt when skip with -i option --- src/ngx_http_headers_more_headers_in.c | 4 ++-- src/ngx_http_headers_more_headers_out.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index 0d75669..c1f2e7f 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -360,7 +360,7 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, } if (hv->ifnotset) { - dd("skip because of it does set"); + dd("skip because %s does set", hv->key.data); return NGX_OK; } @@ -755,7 +755,7 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r, if (headers->nelts > 0) { if (hv->ifnotset) { - dd("skip multi-value headers because of it does set"); + dd("skip multi-value headers because %s does set", hv->key.data); return NGX_OK; } diff --git a/src/ngx_http_headers_more_headers_out.c b/src/ngx_http_headers_more_headers_out.c index 3eb67a0..f436085 100644 --- a/src/ngx_http_headers_more_headers_out.c +++ b/src/ngx_http_headers_more_headers_out.c @@ -224,7 +224,7 @@ ngx_http_set_header_helper(ngx_http_request_t *r, matched: if (hv->ifnotset) { - dd("skip because of it does set"); + dd("skip because %s does set", hv->key.data); matched = 1; continue; } @@ -310,7 +310,7 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, } if (hv->ifnotset) { - dd("skip because of it does set"); + dd("skip because %s does set", hv->key.data); return NGX_OK; } @@ -355,7 +355,7 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r, if (pa->nelts > 0) { if (hv->ifnotset) { - dd("skip because of it does set"); + dd("skip because %s does set", hv->key.data); return NGX_OK; } From c802c274451cbfd311c5c2a998950ff0397741ba Mon Sep 17 00:00:00 2001 From: detailyang Date: Mon, 15 Aug 2016 22:18:48 +0800 Subject: [PATCH 07/26] add -i option description to README --- README.markdown | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 615ce82..bbf67c7 100644 --- a/README.markdown +++ b/README.markdown @@ -74,6 +74,9 @@ Synopsis # replace input header X-Foo *only* if it already exists more_set_input_headers -r 'X-Foo: howdy'; + + # add input header X-Foo *only* if it doesnt exist + more_set_input_headers -i 'X-Foo: howdy'; ``` Description @@ -134,7 +137,7 @@ Directives more_set_headers ---------------- -**syntax:** *more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...* +**syntax:** *more_set_headers [-i] [-t <content-type list>]... [-s <status-code list>]... <new-header>...* **default:** *no* @@ -153,6 +156,8 @@ If either `-s` or `-t` is not specified or has an empty list value, then no matc Existing response headers with the same name are always overridden. If you want to add headers incrementally, use the standard [add_header](http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header) directive instead. +If the `-i` options is specified, then the headers will be added to the new values *only if* they doesnt exist. + A single directive can set/add multiple output headers. For example ```nginx @@ -261,7 +266,7 @@ The `*` wildcard support was first introduced in [v0.09](#v009). more_set_input_headers ---------------------- -**syntax:** *more_set_input_headers [-r] [-t <content-type list>]... <new-header>...* +**syntax:** *more_set_input_headers [-r] [-i] [-t <content-type list>]... <new-header>...* **default:** *no* @@ -280,6 +285,8 @@ and works in subrequests as well. If the `-r` option is specified, then the headers will be replaced to the new values *only if* they already exist. +If the `-i` options is specified, then the headers will be added to the new values *only if* they doesnt exist. + [Back to TOC](#table-of-contents) more_clear_input_headers From 48c4d430f66c914788a615951e7cb5460b13523c Mon Sep 17 00:00:00 2001 From: detailyang Date: Tue, 16 Aug 2016 09:28:58 +0800 Subject: [PATCH 08/26] if no set should work on multi header --- src/ngx_http_headers_more_headers_in.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index c1f2e7f..995d9a3 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -248,6 +248,10 @@ ngx_http_set_header_helper(ngx_http_request_t *r, h[i].key.len) == 0) { if (value->len == 0 || (matched && matched != &h[i])) { + if (hv->ifnotset == 1) { + continue; + } + h[i].hash = 0; rc = ngx_http_headers_more_rm_header_helper( From 12c3a00e4fdae4265f9beb418c9a15898a48e933 Mon Sep 17 00:00:00 2001 From: detailyang Date: Wed, 17 Aug 2016 22:39:33 +0800 Subject: [PATCH 09/26] work together on -r replace and -i if not set --- src/ngx_http_headers_more_headers_in.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index 995d9a3..2cab9ba 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -247,11 +247,11 @@ ngx_http_set_header_helper(ngx_http_request_t *r, && ngx_strncasecmp(h[i].key.data, hv->key.data, h[i].key.len) == 0) { - if (value->len == 0 || (matched && matched != &h[i])) { - if (hv->ifnotset == 1) { - continue; - } + if (hv->ifnotset == 1 && hv->replace == 0) { + continue; + } + if (value->len == 0 || (matched && matched != &h[i])) { h[i].hash = 0; rc = ngx_http_headers_more_rm_header_helper( @@ -272,13 +272,11 @@ ngx_http_set_header_helper(ngx_http_request_t *r, return NGX_ERROR; } - if (hv->ifnotset == 0) { - h[i].value = *value; + h[i].value = *value; - if (output_header) { - *output_header = &h[i]; - dd("setting existing builtin input header"); - } + if (output_header) { + *output_header = &h[i]; + dd("setting existing builtin input header"); } if (matched == NULL) { @@ -291,7 +289,7 @@ ngx_http_set_header_helper(ngx_http_request_t *r, return NGX_OK; } - if (value->len == 0 || hv->replace) { + if (value->len == 0 || (hv->replace && hv->ifnotset == 0)) { return NGX_OK; } From 30daad108182394d643f3e59cd17e1c034a9cfad Mon Sep 17 00:00:00 2001 From: detailyang Date: Wed, 17 Aug 2016 22:40:14 +0800 Subject: [PATCH 10/26] add test for -i (if not set) option --- t/input.t | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/t/input.t b/t/input.t index fa4c283..6d8842c 100644 --- a/t/input.t +++ b/t/input.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; # 'no_plan'; repeat_each(2); -plan tests => repeat_each() * 128; +plan tests => repeat_each() * 136; no_long_string(); #no_diff; @@ -1316,3 +1316,58 @@ X-Foo: blah --- response_body input_header: blah + + +=== TEST 52: do not remove multi request header if set the header with -i option +--- config + location /foo { + more_set_input_headers -i 'X-Foo: howdy'; + content_by_lua ' + local headers = ngx.req.get_headers() + ngx.say(headers["AAA"]) + '; + } +--- request + GET /foo +--- more_headers +AAA: blah +AAA: baz + +--- response_body +blahbaz + +=== TEST 53: test -i -t work together +--- config + location /foo { + more_set_input_headers -i -t 'text/html' 'X-Foo: howdy'; + content_by_lua ' + local headers = ngx.req.get_headers() + ngx.say(headers["X-Foo"]) + '; + } +--- request + GET /foo +--- more_headers +Content-Type: text/html + +--- response_body +howdy + + +=== TEST 54: test -i -r work together +--- config + location /foo { + more_set_input_headers -i -r 'X-Foo: howdy'; + content_by_lua ' + local headers = ngx.req.get_headers() + ngx.say(headers["X-Foo"]) + '; + } +--- request eval +["GET /foo", "GET /foo"] +--- more_headers eval +["X-Foo: hi", ""] + +--- response_body eval +["howdy\n", "howdy\n"] + From 9c53270cb152a99618454ae00e8492ee1ce3e7b6 Mon Sep 17 00:00:00 2001 From: detailyang Date: Wed, 17 Aug 2016 22:59:47 +0800 Subject: [PATCH 11/26] Fix: do not set header when header already set --- src/ngx_http_headers_more_headers_in.c | 46 ++++++++++++-------------- t/input.t | 8 +++-- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index 2cab9ba..7e0434b 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -247,38 +247,36 @@ ngx_http_set_header_helper(ngx_http_request_t *r, && ngx_strncasecmp(h[i].key.data, hv->key.data, h[i].key.len) == 0) { - if (hv->ifnotset == 1 && hv->replace == 0) { - continue; - } - - if (value->len == 0 || (matched && matched != &h[i])) { - h[i].hash = 0; + if ((hv->ifnotset == 1 && hv->replace == 0) == 0) { + if (value->len == 0 || (matched && matched != &h[i])) { + h[i].hash = 0; - rc = ngx_http_headers_more_rm_header_helper( - &r->headers_in.headers, part, i); + rc = ngx_http_headers_more_rm_header_helper( + &r->headers_in.headers, part, i); - ngx_http_headers_more_assert( - !(r->headers_in.headers.part.next == NULL - && r->headers_in.headers.last - != &r->headers_in.headers.part)); + ngx_http_headers_more_assert( + !(r->headers_in.headers.part.next == NULL + && r->headers_in.headers.last + != &r->headers_in.headers.part)); - if (rc == NGX_OK) { - if (output_header) { - *output_header = NULL; + if (rc == NGX_OK) { + if (output_header) { + *output_header = NULL; + } + + goto retry; } - goto retry; + return NGX_ERROR; } + h[i].value = *value; - return NGX_ERROR; - } - h[i].value = *value; - - if (output_header) { - *output_header = &h[i]; - dd("setting existing builtin input header"); + if (output_header) { + *output_header = &h[i]; + dd("setting existing builtin input header"); + } } - + if (matched == NULL) { matched = &h[i]; } diff --git a/t/input.t b/t/input.t index 6d8842c..c0b9057 100644 --- a/t/input.t +++ b/t/input.t @@ -1307,7 +1307,10 @@ input_header: howdy --- config location /foo { more_set_input_headers -i 'X-Foo: howdy'; - echo "input_header: $http_x_foo"; + content_by_lua ' + local headers = ngx.req.get_headers() + ngx.say(headers["X-Foo"]) + '; } --- request GET /foo @@ -1315,8 +1318,7 @@ input_header: howdy X-Foo: blah --- response_body -input_header: blah - +blah === TEST 52: do not remove multi request header if set the header with -i option --- config From ec1fad67f1e6ebe0005180cd1f367008f5a16e73 Mon Sep 17 00:00:00 2001 From: detailyang Date: Wed, 17 Aug 2016 23:05:28 +0800 Subject: [PATCH 12/26] add test for -r -t -i --- t/input.t | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/t/input.t b/t/input.t index c0b9057..3814319 100644 --- a/t/input.t +++ b/t/input.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; # 'no_plan'; repeat_each(2); -plan tests => repeat_each() * 136; +plan tests => repeat_each() * 142; no_long_string(); #no_diff; @@ -1373,3 +1373,21 @@ howdy --- response_body eval ["howdy\n", "howdy\n"] + +=== TEST 55: test -i -r -t work together +--- config + location /foo { + more_set_input_headers -i -r -t 'text/html' 'X-Foo: howdy'; + content_by_lua ' + local headers = ngx.req.get_headers() + ngx.say(headers["X-Foo"]) + '; + } +--- request eval +["GET /foo", "GET /foo", "GET /foo"] +--- more_headers eval +["Content-Type: text/html", "", "Content-Type: text/html\nX-Foo: hi\n"] + +--- response_body eval +["howdy\n", "nil\n", "howdy\n"] + From ac523588b1fef6623e42f5bf4c2e7f4c444ad995 Mon Sep 17 00:00:00 2001 From: detailyang Date: Wed, 17 Aug 2016 23:13:09 +0800 Subject: [PATCH 13/26] add test for more_set_headers on -i -t work together --- t/sanity.t | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/t/sanity.t b/t/sanity.t index 6f59785..e20b570 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; repeat_each(2); -plan tests => repeat_each() * 119; +plan tests => repeat_each() * 124; #master_on(); #workers(2); @@ -599,3 +599,27 @@ X-Foo: baz --- response_body hi + +=== TEST 36: test -t -i work together +--- config + location = /backend { + add_header Content-Type text/html; + echo hi; + } + + location /foo { + more_set_headers -t 'text/plain' -i 'X-Foo: bar'; + proxy_pass http://127.0.0.1:$server_port/backend; + } + + location /bar { + more_set_headers -t 'text/html' -i 'X-Foo: bar'; + proxy_pass http://127.0.0.1:$server_port/backend; + } + +--- request eval +["GET /foo", "GET /bar"] +--- response_headers eval +["", "X-Foo: bar"] +--- response_body eval +["hi\n", "hi\n"] From e9fbadf6eaea1e7d23dfc3d2c4b65c4493a3f982 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 09:43:35 +0800 Subject: [PATCH 14/26] remove misc in gitignore because of we are using the standard building process --- .gitignore | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 982b959..a2b59ab 100644 --- a/.gitignore +++ b/.gitignore @@ -51,7 +51,4 @@ cov nginx *.plist a.patch -Makefile -autoconf.err -autotest -objs/* +Makefile \ No newline at end of file From e4202d1d42ea3e11476d865b5fa8498525705b79 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:04:48 +0800 Subject: [PATCH 15/26] reindex t/*.t --- t/input.t | 32 ++++++++++++++++++++------------ t/sanity.t | 3 +++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/t/input.t b/t/input.t index 3814319..3ac4d26 100644 --- a/t/input.t +++ b/t/input.t @@ -1291,10 +1291,11 @@ Foo: 127.0.0.1 [error] -=== TEST 50: set request header if not set the header with -i option + +=== TEST 50: set request header if not set the header with -a option --- config location /foo { - more_set_input_headers -i 'X-Foo: howdy'; + more_set_input_headers -a 'X-Foo: howdy'; echo "input_header: $http_x_foo"; } --- request @@ -1303,10 +1304,11 @@ Foo: 127.0.0.1 input_header: howdy -=== TEST 51: do not set request header if set the header with -i option + +=== TEST 51: do not set request header if set the header with -a option --- config location /foo { - more_set_input_headers -i 'X-Foo: howdy'; + more_set_input_headers -a 'X-Foo: howdy'; content_by_lua ' local headers = ngx.req.get_headers() ngx.say(headers["X-Foo"]) @@ -1320,10 +1322,12 @@ X-Foo: blah --- response_body blah -=== TEST 52: do not remove multi request header if set the header with -i option + + +=== TEST 52: do not remove multi request header if set the header with -a option --- config location /foo { - more_set_input_headers -i 'X-Foo: howdy'; + more_set_input_headers -a 'X-Foo: howdy'; content_by_lua ' local headers = ngx.req.get_headers() ngx.say(headers["AAA"]) @@ -1338,10 +1342,12 @@ AAA: baz --- response_body blahbaz -=== TEST 53: test -i -t work together + + +=== TEST 53: test -a -t work together --- config location /foo { - more_set_input_headers -i -t 'text/html' 'X-Foo: howdy'; + more_set_input_headers -a -t 'text/html' 'X-Foo: howdy'; content_by_lua ' local headers = ngx.req.get_headers() ngx.say(headers["X-Foo"]) @@ -1356,10 +1362,11 @@ Content-Type: text/html howdy -=== TEST 54: test -i -r work together + +=== TEST 54: test -a -r work together --- config location /foo { - more_set_input_headers -i -r 'X-Foo: howdy'; + more_set_input_headers -a -r 'X-Foo: howdy'; content_by_lua ' local headers = ngx.req.get_headers() ngx.say(headers["X-Foo"]) @@ -1374,10 +1381,11 @@ howdy ["howdy\n", "howdy\n"] -=== TEST 55: test -i -r -t work together + +=== TEST 55: test -a -r -t work together --- config location /foo { - more_set_input_headers -i -r -t 'text/html' 'X-Foo: howdy'; + more_set_input_headers -a -r -t 'text/html' 'X-Foo: howdy'; content_by_lua ' local headers = ngx.req.get_headers() ngx.say(headers["X-Foo"]) diff --git a/t/sanity.t b/t/sanity.t index e20b570..58a665b 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -567,6 +567,7 @@ ok --- http09 + === TEST 34: do not set response header if set the header with -i option --- config location /foo { @@ -581,6 +582,7 @@ X-Foo: bar hi + === TEST 35: set the response header if set the header with -i option --- config location = /backend { @@ -600,6 +602,7 @@ X-Foo: baz hi + === TEST 36: test -t -i work together --- config location = /backend { From 0a6073d02cd5b6200d140188764cbf40449ede49 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:05:04 +0800 Subject: [PATCH 16/26] correct code style --- src/ngx_http_headers_more_filter_module.h | 2 +- src/ngx_http_headers_more_headers_in.c | 30 +++++++++++------------ src/ngx_http_headers_more_headers_out.c | 20 +++++++++------ 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/ngx_http_headers_more_filter_module.h b/src/ngx_http_headers_more_filter_module.h index df02728..abbdc9e 100644 --- a/src/ngx_http_headers_more_filter_module.h +++ b/src/ngx_http_headers_more_filter_module.h @@ -61,7 +61,7 @@ struct ngx_http_headers_more_header_val_s { ngx_http_headers_more_set_header_pt handler; ngx_uint_t offset; ngx_flag_t replace; - ngx_flag_t ifnotset; + ngx_flag_t add_only; ngx_flag_t wildcard; }; diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index 7e0434b..7c96dbf 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -247,7 +247,7 @@ ngx_http_set_header_helper(ngx_http_request_t *r, && ngx_strncasecmp(h[i].key.data, hv->key.data, h[i].key.len) == 0) { - if ((hv->ifnotset == 1 && hv->replace == 0) == 0) { + if ((hv->add_only == 1 && hv->replace == 0) == 0) { if (value->len == 0 || (matched && matched != &h[i])) { h[i].hash = 0; @@ -276,7 +276,7 @@ ngx_http_set_header_helper(ngx_http_request_t *r, dd("setting existing builtin input header"); } } - + if (matched == NULL) { matched = &h[i]; } @@ -287,7 +287,7 @@ ngx_http_set_header_helper(ngx_http_request_t *r, return NGX_OK; } - if (value->len == 0 || (hv->replace && hv->ifnotset == 0)) { + if (value->len == 0 || (hv->replace && !hv->add_only)) { return NGX_OK; } @@ -359,11 +359,11 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, return ngx_http_set_header_helper(r, hv, value, old); } - if (hv->ifnotset) { + if (hv->add_only) { dd("skip because %s does set", hv->key.data); return NGX_OK; } - + h = *old; if (value->len == 0) { @@ -372,7 +372,7 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, return ngx_http_set_header_helper(r, hv, value, old); } - + h->hash = hv->hash; h->value = *value; @@ -503,7 +503,7 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, void *conf, ngx_http_headers_more_opcode_t opcode) { ngx_flag_t replace = 0; - ngx_flag_t ifnotset = 0; + ngx_flag_t add_only = 0; ngx_http_headers_more_loc_conf_t *hlcf = conf; ngx_str_t *arg; @@ -514,8 +514,7 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, ngx_http_headers_more_cmd_t *cmd; ngx_http_headers_more_header_val_t *h; ngx_http_headers_more_main_conf_t *hmcf; - - + if (hlcf->cmds == NULL) { hlcf->cmds = ngx_array_create(cf->pool, 1, sizeof(ngx_http_headers_more_cmd_t)); @@ -602,9 +601,10 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, replace = 1; continue; } - if (arg[i].data[1] == 'i') { - dd("Found if not set flag"); - ifnotset = 1; + + if (arg[i].data[1] == 'a') { + dd("Found add only flag"); + add_only = 1; continue; } } @@ -627,7 +627,7 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, h = cmd->headers->elts; for (i = 0; i < cmd->headers->nelts; i++) { h[i].replace = replace; - h[i].ifnotset = ifnotset; + h[i].add_only = add_only; } } @@ -754,9 +754,9 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r, headers = (ngx_array_t *) ((char *) &r->headers_in + hv->offset); if (headers->nelts > 0) { - if (hv->ifnotset) { + if (hv->add_only) { dd("skip multi-value headers because %s does set", hv->key.data); - return NGX_OK; + return NGX_OK; } ngx_array_destroy(headers); diff --git a/src/ngx_http_headers_more_headers_out.c b/src/ngx_http_headers_more_headers_out.c index f436085..36bc699 100644 --- a/src/ngx_http_headers_more_headers_out.c +++ b/src/ngx_http_headers_more_headers_out.c @@ -223,7 +223,8 @@ ngx_http_set_header_helper(ngx_http_request_t *r, continue; matched: - if (hv->ifnotset) { + + if (hv->add_only) { dd("skip because %s does set", hv->key.data); matched = 1; continue; @@ -308,8 +309,8 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, if (old == NULL || *old == NULL) { return ngx_http_set_header_helper(r, hv, value, old, 0); } - - if (hv->ifnotset) { + + if (hv->add_only) { dd("skip because %s does set", hv->key.data); return NGX_OK; } @@ -354,7 +355,7 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r, /* override old values (if any) */ if (pa->nelts > 0) { - if (hv->ifnotset) { + if (hv->add_only) { dd("skip because %s does set", hv->key.data); return NGX_OK; } @@ -580,7 +581,7 @@ static char * ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, void *conf, ngx_http_headers_more_opcode_t opcode) { - ngx_flag_t ifnotset = 0; + ngx_flag_t add_only = 0; ngx_http_headers_more_loc_conf_t *hlcf = conf; ngx_str_t *arg; @@ -696,8 +697,9 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, ignore_next_arg = 1; continue; - } else if (arg[i].data[1] == 'i') { - ifnotset = 1; + + } else if (arg[i].data[1] == 'a') { + add_only = 1; continue; } } @@ -714,10 +716,12 @@ ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd, if (cmd->headers->nelts == 0) { cmd->headers = NULL; + } else { h = cmd->headers->elts; + for (i = 0; i < cmd->headers->nelts; i++) { - h[i].ifnotset = ifnotset; + h[i].add_only = add_only; } } From fe4f575f1e8b1875c4f74a8555af0d0fb8419c87 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:05:43 +0800 Subject: [PATCH 17/26] typo: grammer fix --- README.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.markdown b/README.markdown index bbf67c7..c8d4801 100644 --- a/README.markdown +++ b/README.markdown @@ -76,7 +76,7 @@ Synopsis more_set_input_headers -r 'X-Foo: howdy'; # add input header X-Foo *only* if it doesnt exist - more_set_input_headers -i 'X-Foo: howdy'; + more_set_input_headers -a 'X-Foo: howdy'; ``` Description @@ -137,7 +137,7 @@ Directives more_set_headers ---------------- -**syntax:** *more_set_headers [-i] [-t <content-type list>]... [-s <status-code list>]... <new-header>...* +**syntax:** *more_set_headers [-a] [-t <content-type list>]... [-s <status-code list>]... <new-header>...* **default:** *no* @@ -156,7 +156,7 @@ If either `-s` or `-t` is not specified or has an empty list value, then no matc Existing response headers with the same name are always overridden. If you want to add headers incrementally, use the standard [add_header](http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header) directive instead. -If the `-i` options is specified, then the headers will be added to the new values *only if* they doesnt exist. +If the `-a` option is specified, then the headers will be added *only if* they do not exist. A single directive can set/add multiple output headers. For example @@ -266,7 +266,7 @@ The `*` wildcard support was first introduced in [v0.09](#v009). more_set_input_headers ---------------------- -**syntax:** *more_set_input_headers [-r] [-i] [-t <content-type list>]... <new-header>...* +**syntax:** *more_set_input_headers [-r] [-a] [-t <content-type list>]... <new-header>...* **default:** *no* @@ -285,7 +285,7 @@ and works in subrequests as well. If the `-r` option is specified, then the headers will be replaced to the new values *only if* they already exist. -If the `-i` options is specified, then the headers will be added to the new values *only if* they doesnt exist. +If the `-a` options is specified, then the headers will be added to the new values *only if* they doesnt exist. [Back to TOC](#table-of-contents) From 0c93a4c925885310c9e453eaf555c58c3b32221c Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:09:07 +0800 Subject: [PATCH 18/26] add new line in gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a2b59ab..dd5d6ab 100644 --- a/.gitignore +++ b/.gitignore @@ -51,4 +51,5 @@ cov nginx *.plist a.patch -Makefile \ No newline at end of file +Makefile + From c987b90d3209e5bc264b74815b9b87020882b2df Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:09:41 +0800 Subject: [PATCH 19/26] remove Makefile new line in gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index dd5d6ab..39c82fa 100644 --- a/.gitignore +++ b/.gitignore @@ -52,4 +52,3 @@ nginx *.plist a.patch Makefile - From df0e156946cd745554ec6f311cde1032aaf609a4 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:11:44 +0800 Subject: [PATCH 20/26] trailing line space --- src/ngx_http_headers_more_headers_in.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index 7c96dbf..b887070 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -252,7 +252,7 @@ ngx_http_set_header_helper(ngx_http_request_t *r, h[i].hash = 0; rc = ngx_http_headers_more_rm_header_helper( - &r->headers_in.headers, part, i); + &r->headers_in.headers, part, i); ngx_http_headers_more_assert( !(r->headers_in.headers.part.next == NULL From 5a0a5627343749b6608e7c0faefe5d35594235ab Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:14:10 +0800 Subject: [PATCH 21/26] typo -i to -a at t/sanity.t --- t/sanity.t | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/t/sanity.t b/t/sanity.t index 58a665b..ab54b64 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -568,10 +568,10 @@ ok -=== TEST 34: do not set response header if set the header with -i option +=== TEST 34: do not set response header if set the header with -a option --- config location /foo { - more_set_headers -i 'X-Foo: bar'; + more_set_headers -a 'X-Foo: bar'; echo hi; } --- request @@ -583,7 +583,7 @@ hi -=== TEST 35: set the response header if set the header with -i option +=== TEST 35: set the response header if set the header with -a option --- config location = /backend { add_header X-Foo baz; @@ -591,7 +591,7 @@ hi } location /foo { - more_set_headers -i 'X-Foo: bar'; + more_set_headers -a 'X-Foo: bar'; proxy_pass http://127.0.0.1:$server_port/backend; } --- request @@ -603,7 +603,7 @@ hi -=== TEST 36: test -t -i work together +=== TEST 36: test -t -a work together --- config location = /backend { add_header Content-Type text/html; @@ -611,12 +611,12 @@ hi } location /foo { - more_set_headers -t 'text/plain' -i 'X-Foo: bar'; + more_set_headers -t 'text/plain' -a 'X-Foo: bar'; proxy_pass http://127.0.0.1:$server_port/backend; } location /bar { - more_set_headers -t 'text/html' -i 'X-Foo: bar'; + more_set_headers -t 'text/html' -a 'X-Foo: bar'; proxy_pass http://127.0.0.1:$server_port/backend; } From cdfc60453382e7c1017f4a7d2885a643282f7eb4 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:17:20 +0800 Subject: [PATCH 22/26] boolean equal shoule use ! rather than 0 --- src/ngx_http_headers_more_headers_in.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index b887070..c3402f1 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -247,7 +247,7 @@ ngx_http_set_header_helper(ngx_http_request_t *r, && ngx_strncasecmp(h[i].key.data, hv->key.data, h[i].key.len) == 0) { - if ((hv->add_only == 1 && hv->replace == 0) == 0) { + if (!(hv->add_only == 1 && hv->replace == 0)) { if (value->len == 0 || (matched && matched != &h[i])) { h[i].hash = 0; From 15ae8852dfdc99dda1185c39b9e81e59a61a97a3 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:23:38 +0800 Subject: [PATCH 23/26] Ditto, Boolean field test should use the !a instead of the a == 0 --- src/ngx_http_headers_more_headers_in.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_http_headers_more_headers_in.c b/src/ngx_http_headers_more_headers_in.c index c3402f1..0b15422 100644 --- a/src/ngx_http_headers_more_headers_in.c +++ b/src/ngx_http_headers_more_headers_in.c @@ -247,7 +247,7 @@ ngx_http_set_header_helper(ngx_http_request_t *r, && ngx_strncasecmp(h[i].key.data, hv->key.data, h[i].key.len) == 0) { - if (!(hv->add_only == 1 && hv->replace == 0)) { + if (!(hv->add_only && !hv->replace)) { if (value->len == 0 || (matched && matched != &h[i])) { h[i].hash = 0; From 34c5f335febd5d8f4156b8ecd9736cd0f6b2e537 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:36:05 +0800 Subject: [PATCH 24/26] typo: doesnt => do not --- README.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index c8d4801..4796a3a 100644 --- a/README.markdown +++ b/README.markdown @@ -75,7 +75,7 @@ Synopsis # replace input header X-Foo *only* if it already exists more_set_input_headers -r 'X-Foo: howdy'; - # add input header X-Foo *only* if it doesnt exist + # add input header X-Foo *only* if it do not exist more_set_input_headers -a 'X-Foo: howdy'; ``` @@ -285,7 +285,7 @@ and works in subrequests as well. If the `-r` option is specified, then the headers will be replaced to the new values *only if* they already exist. -If the `-a` options is specified, then the headers will be added to the new values *only if* they doesnt exist. +If the `-a` options is specified, then the headers will be added to the new values *only if* they do not exist. [Back to TOC](#table-of-contents) From bb8a36546d39c9b338a660192c2d70f5ded4d8d8 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:37:57 +0800 Subject: [PATCH 25/26] typo it do not => it does not --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 4796a3a..bc6a2ad 100644 --- a/README.markdown +++ b/README.markdown @@ -75,7 +75,7 @@ Synopsis # replace input header X-Foo *only* if it already exists more_set_input_headers -r 'X-Foo: howdy'; - # add input header X-Foo *only* if it do not exist + # add input header X-Foo *only* if it does not exist more_set_input_headers -a 'X-Foo: howdy'; ``` From 964d53c2a2537a07a2b50de7b338362a30357c95 Mon Sep 17 00:00:00 2001 From: detailyang Date: Thu, 18 Aug 2016 10:43:01 +0800 Subject: [PATCH 26/26] typo remove new values on README.markdown --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index bc6a2ad..3f95547 100644 --- a/README.markdown +++ b/README.markdown @@ -285,7 +285,7 @@ and works in subrequests as well. If the `-r` option is specified, then the headers will be replaced to the new values *only if* they already exist. -If the `-a` options is specified, then the headers will be added to the new values *only if* they do not exist. +If the `-a` options is specified, then the headers will be added *only if* they do not exist. [Back to TOC](#table-of-contents)