@@ -79,8 +79,14 @@ typedef struct {
79
79
} ngx_http_c_func_srv_conf_t ;
80
80
81
81
typedef struct {
82
- ngx_str_t _method_name ;
82
+ ngx_str_t key ;
83
+ ngx_http_complex_value_t value ;
84
+ } ngx_http_c_func_req_header_t ;
85
+
86
+ typedef struct {
87
+ ngx_str_t _method_name ;
83
88
ngx_http_c_func_app_handler _handler ;
89
+ ngx_array_t * ext_req_headers ;
84
90
// ngx_msec_t proc_timeout;
85
91
} ngx_http_c_func_loc_conf_t ;
86
92
@@ -113,8 +119,10 @@ static void * ngx_http_c_func_create_srv_conf(ngx_conf_t *cf);
113
119
static char * ngx_http_c_func_merge_srv_conf (ngx_conf_t * cf , void * parent , void * child );
114
120
static void * ngx_http_c_func_create_loc_conf (ngx_conf_t * cf );
115
121
static char * ngx_http_c_func_merge_loc_conf (ngx_conf_t * cf , void * parent , void * child );
122
+ static char * ngx_http_c_func_ext_req_headers_add_cmd (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
116
123
static char * ngx_http_c_func_init_method (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
117
124
static ngx_int_t ngx_http_c_func_precontent_handler (ngx_http_request_t * r );
125
+ static void ngx_http_c_func_parse_ext_request_headers (ngx_http_request_t * r , ngx_array_t * ext_req_headers );
118
126
static ngx_int_t ngx_http_c_func_rewrite_handler (ngx_http_request_t * r );
119
127
static ngx_int_t ngx_http_c_func_process_init (ngx_cycle_t * cycle );
120
128
static void ngx_http_c_func_process_exit (ngx_cycle_t * cycle );
@@ -230,6 +238,13 @@ static ngx_command_t ngx_http_c_func_commands[] = {
230
238
offsetof(ngx_http_c_func_srv_conf_t , _ca_cart ),
231
239
NULL
232
240
},
241
+ { ngx_string ("ngx_http_c_func_add_req_header" ),
242
+ NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE2 ,
243
+ ngx_http_c_func_ext_req_headers_add_cmd ,
244
+ NGX_HTTP_LOC_CONF_OFFSET ,
245
+ 0 ,
246
+ NULL
247
+ },
233
248
{ ngx_string ("ngx_http_c_func_call" ), /* directive */
234
249
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 , /* location context and takes 1 or 2 arguments*/
235
250
ngx_http_c_func_init_method , /* configuration setup function */
@@ -425,6 +440,42 @@ ngx_http_c_func_validation_check_and_set_str_slot(ngx_conf_t *cf, ngx_command_t
425
440
// return NGX_CONF_OK;
426
441
// } /* ngx_http_c_func_srv_post_conf_handler */
427
442
443
+ static char *
444
+ ngx_http_c_func_ext_req_headers_add_cmd (ngx_conf_t * cf , ngx_command_t * cmd , void * conf ) {
445
+ ngx_http_c_func_loc_conf_t * lcf = conf ;
446
+ ngx_str_t * value ;
447
+ ngx_http_c_func_req_header_t * hdr ;
448
+ ngx_http_compile_complex_value_t ccv ;
449
+
450
+ value = cf -> args -> elts ;
451
+
452
+ if (lcf -> ext_req_headers == NULL || lcf -> ext_req_headers == NGX_CONF_UNSET_PTR ) {
453
+ lcf -> ext_req_headers = ngx_array_create (cf -> pool , 2 ,
454
+ sizeof (ngx_http_c_func_req_header_t ));
455
+ if (lcf -> ext_req_headers == NULL ) {
456
+ return NGX_CONF_ERROR ;
457
+ }
458
+ }
459
+
460
+ hdr = ngx_array_push (lcf -> ext_req_headers );
461
+ if (hdr == NULL ) {
462
+ return NGX_CONF_ERROR ;
463
+ }
464
+
465
+ hdr -> key = value [1 ];
466
+
467
+ ngx_memzero (& ccv , sizeof (ngx_http_compile_complex_value_t ));
468
+
469
+ ccv .cf = cf ;
470
+ ccv .value = & value [2 ];
471
+ ccv .complex_value = & hdr -> value ;
472
+
473
+ if (ngx_http_compile_complex_value (& ccv ) != NGX_OK ) {
474
+ return NGX_CONF_ERROR ;
475
+ }
476
+ return NGX_CONF_OK ;
477
+ }
478
+
428
479
/**
429
480
* Configuration setup function that installs the content handler.
430
481
*
@@ -439,34 +490,12 @@ ngx_http_c_func_validation_check_and_set_str_slot(ngx_conf_t *cf, ngx_command_t
439
490
*/
440
491
static char *
441
492
ngx_http_c_func_init_method (ngx_conf_t * cf , ngx_command_t * cmd , void * conf ) {
442
- // ngx_str_t *value;
443
493
ngx_http_c_func_srv_conf_t * scf ;
444
494
ngx_http_c_func_loc_conf_t * lcf = conf ;
445
- // ngx_str_t varname;
446
495
447
496
448
497
scf = ngx_http_conf_get_module_srv_conf (cf , ngx_http_c_func_module );
449
498
450
- // value = cf->args->elts;
451
-
452
- // if (cf->args->nelts == 3 &&
453
- // value[2].len > sizeof("respTo=") - 1 &&
454
- // ngx_strncmp(value[2].data, "respTo=", 6) == 0 ) {
455
-
456
- // lcf->_is_call_to_var = 1;
457
-
458
- // varname.data = value[2].data + sizeof("respTo=") - 1;
459
- // varname.len = value[2].len - (sizeof("respTo=") - 1);
460
-
461
- // ngx_http_variable_t *var;
462
- // var = ngx_http_add_variable(cf, &varname, NGX_HTTP_VAR_CHANGEABLE | NGX_HTTP_VAR_NOCACHEABLE | NGX_HTTP_VAR_NOHASH);
463
- // if (var == NULL) {
464
- // return NGX_CONF_ERROR;
465
- // }
466
- // var->get_handler = ngx_http_c_func_get_resp_var;
467
- // var->data = 0;
468
- // }
469
-
470
499
if (scf && scf -> _libname .len > 0 ) {
471
500
ngx_http_c_func_loc_q_t * loc_q = ngx_pcalloc (cf -> pool , sizeof (ngx_http_c_func_loc_q_t ));
472
501
loc_q -> _loc_conf = lcf ;
@@ -883,18 +912,19 @@ ngx_http_c_func_create_loc_conf(ngx_conf_t *cf) {
883
912
if (conf == NULL ) {
884
913
return NGX_CONF_ERROR ;
885
914
}
886
- /***ngx_pcalloc has inited properly*/
887
- // conf->_method_name.len = NGX_CONF_UNSET_SIZE ;
915
+
916
+ conf -> ext_req_headers = NGX_CONF_UNSET_PTR ;
888
917
return conf ;
889
918
}
890
919
891
920
892
921
893
922
static char *
894
923
ngx_http_c_func_merge_loc_conf (ngx_conf_t * cf , void * parent , void * child ) {
895
- // ngx_http_c_func_loc_conf_t *prev = parent;
896
- // ngx_http_c_func_loc_conf_t *conf = child;
924
+ ngx_http_c_func_loc_conf_t * prev = parent ;
925
+ ngx_http_c_func_loc_conf_t * conf = child ;
897
926
927
+ ngx_conf_merge_ptr_value (conf -> ext_req_headers , prev -> ext_req_headers , NULL );
898
928
// ngx_conf_merge_str_value(conf->_method_name, prev->_method_name, "");
899
929
900
930
// if (conf->_method_name.len == 0) {
@@ -1146,7 +1176,48 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) {
1146
1176
1147
1177
} /* ngx_http_c_func_precontent_handler */
1148
1178
1179
+ static void
1180
+ ngx_http_c_func_parse_ext_request_headers (ngx_http_request_t * r , ngx_array_t * ext_req_headers ) {
1181
+ ngx_uint_t i , nelts ;
1182
+ ngx_http_c_func_req_header_t * hdrs ;
1183
+ ngx_str_t hdr_val ;
1184
+ ngx_table_elt_t * h ;
1185
+ ngx_http_header_t * hh ;
1186
+ ngx_http_core_main_conf_t * cmcf ;
1187
+
1188
+ hdrs = ext_req_headers -> elts ;
1189
+ nelts = ext_req_headers -> nelts ;
1190
+ cmcf = ngx_http_get_module_main_conf (r , ngx_http_core_module );
1149
1191
1192
+ for (i = 0 ; i < nelts ; i ++ ) {
1193
+ if (ngx_http_complex_value (r , & hdrs -> value , & hdr_val ) == NGX_OK ) {
1194
+
1195
+ h = ngx_list_push (& r -> headers_in .headers );
1196
+ if (h == NULL ) {
1197
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "error when adding header %s" , "insufficient memory allocate" );
1198
+ break ;
1199
+ }
1200
+
1201
+ h -> key .len = hdrs -> key .len ;
1202
+ h -> key .data = hdrs -> key .data ;
1203
+ h -> hash = ngx_hash_key (h -> key .data , h -> key .len );
1204
+
1205
+ h -> value .len = hdr_val .len ;
1206
+ h -> value .data = hdr_val .data ;
1207
+
1208
+ h -> lowcase_key = h -> key .data ;
1209
+
1210
+ hh = ngx_hash_find (& cmcf -> headers_in_hash , h -> hash , h -> lowcase_key , h -> key .len );
1211
+
1212
+ if (hh && hh -> handler (r , h , hh -> offset ) != NGX_OK ) {
1213
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "%s" , "error when adding header" );
1214
+ }
1215
+ } else {
1216
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "%s" , "error when adding header" );
1217
+ }
1218
+ hdrs ++ ;
1219
+ }
1220
+ }
1150
1221
/**
1151
1222
* Rewrite handler.
1152
1223
* Ref:: https://github.com/calio/form-input-nginx-module
@@ -1161,6 +1232,10 @@ ngx_http_c_func_rewrite_handler(ngx_http_request_t *r) {
1161
1232
ngx_http_c_func_internal_ctx_t * ctx ;
1162
1233
ngx_int_t rc ;
1163
1234
1235
+ if (lcf -> ext_req_headers ) {
1236
+ ngx_http_c_func_parse_ext_request_headers (r , lcf -> ext_req_headers );
1237
+ }
1238
+
1164
1239
if (lcf -> _handler == NULL ) {
1165
1240
return NGX_DECLINED ;
1166
1241
}
0 commit comments