Skip to content

Commit

Permalink
update ABI to match release/v1.15 branch of envoyproxy/envoy-wasm (
Browse files Browse the repository at this point in the history
  • Loading branch information
yskopets authored Aug 28, 2020
1 parent a77ae56 commit 4b98bad
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 82 deletions.
4 changes: 2 additions & 2 deletions examples/http_auth_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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, _: bool) -> Action {
self.dispatch_http_call(
"httpbin",
vec![
Expand All @@ -44,7 +44,7 @@ impl HttpContext for HttpAuthRandom {
Action::Pause
}

fn on_http_response_headers(&mut self, _: usize) -> Action {
fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action {
self.set_http_response_header("Powered-By", Some("proxy-wasm"));
Action::Continue
}
Expand Down
4 changes: 2 additions & 2 deletions examples/http_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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, _: bool) -> Action {
for (name, value) in &self.get_http_request_headers() {
trace!("#{} -> {}: {}", self.context_id, name, value);
}
Expand All @@ -51,7 +51,7 @@ impl HttpContext for HttpHeaders {
}
}

fn on_http_response_headers(&mut self, _: usize) -> Action {
fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action {
for (name, value) in &self.get_http_response_headers() {
trace!("#{} <- {}: {}", self.context_id, name, value);
}
Expand Down
38 changes: 30 additions & 8 deletions src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,15 @@ 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,
end_of_stream: bool,
) -> Action {
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)
http_stream.on_http_request_headers(num_headers, end_of_stream)
} else {
panic!("invalid context_id")
}
Expand Down Expand Up @@ -343,10 +348,15 @@ 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,
end_of_stream: bool,
) -> Action {
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)
http_stream.on_http_response_headers(num_headers, end_of_stream)
} else {
panic!("invalid context_id")
}
Expand Down Expand Up @@ -477,8 +487,14 @@ 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 {
DISPATCHER.with(|dispatcher| dispatcher.on_http_request_headers(context_id, num_headers))
pub extern "C" fn proxy_on_request_headers(
context_id: u32,
num_headers: usize,
end_of_stream: bool,
) -> Action {
DISPATCHER.with(|dispatcher| {
dispatcher.on_http_request_headers(context_id, num_headers, end_of_stream)
})
}

#[no_mangle]
Expand All @@ -497,8 +513,14 @@ pub extern "C" fn proxy_on_request_trailers(context_id: u32, num_trailers: usize
}

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

#[no_mangle]
Expand Down
67 changes: 12 additions & 55 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ mod abi {
pub const PROXY_LOG: &str = "proxy_log";
pub const PROXY_GET_CURRENT_TIME_NANOSECONDS: &str = "proxy_get_current_time_nanoseconds";
pub const PROXY_SET_TICK_PERIOD_MILLISECONDS: &str = "proxy_set_tick_period_milliseconds";
pub const PROXY_GET_CONFIGURATION: &str = "proxy_get_configuration";
pub const PROXY_GET_BUFFER_BYTES: &str = "proxy_get_buffer_bytes";
pub const PROXY_SET_BUFFER_BYTES: &str = "proxy_set_buffer_bytes";
pub const PROXY_GET_HEADER_MAP_PAIRS: &str = "proxy_get_header_map_pairs";
Expand All @@ -49,10 +48,9 @@ mod abi {
pub const PROXY_RESOLVE_SHARED_QUEUE: &str = "proxy_resolve_shared_queue";
pub const PROXY_DEQUEUE_SHARED_QUEUE: &str = "proxy_dequeue_shared_queue";
pub const PROXY_ENQUEUE_SHARED_QUEUE: &str = "proxy_enqueue_shared_queue";
pub const PROXY_CONTINUE_REQUEST: &str = "proxy_continue_request";
pub const PROXY_CONTINUE_RESPONSE: &str = "proxy_continue_response";
pub const PROXY_CONTINUE_STREAM: &str = "proxy_continue_stream";
pub const PROXY_CLOSE_STREAM: &str = "proxy_close_stream";
pub const PROXY_SEND_LOCAL_RESPONSE: &str = "proxy_send_local_response";
pub const PROXY_CLEAR_ROUTE_CACHE: &str = "proxy_clear_route_cache";
pub const PROXY_HTTP_CALL: &str = "proxy_http_call";
pub const PROXY_SET_EFFECTIVE_CONTEXT: &str = "proxy_set_effective_context";
pub const PROXY_DONE: &str = "proxy_done";
Expand Down Expand Up @@ -105,33 +103,6 @@ pub fn set_tick_period(period: Duration) -> Result<()> {
}
}

extern "C" {
fn proxy_get_configuration(
return_buffer_data: *mut *mut u8,
return_buffer_size: *mut usize,
) -> Status;
}

/// Returns configuration, e.g. VM configuration, extension configuration, etc.
pub fn get_configuration() -> Result<Option<ByteString>> {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
unsafe {
match proxy_get_configuration(&mut return_data, &mut return_size) {
Status::Ok => {
if !return_data.is_null() {
Ok(Vec::from_raw_parts(return_data, return_size, return_size))
.map(ByteString::from)
.map(Option::from)
} else {
Ok(None)
}
}
status => Err(HostCallError::new(abi::PROXY_GET_CONFIGURATION, status).into()),
}
}
}

extern "C" {
fn proxy_get_buffer_bytes(
buffer_type: BufferType,
Expand Down Expand Up @@ -752,29 +723,29 @@ where
}

extern "C" {
fn proxy_continue_request() -> Status;
fn proxy_continue_stream(stream: StreamType) -> Status;
}

/// Resume processing of paused HTTP request.
pub fn resume_http_request() -> Result<()> {
/// Resumes processing of a given stream, i.e. HTTP request or HTTP response.
pub fn continue_stream(stream_type: StreamType) -> Result<()> {
unsafe {
match proxy_continue_request() {
match proxy_continue_stream(stream_type) {
Status::Ok => Ok(()),
status => Err(HostCallError::new(abi::PROXY_CONTINUE_REQUEST, status).into()),
status => Err(HostCallError::new(abi::PROXY_CONTINUE_STREAM, status).into()),
}
}
}

extern "C" {
fn proxy_continue_response() -> Status;
fn proxy_close_stream(stream: StreamType) -> Status;
}

/// Resume processing of paused HTTP response.
pub fn resume_http_response() -> Result<()> {
/// Terminates processing of a given stream, i.e. HTTP request or HTTP response.
pub fn close_stream(stream_type: StreamType) -> Result<()> {
unsafe {
match proxy_continue_response() {
match proxy_close_stream(stream_type) {
Status::Ok => Ok(()),
status => Err(HostCallError::new(abi::PROXY_CONTINUE_RESPONSE, status).into()),
status => Err(HostCallError::new(abi::PROXY_CLOSE_STREAM, status).into()),
}
}
}
Expand Down Expand Up @@ -840,20 +811,6 @@ where
}
}

extern "C" {
fn proxy_clear_route_cache() -> Status;
}

/// Clears HTTP route cache.
pub fn clear_http_route_cache() -> Result<()> {
unsafe {
match proxy_clear_route_cache() {
Status::Ok => Ok(()),
status => Err(HostCallError::new(abi::PROXY_CLEAR_ROUTE_CACHE, status).into()),
}
}
}

extern "C" {
fn proxy_http_call(
upstream_data: *const u8,
Expand Down
16 changes: 4 additions & 12 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ pub trait RootContext: Context {
true
}

fn get_configuration(&self) -> Option<ByteString> {
hostcalls::get_configuration().unwrap()
}

fn set_tick_period(&self, period: Duration) {
hostcalls::set_tick_period(period).unwrap()
}
Expand Down Expand Up @@ -161,7 +157,7 @@ pub trait StreamContext: Context {
}

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

Expand Down Expand Up @@ -218,10 +214,10 @@ pub trait HttpContext: Context {
}

fn resume_http_request(&self) {
hostcalls::resume_http_request().unwrap()
hostcalls::continue_stream(StreamType::Request).unwrap()
}

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

Expand Down Expand Up @@ -278,7 +274,7 @@ pub trait HttpContext: Context {
}

fn resume_http_response(&self) {
hostcalls::resume_http_response().unwrap()
hostcalls::continue_stream(StreamType::Response).unwrap()
}

fn send_http_response(
Expand All @@ -290,9 +286,5 @@ pub trait HttpContext: Context {
hostcalls::send_http_response(status_code, &headers, body).unwrap()
}

fn clear_http_route_cache(&self) {
hostcalls::clear_http_route_cache().unwrap()
}

fn on_log(&mut self) {}
}
19 changes: 16 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ pub enum BufferType {
HttpResponseBody = 1,
DownstreamData = 2,
UpstreamData = 3,
HttpCallResponseBody = 4,
HttpCallResponseBody = 4, // Immutable
GrpcReceiveBuffer = 5, // Immutable
VmConfiguration = 6, // Immutable
PluginConfiguration = 7, // Immutable
CallData = 8, // Immutable
}

#[repr(u32)]
Expand All @@ -66,8 +70,10 @@ pub enum MapType {
HttpRequestTrailers = 1,
HttpResponseHeaders = 2,
HttpResponseTrailers = 3,
HttpCallResponseHeaders = 6,
HttpCallResponseTrailers = 7,
GrpcReceiveInitialMetadata = 4, // Immutable
GrpcReceiveTrailingMetadata = 5, // Immutable
HttpCallResponseHeaders = 6, // Immutable
HttpCallResponseTrailers = 7, // Immutable
}

#[repr(u32)]
Expand All @@ -77,3 +83,10 @@ pub enum PeerType {
Local = 1,
Remote = 2,
}

#[repr(u32)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum StreamType {
Request = 0,
Response = 1,
}

0 comments on commit 4b98bad

Please sign in to comment.