@@ -67,6 +67,9 @@ static int (*set_req_header) (ngx_http_request_t *r,
6767 const char * key , size_t key_len , const char * value ,
6868 size_t value_len , ngx_str_t * mvals , size_t mvals_len ,
6969 int override , char * * errmsg );
70+ static int (* set_variable )(ngx_http_request_t * r , u_char * name_data ,
71+ size_t name_len , u_char * lowcase_buf , u_char * value , size_t value_len ,
72+ u_char * errbuf , size_t * errlen );
7073
7174static char * err_bad_ctx = "API disabled in the current context" ;
7275static char * err_no_req_ctx = "no request ctx found" ;
@@ -125,6 +128,7 @@ ngx_http_wasm_resolve_symbol(void)
125128 must_resolve_symbol (get_req_headers_count , ngx_http_lua_ffi_req_get_headers_count );
126129 must_resolve_symbol (get_req_headers , ngx_http_lua_ffi_req_get_headers );
127130 must_resolve_symbol (set_req_header , ngx_http_lua_ffi_req_set_header );
131+ must_resolve_symbol (set_variable , ngx_http_lua_ffi_var_set );
128132
129133 return NGX_OK ;
130134}
@@ -359,6 +363,51 @@ int32_t
359363proxy_set_property (int32_t path_data , int32_t path_size ,
360364 int32_t data , int32_t size )
361365{
366+ int result ;
367+ u_char * key_lowcase ;
368+ u_char * errmsg ;
369+ size_t errlen = 256 ;
370+ ngx_log_t * log ;
371+ const u_char * key ;
372+ const u_char * value ;
373+ ngx_http_request_t * r ;
374+
375+ log = ngx_http_wasm_get_log ();
376+ must_get_req (r );
377+
378+ /* fetch property key and value */
379+ key = ngx_wasm_vm .get_memory (log , path_data , path_size );
380+ if (key == NULL ) {
381+ return PROXY_RESULT_INVALID_MEMORY_ACCESS ;
382+ }
383+
384+ value = ngx_wasm_vm .get_memory (log , data , size );
385+ if (key == NULL ) {
386+ return PROXY_RESULT_INVALID_MEMORY_ACCESS ;
387+ }
388+
389+ /*
390+ * Request a piece of temporary memory to store the
391+ * lowercase characters of the property key and errmsg
392+ * of variable setting.
393+ */
394+ key_lowcase = ngx_http_wasm_get_string_buf (r -> pool , path_size + errlen );
395+ errmsg = key_lowcase + path_size ;
396+
397+ /* Call the functions in lua-resty-core to set the variables. */
398+ result = set_variable (r , (u_char * ) key , path_size , key_lowcase ,
399+ (u_char * ) value , size , errmsg , & errlen );
400+
401+ if (result != NGX_OK ) {
402+ ngx_log_error (NGX_LOG_ERR , log , 0 , (const char * )errmsg );
403+
404+ if (ngx_strstrn (errmsg , "not found for writing" , 20 ) != NULL ) {
405+ return PROXY_RESULT_NOT_FOUND ;
406+ }
407+
408+ return PROXY_RESULT_INTERNAL_FAILURE ;
409+ }
410+
362411 return PROXY_RESULT_OK ;
363412}
364413
0 commit comments