use log::warn;
use proxy_wasm::traits::{Context, HttpContext};
use proxy_wasm::types::{Action, LogLevel};
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpCall) });
}}
struct HttpCall;
impl Context for HttpCall {}
impl HttpContext for HttpCall {
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
warn!("on_http_request_headers");
// set wasm_process_req_body = true
self.set_property(vec!["wasm_process_req_body"], Some("true".as_bytes()));
let test_header = self.get_http_request_header("test");
match test_header {
Some(s) => warn!("header test: {}", s),
None => warn!("no test header"),
}
Action::Continue
}
fn on_http_request_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
warn!("on_http_request_body");
let body = self.get_http_request_body(0, _body_size);
match body {
Some(b) => warn!("body: {:?}", String::from_utf8(b).unwrap()),
None => warn!("no body"),
}
Action::Continue
}
}
## build plugin
cargo build --target wasm32-wasi --release
## setup plugin
wasm:
plugins:
- name: wasm_test
priority: 30000
file: wasm-plugin/target/wasm32-wasi/release/wasm_plugin.wasm
## setup route
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/*",
"plugins": {
"wasm_test": {
"conf": "blahblah"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org": 1
}
}
}'
curl -XPOST 127.0.0.1:9080/anything --data '{"test":"test"}' -H "Content-Type: application/json" -H "test: aHeader"
{
"args": {},
"data": "{\"test\":\"test\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "15",
"Content-Type": "application/json",
"Host": "127.0.0.1",
"Test": "aHeader",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-632c9c74-19a6ebbf6b30bf1a6af9e647",
"X-Forwarded-Host": "127.0.0.1"
},
"json": {
"test": "test"
},
"method": "GET",
"origin": "127.0.0.1, 47.240.173.85",
"url": "http://127.0.0.1/anything"
}
2022/09/23 01:33:40 [warn] 19381#19381: *4006 on_http_request_headers, client: 127.0.0.1, server: _, request: "GET /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:33:40 [warn] 19381#19381: *4006 header test: aHeader, client: 127.0.0.1, server: _, request: "GET /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:33:40 [warn] 19381#19381: *4006 on_http_request_body, client: 127.0.0.1, server: _, request: "GET /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:33:40 [warn] 19381#19381: *4006 body: "{\"test\":\"test\"}", client: 127.0.0.1, server: _, request: "GET /anything HTTP/1.1", host: "127.0.0.1:9080"
curl -XPOST 127.0.0.1:9080/anything -H "Content-Type: application/json" -H "test: aHeader"
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Type": "application/json",
"Host": "127.0.0.1",
"Test": "aHeader",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-632c9d62-408889882d28075c3a94f29c",
"X-Forwarded-Host": "127.0.0.1"
},
"json": null,
"method": "POST",
"origin": "127.0.0.1, 47.240.173.85",
"url": "http://127.0.0.1/anything"
}
2022/09/23 01:37:38 [warn] 19381#19381: *10098 on_http_request_headers, client: 127.0.0.1, server: _, request: "POST /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:37:38 [warn] 19381#19381: *10098 header test: aHeader, client: 127.0.0.1, server: _, request: "POST /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:37:38 [warn] 19381#19381: *10098 on_http_request_body, client: 127.0.0.1, server: _, request: "POST /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:37:38 [warn] 19381#19381: *10098 no body, client: 127.0.0.1, server: _, request: "POST /anything HTTP/1.1", host: "127.0.0.1:9080"
curl -XPOST 127.0.0.1:9080/anything
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-632c9d93-567179c15a03b33b387cc306",
"X-Forwarded-Host": "127.0.0.1"
},
"json": null,
"method": "POST",
"origin": "127.0.0.1, 47.240.173.85",
"url": "http://127.0.0.1/anything"
}
2022/09/23 01:38:27 [warn] 19381#19381: *11347 on_http_request_headers, client: 127.0.0.1, server: _, request: "POST /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:38:27 [warn] 19381#19381: *11347 no test header, client: 127.0.0.1, server: _, request: "POST /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:38:27 [warn] 19381#19381: *11347 on_http_request_body, client: 127.0.0.1, server: _, request: "POST /anything HTTP/1.1", host: "127.0.0.1:9080"
2022/09/23 01:38:27 [warn] 19381#19381: *11347 no body, client: 127.0.0.1, server: _, request: "POST /anything HTTP/1.1", host: "127.0.0.1:9080"
That's amazing, thanks a lot!