Skip to content

Commit a627143

Browse files
committed
add a directive to allow filter undesired parameters
1 parent 29efc88 commit a627143

File tree

3 files changed

+143
-3
lines changed

3 files changed

+143
-3
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ A module to order the querystring parameters in a variable to be used as cache k
55

66
Requests like /index.html?b=2&a=1&c=3, /index.html?b=2&c=3&a=1, /index.html?c=3&a=1&b=2, /index.html?c=3&b=2&a=1 will produce the same value for `$sorted_querystring_args` _'a=1&b=2&c=3'_.
77

8+
It is also possible remove some undesired parameter defining its name with `sorted_querysting_filter_parameter`, like `sorted_querysting_filter_parameter b _ foo;` resulting in a `$sorted_querystring_args` as _'a=1&c=3'_
9+
810
_This module is not distributed with the Nginx source. See [the installation instructions](#installation)._
911

1012

@@ -46,6 +48,17 @@ An example:
4648

4749
access_log logs/nginx-http_access.log main;
4850

51+
location /filtered {
52+
sorted_querysting_filter_parameter v _ time b;
53+
54+
proxy_set_header Host "static_files_server";
55+
proxy_pass http://localhost:8081;
56+
57+
proxy_cache zone;
58+
proxy_cache_key "$sorted_querystring_args";
59+
proxy_cache_valid 200 1m;
60+
}
61+
4962
location / {
5063
proxy_pass http://localhost:8081;
5164

@@ -71,6 +84,12 @@ Variables
7184
* **$sorted_querystring_args** - just list the IP considered as remote IP on the connection
7285

7386

87+
Directives
88+
----------
89+
90+
* **sorted_querystring_filter_parameter** - list parameters to be filtered while using the `$sorted_querystring_args` variable.
91+
92+
7493
<a id="installation"></a>Installation instructions
7594
--------------------------------------------------
7695

nginx.conf

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ http {
3434

3535
access_log logs/nginx-http_access.log main;
3636

37+
location /filtered {
38+
sorted_querysting_filter_parameter v _ v time b;
39+
40+
proxy_set_header Host "static_files_server";
41+
proxy_pass http://localhost:8081;
42+
43+
proxy_cache zone;
44+
proxy_cache_key "$sorted_querystring_args";
45+
proxy_cache_valid 200 1m;
46+
}
47+
3748
location / {
3849
proxy_pass http://localhost:8081;
3950

ngx_http_sorted_querystring_module.c

+113-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55

66

77
ngx_int_t ngx_http_sorted_querystring_pre_config(ngx_conf_t *cf);
8+
void *ngx_http_sorted_querystring_create_loc_conf(ngx_conf_t *cf);
9+
char *ngx_http_sorted_querystring_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
10+
char *ngx_conf_set_parameters_to_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
811
ngx_int_t ngx_http_sorted_querystring_args_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
912
ngx_int_t ngx_http_sorted_querystring_cmp_parameters(const ngx_queue_t *one, const ngx_queue_t *two);
1013

14+
15+
typedef struct {
16+
ngx_array_t *parameters_to_filter;
17+
} ngx_http_sorted_querystring_loc_conf_t;
18+
19+
1120
typedef struct {
1221
ngx_queue_t args_queue;
1322
} ngx_http_sorted_querystring_ctx_t;
@@ -31,6 +40,13 @@ static ngx_http_variable_t ngx_http_sorted_querystring_vars[] = {
3140

3241

3342
static ngx_command_t ngx_http_sorted_querystring_commands[] = {
43+
{ ngx_string("sorted_querysting_filter_parameter"),
44+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_1MORE,
45+
ngx_conf_set_parameters_to_filter,
46+
NGX_HTTP_LOC_CONF_OFFSET,
47+
offsetof(ngx_http_sorted_querystring_loc_conf_t, parameters_to_filter),
48+
NULL },
49+
3450
ngx_null_command
3551
};
3652

@@ -45,8 +61,8 @@ static ngx_http_module_t ngx_http_sorted_querystring_module_ctx = {
4561
NULL, /* create server configuration */
4662
NULL, /* merge server configuration */
4763

48-
NULL, /* create location configuration */
49-
NULL /* merge location configuration */
64+
ngx_http_sorted_querystring_create_loc_conf, /* create location configuration */
65+
ngx_http_sorted_querystring_merge_loc_conf /* merge location configuration */
5066
};
5167

5268

@@ -85,13 +101,92 @@ ngx_http_sorted_querystring_pre_config(ngx_conf_t *cf)
85101
}
86102

87103

104+
void *
105+
ngx_http_sorted_querystring_create_loc_conf(ngx_conf_t *cf)
106+
{
107+
ngx_http_sorted_querystring_loc_conf_t *conf;
108+
109+
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sorted_querystring_loc_conf_t));
110+
if (conf == NULL) {
111+
return NULL;
112+
}
113+
114+
conf->parameters_to_filter = NGX_CONF_UNSET_PTR;
115+
116+
return conf;
117+
}
118+
119+
120+
char *
121+
ngx_http_sorted_querystring_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
122+
{
123+
ngx_http_sorted_querystring_loc_conf_t *prev = parent;
124+
ngx_http_sorted_querystring_loc_conf_t *conf = child;
125+
126+
ngx_conf_merge_ptr_value(conf->parameters_to_filter, prev->parameters_to_filter, NULL);
127+
128+
return NGX_CONF_OK;
129+
}
130+
131+
132+
char *
133+
ngx_conf_set_parameters_to_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
134+
{
135+
char *p = conf;
136+
137+
ngx_array_t **field;
138+
ngx_str_t *value, *s;
139+
ngx_uint_t i, j;
140+
ngx_flag_t exists;
141+
142+
field = (ngx_array_t **) (p + cmd->offset);
143+
144+
if (*field != NGX_CONF_UNSET_PTR) {
145+
return "is duplicate";
146+
}
147+
148+
*field = ngx_array_create(cf->pool, cf->args->nelts, sizeof(ngx_str_t));
149+
if (*field == NULL) {
150+
return NGX_CONF_ERROR;
151+
}
152+
153+
value = cf->args->elts;
154+
155+
for (i = 1; i < cf->args->nelts; i++) {
156+
exists = 0;
157+
s = (*field)->elts;
158+
for (j = 0; j < (*field)->nelts; j++) {
159+
if ((value[i].len == s[j].len) && ngx_strncasecmp(value[i].data, s[j].data, value[i].len) == 0) {
160+
exists = 1;
161+
break;
162+
}
163+
}
164+
165+
if (!exists) {
166+
s = ngx_array_push(*field);
167+
if (s == NULL) {
168+
return NGX_CONF_ERROR;
169+
}
170+
171+
*s = value[i];
172+
}
173+
}
174+
175+
return NGX_CONF_OK;
176+
}
177+
178+
88179
ngx_int_t
89180
ngx_http_sorted_querystring_args_variable(ngx_http_request_t *r, ngx_http_variable_value_t *var, uintptr_t data)
90181
{
182+
ngx_http_sorted_querystring_loc_conf_t *sqlc = ngx_http_get_module_loc_conf(r, ngx_http_sorted_querystring_module);
91183
ngx_http_sorted_querystring_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_sorted_querystring_module);
92184
ngx_http_sorted_querystring_parameter_t *param;
93185
u_char *p, *ampersand, *equal, *last;
94186
ngx_queue_t *q;
187+
ngx_flag_t filter;
188+
ngx_str_t *value;
189+
ngx_uint_t i;
95190

96191
if (r->args.len == 0) {
97192
var->len = 0;
@@ -153,7 +248,22 @@ ngx_http_sorted_querystring_args_variable(ngx_http_request_t *r, ngx_http_variab
153248
p = var->data;
154249
for (q = ngx_queue_head(&ctx->args_queue); q != ngx_queue_sentinel(&ctx->args_queue); q = ngx_queue_next(q)) {
155250
param = ngx_queue_data(q, ngx_http_sorted_querystring_parameter_t, queue);
156-
p = ngx_sprintf(p, "%V&", &param->complete);
251+
252+
filter = 0;
253+
if (sqlc->parameters_to_filter && (param->key.len > 0)) {
254+
value = sqlc->parameters_to_filter->elts;
255+
for (i = 0; i < sqlc->parameters_to_filter->nelts; i++) {
256+
if ((param->key.len == value[i].len) && ngx_strncasecmp(param->key.data, value[i].data, param->key.len) == 0) {
257+
filter = 1;
258+
break;
259+
}
260+
}
261+
262+
}
263+
264+
if (!filter) {
265+
p = ngx_sprintf(p, "%V&", &param->complete);
266+
}
157267
}
158268

159269
var->len = p - var->data - 1;

0 commit comments

Comments
 (0)