Skip to content

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

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d48ca6a
add objs/* and autoconf to gitignore
detailyang Aug 14, 2016
4b5f079
add -i option (if not set) to more_set_input_headers
detailyang Aug 14, 2016
8344531
add -i option (if not set) to more_set_headers
detailyang Aug 14, 2016
3b64ac7
allow if no set on non builtin header on more_set_headers
detailyang Aug 15, 2016
6d45d4f
add test for -i option
detailyang Aug 15, 2016
5c73081
better prompt when skip with -i option
detailyang Aug 15, 2016
c802c27
add -i option description to README
detailyang Aug 15, 2016
48c4d43
if no set should work on multi header
detailyang Aug 16, 2016
12c3a00
work together on -r replace and -i if not set
detailyang Aug 17, 2016
30daad1
add test for -i (if not set) option
detailyang Aug 17, 2016
9c53270
Fix: do not set header when header already set
detailyang Aug 17, 2016
ec1fad6
add test for -r -t -i
detailyang Aug 17, 2016
ac52358
add test for more_set_headers on -i -t work together
detailyang Aug 17, 2016
b073439
Merge branch 'master' of https://github.com/detailyang/headers-more-n…
detailyang Aug 17, 2016
e9fbadf
remove misc in gitignore because of we are using the standard building
detailyang Aug 18, 2016
e4202d1
reindex t/*.t
detailyang Aug 18, 2016
0a6073d
correct code style
detailyang Aug 18, 2016
fe4f575
typo: grammer fix
detailyang Aug 18, 2016
0c93a4c
add new line in gitignore
detailyang Aug 18, 2016
c987b90
remove Makefile new line in gitignore
detailyang Aug 18, 2016
df0e156
trailing line space
detailyang Aug 18, 2016
5a0a562
typo -i to -a at t/sanity.t
detailyang Aug 18, 2016
cdfc604
boolean equal shoule use ! rather than 0
detailyang Aug 18, 2016
15ae885
Ditto, Boolean field test should use the !a instead of the a == 0
detailyang Aug 18, 2016
34c5f33
typo: doesnt => do not
detailyang Aug 18, 2016
bb8a365
typo it do not => it does not
detailyang Aug 18, 2016
964d53c
typo remove new values on README.markdown
detailyang Aug 18, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ nginx
*.plist
a.patch
Makefile
autoconf.err
autotest
objs/*
Copy link
Member

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.

11 changes: 9 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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*

Expand All @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has English grammar errors:

"options" => "option"
"doesnt" => "do not"

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
Expand Down Expand Up @@ -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*

Expand All @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The letter i in the -i option is not very descriptive. Maybe a is better? a means add only.


[Back to TOC](#table-of-contents)

more_clear_input_headers
Expand Down
1 change: 1 addition & 0 deletions src/ngx_http_headers_more_filter_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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;
};

Expand Down
76 changes: 47 additions & 29 deletions src/ngx_http_headers_more_headers_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line trailing spaces?

if (matched == NULL) {
matched = &h[i];
}
Expand All @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boolean field test should use the !a form instead of the a == 0 form.

return NGX_OK;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -366,7 +372,7 @@ ngx_http_set_builtin_header(ngx_http_request_t *r,

return ngx_http_set_header_helper(r, hv, value, old);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line trailing spaces?

h->hash = hv->hash;
h->value = *value;

Expand Down Expand Up @@ -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;



Copy link
Member

Choose a reason for hiding this comment

The 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));
Expand Down Expand Up @@ -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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a blank line before the if statement.

dd("Found if not set flag");
ifnotset = 1;
continue;
}
}

ngx_log_error(NGX_LOG_ERR, cf->log, 0,
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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,
Expand Down
40 changes: 32 additions & 8 deletions src/ngx_http_headers_more_headers_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ ngx_http_set_header_helper(ngx_http_request_t *r,
continue;

matched:
if (hv->ifnotset) {
Copy link
Member

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a blank line before the } else if (...) line.

ifnotset = 1;
continue;
}
}
Expand All @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The 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++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a blank line before the for statement.

h[i].ifnotset = ifnotset;
}
}

if (cmd->types->nelts == 0) {
Expand Down
Loading