Skip to content

Commit 4fc15fc

Browse files
committed
Add backwards compatibility, use Option<> where appropriate
1 parent 9e2e8e1 commit 4fc15fc

File tree

6 files changed

+59
-46
lines changed

6 files changed

+59
-46
lines changed

examples/http_auth_random.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,7 @@ use std::time::Duration;
2020
#[no_mangle]
2121
pub fn _start() {
2222
proxy_wasm::set_log_level(LogLevel::Trace);
23-
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpAuthRandomRoot) });
24-
}
25-
26-
struct HttpAuthRandomRoot;
27-
28-
impl Context for HttpAuthRandomRoot {}
29-
30-
impl RootContext for HttpAuthRandomRoot {
31-
fn get_type(&self) -> ContextType {
32-
ContextType::HttpContext
33-
}
34-
35-
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
36-
Box::new(HttpAuthRandom)
37-
}
23+
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpAuthRandom) });
3824
}
3925

4026
struct HttpAuthRandom;

examples/http_config.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ impl Context for HttpConfigHeaderRootContext {}
4949

5050
impl RootContext for HttpConfigHeaderRootContext {
5151

52-
fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
53-
true
54-
}
55-
5652
fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
5753
if let Some(config_bytes) = self.get_configuration() {
5854
self.header_content = str::from_utf8(config_bytes.as_ref()).unwrap().to_owned()

src/dispatcher.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ pub(crate) fn set_root_context(callback: NewRootContext) {
2626
DISPATCHER.with(|dispatcher| dispatcher.set_root_context(callback));
2727
}
2828

29+
pub(crate) fn set_stream_context(callback: NewStreamContext) {
30+
DISPATCHER.with(|dispatcher| dispatcher.set_stream_context(callback));
31+
}
32+
33+
pub(crate) fn set_http_context(callback: NewHttpContext) {
34+
DISPATCHER.with(|dispatcher| dispatcher.set_http_context(callback));
35+
}
36+
2937
pub(crate) fn register_callout(token_id: u32) {
3038
DISPATCHER.with(|dispatcher| dispatcher.register_callout(token_id));
3139
}
@@ -38,7 +46,9 @@ impl RootContext for NoopRoot {}
3846
struct Dispatcher {
3947
new_root: Cell<Option<NewRootContext>>,
4048
roots: RefCell<HashMap<u32, Box<dyn RootContext>>>,
49+
new_stream: Cell<Option<NewStreamContext>>,
4150
streams: RefCell<HashMap<u32, Box<dyn StreamContext>>>,
51+
new_http_stream: Cell<Option<NewHttpContext>>,
4252
http_streams: RefCell<HashMap<u32, Box<dyn HttpContext>>>,
4353
active_id: Cell<u32>,
4454
callouts: RefCell<HashMap<u32, u32>>,
@@ -49,7 +59,9 @@ impl Dispatcher {
4959
Dispatcher {
5060
new_root: Cell::new(None),
5161
roots: RefCell::new(HashMap::new()),
62+
new_stream: Cell::new(None),
5263
streams: RefCell::new(HashMap::new()),
64+
new_http_stream: Cell::new(None),
5365
http_streams: RefCell::new(HashMap::new()),
5466
active_id: Cell::new(0),
5567
callouts: RefCell::new(HashMap::new()),
@@ -60,6 +72,14 @@ impl Dispatcher {
6072
self.new_root.set(Some(callback));
6173
}
6274

75+
fn set_stream_context(&self, callback: NewStreamContext) {
76+
self.new_stream.set(Some(callback));
77+
}
78+
79+
fn set_http_context(&self, callback: NewHttpContext) {
80+
self.new_http_stream.set(Some(callback));
81+
}
82+
6383
fn register_callout(&self, token_id: u32) {
6484
if self
6585
.callouts
@@ -88,7 +108,13 @@ impl Dispatcher {
88108

89109
fn create_stream_context(&self, context_id: u32, root_context_id: u32) {
90110
let new_context = match self.roots.borrow().get(&root_context_id) {
91-
Some(root_context) => root_context.create_stream_context(context_id),
111+
Some(root_context) => match self.new_stream.get() {
112+
Some(f) => f(context_id, root_context_id),
113+
None => match root_context.create_stream_context(context_id) {
114+
Some(stream_context) => stream_context,
115+
None => panic!("create_stream_context returned None"),
116+
},
117+
},
92118
None => panic!("invalid root_context_id"),
93119
};
94120
if self
@@ -103,7 +129,13 @@ impl Dispatcher {
103129

104130
fn create_http_context(&self, context_id: u32, root_context_id: u32) {
105131
let new_context = match self.roots.borrow().get(&root_context_id) {
106-
Some(root_context) => root_context.create_http_context(context_id),
132+
Some(root_context) => match self.new_http_stream.get() {
133+
Some(f) => f(context_id, root_context_id),
134+
None => match root_context.create_http_context(context_id) {
135+
Some(stream_context) => stream_context,
136+
None => panic!("create_http_context returned None"),
137+
},
138+
},
107139
None => panic!("invalid root_context_id"),
108140
};
109141
if self
@@ -119,16 +151,18 @@ impl Dispatcher {
119151
fn on_create_context(&self, context_id: u32, root_context_id: u32) {
120152
if root_context_id == 0 {
121153
self.create_root_context(context_id);
154+
} else if self.new_http_stream.get().is_some() {
155+
self.create_http_context(context_id, root_context_id);
156+
} else if self.new_stream.get().is_some() {
157+
self.create_stream_context(context_id, root_context_id);
122158
} else if let Some(root_context) = self.roots.borrow().get(&root_context_id) {
123159
match root_context.get_type() {
124-
ContextType::HttpContext => self.create_http_context(context_id, root_context_id),
125-
ContextType::StreamContext => {
126-
self.create_stream_context(context_id, root_context_id)
127-
}
128-
ContextType::RootContext => panic!("missing ContextType on root_context"),
160+
Some(ContextType::HttpContext) => self.create_http_context(context_id, root_context_id),
161+
Some(ContextType::StreamContext) => self.create_stream_context(context_id, root_context_id),
162+
None => panic!("missing ContextType on root_context"),
129163
}
130164
} else {
131-
panic!("invalid root_context_id");
165+
panic!("invalid root_context_id and missing constructors");
132166
}
133167
}
134168

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,13 @@ pub fn set_root_context(callback: types::NewRootContext) {
2828
dispatcher::set_root_context(callback);
2929
}
3030

31+
pub fn set_stream_context(callback: types::NewStreamContext) {
32+
dispatcher::set_stream_context(callback);
33+
}
34+
35+
pub fn set_http_context(callback: types::NewHttpContext) {
36+
dispatcher::set_http_context(callback);
37+
}
38+
3139
#[no_mangle]
3240
pub extern "C" fn proxy_abi_version_0_1_0() {}

src/traits.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,16 @@ pub trait RootContext: Context {
122122

123123
fn on_log(&mut self) {}
124124

125-
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
126-
Box::new(EmptyHttpContext)
125+
fn create_http_context(&self, _context_id: u32) -> Option<Box<dyn HttpContext>> {
126+
None
127127
}
128128

129-
fn create_stream_context(&self, _context_id: u32) -> Box<dyn StreamContext> {
130-
Box::new(EmptyStreamContext)
129+
fn create_stream_context(&self, _context_id: u32) -> Option<Box<dyn StreamContext>> {
130+
None
131131
}
132132

133-
fn get_type(&self) -> ContextType {
134-
ContextType::RootContext
133+
fn get_type(&self) -> Option<ContextType> {
134+
None
135135
}
136136
}
137137

@@ -171,11 +171,6 @@ pub trait StreamContext: Context {
171171
fn on_log(&mut self) {}
172172
}
173173

174-
struct EmptyStreamContext;
175-
176-
impl StreamContext for EmptyStreamContext {}
177-
impl Context for EmptyStreamContext {}
178-
179174
pub trait HttpContext: Context {
180175
fn on_http_request_headers(&mut self, _num_headers: usize) -> Action {
181176
Action::Continue
@@ -320,8 +315,3 @@ pub trait HttpContext: Context {
320315

321316
fn on_log(&mut self) {}
322317
}
323-
324-
struct EmptyHttpContext;
325-
326-
impl HttpContext for EmptyHttpContext {}
327-
impl Context for EmptyHttpContext {}

src/types.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ pub enum Status {
5050
#[repr(u32)]
5151
#[derive(Debug)]
5252
pub enum ContextType {
53-
RootContext = 0,
54-
HttpContext = 1,
55-
StreamContext = 2,
53+
HttpContext = 0,
54+
StreamContext = 1,
5655
}
5756

5857
#[repr(u32)]

0 commit comments

Comments
 (0)