Skip to content

Commit b19b85a

Browse files
committed
types: replace Action to mimic C++ SDK and add missing variants
This is based on PR proxy-wasm#39 upstream. Brings us a step closer to what the C++ SDK uses. Signed-off-by: Alejandro Martinez Ruiz <[email protected]>
1 parent 5b0bbe5 commit b19b85a

File tree

6 files changed

+104
-52
lines changed

6 files changed

+104
-52
lines changed

examples/http_auth_random.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn _start() {
2626
struct HttpAuthRandom;
2727

2828
impl HttpContext for HttpAuthRandom {
29-
fn on_http_request_headers(&mut self, _: usize) -> Action {
29+
fn on_http_request_headers(&mut self, _: usize) -> FilterHeadersStatus {
3030
self.dispatch_http_call(
3131
"httpbin",
3232
vec![
@@ -39,12 +39,12 @@ impl HttpContext for HttpAuthRandom {
3939
Duration::from_secs(5),
4040
)
4141
.unwrap();
42-
Action::Pause
42+
FilterHeadersStatus::StopIteration
4343
}
4444

45-
fn on_http_response_headers(&mut self, _: usize) -> Action {
45+
fn on_http_response_headers(&mut self, _: usize) -> FilterHeadersStatus {
4646
self.set_http_response_header("Powered-By", Some("proxy-wasm"));
47-
Action::Continue
47+
FilterHeadersStatus::Continue
4848
}
4949
}
5050

examples/http_body.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ struct HttpBody;
2626
impl Context for HttpBody {}
2727

2828
impl HttpContext for HttpBody {
29-
fn on_http_response_headers(&mut self, _: usize) -> Action {
29+
fn on_http_response_headers(&mut self, _: usize) -> FilterHeadersStatus {
3030
// If there is a Content-Length header and we change the length of
3131
// the body later, then clients will break. So remove it.
3232
// We must do this here, because once we exit this function we
3333
// can no longer modify the response headers.
3434
self.set_http_response_header("content-length", None);
35-
Action::Continue
35+
FilterHeadersStatus::Continue
3636
}
3737

38-
fn on_http_response_body(&mut self, body_size: usize, end_of_stream: bool) -> Action {
38+
fn on_http_response_body(&mut self, body_size: usize, end_of_stream: bool) -> FilterDataStatus {
3939
if !end_of_stream {
4040
// Wait -- we'll be called again when the complete body is buffered
4141
// at the host side.
42-
return Action::Pause;
42+
return FilterDataStatus::StopIterationAndBuffer;
4343
}
4444

4545
// Replace the message body if it contains the text "secret".
@@ -51,6 +51,6 @@ impl HttpContext for HttpBody {
5151
self.set_http_response_body(0, body_size, &new_body.into_bytes());
5252
}
5353
}
54-
Action::Continue
54+
FilterDataStatus::Continue
5555
}
5656
}

examples/http_headers.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct HttpHeaders {
3131
impl Context for HttpHeaders {}
3232

3333
impl HttpContext for HttpHeaders {
34-
fn on_http_request_headers(&mut self, _: usize) -> Action {
34+
fn on_http_request_headers(&mut self, _: usize) -> FilterHeadersStatus {
3535
for (name, value) in &self.get_http_request_headers() {
3636
trace!("#{} -> {}: {}", self.context_id, name, value);
3737
}
@@ -43,17 +43,17 @@ impl HttpContext for HttpHeaders {
4343
vec![("Hello", "World"), ("Powered-By", "proxy-wasm")],
4444
Some(b"Hello, World!\n"),
4545
);
46-
Action::Pause
46+
FilterHeadersStatus::StopIteration
4747
}
48-
_ => Action::Continue,
48+
_ => FilterHeadersStatus::Continue,
4949
}
5050
}
5151

52-
fn on_http_response_headers(&mut self, _: usize) -> Action {
52+
fn on_http_response_headers(&mut self, _: usize) -> FilterHeadersStatus {
5353
for (name, value) in &self.get_http_response_headers() {
5454
trace!("#{} <- {}: {}", self.context_id, name, value);
5555
}
56-
Action::Continue
56+
FilterHeadersStatus::Continue
5757
}
5858

5959
fn on_log(&mut self) {

src/dispatcher.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl Dispatcher {
262262
}
263263
}
264264

265-
fn on_new_connection(&self, context_id: u32) -> Action {
265+
fn on_new_connection(&self, context_id: u32) -> FilterStatus {
266266
if let Some(stream) = self.streams.borrow_mut().get_mut(&context_id) {
267267
self.active_id.set(context_id);
268268
stream.on_new_connection()
@@ -271,7 +271,7 @@ impl Dispatcher {
271271
}
272272
}
273273

274-
fn on_downstream_data(&self, context_id: u32, data_size: usize, end_of_stream: bool) -> Action {
274+
fn on_downstream_data(&self, context_id: u32, data_size: usize, end_of_stream: bool) -> FilterStatus {
275275
if let Some(stream) = self.streams.borrow_mut().get_mut(&context_id) {
276276
self.active_id.set(context_id);
277277
stream.on_downstream_data(data_size, end_of_stream)
@@ -289,7 +289,7 @@ impl Dispatcher {
289289
}
290290
}
291291

292-
fn on_upstream_data(&self, context_id: u32, data_size: usize, end_of_stream: bool) -> Action {
292+
fn on_upstream_data(&self, context_id: u32, data_size: usize, end_of_stream: bool) -> FilterStatus {
293293
if let Some(stream) = self.streams.borrow_mut().get_mut(&context_id) {
294294
self.active_id.set(context_id);
295295
stream.on_upstream_data(data_size, end_of_stream)
@@ -307,7 +307,7 @@ impl Dispatcher {
307307
}
308308
}
309309

310-
fn on_http_request_headers(&self, context_id: u32, num_headers: usize) -> Action {
310+
fn on_http_request_headers(&self, context_id: u32, num_headers: usize) -> FilterHeadersStatus {
311311
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
312312
self.active_id.set(context_id);
313313
http_stream.on_http_request_headers(num_headers)
@@ -321,7 +321,7 @@ impl Dispatcher {
321321
context_id: u32,
322322
body_size: usize,
323323
end_of_stream: bool,
324-
) -> Action {
324+
) -> FilterDataStatus {
325325
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
326326
self.active_id.set(context_id);
327327
http_stream.on_http_request_body(body_size, end_of_stream)
@@ -330,7 +330,7 @@ impl Dispatcher {
330330
}
331331
}
332332

333-
fn on_http_request_trailers(&self, context_id: u32, num_trailers: usize) -> Action {
333+
fn on_http_request_trailers(&self, context_id: u32, num_trailers: usize) -> FilterTrailersStatus {
334334
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
335335
self.active_id.set(context_id);
336336
http_stream.on_http_request_trailers(num_trailers)
@@ -339,7 +339,7 @@ impl Dispatcher {
339339
}
340340
}
341341

342-
fn on_http_response_headers(&self, context_id: u32, num_headers: usize) -> Action {
342+
fn on_http_response_headers(&self, context_id: u32, num_headers: usize) -> FilterHeadersStatus {
343343
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
344344
self.active_id.set(context_id);
345345
http_stream.on_http_response_headers(num_headers)
@@ -353,7 +353,7 @@ impl Dispatcher {
353353
context_id: u32,
354354
body_size: usize,
355355
end_of_stream: bool,
356-
) -> Action {
356+
) -> FilterDataStatus {
357357
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
358358
self.active_id.set(context_id);
359359
http_stream.on_http_response_body(body_size, end_of_stream)
@@ -362,7 +362,7 @@ impl Dispatcher {
362362
}
363363
}
364364

365-
fn on_http_response_trailers(&self, context_id: u32, num_trailers: usize) -> Action {
365+
fn on_http_response_trailers(&self, context_id: u32, num_trailers: usize) -> FilterTrailersStatus {
366366
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
367367
self.active_id.set(context_id);
368368
http_stream.on_http_response_trailers(num_trailers)
@@ -439,7 +439,7 @@ pub extern "C" fn proxy_on_queue_ready(context_id: u32, queue_id: u32) {
439439
}
440440

441441
#[no_mangle]
442-
pub extern "C" fn proxy_on_new_connection(context_id: u32) -> Action {
442+
pub extern "C" fn proxy_on_new_connection(context_id: u32) -> FilterStatus {
443443
DISPATCHER.with(|dispatcher| dispatcher.on_new_connection(context_id))
444444
}
445445

@@ -448,7 +448,7 @@ pub extern "C" fn proxy_on_downstream_data(
448448
context_id: u32,
449449
data_size: usize,
450450
end_of_stream: bool,
451-
) -> Action {
451+
) -> FilterStatus {
452452
DISPATCHER
453453
.with(|dispatcher| dispatcher.on_downstream_data(context_id, data_size, end_of_stream))
454454
}
@@ -463,7 +463,7 @@ pub extern "C" fn proxy_on_upstream_data(
463463
context_id: u32,
464464
data_size: usize,
465465
end_of_stream: bool,
466-
) -> Action {
466+
) -> FilterStatus {
467467
DISPATCHER.with(|dispatcher| dispatcher.on_upstream_data(context_id, data_size, end_of_stream))
468468
}
469469

@@ -473,7 +473,7 @@ pub extern "C" fn proxy_on_upstream_connection_close(context_id: u32, peer_type:
473473
}
474474

475475
#[no_mangle]
476-
pub extern "C" fn proxy_on_request_headers(context_id: u32, num_headers: usize) -> Action {
476+
pub extern "C" fn proxy_on_request_headers(context_id: u32, num_headers: usize) -> FilterHeadersStatus {
477477
DISPATCHER.with(|dispatcher| dispatcher.on_http_request_headers(context_id, num_headers))
478478
}
479479

@@ -482,18 +482,18 @@ pub extern "C" fn proxy_on_request_body(
482482
context_id: u32,
483483
body_size: usize,
484484
end_of_stream: bool,
485-
) -> Action {
485+
) -> FilterDataStatus {
486486
DISPATCHER
487487
.with(|dispatcher| dispatcher.on_http_request_body(context_id, body_size, end_of_stream))
488488
}
489489

490490
#[no_mangle]
491-
pub extern "C" fn proxy_on_request_trailers(context_id: u32, num_trailers: usize) -> Action {
491+
pub extern "C" fn proxy_on_request_trailers(context_id: u32, num_trailers: usize) -> FilterTrailersStatus {
492492
DISPATCHER.with(|dispatcher| dispatcher.on_http_request_trailers(context_id, num_trailers))
493493
}
494494

495495
#[no_mangle]
496-
pub extern "C" fn proxy_on_response_headers(context_id: u32, num_headers: usize) -> Action {
496+
pub extern "C" fn proxy_on_response_headers(context_id: u32, num_headers: usize) -> FilterHeadersStatus {
497497
DISPATCHER.with(|dispatcher| dispatcher.on_http_response_headers(context_id, num_headers))
498498
}
499499

@@ -502,13 +502,13 @@ pub extern "C" fn proxy_on_response_body(
502502
context_id: u32,
503503
body_size: usize,
504504
end_of_stream: bool,
505-
) -> Action {
505+
) -> FilterDataStatus {
506506
DISPATCHER
507507
.with(|dispatcher| dispatcher.on_http_response_body(context_id, body_size, end_of_stream))
508508
}
509509

510510
#[no_mangle]
511-
pub extern "C" fn proxy_on_response_trailers(context_id: u32, num_trailers: usize) -> Action {
511+
pub extern "C" fn proxy_on_response_trailers(context_id: u32, num_trailers: usize) -> FilterTrailersStatus {
512512
DISPATCHER.with(|dispatcher| dispatcher.on_http_response_trailers(context_id, num_trailers))
513513
}
514514

src/traits.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ pub trait RootContext: Context {
136136
}
137137

138138
pub trait StreamContext: Context {
139-
fn on_new_connection(&mut self) -> Action {
140-
Action::Continue
139+
fn on_new_connection(&mut self) -> FilterStatus {
140+
FilterStatus::Continue
141141
}
142142

143-
fn on_downstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
144-
Action::Continue
143+
fn on_downstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> FilterStatus {
144+
FilterStatus::Continue
145145
}
146146

147147
fn get_downstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
@@ -154,8 +154,8 @@ pub trait StreamContext: Context {
154154

155155
fn on_downstream_close(&mut self, _peer_type: PeerType) {}
156156

157-
fn on_upstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
158-
Action::Continue
157+
fn on_upstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> FilterStatus {
158+
FilterStatus::Continue
159159
}
160160

161161
fn get_upstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
@@ -172,8 +172,8 @@ pub trait StreamContext: Context {
172172
}
173173

174174
pub trait HttpContext: Context {
175-
fn on_http_request_headers(&mut self, _num_headers: usize) -> Action {
176-
Action::Continue
175+
fn on_http_request_headers(&mut self, _num_headers: usize) -> FilterHeadersStatus {
176+
FilterHeadersStatus::Continue
177177
}
178178

179179
fn get_http_request_headers(&self) -> Vec<(String, String)> {
@@ -196,8 +196,8 @@ pub trait HttpContext: Context {
196196
hostcalls::add_map_value(MapType::HttpRequestHeaders, &name, value).unwrap()
197197
}
198198

199-
fn on_http_request_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
200-
Action::Continue
199+
fn on_http_request_body(&mut self, _body_size: usize, _end_of_stream: bool) -> FilterDataStatus {
200+
FilterDataStatus::Continue
201201
}
202202

203203
fn get_http_request_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
@@ -208,8 +208,8 @@ pub trait HttpContext: Context {
208208
hostcalls::set_buffer(BufferType::HttpRequestBody, start, size, value).unwrap()
209209
}
210210

211-
fn on_http_request_trailers(&mut self, _num_trailers: usize) -> Action {
212-
Action::Continue
211+
fn on_http_request_trailers(&mut self, _num_trailers: usize) -> FilterTrailersStatus {
212+
FilterTrailersStatus::Continue
213213
}
214214

215215
fn get_http_request_trailers(&self) -> Vec<(String, String)> {
@@ -236,8 +236,8 @@ pub trait HttpContext: Context {
236236
hostcalls::resume_http_request().unwrap()
237237
}
238238

239-
fn on_http_response_headers(&mut self, _num_headers: usize) -> Action {
240-
Action::Continue
239+
fn on_http_response_headers(&mut self, _num_headers: usize) -> FilterHeadersStatus {
240+
FilterHeadersStatus::Continue
241241
}
242242

243243
fn get_http_response_headers(&self) -> Vec<(String, String)> {
@@ -260,8 +260,8 @@ pub trait HttpContext: Context {
260260
hostcalls::add_map_value(MapType::HttpResponseHeaders, &name, value).unwrap()
261261
}
262262

263-
fn on_http_response_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
264-
Action::Continue
263+
fn on_http_response_body(&mut self, _body_size: usize, _end_of_stream: bool) -> FilterDataStatus {
264+
FilterDataStatus::Continue
265265
}
266266

267267
fn get_http_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
@@ -272,8 +272,8 @@ pub trait HttpContext: Context {
272272
hostcalls::set_buffer(BufferType::HttpResponseBody, start, size, value).unwrap()
273273
}
274274

275-
fn on_http_response_trailers(&mut self, _num_trailers: usize) -> Action {
276-
Action::Continue
275+
fn on_http_response_trailers(&mut self, _num_trailers: usize) -> FilterTrailersStatus {
276+
FilterTrailersStatus::Continue
277277
}
278278

279279
fn get_http_response_trailers(&self) -> Vec<(String, String)> {

0 commit comments

Comments
 (0)