Skip to content

Commit 96a58aa

Browse files
committed
Update
1 parent 2e48ad8 commit 96a58aa

File tree

1 file changed

+121
-26
lines changed

1 file changed

+121
-26
lines changed

ngx_http_if_request_body_module.c

Lines changed: 121 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ typedef struct {
3838
typedef struct {
3939
ngx_flag_t enable;
4040
ngx_flag_t include_body;
41-
ngx_array_t *headers_for_body_matched;
41+
ngx_array_t *headers_in_if_matched;
42+
ngx_array_t *headers_out_if_matched;
4243
ngx_array_t *return_if_variable_map_to;
4344
ngx_array_t *return_if_body_eq_rules;
4445
ngx_array_t *return_if_body_contains_rules;
@@ -57,7 +58,8 @@ static char *ngx_http_if_request_body_merge_conf(ngx_conf_t *cf, void *parent,
5758
void *child);
5859
static ngx_int_t ngx_http_if_request_body_init(ngx_conf_t *cf);
5960

60-
static char *ngx_http_add_header_if_body_matched(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
61+
static char *ngx_http_add_header_in_if_matched(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
62+
static char *ngx_http_add_header_out_if_matched(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
6163
static char *ngx_http_if_variable_map_to(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
6264

6365
static char *ngx_http_if_request_body_set_if_regex(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@@ -70,7 +72,8 @@ static ngx_int_t ngx_http_if_request_body_handler(ngx_http_request_t *r);
7072

7173
static u_char *ngx_http_if_request_body_strncasestr(u_char *s1, size_t len1, u_char *s2, size_t len2);
7274
static u_char *ngx_http_if_request_body_strnstr(u_char *s1, size_t len1, u_char *s2, size_t len2);
73-
static ngx_int_t ngx_http_if_request_body_massage_header(ngx_http_request_t *r, ngx_http_if_request_body_conf_t *bcf);
75+
static ngx_int_t ngx_http_if_request_body_parse_headers(ngx_http_request_t *r, ngx_http_if_request_body_conf_t *bcf);
76+
static ngx_int_t ngx_http_if_request_body_add_header_in(ngx_http_request_t *r, ngx_str_t *key, ngx_str_t *val );
7477
static ngx_int_t ngx_http_if_request_body_add_header_out(ngx_http_request_t *r, ngx_str_t *key, ngx_str_t *val );
7578

7679

@@ -90,9 +93,15 @@ static ngx_command_t ngx_http_if_request_body_commands[] = {
9093
NGX_HTTP_LOC_CONF_OFFSET,
9194
offsetof(ngx_http_if_request_body_conf_t, include_body),
9295
NULL },
93-
{ ngx_string("add_header_if_body_matched"),
96+
{ ngx_string("add_header_in_if_matched"),
9497
NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE2,
95-
ngx_http_add_header_if_body_matched,
98+
ngx_http_add_header_in_if_matched,
99+
NGX_HTTP_LOC_CONF_OFFSET,
100+
0,
101+
NULL },
102+
{ ngx_string("add_header_out_if_matched"),
103+
NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE2,
104+
ngx_http_add_header_out_if_matched,
96105
NGX_HTTP_LOC_CONF_OFFSET,
97106
0,
98107
NULL },
@@ -241,13 +250,13 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
241250
if(req_body.len >= check_value.len){
242251
if(eq_rules->case_sensitive) {
243252
if(ngx_strncmp(req_body.data, check_value.data, check_value.len) == 0) {
244-
if(ngx_http_if_request_body_massage_header(r, bcf) != NGX_OK) {
253+
if(ngx_http_if_request_body_parse_headers(r, bcf) != NGX_OK) {
245254
return NGX_HTTP_INTERNAL_SERVER_ERROR;
246255
}
247256
return eq_rules->status;
248257
}
249258
} else if (ngx_strncasecmp(req_body.data, check_value.data, check_value.len) == 0) {
250-
if(ngx_http_if_request_body_massage_header(r, bcf) != NGX_OK) {
259+
if(ngx_http_if_request_body_parse_headers(r, bcf) != NGX_OK) {
251260
return NGX_HTTP_INTERNAL_SERVER_ERROR;
252261
}
253262
return eq_rules->status;
@@ -256,13 +265,13 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
256265
} else if(req_body.len == check_value.len) {
257266
if(eq_rules->case_sensitive) {
258267
if(ngx_strncmp(check_value.data, req_body.data, req_body.len) == 0) {
259-
if(ngx_http_if_request_body_massage_header(r, bcf) != NGX_OK) {
268+
if(ngx_http_if_request_body_parse_headers(r, bcf) != NGX_OK) {
260269
return NGX_HTTP_INTERNAL_SERVER_ERROR;
261270
}
262271
return eq_rules->status;
263272
}
264273
} else if (ngx_strncasecmp(check_value.data, req_body.data, req_body.len) == 0) {
265-
if(ngx_http_if_request_body_massage_header(r, bcf) != NGX_OK) {
274+
if(ngx_http_if_request_body_parse_headers(r, bcf) != NGX_OK) {
266275
return NGX_HTTP_INTERNAL_SERVER_ERROR;
267276
}
268277
return eq_rules->status;
@@ -291,13 +300,13 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
291300
if(req_body.len >= check_value.len) {
292301
if(contains_rules->case_sensitive) {
293302
if( (ngx_http_if_request_body_strnstr(req_body.data, req_body.len,check_value.data,check_value.len)) ) {
294-
if(ngx_http_if_request_body_massage_header(r, bcf) != NGX_OK) {
303+
if(ngx_http_if_request_body_parse_headers(r, bcf) != NGX_OK) {
295304
return NGX_HTTP_INTERNAL_SERVER_ERROR;
296305
}
297306
return contains_rules->status;
298307
}
299308
} else if ( (ngx_http_if_request_body_strncasestr(req_body.data, req_body.len,check_value.data,check_value.len)) ) {
300-
if(ngx_http_if_request_body_massage_header(r, bcf) != NGX_OK) {
309+
if(ngx_http_if_request_body_parse_headers(r, bcf) != NGX_OK) {
301310
return NGX_HTTP_INTERNAL_SERVER_ERROR;
302311
}
303312
return contains_rules->status;
@@ -326,7 +335,7 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
326335

327336
n = ngx_regex_exec(regex_rules->rule, &req_body, captures, 3);
328337
if (n >= 0) {
329-
if(ngx_http_if_request_body_massage_header(r, bcf) != NGX_OK) {
338+
if(ngx_http_if_request_body_parse_headers(r, bcf) != NGX_OK) {
330339
return NGX_HTTP_INTERNAL_SERVER_ERROR;
331340
}
332341
/* string matches expression */
@@ -365,7 +374,7 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
365374
return NGX_HTTP_INTERNAL_SERVER_ERROR;
366375
}
367376
if(status != 100) { // if 100 Means bypass
368-
if(ngx_http_if_request_body_massage_header(r, bcf) != NGX_OK) {
377+
if(ngx_http_if_request_body_parse_headers(r, bcf) != NGX_OK) {
369378
return NGX_HTTP_INTERNAL_SERVER_ERROR;
370379
}
371380
return status;
@@ -394,7 +403,8 @@ ngx_http_if_request_body_create_conf(ngx_conf_t *cf)
394403

395404
conf->enable = NGX_CONF_UNSET;
396405
conf->include_body = NGX_CONF_UNSET;
397-
conf->headers_for_body_matched = NGX_CONF_UNSET_PTR;
406+
conf->headers_in_if_matched = NGX_CONF_UNSET_PTR;
407+
conf->headers_out_if_matched = NGX_CONF_UNSET_PTR;
398408
conf->return_if_variable_map_to = NGX_CONF_UNSET_PTR;
399409
conf->return_if_body_eq_rules = NGX_CONF_UNSET_PTR;
400410
conf->return_if_body_contains_rules = NGX_CONF_UNSET_PTR;
@@ -413,7 +423,9 @@ ngx_http_if_request_body_merge_conf(ngx_conf_t *cf, void *parent, void *child)
413423
ngx_conf_merge_value(conf->enable, prev->enable, 0);
414424
ngx_conf_merge_value(conf->include_body, prev->include_body, 1);
415425

416-
ngx_conf_merge_ptr_value(conf->headers_for_body_matched, prev->headers_for_body_matched, NULL);
426+
ngx_conf_merge_ptr_value(conf->headers_in_if_matched, prev->headers_in_if_matched, NULL);
427+
ngx_conf_merge_ptr_value(conf->headers_out_if_matched, prev->headers_out_if_matched, NULL);
428+
417429
ngx_conf_merge_ptr_value(conf->return_if_variable_map_to, prev->return_if_variable_map_to, NULL);
418430
ngx_conf_merge_ptr_value(conf->return_if_body_eq_rules, prev->return_if_body_eq_rules, NULL);
419431
ngx_conf_merge_ptr_value(conf->return_if_body_contains_rules, prev->return_if_body_contains_rules, NULL);
@@ -751,25 +763,60 @@ ngx_http_if_variable_map_to(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
751763
return NGX_CONF_OK;
752764
}
753765

766+
static char *
767+
ngx_http_add_header_in_if_matched(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
768+
ngx_http_if_request_body_conf_t *bcf = conf;
769+
ngx_str_t *value;
770+
body_with_header_t *hdr;
771+
ngx_http_compile_complex_value_t ccv;
772+
773+
value = cf->args->elts;
774+
775+
if (bcf->headers_in_if_matched == NULL || bcf->headers_in_if_matched == NGX_CONF_UNSET_PTR) {
776+
bcf->headers_in_if_matched = ngx_array_create(cf->pool, 2,
777+
sizeof(body_with_header_t));
778+
if (bcf->headers_in_if_matched == NULL) {
779+
return NGX_CONF_ERROR;
780+
}
781+
}
782+
783+
hdr = ngx_array_push(bcf->headers_in_if_matched);
784+
if (hdr == NULL) {
785+
return NGX_CONF_ERROR;
786+
}
787+
788+
hdr->key = value[1];
789+
790+
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
791+
792+
ccv.cf = cf;
793+
ccv.value = &value[2];
794+
ccv.complex_value = &hdr->value;
795+
796+
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
797+
return NGX_CONF_ERROR;
798+
}
799+
return NGX_CONF_OK;
800+
}
754801

755802
static char *
756-
ngx_http_add_header_if_body_matched(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
803+
ngx_http_add_header_out_if_matched(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
757804
ngx_http_if_request_body_conf_t *bcf = conf;
758805
ngx_str_t *value;
759806
body_with_header_t *hdr;
760807
ngx_http_compile_complex_value_t ccv;
761808

762809
value = cf->args->elts;
763810

764-
if (bcf->headers_for_body_matched == NULL || bcf->headers_for_body_matched == NGX_CONF_UNSET_PTR) {
765-
bcf->headers_for_body_matched = ngx_array_create(cf->pool, 2,
811+
if (bcf->headers_out_if_matched == NULL || bcf->headers_out_if_matched == NGX_CONF_UNSET_PTR) {
812+
bcf->headers_out_if_matched = ngx_array_create(cf->pool, 2,
766813
sizeof(body_with_header_t));
767-
if (bcf->headers_for_body_matched == NULL) {
814+
if (bcf->headers_out_if_matched == NULL) {
768815
return NGX_CONF_ERROR;
769816
}
770817
}
771818

772-
hdr = ngx_array_push(bcf->headers_for_body_matched);
819+
hdr = ngx_array_push(bcf->headers_out_if_matched);
773820
if (hdr == NULL) {
774821
return NGX_CONF_ERROR;
775822
}
@@ -789,16 +836,35 @@ ngx_http_add_header_if_body_matched(ngx_conf_t *cf, ngx_command_t *cmd, void *co
789836
}
790837

791838
static ngx_int_t
792-
ngx_http_if_request_body_massage_header(ngx_http_request_t *r, ngx_http_if_request_body_conf_t *bcf) {
839+
ngx_http_if_request_body_parse_headers(ngx_http_request_t *r, ngx_http_if_request_body_conf_t *bcf) {
793840
ngx_uint_t i, nelts;
794841
body_with_header_t *hdrs;
795842
ngx_str_t hdr_val;
796-
ngx_array_t *headers_for_body_matched;
843+
ngx_array_t *headers_in_if_matched;
844+
ngx_array_t *headers_out_if_matched;
797845

798-
if(bcf->headers_for_body_matched) {
799-
headers_for_body_matched = bcf->headers_for_body_matched;
800-
hdrs = headers_for_body_matched->elts;
801-
nelts = headers_for_body_matched->nelts;
846+
if(bcf->headers_in_if_matched) {
847+
headers_in_if_matched = bcf->headers_in_if_matched;
848+
hdrs = headers_in_if_matched->elts;
849+
nelts = headers_in_if_matched->nelts;
850+
851+
for (i = 0; i < nelts; i++) {
852+
if (ngx_http_complex_value(r, &hdrs->value, &hdr_val) == NGX_OK) {
853+
if (ngx_http_if_request_body_add_header_in(r, &hdrs->key, &hdr_val) == NGX_ERROR) {
854+
return NGX_ERROR;
855+
}
856+
} else {
857+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
858+
"unable to parse headers %V ", &hdrs->key);
859+
return NGX_ERROR;
860+
}
861+
}
862+
}
863+
864+
if(bcf->headers_out_if_matched) {
865+
headers_out_if_matched = bcf->headers_out_if_matched;
866+
hdrs = headers_out_if_matched->elts;
867+
nelts = headers_out_if_matched->nelts;
802868

803869
for (i = 0; i < nelts; i++) {
804870
if (ngx_http_complex_value(r, &hdrs->value, &hdr_val) == NGX_OK) {
@@ -835,6 +901,35 @@ ngx_http_if_request_body_add_header_out(ngx_http_request_t *r, ngx_str_t *key, n
835901
return NGX_OK;
836902
}
837903

904+
static ngx_int_t
905+
ngx_http_if_request_body_add_header_in(ngx_http_request_t *r, ngx_str_t *key, ngx_str_t *val ) {
906+
ngx_table_elt_t *h;
907+
ngx_http_header_t *hh;
908+
ngx_http_core_main_conf_t *cmcf;
909+
910+
h = ngx_list_push(&r->headers_in.headers);
911+
if (h == NULL) {
912+
return NGX_ERROR;
913+
}
914+
915+
h->key.len = key->len;
916+
h->key.data = key->data;
917+
h->hash = ngx_hash_key(h->key.data, h->key.len);
918+
h->value.len = val->len;
919+
h->value.data = val->data;
920+
921+
h->lowcase_key = h->key.data;
922+
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
923+
924+
hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash, h->lowcase_key, h->key.len);
925+
926+
if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
927+
return NGX_ERROR;
928+
}
929+
930+
return NGX_OK;
931+
}
932+
838933

839934
// REFER ngx_string.c in order to compare case sensitive and second string len
840935
u_char *

0 commit comments

Comments
 (0)