Skip to content

Commit ccc83f7

Browse files
authored
fix: proxywasm.GetHttpRequestHeader(:authority) (#154)
* fix: proxywasm.GetHttpRequestHeader(:authority) Signed-off-by: Sn0rt <[email protected]>
1 parent 5a82b90 commit ccc83f7

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

src/http/ngx_http_wasm_api.c

+9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static ngx_str_t *ngx_http_wasm_get_path(ngx_http_request_t *r);
7171
static ngx_str_t *ngx_http_wasm_get_method(ngx_http_request_t *r);
7272
static ngx_str_t *ngx_http_wasm_get_scheme(ngx_http_request_t *r);
7373
static ngx_str_t *ngx_http_wasm_get_status(ngx_http_request_t *r);
74+
static ngx_str_t *ngx_http_wasm_get_authority(ngx_http_request_t *r);
7475
static ngx_str_t *ngx_http_wasm_get_pseudo_header(ngx_http_request_t *r,
7576
u_char *key_data, size_t key_size, int ty);
7677
static ngx_str_t *ngx_http_wasm_get_pseudo_req_header(ngx_http_request_t *r,
@@ -114,6 +115,7 @@ static ngx_http_wasm_pseudo_header_t wasm_pseudo_req_header_static_table[] = {
114115
{ngx_string(":scheme"), PROXY_WASM_REQUEST_HEADER_SCHEME, ngx_http_wasm_get_scheme},
115116
{ngx_string(":path"), PROXY_WASM_REQUEST_HEADER_PATH, ngx_http_wasm_get_path},
116117
{ngx_string(":method"), PROXY_WASM_REQUEST_HEADER_METHOD, ngx_http_wasm_get_method},
118+
{ngx_string(":authority"), PROXY_WASM_REQUEST_HEADER_AUTHORITY, ngx_http_wasm_get_authority},
117119
};
118120

119121
static ngx_http_wasm_pseudo_header_t wasm_pseudo_resp_header_static_table[] = {
@@ -823,6 +825,13 @@ ngx_http_wasm_get_path(ngx_http_request_t *r)
823825
return &r->unparsed_uri;
824826
}
825827

828+
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.2
829+
// TODO: need bugfix
830+
static ngx_str_t *
831+
ngx_http_wasm_get_authority(ngx_http_request_t *r)
832+
{
833+
return &r->headers_in.host->value;
834+
}
826835

827836
static ngx_str_t *
828837
ngx_http_wasm_get_method(ngx_http_request_t *r)

t/http_req_header.t

+42-25
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,22 @@ error status returned by host: not found
7474

7575

7676

77-
=== TEST 4: get header, ignoring case
77+
=== TEST 4: get http request authority
78+
--- config
79+
location /t {
80+
content_by_lua_block {
81+
local wasm = require("resty.proxy-wasm")
82+
local plugin = assert(wasm.load("plugin", "t/testdata/http_header/main.go.wasm"))
83+
local ctx = assert(wasm.on_configure(plugin, 'req_hdr_get_authority'))
84+
assert(wasm.on_http_request_headers(ctx))
85+
}
86+
}
87+
--- error_log
88+
get request authority: localhost
89+
90+
91+
92+
=== TEST 5: get header, ignoring case
7893
--- config
7994
location /t {
8095
content_by_lua_block {
@@ -93,7 +108,7 @@ get request header: foo,
93108

94109

95110

96-
=== TEST 5: get all header
111+
=== TEST 6: get all header
97112
--- config
98113
location /t {
99114
content_by_lua_block {
@@ -114,10 +129,11 @@ get request header: x-api foo
114129
get request header: :scheme http
115130
get request header: :path /t
116131
get request header: :method GET
132+
get request header: :authority localhost
117133

118134

119135

120-
=== TEST 6: get all header, repeated headers
136+
=== TEST 7: get all header, repeated headers
121137
--- config
122138
location /t {
123139
content_by_lua_block {
@@ -140,10 +156,11 @@ get request header: x-api bar
140156
get request header: :scheme http
141157
get request header: :path /t
142158
get request header: :method GET
159+
get request header: :authority localhost
143160

144161

145162

146-
=== TEST 7: set header
163+
=== TEST 8: set header
147164
--- config
148165
location /t {
149166
access_by_lua_block {
@@ -163,7 +180,7 @@ bar
163180

164181

165182

166-
=== TEST 8: set header, missing
183+
=== TEST 9: set header, missing
167184
--- config
168185
location /t {
169186
access_by_lua_block {
@@ -181,7 +198,7 @@ bar
181198

182199

183200

184-
=== TEST 9: add header
201+
=== TEST 10: add header
185202
--- config
186203
location /t {
187204
access_by_lua_block {
@@ -201,7 +218,7 @@ foobar
201218

202219

203220

204-
=== TEST 10: add header, missing
221+
=== TEST 11: add header, missing
205222
--- config
206223
location /t {
207224
access_by_lua_block {
@@ -219,7 +236,7 @@ bar
219236

220237

221238

222-
=== TEST 11: remove header
239+
=== TEST 12: remove header
223240
--- config
224241
location /t {
225242
access_by_lua_block {
@@ -239,7 +256,7 @@ nil
239256

240257

241258

242-
=== TEST 12: remove header, missing
259+
=== TEST 13: remove header, missing
243260
--- config
244261
location /t {
245262
access_by_lua_block {
@@ -257,7 +274,7 @@ nil
257274

258275

259276

260-
=== TEST 13: remove header, all of it
277+
=== TEST 14: remove header, all of it
261278
--- config
262279
location /t {
263280
access_by_lua_block {
@@ -278,7 +295,7 @@ nil
278295

279296

280297

281-
=== TEST 14: get path (not args)
298+
=== TEST 15: get path (not args)
282299
--- config
283300
location /t {
284301
content_by_lua_block {
@@ -295,7 +312,7 @@ get request path: /t,
295312

296313

297314

298-
=== TEST 15: get path (args)
315+
=== TEST 16: get path (args)
299316
--- config
300317
location /t {
301318
content_by_lua_block {
@@ -314,7 +331,7 @@ get request path: /t?q=hello&a=world,
314331

315332

316333

317-
=== TEST 16: get method (GET)
334+
=== TEST 17: get method (GET)
318335
--- config
319336
location /t {
320337
content_by_lua_block {
@@ -331,7 +348,7 @@ get request method: GET,
331348

332349

333350

334-
=== TEST 17: get method (POST)
351+
=== TEST 18: get method (POST)
335352
--- config
336353
location /t {
337354
content_by_lua_block {
@@ -350,7 +367,7 @@ get request method: POST,
350367

351368

352369

353-
=== TEST 18: get method (PUT)
370+
=== TEST 19: get method (PUT)
354371
--- config
355372
location /t {
356373
content_by_lua_block {
@@ -369,7 +386,7 @@ get request method: PUT,
369386

370387

371388

372-
=== TEST 19: get method (DELETE)
389+
=== TEST 20: get method (DELETE)
373390
--- config
374391
location /t {
375392
content_by_lua_block {
@@ -388,7 +405,7 @@ get request method: DELETE,
388405

389406

390407

391-
=== TEST 20: get schema(http2)
408+
=== TEST 21: get schema(http2)
392409
--- http2
393410
--- config
394411
location /t {
@@ -406,7 +423,7 @@ get request scheme: http,
406423

407424

408425

409-
=== TEST 21: get header, abnormal
426+
=== TEST 22: get header, abnormal
410427
--- config
411428
location /t {
412429
content_by_lua_block {
@@ -430,7 +447,7 @@ error status returned by host: not found
430447

431448

432449

433-
=== TEST 22: set header, abnormal
450+
=== TEST 23: set header, abnormal
434451
--- config
435452
location /t {
436453
content_by_lua_block {
@@ -451,7 +468,7 @@ location /t {
451468

452469

453470

454-
=== TEST 23: get schema(http)
471+
=== TEST 24: get schema(http)
455472
--- config
456473
location /t {
457474
content_by_lua_block {
@@ -468,7 +485,7 @@ get request scheme: http,
468485

469486

470487

471-
=== TEST 24: delete pseduo headers
488+
=== TEST 25: delete pseduo headers
472489
--- config
473490
location /t {
474491
content_by_lua_block {
@@ -489,7 +506,7 @@ can't remove pseudo header
489506

490507

491508

492-
=== TEST 25: set path, abnormal
509+
=== TEST 26: set path, abnormal
493510
--- config
494511
location /t {
495512
content_by_lua_block {
@@ -518,7 +535,7 @@ failed to set request header :path: invalid char
518535

519536

520537

521-
=== TEST 26: set path
538+
=== TEST 27: set path
522539
--- config
523540
location /t {
524541
content_by_lua_block {
@@ -549,7 +566,7 @@ a?a=c
549566

550567

551568

552-
=== TEST 27: set method, abnormal
569+
=== TEST 28: set method, abnormal
553570
--- config
554571
location /t {
555572
content_by_lua_block {
@@ -580,7 +597,7 @@ GET
580597

581598

582599

583-
=== TEST 28: set method
600+
=== TEST 29: set method
584601
--- config
585602
location /t {
586603
content_by_lua_block {

t/testdata/http_header/main.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
//
14+
1515
package main
1616

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

150+
case "req_hdr_get_authority":
151+
authority, err := proxywasm.GetHttpRequestHeader(":authority")
152+
if err != nil {
153+
proxywasm.LogErrorf("error get request authority: %v", err)
154+
return types.ActionContinue
155+
}
156+
proxywasm.LogWarnf("get request authority: %v", authority)
157+
150158
case "req_pseduo_del":
151159
proxywasm.RemoveHttpRequestHeader(":method")
152160
proxywasm.RemoveHttpRequestHeader(":path")

0 commit comments

Comments
 (0)