Skip to content

Commit 50b17c0

Browse files
authored
Add hostcalls for maps that use bytes instead of UTF-8 strings. (#111)
Partially addresses #25. Signed-off-by: Piotr Sikora <[email protected]>
1 parent a3a4524 commit 50b17c0

File tree

2 files changed

+154
-7
lines changed

2 files changed

+154
-7
lines changed

src/hostcalls.rs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub fn get_map_bytes(map_type: MapType) -> Result<Vec<(String, Bytes)>, Status>
185185
Status::Ok => {
186186
if !return_data.is_null() {
187187
let serialized_map = Vec::from_raw_parts(return_data, return_size, return_size);
188-
Ok(utils::deserialize_bytes_map(&serialized_map))
188+
Ok(utils::deserialize_map_bytes(&serialized_map))
189189
} else {
190190
Ok(Vec::new())
191191
}
@@ -213,6 +213,16 @@ pub fn set_map(map_type: MapType, map: Vec<(&str, &str)>) -> Result<(), Status>
213213
}
214214
}
215215

216+
pub fn set_map_bytes(map_type: MapType, map: Vec<(&str, &[u8])>) -> Result<(), Status> {
217+
let serialized_map = utils::serialize_map_bytes(map);
218+
unsafe {
219+
match proxy_set_header_map_pairs(map_type, serialized_map.as_ptr(), serialized_map.len()) {
220+
Status::Ok => Ok(()),
221+
status => panic!("unexpected status: {}", status as u32),
222+
}
223+
}
224+
}
225+
216226
extern "C" {
217227
fn proxy_get_header_map_value(
218228
map_type: MapType,
@@ -320,6 +330,32 @@ pub fn set_map_value(map_type: MapType, key: &str, value: Option<&str>) -> Resul
320330
}
321331
}
322332

333+
pub fn set_map_value_bytes(
334+
map_type: MapType,
335+
key: &str,
336+
value: Option<&[u8]>,
337+
) -> Result<(), Status> {
338+
unsafe {
339+
if let Some(value) = value {
340+
match proxy_replace_header_map_value(
341+
map_type,
342+
key.as_ptr(),
343+
key.len(),
344+
value.as_ptr(),
345+
value.len(),
346+
) {
347+
Status::Ok => Ok(()),
348+
status => panic!("unexpected status: {}", status as u32),
349+
}
350+
} else {
351+
match proxy_remove_header_map_value(map_type, key.as_ptr(), key.len()) {
352+
Status::Ok => Ok(()),
353+
status => panic!("unexpected status: {}", status as u32),
354+
}
355+
}
356+
}
357+
}
358+
323359
extern "C" {
324360
fn proxy_add_header_map_value(
325361
map_type: MapType,
@@ -345,6 +381,21 @@ pub fn add_map_value(map_type: MapType, key: &str, value: &str) -> Result<(), St
345381
}
346382
}
347383

