I have the following code but I have always the same mistake, I just wanna make a simple HTTP Request. PLEASE HELP
use proxy_wasm::traits::;
use proxy_wasm::types::;
use serde::Deserialize;
use log::{info};
use reqwest;
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box {
Box::new(CustomAuthRootContext {
config: CustomAuthConfig::default(),
})
});
}}
//IT SEEMS THAT IS THE DEFINITION OF THE CLASS STRUCTURE CustomAuthRootContext THAT IS THE ONE THAT "MATCHES OR DETECTS THE HTTP PROTOCOL"
struct CustomAuthRootContext {
config: CustomAuthConfig,
}
//FUNCTION THAT GETS THE VALUE THAT WE DEFINE IN THE CONFIGURATION WHEN WE APPLIED THE CUSTOM POLIY
#[derive(Default, Clone, Deserialize)]
struct CustomAuthConfig {
#[serde(alias = "dapiUrl")]
secret_value: String,
}
//IT SEEMS THAT IS THE DEFINITION OF THE CLASS STRUCTURE CustomAuthHttpContext THAT IS THE ONE THAT EXECUTES "ACTIONS" ONCE THE HTTP TRANSACTION IS BEING EXECUTED.
struct CustomAuthHttpContext {
pub config: CustomAuthConfig,
}
impl Context for CustomAuthRootContext {}
impl Context for CustomAuthHttpContext {}
//AS THE FIRST WORD SAYS, IS THE IMPLEMENTATION FOR THE METHOR CustomAuthHttpContext
//THAT SEEMS IS THE PRINCIPAL ONCE THE HTTP TRANSACTION STARTS
impl HttpContext for CustomAuthHttpContext {
fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
info!("STARTING POLICY!!");
let client = reqwest::Client::new();
let _res = client.post("http://httpbin.org/post")
.body("the exact body that is sent")
.send();
println!("body = {:?}", _res);
if let Some(value) = self.get_http_request_header("x-custom-auth") {
info!("the answer was: {:?}", value);
if self.config.secret_value == value {
return Action::Continue;
}
}
self.send_http_response(401, Vec::new(), None);
Action::Pause
}
}
//AS THE FIRST WORD SAYS, IS THE IMPLEMENTATION FOR THE METHOD CustomAuthRootContext
//THAT SEEMS IS THE ONE WHERE YOU CAN DEFINE THE INIZILIZATION OF METHODS OR SOMETHING LIKE THAT
//BEFORE ANY TRANSACTION BEGIN.... THIS IS THE METHOD THAT LESS UNDERSTAND.
impl RootContext for CustomAuthRootContext {
fn on_configure(&mut self, _: usize) -> bool {
if let Some(config_bytes) = self.get_plugin_configuration() {
self.config = serde_json::from_slice(config_bytes.as_slice()).unwrap();
}
true
}
fn create_http_context(&self, _: u32) -> Option<Box<dyn HttpContext>> {
Some(Box::new(CustomAuthHttpContext {
config: self.config.clone(),
}))
}
fn get_type(&self) -> Option<ContextType> {
Some(ContextType::HttpContext)
}
}