@@ -38,7 +38,8 @@ typedef struct {
38
38
typedef struct {
39
39
ngx_flag_t enable ;
40
40
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 ;
42
43
ngx_array_t * return_if_variable_map_to ;
43
44
ngx_array_t * return_if_body_eq_rules ;
44
45
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,
57
58
void * child );
58
59
static ngx_int_t ngx_http_if_request_body_init (ngx_conf_t * cf );
59
60
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 );
61
63
static char * ngx_http_if_variable_map_to (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
62
64
63
65
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);
70
72
71
73
static u_char * ngx_http_if_request_body_strncasestr (u_char * s1 , size_t len1 , u_char * s2 , size_t len2 );
72
74
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 );
74
77
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 );
75
78
76
79
@@ -90,9 +93,15 @@ static ngx_command_t ngx_http_if_request_body_commands[] = {
90
93
NGX_HTTP_LOC_CONF_OFFSET ,
91
94
offsetof(ngx_http_if_request_body_conf_t , include_body ),
92
95
NULL },
93
- { ngx_string ("add_header_if_body_matched " ),
96
+ { ngx_string ("add_header_in_if_matched " ),
94
97
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 ,
96
105
NGX_HTTP_LOC_CONF_OFFSET ,
97
106
0 ,
98
107
NULL },
@@ -241,13 +250,13 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
241
250
if (req_body .len >= check_value .len ){
242
251
if (eq_rules -> case_sensitive ) {
243
252
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 ) {
245
254
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
246
255
}
247
256
return eq_rules -> status ;
248
257
}
249
258
} 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 ) {
251
260
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
252
261
}
253
262
return eq_rules -> status ;
@@ -256,13 +265,13 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
256
265
} else if (req_body .len == check_value .len ) {
257
266
if (eq_rules -> case_sensitive ) {
258
267
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 ) {
260
269
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
261
270
}
262
271
return eq_rules -> status ;
263
272
}
264
273
} 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 ) {
266
275
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
267
276
}
268
277
return eq_rules -> status ;
@@ -291,13 +300,13 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
291
300
if (req_body .len >= check_value .len ) {
292
301
if (contains_rules -> case_sensitive ) {
293
302
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 ) {
295
304
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
296
305
}
297
306
return contains_rules -> status ;
298
307
}
299
308
} 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 ) {
301
310
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
302
311
}
303
312
return contains_rules -> status ;
@@ -326,7 +335,7 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
326
335
327
336
n = ngx_regex_exec (regex_rules -> rule , & req_body , captures , 3 );
328
337
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 ) {
330
339
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
331
340
}
332
341
/* string matches expression */
@@ -365,7 +374,7 @@ ngx_http_if_request_body_filter(ngx_http_request_t *r) {
365
374
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
366
375
}
367
376
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 ) {
369
378
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
370
379
}
371
380
return status ;
@@ -394,7 +403,8 @@ ngx_http_if_request_body_create_conf(ngx_conf_t *cf)
394
403
395
404
conf -> enable = NGX_CONF_UNSET ;
396
405
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 ;
398
408
conf -> return_if_variable_map_to = NGX_CONF_UNSET_PTR ;
399
409
conf -> return_if_body_eq_rules = NGX_CONF_UNSET_PTR ;
400
410
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)
413
423
ngx_conf_merge_value (conf -> enable , prev -> enable , 0 );
414
424
ngx_conf_merge_value (conf -> include_body , prev -> include_body , 1 );
415
425
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
+
417
429
ngx_conf_merge_ptr_value (conf -> return_if_variable_map_to , prev -> return_if_variable_map_to , NULL );
418
430
ngx_conf_merge_ptr_value (conf -> return_if_body_eq_rules , prev -> return_if_body_eq_rules , NULL );
419
431
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)
751
763
return NGX_CONF_OK ;
752
764
}
753
765
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
+ }
754
801
755
802
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 ) {
757
804
ngx_http_if_request_body_conf_t * bcf = conf ;
758
805
ngx_str_t * value ;
759
806
body_with_header_t * hdr ;
760
807
ngx_http_compile_complex_value_t ccv ;
761
808
762
809
value = cf -> args -> elts ;
763
810
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 ,
766
813
sizeof (body_with_header_t ));
767
- if (bcf -> headers_for_body_matched == NULL ) {
814
+ if (bcf -> headers_out_if_matched == NULL ) {
768
815
return NGX_CONF_ERROR ;
769
816
}
770
817
}
771
818
772
- hdr = ngx_array_push (bcf -> headers_for_body_matched );
819
+ hdr = ngx_array_push (bcf -> headers_out_if_matched );
773
820
if (hdr == NULL ) {
774
821
return NGX_CONF_ERROR ;
775
822
}
@@ -789,16 +836,35 @@ ngx_http_add_header_if_body_matched(ngx_conf_t *cf, ngx_command_t *cmd, void *co
789
836
}
790
837
791
838
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 ) {
793
840
ngx_uint_t i , nelts ;
794
841
body_with_header_t * hdrs ;
795
842
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 ;
797
845
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 ;
802
868
803
869
for (i = 0 ; i < nelts ; i ++ ) {
804
870
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
835
901
return NGX_OK ;
836
902
}
837
903
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
+
838
933
839
934
// REFER ngx_string.c in order to compare case sensitive and second string len
840
935
u_char *
0 commit comments