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

Update ABI to Proxy-Wasm ABI v0.2.1. #64

Merged
merged 3 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Update ABI to Proxy-Wasm ABI v0.2.1.
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
  • Loading branch information
PiotrSikora committed Dec 7, 2020
commit feb1c88c0e6307e426d372e71ae663d9a26aaa05
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Changed

- Updated ABI to Proxy-Wasm ABI v0.2.1.

## [0.1.3] - 2020-12-04

### Fixed
Expand Down Expand Up @@ -46,6 +52,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Initial release.


[Unreleased]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/compare/v0.1.3...HEAD
[0.1.3]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/compare/v0.1.2...v0.1.3
[0.1.2]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/compare/v0.1.1...v0.1.2
[0.1.1]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/compare/v0.1.0...v0.1.1
Expand Down
4 changes: 2 additions & 2 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, _: bool) -> Action {
self.dispatch_http_call(
"httpbin",
vec![
Expand All @@ -42,7 +42,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
2 changes: 1 addition & 1 deletion examples/http_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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, _: bool) -> Action {
// 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
Expand Down
4 changes: 2 additions & 2 deletions examples/http_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct HttpConfigHeader {
impl Context for HttpConfigHeader {}

impl HttpContext for HttpConfigHeader {
fn on_http_response_headers(&mut self, _: usize) -> Action {
fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action {
self.add_http_response_header("custom-header", self.header_content.as_str());
Action::Continue
}
Expand All @@ -46,7 +46,7 @@ impl Context for HttpConfigHeaderRoot {}

impl RootContext for HttpConfigHeaderRoot {
fn on_configure(&mut self, _: usize) -> bool {
if let Some(config_bytes) = self.get_configuration() {
if let Some(config_bytes) = self.get_plugin_configuration() {
self.header_content = String::from_utf8(config_bytes).unwrap()
}
true
Expand Down
4 changes: 2 additions & 2 deletions examples/http_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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 @@ -61,7 +61,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 @@ -290,10 +290,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 @@ -322,10 +327,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 @@ -458,8 +468,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 @@ -478,8 +494,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
70 changes: 23 additions & 47 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,41 @@ pub fn log(level: LogLevel, message: &str) -> Result<(), Status> {
}

extern "C" {
fn proxy_get_current_time_nanoseconds(return_time: *mut u64) -> Status;
fn proxy_get_log_level(return_level: *mut LogLevel) -> Status;
}

pub fn get_current_time() -> Result<SystemTime, Status> {
let mut return_time: u64 = 0;
pub fn get_log_level() -> Result<LogLevel, Status> {
let mut return_level: LogLevel = LogLevel::Trace;
unsafe {
match proxy_get_current_time_nanoseconds(&mut return_time) {
Status::Ok => Ok(UNIX_EPOCH + Duration::from_nanos(return_time)),
match proxy_get_log_level(&mut return_level) {
Status::Ok => Ok(return_level),
status => panic!("unexpected status: {}", status as u32),
}
}
}

extern "C" {
fn proxy_set_tick_period_milliseconds(period: u32) -> Status;
fn proxy_get_current_time_nanoseconds(return_time: *mut u64) -> Status;
}

pub fn set_tick_period(period: Duration) -> Result<(), Status> {
pub fn get_current_time() -> Result<SystemTime, Status> {
let mut return_time: u64 = 0;
unsafe {
match proxy_set_tick_period_milliseconds(period.as_millis() as u32) {
Status::Ok => Ok(()),
match proxy_get_current_time_nanoseconds(&mut return_time) {
Status::Ok => Ok(UNIX_EPOCH + Duration::from_nanos(return_time)),
status => panic!("unexpected status: {}", status as u32),
}
}
}

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

pub fn get_configuration() -> Result<Option<Bytes>, Status> {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
pub fn set_tick_period(period: Duration) -> Result<(), Status> {
unsafe {
match proxy_get_configuration(&mut return_data, &mut return_size) {
Status::Ok => {
if !return_data.is_null() {
Ok(Some(Vec::from_raw_parts(
return_data,
return_size,
return_size,
)))
} else {
Ok(None)
}
}
match proxy_set_tick_period_milliseconds(period.as_millis() as u32) {
Status::Ok => Ok(()),
status => panic!("unexpected status: {}", status as u32),
}
}
Expand Down Expand Up @@ -230,6 +216,7 @@ pub fn get_map_value(map_type: MapType, key: &str) -> Result<Option<String>, Sta
Ok(None)
}
}
Status::NotFound => Ok(None),
status => panic!("unexpected status: {}", status as u32),
}
}
Expand Down Expand Up @@ -528,26 +515,28 @@ pub fn enqueue_shared_queue(queue_id: u32, value: Option<&[u8]>) -> Result<(), S
}

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

pub fn resume_http_request() -> Result<(), Status> {
pub fn resume_stream(stream_type: StreamType) -> Result<(), Status> {
unsafe {
match proxy_continue_request() {
match proxy_continue_stream(stream_type) {
Status::Ok => Ok(()),
Status::BadArgument => Err(Status::BadArgument),
status => panic!("unexpected status: {}", status as u32),
}
}
}

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

pub fn resume_http_response() -> Result<(), Status> {
pub fn close_stream(stream_type: StreamType) -> Result<(), Status> {
unsafe {
match proxy_continue_response() {
match proxy_close_stream(stream_type) {
Status::Ok => Ok(()),
Status::BadArgument => Err(Status::BadArgument),
status => panic!("unexpected status: {}", status as u32),
}
}
Expand Down Expand Up @@ -589,19 +578,6 @@ pub fn send_http_response(
}
}

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

pub fn clear_http_route_cache() -> Result<(), Status> {
unsafe {
match proxy_clear_route_cache() {
Status::Ok => Ok(()),
status => panic!("unexpected status: {}", status as u32),
}
}
}

extern "C" {
fn proxy_http_call(
upstream_data: *const u8,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ pub fn set_http_context(callback: types::NewHttpContext) {
}

#[no_mangle]
pub extern "C" fn proxy_abi_version_0_1_0() {}
pub extern "C" fn proxy_abi_version_0_2_1() {}
36 changes: 26 additions & 10 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ pub trait RootContext: Context {
true
}

fn get_vm_configuration(&self) -> Option<Bytes> {
hostcalls::get_buffer(BufferType::VmConfiguration, 0, usize::MAX).unwrap()
}

fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
true
}

fn get_configuration(&self) -> Option<Bytes> {
hostcalls::get_configuration().unwrap()
fn get_plugin_configuration(&self) -> Option<Bytes> {
hostcalls::get_buffer(BufferType::PluginConfiguration, 0, usize::MAX).unwrap()
}

fn set_tick_period(&self, period: Duration) {
Expand Down Expand Up @@ -152,6 +156,14 @@ pub trait StreamContext: Context {
hostcalls::set_buffer(BufferType::DownstreamData, start, size, value).unwrap()
}

fn resume_downstream(&self) {
hostcalls::resume_stream(StreamType::Downstream).unwrap()
}

fn close_downstream(&self) {
hostcalls::close_stream(StreamType::Downstream).unwrap()
}

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

fn on_upstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
Expand All @@ -166,13 +178,21 @@ pub trait StreamContext: Context {
hostcalls::set_buffer(BufferType::UpstreamData, start, size, value).unwrap()
}

fn resume_upstream(&self) {
hostcalls::resume_stream(StreamType::Upstream).unwrap()
}

fn close_upstream(&self) {
hostcalls::close_stream(StreamType::Upstream).unwrap()
}

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

fn on_log(&mut self) {}
}

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 @@ -233,10 +253,10 @@ pub trait HttpContext: Context {
}

fn resume_http_request(&self) {
hostcalls::resume_http_request().unwrap()
hostcalls::resume_stream(StreamType::HttpRequest).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 @@ -297,7 +317,7 @@ pub trait HttpContext: Context {
}

fn resume_http_response(&self) {
hostcalls::resume_http_response().unwrap()
hostcalls::resume_stream(StreamType::HttpResponse).unwrap()
}

fn send_http_response(
Expand All @@ -309,9 +329,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) {}
}
Loading