You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/05/09 02:07:11 UTC

[GitHub] [apisix-ingress-controller] SRCchen commented on issue #1004: request help: 关于对wasm的支持

SRCchen commented on issue #1004:
URL: https://github.com/apache/apisix-ingress-controller/issues/1004#issuecomment-1120558391

   `proxy_wasm_rust_sdk` version: 0.2
   
   cargo.toml
   ```toml
   [package]
   name = "wasmdemo"
   version = "0.1.0"
   edition = "2021"
   
   [lib]
   path = "src/lib.rs"
   crate-type = ["cdylib"]
   
   
   [dependencies]
   log = "0.4"
   proxy-wasm = "0.2"
   ```
   
   example code:
   ```rust
   use log::trace;
   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(HttpAuthRandom) });
   }}
   
   struct HttpAuthRandom;
   
   impl HttpContext for HttpAuthRandom {
       fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
           self.dispatch_http_call(
               "httpbin",
               vec![
                   (":method", "GET"),
                   (":path", "/bytes/1"),
                   (":authority", "httpbin.org"),
               ],
               None,
               vec![],
               Duration::from_secs(5),
           )
           .unwrap();
           Action::Pause
       }
   
       fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action {
           self.set_http_response_header("Powered-By", Some("proxy-wasm"));
           Action::Continue
       }
   }
   
   impl Context for HttpAuthRandom {
       fn on_http_call_response(&mut self, _: u32, _: usize, body_size: usize, _: usize) {
           if let Some(body) = self.get_http_call_response_body(0, body_size) {
               if !body.is_empty() && body[0] % 2 == 0 {
                   trace!("Access granted.");
                   self.resume_http_request();
                   return;
               }
           }
           trace!("Access forbidden.");
           self.send_http_response(
               403,
               vec![("Powered-By", "proxy-wasm")],
               Some(b"Access forbidden.\n"),
           );
       }
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org