Skip to content

Commit

Permalink
fix: proxywasm.GetHttpRequestHeader(:authority) (#154)
Browse files Browse the repository at this point in the history
* fix: proxywasm.GetHttpRequestHeader(:authority)

Signed-off-by: Sn0rt <wangguohao.2009@gmail.com>
  • Loading branch information
Sn0rt authored Aug 16, 2023
1 parent 5a82b90 commit ccc83f7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
9 changes: 9 additions & 0 deletions src/http/ngx_http_wasm_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ static ngx_str_t *ngx_http_wasm_get_path(ngx_http_request_t *r);
static ngx_str_t *ngx_http_wasm_get_method(ngx_http_request_t *r);
static ngx_str_t *ngx_http_wasm_get_scheme(ngx_http_request_t *r);
static ngx_str_t *ngx_http_wasm_get_status(ngx_http_request_t *r);
static ngx_str_t *ngx_http_wasm_get_authority(ngx_http_request_t *r);
static ngx_str_t *ngx_http_wasm_get_pseudo_header(ngx_http_request_t *r,
u_char *key_data, size_t key_size, int ty);
static ngx_str_t *ngx_http_wasm_get_pseudo_req_header(ngx_http_request_t *r,
Expand Down Expand Up @@ -114,6 +115,7 @@ static ngx_http_wasm_pseudo_header_t wasm_pseudo_req_header_static_table[] = {
{ngx_string(":scheme"), PROXY_WASM_REQUEST_HEADER_SCHEME, ngx_http_wasm_get_scheme},
{ngx_string(":path"), PROXY_WASM_REQUEST_HEADER_PATH, ngx_http_wasm_get_path},
{ngx_string(":method"), PROXY_WASM_REQUEST_HEADER_METHOD, ngx_http_wasm_get_method},
{ngx_string(":authority"), PROXY_WASM_REQUEST_HEADER_AUTHORITY, ngx_http_wasm_get_authority},
};

static ngx_http_wasm_pseudo_header_t wasm_pseudo_resp_header_static_table[] = {
Expand Down Expand Up @@ -823,6 +825,13 @@ ngx_http_wasm_get_path(ngx_http_request_t *r)
return &r->unparsed_uri;
}

// https://datatracker.ietf.org/doc/html/rfc3986#section-3.2
// TODO: need bugfix
static ngx_str_t *
ngx_http_wasm_get_authority(ngx_http_request_t *r)
{
return &r->headers_in.host->value;
}

static ngx_str_t *
ngx_http_wasm_get_method(ngx_http_request_t *r)
Expand Down
67 changes: 42 additions & 25 deletions t/http_req_header.t
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,22 @@ error status returned by host: not found



=== TEST 4: get header, ignoring case
=== TEST 4: get http request authority
--- config
location /t {
content_by_lua_block {
local wasm = require("resty.proxy-wasm")
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
local ctx = assert(wasm.on_configure(plugin, 'req_hdr_get_authority'))
assert(wasm.on_http_request_headers(ctx))
}
}
--- error_log
get request authority: localhost



=== TEST 5: get header, ignoring case
--- config
location /t {
content_by_lua_block {
Expand All @@ -93,7 +108,7 @@ get request header: foo,



=== TEST 5: get all header
=== TEST 6: get all header
--- config
location /t {
content_by_lua_block {
Expand All @@ -114,10 +129,11 @@ get request header: x-api foo
get request header: :scheme http
get request header: :path /t
get request header: :method GET
get request header: :authority localhost



=== TEST 6: get all header, repeated headers
=== TEST 7: get all header, repeated headers
--- config
location /t {
content_by_lua_block {
Expand All @@ -140,10 +156,11 @@ get request header: x-api bar
get request header: :scheme http
get request header: :path /t
get request header: :method GET
get request header: :authority localhost



=== TEST 7: set header
=== TEST 8: set header
--- config
location /t {
access_by_lua_block {
Expand All @@ -163,7 +180,7 @@ bar



=== TEST 8: set header, missing
=== TEST 9: set header, missing
--- config
location /t {
access_by_lua_block {
Expand All @@ -181,7 +198,7 @@ bar



=== TEST 9: add header
=== TEST 10: add header
--- config
location /t {
access_by_lua_block {
Expand All @@ -201,7 +218,7 @@ foobar



=== TEST 10: add header, missing
=== TEST 11: add header, missing
--- config
location /t {
access_by_lua_block {
Expand All @@ -219,7 +236,7 @@ bar



=== TEST 11: remove header
=== TEST 12: remove header
--- config
location /t {
access_by_lua_block {
Expand All @@ -239,7 +256,7 @@ nil



=== TEST 12: remove header, missing
=== TEST 13: remove header, missing
--- config
location /t {
access_by_lua_block {
Expand All @@ -257,7 +274,7 @@ nil



=== TEST 13: remove header, all of it
=== TEST 14: remove header, all of it
--- config
location /t {
access_by_lua_block {
Expand All @@ -278,7 +295,7 @@ nil



=== TEST 14: get path (not args)
=== TEST 15: get path (not args)
--- config
location /t {
content_by_lua_block {
Expand All @@ -295,7 +312,7 @@ get request path: /t,



=== TEST 15: get path (args)
=== TEST 16: get path (args)
--- config
location /t {
content_by_lua_block {
Expand All @@ -314,7 +331,7 @@ get request path: /t?q=hello&a=world,



=== TEST 16: get method (GET)
=== TEST 17: get method (GET)
--- config
location /t {
content_by_lua_block {
Expand All @@ -331,7 +348,7 @@ get request method: GET,



=== TEST 17: get method (POST)
=== TEST 18: get method (POST)
--- config
location /t {
content_by_lua_block {
Expand All @@ -350,7 +367,7 @@ get request method: POST,



=== TEST 18: get method (PUT)
=== TEST 19: get method (PUT)
--- config
location /t {
content_by_lua_block {
Expand All @@ -369,7 +386,7 @@ get request method: PUT,



=== TEST 19: get method (DELETE)
=== TEST 20: get method (DELETE)
--- config
location /t {
content_by_lua_block {
Expand All @@ -388,7 +405,7 @@ get request method: DELETE,



=== TEST 20: get schema(http2)
=== TEST 21: get schema(http2)
--- http2
--- config
location /t {
Expand All @@ -406,7 +423,7 @@ get request scheme: http,



=== TEST 21: get header, abnormal
=== TEST 22: get header, abnormal
--- config
location /t {
content_by_lua_block {
Expand All @@ -430,7 +447,7 @@ error status returned by host: not found



=== TEST 22: set header, abnormal
=== TEST 23: set header, abnormal
--- config
location /t {
content_by_lua_block {
Expand All @@ -451,7 +468,7 @@ location /t {



=== TEST 23: get schema(http)
=== TEST 24: get schema(http)
--- config
location /t {
content_by_lua_block {
Expand All @@ -468,7 +485,7 @@ get request scheme: http,



=== TEST 24: delete pseduo headers
=== TEST 25: delete pseduo headers
--- config
location /t {
content_by_lua_block {
Expand All @@ -489,7 +506,7 @@ can't remove pseudo header



=== TEST 25: set path, abnormal
=== TEST 26: set path, abnormal
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -518,7 +535,7 @@ failed to set request header :path: invalid char



=== TEST 26: set path
=== TEST 27: set path
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -549,7 +566,7 @@ a?a=c



=== TEST 27: set method, abnormal
=== TEST 28: set method, abnormal
--- config
location /t {
content_by_lua_block {
Expand Down Expand Up @@ -580,7 +597,7 @@ GET



=== TEST 28: set method
=== TEST 29: set method
--- config
location /t {
content_by_lua_block {
Expand Down
10 changes: 9 additions & 1 deletion t/testdata/http_header/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
Expand Down Expand Up @@ -147,6 +147,14 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t
}
proxywasm.LogWarnf("get request scheme: %v", res)

case "req_hdr_get_authority":
authority, err := proxywasm.GetHttpRequestHeader(":authority")
if err != nil {
proxywasm.LogErrorf("error get request authority: %v", err)
return types.ActionContinue
}
proxywasm.LogWarnf("get request authority: %v", authority)

case "req_pseduo_del":
proxywasm.RemoveHttpRequestHeader(":method")
proxywasm.RemoveHttpRequestHeader(":path")
Expand Down

0 comments on commit ccc83f7

Please sign in to comment.