The following code samples show how to add HTTP request and response
headers.
C++
#include"proxy_wasm_intrinsics.h"classMyHttpContext:publicContext{public:explicitMyHttpContext(uint32_tid,RootContext*root):Context(id,root){}FilterHeadersStatusonRequestHeaders(uint32_theaders,boolend_of_stream)override{// Always be a friendly proxy.addRequestHeader("Message","hello");replaceRequestHeader("Welcome","warm");returnFilterHeadersStatus::Continue;}FilterHeadersStatusonResponseHeaders(uint32_theaders,boolend_of_stream)override{// Conditionally add to a header value.automsg=getResponseHeader("Message");if(msg && msg->view()=="foo"){addResponseHeader("Message","bar");}// Unconditionally remove a header.removeResponseHeader("Welcome");returnFilterHeadersStatus::Continue;}};staticRegisterContextFactoryregister_StaticContext(CONTEXT_FACTORY(MyHttpContext),ROOT_FACTORY(RootContext));
Rust
useproxy_wasm::traits::*;useproxy_wasm::types::*;proxy_wasm::main!{{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(MyHttpContext) });
}}structMyHttpContext;implContextforMyHttpContext{}implHttpContextforMyHttpContext{fnon_http_request_headers(&mutself,_:usize,_:bool)->Action{// Always be a friendly proxy.self.add_http_request_header("Message","hello");self.set_http_request_header("Welcome",Some("warm"));returnAction::Continue;}fnon_http_response_headers(&mutself,_:usize,_:bool)->Action{// Conditionally add to a header value.letmsg=self.get_http_response_header("Message");ifmsg.unwrap_or_default()=="foo"{self.add_http_response_header("Message","bar");}// Unconditionally remove a header.self.set_http_response_header("Welcome",None);returnAction::Continue;}}
Rewrite the request URL
The following code samples show how to rewrite the request URL by using
regular expressions. The following code samples remove part of the path, but
any URI mutation, such as path, query, or fragment, is feasible.
These samples also show best practices around regular expressions, namely
using linear-time regular expression libraries and compiling the expressions at
plugin initialization time.
C++
#include"proxy_wasm_intrinsics.h"#include"re2/re2.h"classMyRootContext:publicRootContext{public:explicitMyRootContext(uint32_tid,std::string_viewroot_id):RootContext(id,root_id){}boolonConfigure(size_t)override{// Compile the regex expression at plugin setup time.path_match.emplace("/foo-([^/]+)/");returnpath_match->ok();}std::optional<re2::RE2>path_match;};classMyHttpContext:publicContext{public:explicitMyHttpContext(uint32_tid,RootContext*root):Context(id,root),root_(static_cast<MyRootContext*>(root)){}FilterHeadersStatusonRequestHeaders(uint32_theaders,boolend_of_stream)override{autopath=getRequestHeader(":path");if(path){std::stringedit=path->toString();// mutable copyif(re2::RE2::Replace(&edit,*root_->path_match,"/\\1/")){replaceRequestHeader(":path",edit);}}returnFilterHeadersStatus::Continue;}private:constMyRootContext*root_;};staticRegisterContextFactoryregister_StaticContext(CONTEXT_FACTORY(MyHttpContext),ROOT_FACTORY(MyRootContext));
The following code samples show how to perform a basic check on the
request query string parameters and emit parsed information to Cloud Logging.
These samples also show how to parse URLs.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-01-28 UTC."],[],[]]