Skip to content

Commit 9e2e8e1

Browse files
committed
Add examples/http_config.rs
1 parent 60589b1 commit 9e2e8e1

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,8 @@ crate-type = ["cdylib"]
6060
name = "http_body"
6161
path = "examples/http_body.rs"
6262
crate-type = ["cdylib"]
63+
64+
[[example]]
65+
name = "http_config"
66+
path = "examples/http_config.rs"
67+
crate-type = ["cdylib"]

examples/http_config.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::str;
16+
use proxy_wasm::traits::*;
17+
use proxy_wasm::types::*;
18+
19+
#[no_mangle]
20+
pub fn _start() {
21+
proxy_wasm::set_log_level(LogLevel::Trace);
22+
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> {
23+
Box::new(HttpConfigHeaderRootContext{
24+
header_content: "".to_string(),
25+
})
26+
});
27+
}
28+
29+
struct HttpConfigHeader{
30+
header_content: String
31+
}
32+
33+
impl Context for HttpConfigHeader {}
34+
35+
impl HttpContext for HttpConfigHeader {
36+
37+
fn on_http_response_headers(&mut self, _num_headers: usize) -> Action {
38+
self.add_http_response_header("custom-header", self.header_content.as_str());
39+
40+
Action::Continue
41+
}
42+
}
43+
44+
struct HttpConfigHeaderRootContext {
45+
header_content: String
46+
}
47+
48+
impl Context for HttpConfigHeaderRootContext {}
49+
50+
impl RootContext for HttpConfigHeaderRootContext {
51+
52+
fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
53+
true
54+
}
55+
56+
fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
57+
if let Some(config_bytes) = self.get_configuration() {
58+
self.header_content = str::from_utf8(config_bytes.as_ref()).unwrap().to_owned()
59+
}
60+
true
61+
}
62+
63+
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
64+
Box::new(HttpConfigHeader{
65+
header_content: self.header_content.clone(),
66+
})
67+
}
68+
69+
fn get_type(&self) -> ContextType {
70+
ContextType::HttpContext
71+
}
72+
73+
}

0 commit comments

Comments
 (0)