-
Notifications
You must be signed in to change notification settings - Fork 228
Add headers if not set #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 14 commits
d48ca6a
4b5f079
8344531
3b64ac7
6d45d4f
5c73081
c802c27
48c4d43
12c3a00
30daad1
9c53270
ec1fad6
ac52358
b073439
e9fbadf
e4202d1
0a6073d
fe4f575
0c93a4c
c987b90
df0e156
5a0a562
cdfc604
15ae885
34c5f33
bb8a365
964d53c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,6 @@ nginx | |
*.plist | ||
a.patch | ||
Makefile | ||
autoconf.err | ||
autotest | ||
objs/* | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line has English grammar errors: "options" => "option" Also "the headers will be added to the new values" do not make sense. The "to the new values" part can be removed. |
||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The letter |
||
|
||
[Back to TOC](#table-of-contents) | ||
|
||
more_clear_input_headers | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field name is not good enough. Maybe "add_only" is better? |
||
ngx_flag_t wildcard; | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,35 +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 (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"); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line trailing spaces? |
||
if (matched == NULL) { | ||
matched = &h[i]; | ||
} | ||
|
@@ -286,7 +287,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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Boolean field test should use the |
||
return NGX_OK; | ||
} | ||
|
||
|
@@ -358,6 +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) { | ||
dd("skip because %s does set", hv->key.data); | ||
return NGX_OK; | ||
} | ||
|
||
h = *old; | ||
|
||
if (value->len == 0) { | ||
|
@@ -366,7 +372,7 @@ ngx_http_set_builtin_header(ngx_http_request_t *r, | |
|
||
return ngx_http_set_header_helper(r, hv, value, old); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line trailing spaces? |
||
h->hash = hv->hash; | ||
h->value = *value; | ||
|
||
|
@@ -496,19 +502,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; | ||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: we should use only one blank line here. |
||
if (hlcf->cmds == NULL) { | ||
hlcf->cmds = ngx_array_create(cf->pool, 1, | ||
sizeof(ngx_http_headers_more_cmd_t)); | ||
|
@@ -595,6 +602,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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: need a blank line before the |
||
dd("Found if not set flag"); | ||
ifnotset = 1; | ||
continue; | ||
} | ||
} | ||
|
||
ngx_log_error(NGX_LOG_ERR, cf->log, 0, | ||
|
@@ -615,6 +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; | ||
} | ||
} | ||
|
||
|
@@ -741,6 +754,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 %s does set", hv->key.data); | ||
return NGX_OK; | ||
} | ||
|
||
ngx_array_destroy(headers); | ||
|
||
if (ngx_array_init(headers, r->pool, 2, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,6 +223,11 @@ ngx_http_set_header_helper(ngx_http_request_t *r, | |
continue; | ||
|
||
matched: | ||
if (hv->ifnotset) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: need a blank line after the code label. |
||
dd("skip because %s does set", hv->key.data); | ||
matched = 1; | ||
continue; | ||
} | ||
|
||
if (value->len == 0 || matched) { | ||
dd("clearing normal header for %.*s", (int) hv->key.len, | ||
|
@@ -303,6 +308,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 %s does set", hv->key.data); | ||
return NGX_OK; | ||
} | ||
|
||
h = *old; | ||
|
||
|
@@ -344,6 +354,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 %s does set", hv->key.data); | ||
return NGX_OK; | ||
} | ||
|
||
ph = pa->elts; | ||
for (i = 1; i < pa->nelts; i++) { | ||
ph[i]->hash = 0; | ||
|
@@ -565,16 +580,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 +695,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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: need a blank line before the |
||
ifnotset = 1; | ||
continue; | ||
} | ||
} | ||
|
@@ -695,6 +714,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
h = cmd->headers->elts; | ||
for (i = 0; i < cmd->headers->nelts; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a blank line before the |
||
h[i].ifnotset = ifnotset; | ||
} | ||
} | ||
|
||
if (cmd->types->nelts == 0) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will you remove these? Seems like you are not using the standard building process based on
util/build.sh
.