Skip to content

Commit a3a4524

Browse files
authored
Add missing hostcalls for gRPC and HTTP callouts. (#107)
While there, unify style and unwrap errors in gRPC traits. Signed-off-by: Piotr Sikora <[email protected]>
1 parent c94f6e4 commit a3a4524

File tree

2 files changed

+107
-15
lines changed

2 files changed

+107
-15
lines changed

src/hostcalls.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub fn get_map(map_type: MapType) -> Result<Vec<(String, String)>, Status> {
177177
}
178178
}
179179

180-
pub fn get_map_bytes(map_type: MapType) -> Result<Vec<(String, Vec<u8>)>, Status> {
180+
pub fn get_map_bytes(map_type: MapType) -> Result<Vec<(String, Bytes)>, Status> {
181181
unsafe {
182182
let mut return_data: *mut u8 = null_mut();
183183
let mut return_size: usize = 0;
@@ -253,6 +253,33 @@ pub fn get_map_value(map_type: MapType, key: &str) -> Result<Option<String>, Sta
253253
}
254254
}
255255

256+
pub fn get_map_value_bytes(map_type: MapType, key: &str) -> Result<Option<Bytes>, Status> {
257+
let mut return_data: *mut u8 = null_mut();
258+
let mut return_size: usize = 0;
259+
unsafe {
260+
match proxy_get_header_map_value(
261+
map_type,
262+
key.as_ptr(),
263+
key.len(),
264+
&mut return_data,
265+
&mut return_size,
266+
) {
267+
Status::Ok => {
268+
if !return_data.is_null() {
269+
Ok(Some(Vec::from_raw_parts(
270+
return_data,
271+
return_size,
272+
return_size,
273+
)))
274+
} else {
275+
Ok(None)
276+
}
277+
}
278+
status => panic!("unexpected status: {}", status as u32),
279+
}
280+
}
281+
}
282+
256283
extern "C" {
257284
fn proxy_replace_header_map_value(
258285
map_type: MapType,
@@ -810,6 +837,16 @@ pub fn cancel_grpc_call(token_id: u32) -> Result<(), Status> {
810837
}
811838
}
812839

840+
pub fn cancel_grpc_stream(token_id: u32) -> Result<(), Status> {
841+
unsafe {
842+
match proxy_grpc_cancel(token_id) {
843+
Status::Ok => Ok(()),
844+
Status::NotFound => Err(Status::NotFound),
845+
status => panic!("unexpected status: {}", status as u32),
846+
}
847+
}
848+
}
849+
813850
extern "C" {
814851
fn proxy_grpc_close(token_id: u32) -> Status;
815852
}
@@ -824,6 +861,42 @@ pub fn close_grpc_stream(token_id: u32) -> Result<(), Status> {
824861
}
825862
}
826863

864+
extern "C" {
865+
fn proxy_get_status(
866+
return_code: *mut u32,
867+
return_message_data: *mut *mut u8,
868+
return_message_size: *mut usize,
869+
) -> Status;
870+
}
871+
872+
pub fn get_grpc_status() -> Result<(u32, Option<String>), Status> {
873+
let mut return_code: u32 = 0;
874+
let mut return_data: *mut u8 = null_mut();
875+
let mut return_size: usize = 0;
876+
unsafe {
877+
match proxy_get_status(&mut return_code, &mut return_data, &mut return_size) {
878+
Status::Ok => {
879+
if !return_data.is_null() {
880+
Ok((
881+
return_code,
882+
Some(
883+
String::from_utf8(Vec::from_raw_parts(
884+
return_data,
885+
return_size,
886+
return_size,
887+
))
888+
.unwrap(),
889+
),
890+
))
891+
} else {
892+
Ok((return_code, None))
893+
}
894+
}
895+
status => panic!("unexpected status: {}", status as u32),
896+
}
897+
}
898+
}
899+
827900
extern "C" {
828901
fn proxy_set_effective_context(context_id: u32) -> Status;
829902
}
@@ -1000,7 +1073,7 @@ mod utils {
10001073
map
10011074
}
10021075

1003-
pub(super) fn deserialize_bytes_map(bytes: &[u8]) -> Vec<(String, Vec<u8>)> {
1076+
pub(super) fn deserialize_bytes_map(bytes: &[u8]) -> Vec<(String, Bytes)> {
10041077
let mut map = Vec::new();
10051078
if bytes.is_empty() {
10061079
return map;

src/traits.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ pub trait Context {
8282
hostcalls::get_map(MapType::HttpCallResponseHeaders).unwrap()
8383
}
8484

85+
fn get_http_call_response_header(&self, name: &str) -> Option<String> {
86+
hostcalls::get_map_value(MapType::HttpCallResponseHeaders, &name).unwrap()
87+
}
88+
8589
fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
8690
hostcalls::get_buffer(BufferType::HttpCallResponseBody, start, max_size).unwrap()
8791
}
@@ -90,6 +94,10 @@ pub trait Context {
9094
hostcalls::get_map(MapType::HttpCallResponseTrailers).unwrap()
9195
}
9296

97+
fn get_http_call_response_trailer(&self, name: &str) -> Option<String> {
98+
hostcalls::get_map_value(MapType::HttpCallResponseTrailers, &name).unwrap()
99+
}
100+
93101
fn dispatch_grpc_call(
94102
&self,
95103
upstream_name: &str,
@@ -115,8 +123,8 @@ pub trait Context {
115123
hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, start, max_size).unwrap()
116124
}
117125

118-
fn cancel_grpc_call(&self, token_id: u32) -> Result<(), Status> {
119-
hostcalls::cancel_grpc_call(token_id)
126+
fn cancel_grpc_call(&self, token_id: u32) {
127+
hostcalls::cancel_grpc_call(token_id).unwrap()
120128
}
121129

122130
fn open_grpc_stream(
@@ -131,17 +139,16 @@ pub trait Context {
131139

132140
fn on_grpc_stream_initial_metadata(&mut self, _token_id: u32, _num_elements: u32) {}
133141

134-
fn get_grpc_stream_initial_metadata(&self) -> Vec<(String, Vec<u8>)> {
142+
fn get_grpc_stream_initial_metadata(&self) -> Vec<(String, Bytes)> {
135143
hostcalls::get_map_bytes(MapType::GrpcReceiveInitialMetadata).unwrap()
136144
}
137145

138-
fn send_grpc_stream_message(
139-
&self,
140-
token_id: u32,
141-
message: Option<&[u8]>,
142-
end_stream: bool,
143-
) -> Result<(), Status> {
144-
hostcalls::send_grpc_stream_message(token_id, message, end_stream)
146+
fn get_grpc_stream_initial_metadata_value(&self, name: &str) -> Option<Bytes> {
147+
hostcalls::get_map_value_bytes(MapType::GrpcReceiveInitialMetadata, &name).unwrap()
148+
}
149+
150+
fn send_grpc_stream_message(&self, token_id: u32, message: Option<&[u8]>, end_stream: bool) {
151+
hostcalls::send_grpc_stream_message(token_id, message, end_stream).unwrap()
145152
}
146153

147154
fn on_grpc_stream_message(&mut self, _token_id: u32, _message_size: usize) {}
@@ -152,16 +159,28 @@ pub trait Context {
152159

153160
fn on_grpc_stream_trailing_metadata(&mut self, _token_id: u32, _num_elements: u32) {}
154161

155-
fn get_grpc_stream_trailing_metadata(&self) -> Vec<(String, Vec<u8>)> {
162+
fn get_grpc_stream_trailing_metadata(&self) -> Vec<(String, Bytes)> {
156163
hostcalls::get_map_bytes(MapType::GrpcReceiveTrailingMetadata).unwrap()
157164
}
158165

159-
fn close_grpc_stream(&self, token_id: u32) -> Result<(), Status> {
160-
hostcalls::close_grpc_stream(token_id)
166+
fn get_grpc_stream_trailing_metadata_value(&self, name: &str) -> Option<Bytes> {
167+
hostcalls::get_map_value_bytes(MapType::GrpcReceiveTrailingMetadata, &name).unwrap()
168+
}
169+
170+
fn cancel_grpc_stream(&self, token_id: u32) {
171+
hostcalls::cancel_grpc_stream(token_id).unwrap()
172+
}
173+
174+
fn close_grpc_stream(&self, token_id: u32) {
175+
hostcalls::close_grpc_stream(token_id).unwrap()
161176
}
162177

163178
fn on_grpc_stream_close(&mut self, _token_id: u32, _status_code: u32) {}
164179

180+
fn get_grpc_status(&self) -> (u32, Option<String>) {
181+
hostcalls::get_grpc_status().unwrap()
182+
}
183+
165184
fn on_done(&mut self) -> bool {
166185
true
167186
}

0 commit comments

Comments
 (0)