@@ -20,6 +20,8 @@ static ngx_str_t proxy_on_context_create = ngx_string("proxy_on_context_create")
2020static ngx_str_t proxy_on_configure = ngx_string ("proxy_on_configure" );
2121static ngx_str_t proxy_on_done = ngx_string ("proxy_on_done" );
2222static ngx_str_t proxy_on_delete = ngx_string ("proxy_on_delete" );
23+ static ngx_str_t proxy_on_request_headers =
24+ ngx_string ("proxy_on_request_headers" );
2325
2426
2527typedef struct {
@@ -348,6 +350,7 @@ ngx_http_wasm_on_configure(ngx_http_wasm_plugin_t *hw_plugin, const char *conf,
348350 if (hwp_ctx -> state == NULL ) {
349351 goto free_hwp_ctx ;
350352 }
353+ hwp_ctx -> state -> r = NULL ;
351354
352355 state_conf = (u_char * ) (hwp_ctx -> state + 1 );
353356 /* copy conf so we can access it anytime */
@@ -360,6 +363,9 @@ ngx_http_wasm_on_configure(ngx_http_wasm_plugin_t *hw_plugin, const char *conf,
360363
361364 rc = ngx_wasm_vm .call (plugin , & proxy_on_configure , true,
362365 NGX_WASM_PARAM_I32_I32 , ctx_id , size );
366+
367+ ngx_http_wasm_set_state (NULL );
368+
363369 if (rc <= 0 ) {
364370 ngx_log_error (NGX_LOG_ERR , log , 0 , "failed to configure plugin context %d, rc: %d" ,
365371 ctx_id , rc );
@@ -431,42 +437,47 @@ static void
431437ngx_http_wasm_cleanup (void * data )
432438{
433439 ngx_int_t rc ;
440+ ngx_uint_t i ;
434441 ngx_http_wasm_ctx_t * ctx = data ;
435- ngx_http_wasm_http_ctx_t * http_ctx = ctx -> http_ctx ;
442+ ngx_array_t * http_ctxs = ctx -> http_ctxs ;
436443 uint32_t ctx_id ;
444+ ngx_http_wasm_http_ctx_t * http_ctx ;
437445 ngx_http_wasm_plugin_ctx_t * hwp_ctx ;
438446 void * plugin ;
439447 ngx_log_t * log ;
440448
441449 log = ngx_cycle -> log ;
442450
443- if (http_ctx == NULL ) {
451+ if (http_ctxs == NULL ) {
444452 return ;
445453 }
446454
447- ctx_id = http_ctx -> id ;
448- hwp_ctx = http_ctx -> hwp_ctx ;
449- plugin = hwp_ctx -> hw_plugin -> plugin ;
455+ for (i = 0 ; i < http_ctxs -> nelts ; i ++ ) {
456+ http_ctx = ((ngx_http_wasm_http_ctx_t * * ) http_ctxs -> elts )[i ];
457+ ctx_id = http_ctx -> id ;
458+ hwp_ctx = http_ctx -> hwp_ctx ;
459+ plugin = hwp_ctx -> hw_plugin -> plugin ;
450460
451- ngx_queue_remove (& http_ctx -> queue );
452- ngx_queue_insert_head (& hwp_ctx -> free , & http_ctx -> queue );
461+ ngx_queue_remove (& http_ctx -> queue );
462+ ngx_queue_insert_head (& hwp_ctx -> free , & http_ctx -> queue );
453463
454- rc = ngx_wasm_vm .call (plugin , & proxy_on_done , true, NGX_WASM_PARAM_I32 , ctx_id );
455- if (rc <= 0 ) {
456- ngx_log_error (NGX_LOG_ERR , log , 0 , "failed to mark context %d as done, rc: %d" ,
457- ctx_id , rc );
458- }
464+ rc = ngx_wasm_vm .call (plugin , & proxy_on_done , true, NGX_WASM_PARAM_I32 , ctx_id );
465+ if (rc <= 0 ) {
466+ ngx_log_error (NGX_LOG_ERR , log , 0 , "failed to mark context %d as done, rc: %d" ,
467+ ctx_id , rc );
468+ }
459469
460- rc = ngx_wasm_vm .call (plugin , & proxy_on_delete , false, NGX_WASM_PARAM_I32 , ctx_id );
461- if (rc != NGX_OK ) {
462- ngx_log_error (NGX_LOG_ERR , log , 0 , "failed to delete context %d, rc: %d" ,
463- ctx_id , rc );
464- }
470+ rc = ngx_wasm_vm .call (plugin , & proxy_on_delete , false, NGX_WASM_PARAM_I32 , ctx_id );
471+ if (rc != NGX_OK ) {
472+ ngx_log_error (NGX_LOG_ERR , log , 0 , "failed to delete context %d, rc: %d" ,
473+ ctx_id , rc );
474+ }
465475
466- ngx_log_error (NGX_LOG_INFO , log , 0 , "free http context %d" , ctx_id );
476+ ngx_log_error (NGX_LOG_INFO , log , 0 , "free http context %d" , ctx_id );
467477
468- if (hwp_ctx -> done ) {
469- ngx_http_wasm_free_plugin_ctx (hwp_ctx );
478+ if (hwp_ctx -> done ) {
479+ ngx_http_wasm_free_plugin_ctx (hwp_ctx );
480+ }
470481 }
471482}
472483
@@ -505,19 +516,41 @@ ngx_http_wasm_get_module_ctx(ngx_http_request_t *r)
505516ngx_http_wasm_http_ctx_t *
506517ngx_http_wasm_fetch_http_ctx (ngx_http_wasm_plugin_ctx_t * hwp_ctx , ngx_http_request_t * r )
507518{
519+ ngx_uint_t i ;
508520 ngx_http_wasm_ctx_t * ctx ;
521+ ngx_http_wasm_http_ctx_t * http_ctx ;
522+ ngx_http_wasm_http_ctx_t * * p ;
509523
510524
511525 ctx = ngx_http_wasm_get_module_ctx (r );
512526 if (ctx == NULL ) {
513527 return NULL ;
514528 }
515529
516- if (ctx -> http_ctx == NULL ) {
517- ctx -> http_ctx = ngx_http_wasm_create_http_ctx (hwp_ctx , r );
530+ if (ctx -> http_ctxs == NULL ) {
531+ ctx -> http_ctxs = ngx_array_create (r -> pool , 1 , sizeof (ngx_http_wasm_ctx_t * ));
532+ if (ctx -> http_ctxs == NULL ) {
533+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 , "no memory" );
534+ return NULL ;
535+ }
518536 }
519537
520- return ctx -> http_ctx ;
538+ p = ctx -> http_ctxs -> elts ;
539+
540+ for (i = 0 ; i < ctx -> http_ctxs -> nelts ; i ++ ) {
541+ if (p [i ]-> hwp_ctx == hwp_ctx ) {
542+ return p [i ];
543+ }
544+ }
545+
546+ http_ctx = ngx_http_wasm_create_http_ctx (hwp_ctx , r );
547+ if (http_ctx == NULL ) {
548+ return NULL ;
549+ }
550+
551+ p = ngx_array_push (ctx -> http_ctxs );
552+ * p = http_ctx ;
553+ return http_ctx ;
521554}
522555
523556
@@ -536,12 +569,19 @@ ngx_http_wasm_on_http(ngx_http_wasm_plugin_ctx_t *hwp_ctx, ngx_http_request_t *r
536569 return NGX_DECLINED ;
537570 }
538571
539- rc = NGX_OK ;
572+ hwp_ctx -> state -> r = r ;
573+ ngx_http_wasm_set_state (hwp_ctx -> state );
540574
541575 http_ctx = ngx_http_wasm_fetch_http_ctx (hwp_ctx , r );
542576 if (http_ctx == NULL ) {
577+ ngx_http_wasm_set_state (NULL );
543578 return NGX_DECLINED ;
544579 }
545580
581+ rc = ngx_wasm_vm .call (hwp_ctx -> hw_plugin -> plugin ,
582+ & proxy_on_request_headers ,
583+ true, NGX_WASM_PARAM_I32_I32_I32 , http_ctx -> id ,
584+ 0 , 1 );
585+ ngx_http_wasm_set_state (NULL );
546586 return rc ;
547587}
0 commit comments