384+
pub fn add_map_value_bytes(map_type: MapType, key: &str, value: &[u8]) -> Result<(), Status> {
385+
unsafe {
386+
match proxy_add_header_map_value(
387+
map_type,
388+
key.as_ptr(),
389+
key.len(),
390+
value.as_ptr(),
391+
value.len(),
392+
) {
393+
Status::Ok => Ok(()),
394+
status => panic!("unexpected status: {}", status as u32),
395+
}
396+
}
397+
}
398+
348399
extern "C" {
349400
fn proxy_get_property(
350401
path_data: *const u8,
@@ -722,7 +773,7 @@ pub fn dispatch_grpc_call(
722773
timeout: Duration,
723774
) -> Result<u32, Status> {
724775
let mut return_callout_id = 0;
725-
let serialized_initial_metadata = utils::serialize_bytes_map(initial_metadata);
776+
let serialized_initial_metadata = utils::serialize_map_bytes(initial_metadata);
726777
unsafe {
727778
match proxy_grpc_call(
728779
upstream_name.as_ptr(),
@@ -770,7 +821,7 @@ pub fn open_grpc_stream(
770821
initial_metadata: Vec<(&str, &[u8])>,
771822
) -> Result<u32, Status> {
772823
let mut return_stream_id = 0;
773-
let serialized_initial_metadata = utils::serialize_bytes_map(initial_metadata);
824+
let serialized_initial_metadata = utils::serialize_map_bytes(initial_metadata);
774825
unsafe {
775826
match proxy_grpc_stream(
776827
upstream_name.as_ptr(),
@@ -1029,7 +1080,7 @@ mod utils {
10291080
bytes
10301081
}
10311082

1032-
pub(super) fn serialize_bytes_map(map: Vec<(&str, &[u8])>) -> Bytes {
1083+
pub(super) fn serialize_map_bytes(map: Vec<(&str, &[u8])>) -> Bytes {
10331084
let mut size: usize = 4;
10341085
for (name, value) in &map {
10351086
size += name.len() + value.len() + 10;
@@ -1073,7 +1124,7 @@ mod utils {
10731124
map
10741125
}
10751126

1076-
pub(super) fn deserialize_bytes_map(bytes: &[u8]) -> Vec<(String, Bytes)> {
1127+
pub(super) fn deserialize_map_bytes(bytes: &[u8]) -> Vec<(String, Bytes)> {
10771128
let mut map = Vec::new();
10781129
if bytes.is_empty() {
10791130
return map;

src/traits.rs

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

85+
fn get_http_call_response_headers_bytes(&self) -> Vec<(String, Bytes)> {
86+
hostcalls::get_map_bytes(MapType::HttpCallResponseHeaders).unwrap()
87+
}
88+
8589
fn get_http_call_response_header(&self, name: &str) -> Option<String> {
8690
hostcalls::get_map_value(MapType::HttpCallResponseHeaders, &name).unwrap()
8791
}
8892

93+
fn get_http_call_response_header_bytes(&self, name: &str) -> Option<Bytes> {
94+
hostcalls::get_map_value_bytes(MapType::HttpCallResponseHeaders, name).unwrap()
95+
}
96+
8997
fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
9098
hostcalls::get_buffer(BufferType::HttpCallResponseBody, start, max_size).unwrap()
9199
}
@@ -94,10 +102,18 @@ pub trait Context {
94102
hostcalls::get_map(MapType::HttpCallResponseTrailers).unwrap()
95103
}
96104

105+
fn get_http_call_response_trailers_bytes(&self) -> Vec<(String, Bytes)> {
106+
hostcalls::get_map_bytes(MapType::HttpCallResponseTrailers).unwrap()
107+
}
108+
97109
fn get_http_call_response_trailer(&self, name: &str) -> Option<String> {
98110
hostcalls::get_map_value(MapType::HttpCallResponseTrailers, &name).unwrap()
99111
}
100112

113+
fn get_http_call_response_trailer_bytes(&self, name: &str) -> Option<Bytes> {
114+
hostcalls::get_map_value_bytes(MapType::HttpCallResponseTrailers, name).unwrap()
115+
}
116+
101117
fn dispatch_grpc_call(
102118
&self,
103119
upstream_name: &str,
@@ -271,22 +287,42 @@ pub trait HttpContext: Context {
271287
hostcalls::get_map(MapType::HttpRequestHeaders).unwrap()
272288
}
273289

290+
fn get_http_request_headers_bytes(&self) -> Vec<(String, Bytes)> {
291+
hostcalls::get_map_bytes(MapType::HttpRequestHeaders).unwrap()
292+
}
293+
274294
fn set_http_request_headers(&self, headers: Vec<(&str, &str)>) {
275295
hostcalls::set_map(MapType::HttpRequestHeaders, headers).unwrap()
276296
}
277297

298+
fn set_http_request_headers_bytes(&self, headers: Vec<(&str, &[u8])>) {
299+
hostcalls::set_map_bytes(MapType::HttpRequestHeaders, headers).unwrap()
300+
}
301+
278302
fn get_http_request_header(&self, name: &str) -> Option<String> {
279303
hostcalls::get_map_value(MapType::HttpRequestHeaders, &name).unwrap()
280304
}
281305

306+
fn get_http_request_header_bytes(&self, name: &str) -> Option<Bytes> {
307+
hostcalls::get_map_value_bytes(MapType::HttpRequestHeaders, name).unwrap()
308+
}
309+
282310
fn set_http_request_header(&self, name: &str, value: Option<&str>) {
283311
hostcalls::set_map_value(MapType::HttpRequestHeaders, &name, value).unwrap()
284312
}
285313

314+
fn set_http_request_header_bytes(&self, name: &str, value: Option<&[u8]>) {
315+
hostcalls::set_map_value_bytes(MapType::HttpRequestHeaders, name, value).unwrap()
316+
}
317+
286318
fn add_http_request_header(&self, name: &str, value: &str) {
287319
hostcalls::add_map_value(MapType::HttpRequestHeaders, &name, value).unwrap()
288320
}
289321

322+
fn add_http_request_header_bytes(&self, name: &str, value: &[u8]) {
323+
hostcalls::add_map_value_bytes(MapType::HttpRequestHeaders, name, value).unwrap()
324+
}
325+
290326
fn on_http_request_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
291327
Action::Continue
292328
}
@@ -307,22 +343,42 @@ pub trait HttpContext: Context {
307343
hostcalls::get_map(MapType::HttpRequestTrailers).unwrap()
308344
}
309345

346+
fn get_http_request_trailers_bytes(&self) -> Vec<(String, Bytes)> {
347+
hostcalls::get_map_bytes(MapType::HttpRequestTrailers).unwrap()
348+
}
349+
310350
fn set_http_request_trailers(&self, trailers: Vec<(&str, &str)>) {
311351
hostcalls::set_map(MapType::HttpRequestTrailers, trailers).unwrap()
312352
}
313353

354+
fn set_http_request_trailers_bytes(&self, trailers: Vec<(&str, &[u8])>) {
355+
hostcalls::set_map_bytes(MapType::HttpRequestTrailers, trailers).unwrap()
356+
}
357+
314358
fn get_http_request_trailer(&self, name: &str) -> Option<String> {
315359
hostcalls::get_map_value(MapType::HttpRequestTrailers, &name).unwrap()
316360
}
317361

362+
fn get_http_request_trailer_bytes(&self, name: &str) -> Option<Bytes> {
363+
hostcalls::get_map_value_bytes(MapType::HttpRequestTrailers, name).unwrap()
364+
}
365+
318366
fn set_http_request_trailer(&self, name: &str, value: Option<&str>) {
319367
hostcalls::set_map_value(MapType::HttpRequestTrailers, &name, value).unwrap()
320368
}
321369

370+
fn set_http_request_trailer_bytes(&self, name: &str, value: Option<&[u8]>) {
371+
hostcalls::set_map_value_bytes(MapType::HttpRequestTrailers, name, value).unwrap()
372+
}
373+
322374
fn add_http_request_trailer(&self, name: &str, value: &str) {
323375
hostcalls::add_map_value(MapType::HttpRequestTrailers, &name, value).unwrap()
324376
}
325377

378+
fn add_http_request_trailer_bytes(&self, name: &str, value: &[u8]) {
379+
hostcalls::add_map_value_bytes(MapType::HttpRequestTrailers, name, value).unwrap()
380+
}
381+
326382
fn resume_http_request(&self) {
327383
hostcalls::resume_http_request().unwrap()
328384
}
@@ -335,22 +391,42 @@ pub trait HttpContext: Context {
335391
hostcalls::get_map(MapType::HttpResponseHeaders).unwrap()
336392
}
337393

394+
fn get_http_response_headers_bytes(&self) -> Vec<(String, Bytes)> {
395+
hostcalls::get_map_bytes(MapType::HttpResponseHeaders).unwrap()
396+
}
397+
338398
fn set_http_response_headers(&self, headers: Vec<(&str, &str)>) {
339399
hostcalls::set_map(MapType::HttpResponseHeaders, headers).unwrap()
340400
}
341401

402+
fn set_http_response_headers_bytes(&self, headers: Vec<(&str, &[u8])>) {
403+
hostcalls::set_map_bytes(MapType::HttpResponseHeaders, headers).unwrap()
404+
}
405+
342406
fn get_http_response_header(&self, name: &str) -> Option<String> {
343407
hostcalls::get_map_value(MapType::HttpResponseHeaders, &name).unwrap()
344408
}
345409

410+
fn get_http_response_header_bytes(&self, name: &str) -> Option<Bytes> {
411+
hostcalls::get_map_value_bytes(MapType::HttpResponseHeaders, name).unwrap()
412+
}
413+
346414
fn set_http_response_header(&self, name: &str, value: Option<&str>) {
347415
hostcalls::set_map_value(MapType::HttpResponseHeaders, &name, value).unwrap()
348416
}
349417

418+
fn set_http_response_header_bytes(&self, name: &str, value: Option<&[u8]>) {
419+
hostcalls::set_map_value_bytes(MapType::HttpResponseHeaders, name, value).unwrap()
420+
}
421+
350422
fn add_http_response_header(&self, name: &str, value: &str) {
351423
hostcalls::add_map_value(MapType::HttpResponseHeaders, &name, value).unwrap()
352424
}
353425

426+
fn add_http_response_header_bytes(&self, name: &str, value: &[u8]) {
427+
hostcalls::add_map_value_bytes(MapType::HttpResponseHeaders, name, value).unwrap()
428+
}
429+
354430
fn on_http_response_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
355431
Action::Continue
356432
}
@@ -371,22 +447,42 @@ pub trait HttpContext: Context {
371447
hostcalls::get_map(MapType::HttpResponseTrailers).unwrap()
372448
}
373449

374-
fn set_http_response_trailers(&self, headers: Vec<(&str, &str)>) {
375-
hostcalls::set_map(MapType::HttpResponseTrailers, headers).unwrap()
450+
fn get_http_response_trailers_bytes(&self) -> Vec<(String, Bytes)> {
451+
hostcalls::get_map_bytes(MapType::HttpResponseTrailers).unwrap()
452+
}
453+
454+
fn set_http_response_trailers(&self, trailers: Vec<(&str, &str)>) {
455+
hostcalls::set_map(MapType::HttpResponseTrailers, trailers).unwrap()
456+
}
457+
458+
fn set_http_response_trailers_bytes(&self, trailers: Vec<(&str, &[u8])>) {
459+
hostcalls::set_map_bytes(MapType::HttpResponseTrailers, trailers).unwrap()
376460
}
377461

378462
fn get_http_response_trailer(&self, name: &str) -> Option<String> {
379463
hostcalls::get_map_value(MapType::HttpResponseTrailers, &name).unwrap()
380464
}
381465

466+
fn get_http_response_trailer_bytes(&self, name: &str) -> Option<Bytes> {
467+
hostcalls::get_map_value_bytes(MapType::HttpResponseTrailers, name).unwrap()
468+
}
469+
382470
fn set_http_response_trailer(&self, name: &str, value: Option<&str>) {
383471
hostcalls::set_map_value(MapType::HttpResponseTrailers, &name, value).unwrap()
384472
}
385473

474+
fn set_http_response_trailer_bytes(&self, name: &str, value: Option<&[u8]>) {
475+
hostcalls::set_map_value_bytes(MapType::HttpResponseTrailers, name, value).unwrap()
476+
}
477+
386478
fn add_http_response_trailer(&self, name: &str, value: &str) {
387479
hostcalls::add_map_value(MapType::HttpResponseTrailers, &name, value).unwrap()
388480
}
389481

482+
fn add_http_response_trailer_bytes(&self, name: &str, value: &[u8]) {
483+
hostcalls::add_map_value_bytes(MapType::HttpResponseTrailers, name, value).unwrap()
484+
}
485+
390486
fn resume_http_response(&self) {
391487
hostcalls::resume_http_response().unwrap()
392488
}

0 commit comments

Comments
 (0)