Skip to content

Commit

Permalink
types: replace Action to mimic C++ SDK and add missing variants
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
unleashed committed Nov 5, 2020
1 parent 5b0bbe5 commit b19b85a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 52 deletions.
8 changes: 4 additions & 4 deletions examples/http_auth_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn _start() {
struct HttpAuthRandom;

impl HttpContext for HttpAuthRandom {
fn on_http_request_headers(&mut self, _: usize) -> Action {
fn on_http_request_headers(&mut self, _: usize) -> FilterHeadersStatus {
self.dispatch_http_call(
"httpbin",
vec![
Expand All @@ -39,12 +39,12 @@ impl HttpContext for HttpAuthRandom {
Duration::from_secs(5),
)
.unwrap();
Action::Pause
FilterHeadersStatus::StopIteration
}

fn on_http_response_headers(&mut self, _: usize) -> Action {
fn on_http_response_headers(&mut self, _: usize) -> FilterHeadersStatus {
self.set_http_response_header("Powered-By", Some("proxy-wasm"));
Action::Continue
FilterHeadersStatus::Continue
}
}

Expand Down
10 changes: 5 additions & 5 deletions examples/http_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ struct HttpBody;
impl Context for HttpBody {}

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

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

// Replace the message body if it contains the text "secret".
Expand All @@ -51,6 +51,6 @@ impl HttpContext for HttpBody {
self.set_http_response_body(0, body_size, &new_body.into_bytes());
}
}
Action::Continue
FilterDataStatus::Continue
}
}
10 changes: 5 additions & 5 deletions examples/http_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct HttpHeaders {
impl Context for HttpHeaders {}

impl HttpContext for HttpHeaders {
fn on_http_request_headers(&mut self, _: usize) -> Action {
fn on_http_request_headers(&mut self, _: usize) -> FilterHeadersStatus {
for (name, value) in &self.get_http_request_headers() {
trace!("#{} -> {}: {}", self.context_id, name, value);
}
Expand All @@ -43,17 +43,17 @@ impl HttpContext for HttpHeaders {
vec![("Hello", "World"), ("Powered-By", "proxy-wasm")],
Some(b"Hello, World!\n"),
);
Action::Pause
FilterHeadersStatus::StopIteration
}
_ => Action::Continue,
_ => FilterHeadersStatus::Continue,
}
}

fn on_http_response_headers(&mut self, _: usize) -> Action {
fn on_http_response_headers(&mut self, _: usize) -> FilterHeadersStatus {
for (name, value) in &self.get_http_response_headers() {
trace!("#{} <- {}: {}", self.context_id, name, value);
}
Action::Continue
FilterHeadersStatus::Continue
}

fn on_log(&mut self) {
Expand Down
36 changes: 18 additions & 18 deletions src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl Dispatcher {
}
}

fn on_new_connection(&self, context_id: u32) -> Action {
fn on_new_connection(&self, context_id: u32) -> FilterStatus {
if let Some(stream) = self.streams.borrow_mut().get_mut(&context_id) {
self.active_id.set(context_id);
stream.on_new_connection()
Expand All @@ -271,7 +271,7 @@ impl Dispatcher {
}
}

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

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

fn on_http_request_headers(&self, context_id: u32, num_headers: usize) -> Action {
fn on_http_request_headers(&self, context_id: u32, num_headers: usize) -> FilterHeadersStatus {
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
self.active_id.set(context_id);
http_stream.on_http_request_headers(num_headers)
Expand All @@ -321,7 +321,7 @@ impl Dispatcher {
context_id: u32,
body_size: usize,
end_of_stream: bool,
) -> Action {
) -> FilterDataStatus {
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
self.active_id.set(context_id);
http_stream.on_http_request_body(body_size, end_of_stream)
Expand All @@ -330,7 +330,7 @@ impl Dispatcher {
}
}

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

fn on_http_response_headers(&self, context_id: u32, num_headers: usize) -> Action {
fn on_http_response_headers(&self, context_id: u32, num_headers: usize) -> FilterHeadersStatus {
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
self.active_id.set(context_id);
http_stream.on_http_response_headers(num_headers)
Expand All @@ -353,7 +353,7 @@ impl Dispatcher {
context_id: u32,
body_size: usize,
end_of_stream: bool,
) -> Action {
) -> FilterDataStatus {
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
self.active_id.set(context_id);
http_stream.on_http_response_body(body_size, end_of_stream)
Expand All @@ -362,7 +362,7 @@ impl Dispatcher {
}
}

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

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

Expand All @@ -448,7 +448,7 @@ pub extern "C" fn proxy_on_downstream_data(
context_id: u32,
data_size: usize,
end_of_stream: bool,
) -> Action {
) -> FilterStatus {
DISPATCHER
.with(|dispatcher| dispatcher.on_downstream_data(context_id, data_size, end_of_stream))
}
Expand All @@ -463,7 +463,7 @@ pub extern "C" fn proxy_on_upstream_data(
context_id: u32,
data_size: usize,
end_of_stream: bool,
) -> Action {
) -> FilterStatus {
DISPATCHER.with(|dispatcher| dispatcher.on_upstream_data(context_id, data_size, end_of_stream))
}

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

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

Expand All @@ -482,18 +482,18 @@ pub extern "C" fn proxy_on_request_body(
context_id: u32,
body_size: usize,
end_of_stream: bool,
) -> Action {
) -> FilterDataStatus {
DISPATCHER
.with(|dispatcher| dispatcher.on_http_request_body(context_id, body_size, end_of_stream))
}

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

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

Expand All @@ -502,13 +502,13 @@ pub extern "C" fn proxy_on_response_body(
context_id: u32,
body_size: usize,
end_of_stream: bool,
) -> Action {
) -> FilterDataStatus {
DISPATCHER
.with(|dispatcher| dispatcher.on_http_response_body(context_id, body_size, end_of_stream))
}

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

Expand Down
36 changes: 18 additions & 18 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ pub trait RootContext: Context {
}

pub trait StreamContext: Context {
fn on_new_connection(&mut self) -> Action {
Action::Continue
fn on_new_connection(&mut self) -> FilterStatus {
FilterStatus::Continue
}

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

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

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

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

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

pub trait HttpContext: Context {
fn on_http_request_headers(&mut self, _num_headers: usize) -> Action {
Action::Continue
fn on_http_request_headers(&mut self, _num_headers: usize) -> FilterHeadersStatus {
FilterHeadersStatus::Continue
}

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

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

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

fn on_http_request_trailers(&mut self, _num_trailers: usize) -> Action {
Action::Continue
fn on_http_request_trailers(&mut self, _num_trailers: usize) -> FilterTrailersStatus {
FilterTrailersStatus::Continue
}

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

fn on_http_response_headers(&mut self, _num_headers: usize) -> Action {
Action::Continue
fn on_http_response_headers(&mut self, _num_headers: usize) -> FilterHeadersStatus {
FilterHeadersStatus::Continue
}

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

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

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

fn on_http_response_trailers(&mut self, _num_trailers: usize) -> Action {
Action::Continue
fn on_http_response_trailers(&mut self, _num_trailers: usize) -> FilterTrailersStatus {
FilterTrailersStatus::Continue
}

fn get_http_response_trailers(&self) -> Vec<(String, String)> {
Expand Down
Loading

0 comments on commit b19b85a

Please sign in to comment.