Skip to content

Commit abd0f54

Browse files
authored
Update ABI to Proxy-Wasm ABI v0.2.1. (#64)
Signed-off-by: Piotr Sikora <[email protected]>
1 parent cbfc7f2 commit abd0f54

File tree

10 files changed

+163
-70
lines changed

10 files changed

+163
-70
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

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

7+
## [Unreleased]
8+
9+
### Changed
10+
11+
- Updated ABI to Proxy-Wasm ABI v0.2.1.
12+
713
## [0.1.4] - 2021-07-01
814

915
### Added
@@ -53,6 +59,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
5359
- Initial release.
5460

5561

62+
[Unreleased]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/compare/v0.1.4...HEAD
5663
[0.1.4]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/compare/v0.1.3...v0.1.4
5764
[0.1.3]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/compare/v0.1.2...v0.1.3
5865
[0.1.2]: https://github.com/proxy-wasm/proxy-wasm-rust-sdk/compare/v0.1.1...v0.1.2

examples/http_auth_random.rs

Lines changed: 2 additions & 2 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, _: bool) -> Action {
3030
self.dispatch_http_call(
3131
"httpbin",
3232
vec![
@@ -42,7 +42,7 @@ impl HttpContext for HttpAuthRandom {
4242
Action::Pause
4343
}
4444

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

examples/http_body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct HttpBody;
4040
impl Context for HttpBody {}
4141

4242
impl HttpContext for HttpBody {
43-
fn on_http_response_headers(&mut self, _: usize) -> Action {
43+
fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action {
4444
// If there is a Content-Length header and we change the length of
4545
// the body later, then clients will break. So remove it.
4646
// We must do this here, because once we exit this function we

examples/http_config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct HttpConfigHeader {
3232
impl Context for HttpConfigHeader {}
3333

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

4747
impl RootContext for HttpConfigHeaderRoot {
4848
fn on_configure(&mut self, _: usize) -> bool {
49-
if let Some(config_bytes) = self.get_configuration() {
49+
if let Some(config_bytes) = self.get_plugin_configuration() {
5050
self.header_content = String::from_utf8(config_bytes).unwrap()
5151
}
5252
true

examples/http_headers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct HttpHeaders {
4343
impl Context for HttpHeaders {}
4444

4545
impl HttpContext for HttpHeaders {
46-
fn on_http_request_headers(&mut self, _: usize) -> Action {
46+
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
4747
for (name, value) in &self.get_http_request_headers() {
4848
trace!("#{} -> {}: {}", self.context_id, name, value);
4949
}
@@ -61,7 +61,7 @@ impl HttpContext for HttpHeaders {
6161
}
6262
}
6363

64-
fn on_http_response_headers(&mut self, _: usize) -> Action {
64+
fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action {
6565
for (name, value) in &self.get_http_response_headers() {
6666
trace!("#{} <- {}: {}", self.context_id, name, value);
6767
}

src/dispatcher.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,15 @@ impl Dispatcher {
324324
}
325325
}
326326

327-
fn on_http_request_headers(&self, context_id: u32, num_headers: usize) -> Action {
327+
fn on_http_request_headers(
328+
&self,
329+
context_id: u32,
330+
num_headers: usize,
331+
end_of_stream: bool,
332+
) -> Action {
328333
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
329334
self.active_id.set(context_id);
330-
http_stream.on_http_request_headers(num_headers)
335+
http_stream.on_http_request_headers(num_headers, end_of_stream)
331336
} else {
332337
panic!("invalid context_id")
333338
}
@@ -356,10 +361,15 @@ impl Dispatcher {
356361
}
357362
}
358363

359-
fn on_http_response_headers(&self, context_id: u32, num_headers: usize) -> Action {
364+
fn on_http_response_headers(
365+
&self,
366+
context_id: u32,
367+
num_headers: usize,
368+
end_of_stream: bool,
369+
) -> Action {
360370
if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
361371
self.active_id.set(context_id);
362-
http_stream.on_http_response_headers(num_headers)
372+
http_stream.on_http_response_headers(num_headers, end_of_stream)
363373
} else {
364374
panic!("invalid context_id")
365375
}
@@ -605,8 +615,14 @@ pub extern "C" fn proxy_on_upstream_connection_close(context_id: u32, peer_type:
605615
}
606616

607617
#[no_mangle]
608-
pub extern "C" fn proxy_on_request_headers(context_id: u32, num_headers: usize) -> Action {
609-
DISPATCHER.with(|dispatcher| dispatcher.on_http_request_headers(context_id, num_headers))
618+
pub extern "C" fn proxy_on_request_headers(
619+
context_id: u32,
620+
num_headers: usize,
621+
end_of_stream: bool,
622+
) -> Action {
623+
DISPATCHER.with(|dispatcher| {
624+
dispatcher.on_http_request_headers(context_id, num_headers, end_of_stream)
625+
})
610626
}
611627

612628
#[no_mangle]
@@ -625,8 +641,14 @@ pub extern "C" fn proxy_on_request_trailers(context_id: u32, num_trailers: usize
625641
}
626642

627643
#[no_mangle]
628-
pub extern "C" fn proxy_on_response_headers(context_id: u32, num_headers: usize) -> Action {
629-
DISPATCHER.with(|dispatcher| dispatcher.on_http_response_headers(context_id, num_headers))
644+
pub extern "C" fn proxy_on_response_headers(
645+
context_id: u32,
646+
num_headers: usize,
647+
end_of_stream: bool,
648+
) -> Action {
649+
DISPATCHER.with(|dispatcher| {
650+
dispatcher.on_http_response_headers(context_id, num_headers, end_of_stream)
651+
})
630652
}
631653

632654
#[no_mangle]

src/hostcalls.rs

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,41 @@ pub fn log(level: LogLevel, message: &str) -> Result<(), Status> {
3131
}
3232

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

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

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

51-
pub fn set_tick_period(period: Duration) -> Result<(), Status> {
51+
pub fn get_current_time() -> Result<SystemTime, Status> {
52+
let mut return_time: u64 = 0;
5253
unsafe {
53-
match proxy_set_tick_period_milliseconds(period.as_millis() as u32) {
54-
Status::Ok => Ok(()),
54+
match proxy_get_current_time_nanoseconds(&mut return_time) {
55+
Status::Ok => Ok(UNIX_EPOCH + Duration::from_nanos(return_time)),
5556
status => panic!("unexpected status: {}", status as u32),
5657
}
5758
}
5859
}
5960

6061
extern "C" {
61-
fn proxy_get_configuration(
62-
return_buffer_data: *mut *mut u8,
63-
return_buffer_size: *mut usize,
64-
) -> Status;
62+
fn proxy_set_tick_period_milliseconds(period: u32) -> Status;
6563
}
6664

67-
pub fn get_configuration() -> Result<Option<Bytes>, Status> {
68-
let mut return_data: *mut u8 = null_mut();
69-
let mut return_size: usize = 0;
65+
pub fn set_tick_period(period: Duration) -> Result<(), Status> {
7066
unsafe {
71-
match proxy_get_configuration(&mut return_data, &mut return_size) {
72-
Status::Ok => {
73-
if !return_data.is_null() {
74-
Ok(Some(Vec::from_raw_parts(
75-
return_data,
76-
return_size,
77-
return_size,
78-
)))
79-
} else {
80-
Ok(None)
81-
}
82-
}
67+
match proxy_set_tick_period_milliseconds(period.as_millis() as u32) {
68+
Status::Ok => Ok(()),
8369
status => panic!("unexpected status: {}", status as u32),
8470
}
8571
}
@@ -258,6 +244,7 @@ pub fn get_map_value(map_type: MapType, key: &str) -> Result<Option<String>, Sta
258244
Ok(None)
259245
}
260246
}
247+
Status::NotFound => Ok(None),
261248
status => panic!("unexpected status: {}", status as u32),
262249
}
263250
}
@@ -285,6 +272,7 @@ pub fn get_map_value_bytes(map_type: MapType, key: &str) -> Result<Option<Bytes>
285272
Ok(None)
286273
}
287274
}
275+
Status::NotFound => Ok(None),
288276
status => panic!("unexpected status: {}", status as u32),
289277
}
290278
}
@@ -624,25 +612,78 @@ pub fn enqueue_shared_queue(queue_id: u32, value: Option<&[u8]>) -> Result<(), S
624612
}
625613

626614
extern "C" {
627-
fn proxy_continue_request() -> Status;
615+
fn proxy_continue_stream(stream_type: StreamType) -> Status;
616+
}
617+
618+
pub fn resume_downstream() -> Result<(), Status> {
619+
unsafe {
620+
match proxy_continue_stream(StreamType::Downstream) {
621+
Status::Ok => Ok(()),
622+
status => panic!("unexpected status: {}", status as u32),
623+
}
624+
}
625+
}
626+
627+
pub fn resume_upstream() -> Result<(), Status> {
628+
unsafe {
629+
match proxy_continue_stream(StreamType::Upstream) {
630+
Status::Ok => Ok(()),
631+
status => panic!("unexpected status: {}", status as u32),
632+
}
633+
}
628634
}
629635

630636
pub fn resume_http_request() -> Result<(), Status> {
631637
unsafe {
632-
match proxy_continue_request() {
638+
match proxy_continue_stream(StreamType::HttpRequest) {
639+
Status::Ok => Ok(()),
640+
status => panic!("unexpected status: {}", status as u32),
641+
}
642+
}
643+
}
644+
645+
pub fn resume_http_response() -> Result<(), Status> {
646+
unsafe {
647+
match proxy_continue_stream(StreamType::HttpResponse) {
633648
Status::Ok => Ok(()),
634649
status => panic!("unexpected status: {}", status as u32),
635650
}
636651
}
637652
}
638653

639654
extern "C" {
640-
fn proxy_continue_response() -> Status;
655+
fn proxy_close_stream(stream_type: StreamType) -> Status;
641656
}
642657

643-
pub fn resume_http_response() -> Result<(), Status> {
658+
pub fn close_downstream() -> Result<(), Status> {
659+
unsafe {
660+
match proxy_close_stream(StreamType::Downstream) {
661+
Status::Ok => Ok(()),
662+
status => panic!("unexpected status: {}", status as u32),
663+
}
664+
}
665+
}
666+
pub fn close_upstream() -> Result<(), Status> {
667+
unsafe {
668+
match proxy_close_stream(StreamType::Upstream) {
669+
Status::Ok => Ok(()),
670+
status => panic!("unexpected status: {}", status as u32),
671+
}
672+
}
673+
}
674+
675+
pub fn reset_http_request() -> Result<(), Status> {
676+
unsafe {
677+
match proxy_close_stream(StreamType::HttpRequest) {
678+
Status::Ok => Ok(()),
679+
status => panic!("unexpected status: {}", status as u32),
680+
}
681+
}
682+
}
683+
684+
pub fn reset_http_response() -> Result<(), Status> {
644685
unsafe {
645-
match proxy_continue_response() {
686+
match proxy_close_stream(StreamType::HttpResponse) {
646687
Status::Ok => Ok(()),
647688
status => panic!("unexpected status: {}", status as u32),
648689
}
@@ -685,19 +726,6 @@ pub fn send_http_response(
685726
}
686727
}
687728

688-
extern "C" {
689-
fn proxy_clear_route_cache() -> Status;
690-
}
691-
692-
pub fn clear_http_route_cache() -> Result<(), Status> {
693-
unsafe {
694-
match proxy_clear_route_cache() {
695-
Status::Ok => Ok(()),
696-
status => panic!("unexpected status: {}", status as u32),
697-
}
698-
}
699-
}
700-
701729
extern "C" {
702730
fn proxy_http_call(
703731
upstream_data: *const u8,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ pub fn set_http_context(callback: types::NewHttpContext) {
3737
}
3838

3939
#[no_mangle]
40-
pub extern "C" fn proxy_abi_version_0_1_0() {}
40+
pub extern "C" fn proxy_abi_version_0_2_1() {}

0 commit comments

Comments
 (0)