-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement get_http_call_response_header (#107)
Fix #106
- Loading branch information
1 parent
ff257a0
commit e6a8009
Showing
5 changed files
with
260 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# 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. | ||
# | ||
use t::WASM 'no_plan'; | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: get header | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local wasm = require("resty.proxy-wasm") | ||
local plugin = assert(wasm.load("fault_injection", | ||
"t/testdata/rust/http-call/target/wasm32-wasi/debug/http_call.wasm")) | ||
local ctx = assert(wasm.on_configure(plugin, '{}')) | ||
assert(wasm.on_http_request_headers(ctx)) | ||
} | ||
} | ||
--- response_headers | ||
resp-status: 200 | ||
resp-content-type: text/plain | ||
--- error_code: 403 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# 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] | ||
name = "http_call" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
path = "./http_call.rs" | ||
|
||
[dependencies] | ||
log = "0.4" | ||
proxy-wasm = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// 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. | ||
// | ||
use proxy_wasm::traits::*; | ||
use proxy_wasm::types::*; | ||
use std::time::Duration; | ||
|
||
proxy_wasm::main! {{ | ||
proxy_wasm::set_log_level(LogLevel::Trace); | ||
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpCall) }); | ||
}} | ||
|
||
struct HttpCall; | ||
|
||
impl HttpContext for HttpCall { | ||
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { | ||
self.dispatch_http_call( | ||
"127.0.0.1:1980", | ||
vec![ | ||
(":method", "GET"), | ||
(":path", "/bytes/1"), | ||
(":authority", "httpbin.org"), | ||
], | ||
None, | ||
vec![], | ||
Duration::from_secs(5), | ||
) | ||
.unwrap(); | ||
Action::Pause | ||
} | ||
} | ||
|
||
impl Context for HttpCall { | ||
fn on_http_call_response(&mut self, _: u32, _: usize, _: usize, _: usize) { | ||
let s = self.get_http_call_response_header("Content-Type").unwrap(); | ||
let st = self.get_http_call_response_header(":status").unwrap(); | ||
self.send_http_response( | ||
403, | ||
vec![("Resp-Content-Type", &s), ("Resp-Status", &st)], | ||
Some(b"Access forbidden.\n"), | ||
); | ||
} | ||
} |