Skip to content

Commit a417fda

Browse files
committed
fix
1 parent 91e1d65 commit a417fda

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/hostcalls.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ 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> {
181+
unsafe {
182+
let mut return_data: *mut u8 = null_mut();
183+
let mut return_size: usize = 0;
184+
match proxy_get_header_map_pairs(map_type, &mut return_data, &mut return_size) {
185+
Status::Ok => {
186+
if !return_data.is_null() {
187+
let serialized_map = Vec::from_raw_parts(return_data, return_size, return_size);
188+
Ok(utils::deserialize_bytes_map(&serialized_map))
189+
} else {
190+
Ok(Vec::new())
191+
}
192+
}
193+
status => panic!("unexpected status: {}", status as u32),
194+
}
195+
}
196+
}
197+
180198
extern "C" {
181199
fn proxy_set_header_map_pairs(
182200
map_type: MapType,
@@ -893,4 +911,25 @@ mod utils {
893911
}
894912
map
895913
}
914+
915+
pub(super) fn deserialize_bytes_map(bytes: &[u8]) -> Vec<(String, Vec<u8>)> {
916+
let mut map = Vec::new();
917+
if bytes.is_empty() {
918+
return map;
919+
}
920+
let size = u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[0..4]).unwrap()) as usize;
921+
let mut p = 4 + size * 8;
922+
for n in 0..size {
923+
let s = 4 + n * 8;
924+
let size = u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[s..s + 4]).unwrap()) as usize;
925+
let key = bytes[p..p + size].to_vec();
926+
p += size + 1;
927+
let size =
928+
u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[s + 4..s + 8]).unwrap()) as usize;
929+
let value = bytes[p..p + size].to_vec();
930+
p += size + 1;
931+
map.push((String::from_utf8(key).unwrap(), value));
932+
}
933+
map
934+
}
896935
}

src/traits.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ pub trait Context {
109109
)
110110
}
111111

112+
fn get_grpc_call_initial_metadata(&self) -> Vec<(String, Vec<u8>)> {
113+
hostcalls::get_map_bytes(MapType::GrpcReceiveInitialMetadata).unwrap()
114+
}
115+
116+
fn get_grpc_call_trailing_metadata(&self) -> Vec<(String, Vec<u8>)> {
117+
hostcalls::get_map_bytes(MapType::GrpcReceiveTrailingMetadata).unwrap()
118+
}
119+
120+
fn get_grpc_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
121+
hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, start, max_size).unwrap()
122+
}
123+
112124
fn cancel_grpc_call(&self, token_id: u32) -> Result<(), Status> {
113125
hostcalls::cancel_grpc_call(token_id)
114126
}

src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub enum BufferType {
6363
DownstreamData = 2,
6464
UpstreamData = 3,
6565
HttpCallResponseBody = 4,
66+
GrpcReceiveBuffer = 5,
6667
}
6768

6869
#[repr(u32)]
@@ -72,6 +73,8 @@ pub enum MapType {
7273
HttpRequestTrailers = 1,
7374
HttpResponseHeaders = 2,
7475
HttpResponseTrailers = 3,
76+
GrpcReceiveInitialMetadata = 4,
77+
GrpcReceiveTrailingMetadata = 5,
7578
HttpCallResponseHeaders = 6,
7679
HttpCallResponseTrailers = 7,
7780
}

0 commit comments

Comments
 (0)