diff --git a/src/common/proxy_wasm/ngx_proxy_wasm.h b/src/common/proxy_wasm/ngx_proxy_wasm.h index 7d7f9d3e6..2daf69092 100644 --- a/src/common/proxy_wasm/ngx_proxy_wasm.h +++ b/src/common/proxy_wasm/ngx_proxy_wasm.h @@ -241,6 +241,7 @@ struct ngx_proxy_wasm_ctx_s { ngx_str_t connection_id; /* r->connection->number */ ngx_str_t mtls; /* ngx.https && ngx.ssl_client_verify */ ngx_str_t root_id; /* pwexec->root_id */ + ngx_str_t worker_id; /* ngx_worker */ ngx_str_t call_status; /* dispatch response status */ ngx_str_t response_status; /* response status */ ngx_uint_t call_code; diff --git a/src/common/proxy_wasm/ngx_proxy_wasm_properties.c b/src/common/proxy_wasm/ngx_proxy_wasm_properties.c index e26b92411..2d0b5de9f 100644 --- a/src/common/proxy_wasm/ngx_proxy_wasm_properties.c +++ b/src/common/proxy_wasm/ngx_proxy_wasm_properties.c @@ -394,6 +394,32 @@ get_filter_root_id(ngx_proxy_wasm_ctx_t *pwctx, ngx_str_t *path, } +static ngx_int_t +get_worker_id(ngx_proxy_wasm_ctx_t *pwctx, ngx_str_t *path, + ngx_str_t *value) +{ + size_t len; + u_char buf[NGX_OFF_T_LEN]; + + if (!pwctx->worker_id.len) { + len = ngx_sprintf(buf, "%i", ngx_worker) - buf; + + pwctx->worker_id.data = ngx_pnalloc(pwctx->pool, len); + if (pwctx->worker_id.data == NULL) { + return NGX_ERROR; + } + + ngx_memcpy(pwctx->worker_id.data, buf, len); + pwctx->worker_id.len = len; + } + + value->len = pwctx->worker_id.len; + value->data = pwctx->worker_id.data; + + return NGX_OK; +} + + static pwm2ngx_mapping_t pw2ngx[] = { #ifdef NGX_WASM_HTTP @@ -599,6 +625,12 @@ static pwm2ngx_mapping_t pw2ngx[] = { ngx_null_string, ¬_supported, NULL }, + /* host properties */ + + { ngx_string("worker_id"), + ngx_null_string, + &get_worker_id, NULL }, + { ngx_null_string, ngx_null_string, NULL, NULL } }; @@ -654,7 +686,7 @@ ngx_proxy_wasm_properties_init(ngx_conf_t *cf) pwm2ngx_init.hash = &pwm2ngx_hash.hash; pwm2ngx_init.key = ngx_hash_key; - pwm2ngx_init.max_size = 256; + pwm2ngx_init.max_size = 512; pwm2ngx_init.bucket_size = ngx_align(64, ngx_cacheline_size); pwm2ngx_init.name = "pwm2ngx_properties"; pwm2ngx_init.pool = cf->pool; diff --git a/t/03-proxy_wasm/hfuncs/117-proxy_properties_get.t b/t/03-proxy_wasm/hfuncs/117-proxy_properties_get.t index d6289e271..a79c1a148 100644 --- a/t/03-proxy_wasm/hfuncs/117-proxy_properties_get.t +++ b/t/03-proxy_wasm/hfuncs/117-proxy_properties_get.t @@ -596,7 +596,49 @@ qr/$checks/ -=== TEST 13: proxy_wasm - get_property() - uri encoded request.path on: request_headers +=== TEST 13: proxy_wasm - get_property() - host properties on: request_headers, request_body, response_headers, response_body +--- load_nginx_modules: ngx_http_echo_module +--- wasm_modules: hostcalls +--- config eval +my $phases = CORE::join(',', qw( + request_headers + request_body + response_headers + response_body +)); + +qq { + location /t { + proxy_wasm hostcalls 'on=$phases \ + test=/t/log/properties \ + name=worker_id'; + echo ok; + } +} +--- response_body +ok +--- grep_error_log eval: qr/worker_id+: [\w\.]+ at \w+/ +--- grep_error_log_out eval +my $checks; +my @phases = qw( + RequestHeaders + ResponseHeaders + ResponseBody + ResponseBody +); + +foreach my $phase (@phases) { + $checks .= "worker_id: 0 at $phase\n"; +} + +qr/$checks/ +--- no_error_log +[error] +[crit] + + + +=== TEST 14: proxy_wasm - get_property() - uri encoded request.path on: request_headers --- load_nginx_modules: ngx_http_echo_module --- wasm_modules: hostcalls --- config @@ -620,7 +662,7 @@ qr/request.path: \/t\?foo=std\:\:min\&bar=\[1,2\]/ -=== TEST 14: proxy_wasm - get_property() - explicitly unsupported properties on: request_headers +=== TEST 15: proxy_wasm - get_property() - explicitly unsupported properties on: request_headers --- load_nginx_modules: ngx_http_echo_module --- wasm_modules: hostcalls --- config eval @@ -667,7 +709,7 @@ qr/"response.code_details" property not supported -=== TEST 15: proxy_wasm - get_property() - unknown property on: request_headers +=== TEST 16: proxy_wasm - get_property() - unknown property on: request_headers --- wasm_modules: hostcalls --- load_nginx_modules: ngx_http_echo_module --- config