Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change callbacks to return specific response types #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -229,7 +229,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 @@ -238,7 +238,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 @@ -256,7 +256,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 @@ -274,7 +274,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 @@ -288,7 +288,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 @@ -297,7 +297,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 @@ -306,7 +306,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 @@ -320,7 +320,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 @@ -329,7 +329,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 @@ -406,7 +406,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 @@ -415,7 +415,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 @@ -430,7 +430,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 @@ -440,7 +440,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 @@ -449,18 +449,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 @@ -469,13 +469,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
44 changes: 26 additions & 18 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,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 @@ -142,8 +142,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 @@ -160,8 +160,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 @@ -184,8 +184,12 @@ 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 @@ -196,8 +200,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 @@ -224,8 +228,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 @@ -248,8 +252,12 @@ 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 @@ -260,8 +268,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