Skip to content

Commit

Permalink
Add examples/http_config.rs
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Grimm <dgrimm@redhat.com>
  • Loading branch information
dgn committed Nov 20, 2020
1 parent 6488b8a commit 79e8621
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ crate-type = ["cdylib"]
name = "http_body"
path = "examples/http_body.rs"
crate-type = ["cdylib"]

[[example]]
name = "http_config"
path = "examples/http_config.rs"
crate-type = ["cdylib"]
73 changes: 73 additions & 0 deletions examples/http_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2020 Google LLC
//
// 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 std::str;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;

#[no_mangle]
pub fn _start() {
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> {
Box::new(HttpConfigHeaderRootContext{
header_content: "".to_string(),
})
});
}

struct HttpConfigHeader{
header_content: String
}

impl Context for HttpConfigHeader {}

impl HttpContext for HttpConfigHeader {

fn on_http_response_headers(&mut self, _num_headers: usize) -> Action {
self.add_http_response_header("custom-header", self.header_content.as_str());

Action::Continue
}
}

struct HttpConfigHeaderRootContext {
header_content: String
}

impl Context for HttpConfigHeaderRootContext {}

impl RootContext for HttpConfigHeaderRootContext {

fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
true
}

fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
if let Some(config_bytes) = self.get_configuration() {
self.header_content = str::from_utf8(config_bytes.as_ref()).unwrap().to_owned()
}
true
}

fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
Box::new(HttpConfigHeader{
header_content: self.header_content.clone(),
})
}

fn get_type(&self) -> ContextType {
ContextType::HttpContext
}

}

0 comments on commit 79e8621

Please sign in to comment.