Skip to content

Commit 1cad709

Browse files
committed
Addressing review comments
- Remove root_context_id from RootContext's create_http_stream and create_http_context calls - Move EmptyHttpContext after HttpContext declaration - Reorder trait impl's in examples to be consistent Signed-off-by: Daniel Grimm <dgrimm@redhat.com>
1 parent 94ecd03 commit 1cad709

File tree

5 files changed

+46
-50
lines changed

5 files changed

+46
-50
lines changed

examples/http_auth_random.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,35 @@ pub fn _start() {
2626
struct HttpAuthRandom;
2727
struct HttpAuthRandomRoot;
2828

29+
impl Context for HttpAuthRandomRoot {}
30+
2931
impl RootContext for HttpAuthRandomRoot {
3032
fn get_type(&self) -> ContextType {
3133
ContextType::HttpContext
3234
}
3335

34-
fn create_http_context(&self, _root_context_id: u32, _context_id: u32) -> Box<dyn HttpContext> {
36+
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
3537
Box::new(HttpAuthRandom)
3638
}
3739
}
3840

39-
impl Context for HttpAuthRandomRoot {}
41+
impl Context for HttpAuthRandom {
42+
fn on_http_call_response(&mut self, _: u32, _: usize, body_size: usize, _: usize) {
43+
if let Some(body) = self.get_http_call_response_body(0, body_size) {
44+
if !body.is_empty() && body[0] % 2 == 0 {
45+
trace!("Access granted.");
46+
self.resume_http_request();
47+
return;
48+
}
49+
}
50+
trace!("Access forbidden.");
51+
self.send_http_response(
52+
403,
53+
vec![("Powered-By", "proxy-wasm")],
54+
Some(b"Access forbidden.\n"),
55+
);
56+
}
57+
}
4058

4159
impl HttpContext for HttpAuthRandom {
4260
fn on_http_request_headers(&mut self, _: usize) -> Action {
@@ -60,21 +78,3 @@ impl HttpContext for HttpAuthRandom {
6078
Action::Continue
6179
}
6280
}
63-
64-
impl Context for HttpAuthRandom {
65-
fn on_http_call_response(&mut self, _: u32, _: usize, body_size: usize, _: usize) {
66-
if let Some(body) = self.get_http_call_response_body(0, body_size) {
67-
if !body.is_empty() && body[0] % 2 == 0 {
68-
trace!("Access granted.");
69-
self.resume_http_request();
70-
return;
71-
}
72-
}
73-
trace!("Access forbidden.");
74-
self.send_http_response(
75-
403,
76-
vec![("Powered-By", "proxy-wasm")],
77-
Some(b"Access forbidden.\n"),
78-
);
79-
}
80-
}

examples/http_body.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ pub fn _start() {
2424
struct HttpBody;
2525
struct HttpBodyRoot;
2626

27+
impl Context for HttpBodyRoot {}
28+
2729
impl RootContext for HttpBodyRoot {
2830
fn get_type(&self) -> ContextType {
2931
ContextType::HttpContext
3032
}
3133

32-
fn create_http_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn HttpContext> {
34+
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
3335
Box::new(HttpBody)
3436
}
3537
}
3638

37-
impl Context for HttpBodyRoot {}
38-
3939
impl Context for HttpBody {}
4040

4141
impl HttpContext for HttpBody {

examples/http_headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl RootContext for HttpHeadersRoot {
3131
ContextType::HttpContext
3232
}
3333

34-
fn create_http_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn HttpContext> {
34+
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
3535
Box::new(HttpHeaders {
3636
context_id: _context_id,
3737
})

src/dispatcher.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ impl Dispatcher {
6060
self.new_root.set(Some(callback));
6161
}
6262

63+
fn register_callout(&self, token_id: u32) {
64+
if self
65+
.callouts
66+
.borrow_mut()
67+
.insert(token_id, self.active_id.get())
68+
.is_some()
69+
{
70+
panic!("duplicate token_id")
71+
}
72+
}
73+
6374
fn create_root_context(&self, context_id: u32) {
6475
let new_context = match self.new_root.get() {
6576
Some(f) => f(context_id),
@@ -77,7 +88,7 @@ impl Dispatcher {
7788

7889
fn create_stream_context(&self, context_id: u32, root_context_id: u32) {
7990
let new_context = match self.roots.borrow().get(&root_context_id) {
80-
Some(root_context) => root_context.create_stream_context(context_id, root_context_id),
91+
Some(root_context) => root_context.create_stream_context(context_id),
8192
None => panic!("invalid root_context_id"),
8293
};
8394
if self
@@ -92,7 +103,7 @@ impl Dispatcher {
92103

93104
fn create_http_context(&self, context_id: u32, root_context_id: u32) {
94105
let new_context = match self.roots.borrow().get(&root_context_id) {
95-
Some(root_context) => root_context.create_http_context(context_id, root_context_id),
106+
Some(root_context) => root_context.create_http_context(context_id),
96107
None => panic!("invalid root_context_id"),
97108
};
98109
if self
@@ -105,17 +116,6 @@ impl Dispatcher {
105116
}
106117
}
107118

108-
fn register_callout(&self, token_id: u32) {
109-
if self
110-
.callouts
111-
.borrow_mut()
112-
.insert(token_id, self.active_id.get())
113-
.is_some()
114-
{
115-
panic!("duplicate token_id")
116-
}
117-
}
118-
119119
fn on_create_context(&self, context_id: u32, root_context_id: u32) {
120120
if root_context_id == 0 {
121121
self.create_root_context(context_id);
@@ -125,10 +125,10 @@ impl Dispatcher {
125125
ContextType::StreamContext => {
126126
self.create_stream_context(context_id, root_context_id)
127127
}
128-
ContextType::RootContext => panic!("missing constructors"),
128+
ContextType::RootContext => panic!("missing ContextType on root_context"),
129129
}
130130
} else {
131-
panic!("missing constructors")
131+
panic!("invalid root_context_id");
132132
}
133133
}
134134

src/traits.rs

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

123123
fn on_log(&mut self) {}
124124

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

129-
fn create_stream_context(
130-
&self,
131-
_context_id: u32,
132-
_root_context_id: u32,
133-
) -> Box<dyn StreamContext> {
129+
fn create_stream_context(&self, _context_id: u32) -> Box<dyn StreamContext> {
134130
Box::new(EmptyStreamContext)
135131
}
136132

@@ -175,11 +171,6 @@ pub trait StreamContext: Context {
175171
fn on_log(&mut self) {}
176172
}
177173

178-
struct EmptyHttpContext;
179-
180-
impl HttpContext for EmptyHttpContext {}
181-
impl Context for EmptyHttpContext {}
182-
183174
struct EmptyStreamContext;
184175

185176
impl StreamContext for EmptyStreamContext {}
@@ -329,3 +320,8 @@ pub trait HttpContext: Context {
329320

330321
fn on_log(&mut self) {}
331322
}
323+
324+
struct EmptyHttpContext;
325+
326+
impl HttpContext for EmptyHttpContext {}
327+
impl Context for EmptyHttpContext {}

0 commit comments

Comments
 (0)