Skip to content
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

Add upload_pass_form_field_default option #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protocol.
* [upload_store_access](#upload_store_access)
* [upload_set_form_field](#upload_set_form_field)
* [upload_aggregate_form_field](#upload_aggregate_form_field)
* [upload_pass_form_field_default](#upload_pass_form_field_default)
* [upload_pass_form_field](#upload_pass_form_field)
* [upload_cleanup](#upload_cleanup)
* [upload_buffer_size](#upload_buffer_size)
Expand Down Expand Up @@ -169,6 +170,16 @@ upload_aggregate_form_field $upload_field_name.size "$upload_file_size";

```

### upload_pass_form_field_default

**Syntax:** <code><b>upload_pass_form_field_default</b> on | off</code><br>
**Default:** `upload_pass_form_field_default off`<br>
**Context:** `main,server,location`

Specifies whether fields will be passed to backend from original request
body by default or not. By default, they will not be passed unless there
is a match with [upload_pass_form_field](#upload_pass_form_field).

### upload_pass_form_field

**Syntax:** <code><b>upload_pass_form_field</b> <i>regex</i></code><br>
Expand All @@ -180,7 +191,8 @@ backend from original request body. This directive could be specified
multiple times per location. Field will be passed to backend as soon as
first pattern matches. For PCRE-unaware enviroments this directive
specifies exact name of a field to pass to backend. If directive is
omitted, no fields will be passed to backend from client.
omitted, fields will be passed to backend from client depending on the
value of [upload_pass_form_field_default](#upload_pass_form_field_default).

Usage example:

Expand Down
35 changes: 33 additions & 2 deletions ngx_http_upload_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ typedef struct {
ngx_flag_t forward_args;
ngx_flag_t tame_arrays;
ngx_flag_t resumable_uploads;
ngx_flag_t upload_pass_form_field_default;
ngx_flag_t empty_field_names;
size_t limit_rate;

Expand Down Expand Up @@ -581,6 +582,17 @@ static ngx_command_t ngx_http_upload_commands[] = { /* {{{ */
offsetof(ngx_http_upload_loc_conf_t, aggregate_field_templates),
NULL},

/*
* Specifies whether to pass fields to backend by default
*/
{ ngx_string("upload_pass_form_field_default"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_upload_loc_conf_t, upload_pass_form_field_default),
NULL },

/*
* Specifies the field to pass to backend
*/
Expand Down Expand Up @@ -1557,6 +1569,10 @@ static ngx_int_t ngx_http_upload_start_handler(ngx_http_upload_ctx_t *u) { /* {{
}else{
pass_field = 0;

if(ulcf->upload_pass_form_field_default) {
pass_field = 1;
}

if(ulcf->field_filters) {
f = ulcf->field_filters->elts;
for (i = 0; i < ulcf->field_filters->nelts; i++) {
Expand All @@ -1582,7 +1598,10 @@ static ngx_int_t ngx_http_upload_start_handler(ngx_http_upload_ctx_t *u) { /* {{
}
}

if(pass_field && u->field_name.len != 0) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"passing other field \"%V\" = %d", &u->field_name, pass_field);

if(pass_field && u->field_name.len != 0) {
/*
* Here we do a small hack: the content of a non-file field
* is not known until ngx_http_upload_flush_output_buffer
Expand All @@ -1592,8 +1611,14 @@ static ngx_int_t ngx_http_upload_start_handler(ngx_http_upload_ctx_t *u) { /* {{

if(rc != NGX_OK)
return rc;
}else
}else{
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0
, "discarding other form field \"%V\""
, &u->field_name
);

u->discard_data = 1;
}
}


Expand Down Expand Up @@ -2218,6 +2243,7 @@ ngx_http_upload_create_loc_conf(ngx_conf_t *cf)
conf->tame_arrays = NGX_CONF_UNSET;
conf->resumable_uploads = NGX_CONF_UNSET;
conf->empty_field_names = NGX_CONF_UNSET;
conf->upload_pass_form_field_default = NGX_CONF_UNSET;

conf->buffer_size = NGX_CONF_UNSET_SIZE;
conf->merge_buffer_size = NGX_CONF_UNSET_SIZE;
Expand Down Expand Up @@ -2305,6 +2331,11 @@ ngx_http_upload_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
prev->resumable_uploads : 0;
}

if(conf->upload_pass_form_field_default == NGX_CONF_UNSET) {
conf->upload_pass_form_field_default = (prev->upload_pass_form_field_default != NGX_CONF_UNSET) ?
prev->upload_pass_form_field_default : 0;
}

if(conf->empty_field_names == NGX_CONF_UNSET) {
conf->empty_field_names = (prev->empty_field_names != NGX_CONF_UNSET) ?
prev->empty_field_names : 0;
Expand Down