@@ -79,8 +79,14 @@ typedef struct {
7979} ngx_http_c_func_srv_conf_t ;
8080
8181typedef 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 ;
8388 ngx_http_c_func_app_handler _handler ;
89+ ngx_array_t * ext_req_headers ;
8490 // ngx_msec_t proc_timeout;
8591} ngx_http_c_func_loc_conf_t ;
8692
@@ -113,8 +119,10 @@ static void * ngx_http_c_func_create_srv_conf(ngx_conf_t *cf);
113119static char * ngx_http_c_func_merge_srv_conf (ngx_conf_t * cf , void * parent , void * child );
114120static void * ngx_http_c_func_create_loc_conf (ngx_conf_t * cf );
115121static 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 );
116123static char * ngx_http_c_func_init_method (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
117124static 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 );
118126static ngx_int_t ngx_http_c_func_rewrite_handler (ngx_http_request_t * r );
119127static ngx_int_t ngx_http_c_func_process_init (ngx_cycle_t * cycle );
120128static void ngx_http_c_func_process_exit (ngx_cycle_t * cycle );
@@ -230,6 +238,13 @@ static ngx_command_t ngx_http_c_func_commands[] = {
230238 offsetof(ngx_http_c_func_srv_conf_t , _ca_cart ),
231239 NULL
232240 },
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+ },
233248 { ngx_string ("ngx_http_c_func_call" ), /* directive */
234249 NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 , /* location context and takes 1 or 2 arguments*/
235250 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
425440// return NGX_CONF_OK;
426441// } /* ngx_http_c_func_srv_post_conf_handler */
427442
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+
428479/**
429480 * Configuration setup function that installs the content handler.
430481 *
@@ -439,34 +490,12 @@ ngx_http_c_func_validation_check_and_set_str_slot(ngx_conf_t *cf, ngx_command_t
439490 */
440491static char *
441492ngx_http_c_func_init_method (ngx_conf_t * cf , ngx_command_t * cmd , void * conf ) {
442- // ngx_str_t *value;
443493 ngx_http_c_func_srv_conf_t * scf ;
444494 ngx_http_c_func_loc_conf_t * lcf = conf ;
445- // ngx_str_t varname;
446495
447496
448497 scf = ngx_http_conf_get_module_srv_conf (cf , ngx_http_c_func_module );
449498
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-
470499 if (scf && scf -> _libname .len > 0 ) {
471500 ngx_http_c_func_loc_q_t * loc_q = ngx_pcalloc (cf -> pool , sizeof (ngx_http_c_func_loc_q_t ));
472501 loc_q -> _loc_conf = lcf ;
@@ -883,18 +912,19 @@ ngx_http_c_func_create_loc_conf(ngx_conf_t *cf) {
883912 if (conf == NULL ) {
884913 return NGX_CONF_ERROR ;
885914 }
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 ;
888917 return conf ;
889918}
890919
891920
892921
893922static char *
894923ngx_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 ;
897926
927+ ngx_conf_merge_ptr_value (conf -> ext_req_headers , prev -> ext_req_headers , NULL );
898928 // ngx_conf_merge_str_value(conf->_method_name, prev->_method_name, "");
899929
900930 // if (conf->_method_name.len == 0) {
@@ -1146,7 +1176,48 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) {
11461176
11471177} /* ngx_http_c_func_precontent_handler */
11481178
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 );
11491191
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+ }
11501221/**
11511222 * Rewrite handler.
11521223 * Ref:: https://github.com/calio/form-input-nginx-module
@@ -1161,6 +1232,10 @@ ngx_http_c_func_rewrite_handler(ngx_http_request_t *r) {
11611232 ngx_http_c_func_internal_ctx_t * ctx ;
11621233 ngx_int_t rc ;
11631234
1235+ if (lcf -> ext_req_headers ) {
1236+ ngx_http_c_func_parse_ext_request_headers (r , lcf -> ext_req_headers );
1237+ }
1238+
11641239 if (lcf -> _handler == NULL ) {
11651240 return NGX_DECLINED ;
11661241 }
0 commit comments