@@ -365,6 +365,7 @@ pw_proxy_add_header_map_value(void *env, wasmtime_caller_t *caller,
365365 struct vwasm_proxy_ctx * ctx ;
366366 struct http * hp ;
367367 char key_buf [256 ], val_buf [4096 ], hdr_line [4352 ];
368+ const char * ws_hdr ;
368369 int32_t map_type ;
369370
370371 (void )env ;
@@ -407,8 +408,13 @@ pw_proxy_add_header_map_value(void *env, wasmtime_caller_t *caller,
407408 }
408409
409410 snprintf (hdr_line , sizeof (hdr_line ), "%s: %s" , key_buf , val_buf );
410- http_SetHeader (hp ,
411- WS_Copy (ctx -> vrt_ctx -> ws , hdr_line , (int )(strlen (hdr_line ) + 1 )));
411+ ws_hdr = WS_Copy (ctx -> vrt_ctx -> ws , hdr_line ,
412+ (int )(strlen (hdr_line ) + 1 ));
413+ if (ws_hdr == NULL ) {
414+ results [0 ].of .i32 = PROXY_INTERNAL ;
415+ return (NULL );
416+ }
417+ http_SetHeader (hp , ws_hdr );
412418
413419 results [0 ].of .i32 = PROXY_OK ;
414420 return (NULL );
@@ -426,6 +432,7 @@ pw_proxy_replace_header_map_value(void *env, wasmtime_caller_t *caller,
426432 struct vwasm_proxy_ctx * ctx ;
427433 struct http * hp ;
428434 char key_buf [256 ], val_buf [4096 ], hdr_search [260 ], hdr_line [4352 ];
435+ const char * ws_hdr ;
429436 int i ;
430437 int32_t map_type ;
431438
@@ -473,8 +480,13 @@ pw_proxy_replace_header_map_value(void *env, wasmtime_caller_t *caller,
473480 }
474481
475482 snprintf (hdr_line , sizeof (hdr_line ), "%s: %s" , key_buf , val_buf );
476- http_SetHeader (hp ,
477- WS_Copy (ctx -> vrt_ctx -> ws , hdr_line , (int )(strlen (hdr_line ) + 1 )));
483+ ws_hdr = WS_Copy (ctx -> vrt_ctx -> ws , hdr_line ,
484+ (int )(strlen (hdr_line ) + 1 ));
485+ if (ws_hdr == NULL ) {
486+ results [0 ].of .i32 = PROXY_INTERNAL ;
487+ return (NULL );
488+ }
489+ http_SetHeader (hp , ws_hdr );
478490
479491 results [0 ].of .i32 = PROXY_OK ;
480492 return (NULL );
@@ -783,7 +795,7 @@ pw_proxy_get_header_map_pairs(void *env, wasmtime_caller_t *caller,
783795 break ;
784796 }
785797 }
786- } else if (map_type == 1 ) {
798+ } else if (map_type == PROXY_MAP_HTTP_RESPONSE_HEADERS ) {
787799 if (hp -> hd [HTTP_HDR_STATUS ].b != NULL ) {
788800 pseudo_keys [num_pseudo ] = ":status" ;
789801 pseudo_vals [num_pseudo ] = hp -> hd [HTTP_HDR_STATUS ].b ;
@@ -951,6 +963,7 @@ pw_proxy_set_header_map_pairs(void *env, wasmtime_caller_t *caller,
951963 uint32_t map_type , data_ptr , data_size ;
952964 uint32_t num_pairs , i , offset ;
953965 char hdr_line [4352 ];
966+ const char * ws_hdr ;
954967
955968 (void )env ;
956969 (void )nargs ;
@@ -1050,7 +1063,13 @@ pw_proxy_set_header_map_pairs(void *env, wasmtime_caller_t *caller,
10501063 if (key_size > 0 && key_size < 256 && val_size < 4096 ) {
10511064 snprintf (hdr_line , sizeof (hdr_line ), "%.*s: %.*s" ,
10521065 (int )key_size , key , (int )val_size , val );
1053- http_SetHeader (hp , hdr_line );
1066+ ws_hdr = WS_Copy (ctx -> vrt_ctx -> ws , hdr_line ,
1067+ (int )(strlen (hdr_line ) + 1 ));
1068+ if (ws_hdr == NULL ) {
1069+ results [0 ].of .i32 = PROXY_INTERNAL ;
1070+ return (NULL );
1071+ }
1072+ http_SetHeader (hp , ws_hdr );
10541073 }
10551074 }
10561075
@@ -1072,6 +1091,7 @@ pw_proxy_get_header_map_size(void *env, wasmtime_caller_t *caller,
10721091 struct vwasm_proxy_ctx * ctx ;
10731092 int32_t map_type ;
10741093 uint32_t count = 0 ;
1094+ uint32_t i ;
10751095
10761096 (void )env ;
10771097 (void )nargs ;
@@ -1089,8 +1109,26 @@ pw_proxy_get_header_map_size(void *env, wasmtime_caller_t *caller,
10891109 count = tm -> count ;
10901110 } else {
10911111 const struct http * hp = pw_get_header_map (ctx , map_type );
1092- if (hp != NULL )
1112+ if (hp != NULL ) {
10931113 count = (uint32_t )(hp -> nhd - HTTP_HDR_FIRST );
1114+ if (map_type == PROXY_MAP_HTTP_REQUEST_HEADERS ) {
1115+ if (hp -> hd [HTTP_HDR_METHOD ].b != NULL )
1116+ count ++ ;
1117+ if (hp -> hd [HTTP_HDR_URL ].b != NULL )
1118+ count ++ ;
1119+ for (i = HTTP_HDR_FIRST ; i < hp -> nhd ; i ++ ) {
1120+ if (hp -> hd [i ].b != NULL &&
1121+ strncasecmp (hp -> hd [i ].b , "Host:" ,
1122+ 5 ) == 0 ) {
1123+ count ++ ;
1124+ break ;
1125+ }
1126+ }
1127+ } else if (map_type == PROXY_MAP_HTTP_RESPONSE_HEADERS &&
1128+ hp -> hd [HTTP_HDR_STATUS ].b != NULL ) {
1129+ count ++ ;
1130+ }
1131+ }
10941132 }
10951133
10961134 if (pw_write_u32 (ctx , (uint32_t )args [1 ].of .i32 , count ) != 0 ) {
0 commit comments