Skip to content

Commit 57a9a5f

Browse files
committed
refactor HeaderValue into ByteString
1 parent f724912 commit 57a9a5f

File tree

5 files changed

+56
-327
lines changed

5 files changed

+56
-327
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ wee_alloc = "0.4"
1616
[dev-dependencies]
1717
version-sync = "0.9"
1818
chrono = "0.4"
19+
bstr = "0.2"
1920

2021
[profile.release]
2122
lto = true

src/hostcalls.rs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,16 @@ extern "C" {
112112
}
113113

114114
/// Returns configuration, e.g. VM configuration, extension configuration, etc.
115-
pub fn get_configuration() -> Result<Option<Bytes>> {
115+
pub fn get_configuration() -> Result<Option<ByteString>> {
116116
let mut return_data: *mut u8 = null_mut();
117117
let mut return_size: usize = 0;
118118
unsafe {
119119
match proxy_get_configuration(&mut return_data, &mut return_size) {
120120
Status::Ok => {
121121
if !return_data.is_null() {
122-
Ok(Some(Vec::from_raw_parts(
123-
return_data,
124-
return_size,
125-
return_size,
126-
)))
122+
Ok(Vec::from_raw_parts(return_data, return_size, return_size))
123+
.map(ByteString::from)
124+
.map(Option::from)
127125
} else {
128126
Ok(None)
129127
}
@@ -144,7 +142,11 @@ extern "C" {
144142
}
145143

146144
/// Returns content from a given buffer.
147-
pub fn get_buffer(buffer_type: BufferType, start: usize, max_size: usize) -> Result<Option<Bytes>> {
145+
pub fn get_buffer(
146+
buffer_type: BufferType,
147+
start: usize,
148+
max_size: usize,
149+
) -> Result<Option<ByteString>> {
148150
let mut return_data: *mut u8 = null_mut();
149151
let mut return_size: usize = 0;
150152
unsafe {
@@ -157,11 +159,9 @@ pub fn get_buffer(buffer_type: BufferType, start: usize, max_size: usize) -> Res
157159
) {
158160
Status::Ok => {
159161
if !return_data.is_null() {
160-
Ok(Some(Vec::from_raw_parts(
161-
return_data,
162-
return_size,
163-
return_size,
164-
)))
162+
Ok(Vec::from_raw_parts(return_data, return_size, return_size))
163+
.map(ByteString::from)
164+
.map(Option::from)
165165
} else {
166166
Ok(None)
167167
}
@@ -181,7 +181,7 @@ extern "C" {
181181
}
182182

183183
/// Returns all key-value pairs from a given map.
184-
pub fn get_map(map_type: MapType) -> Result<Vec<(String, HeaderValue)>> {
184+
pub fn get_map(map_type: MapType) -> Result<Vec<(ByteString, ByteString)>> {
185185
unsafe {
186186
let mut return_data: *mut u8 = null_mut();
187187
let mut return_size: usize = 0;
@@ -264,7 +264,7 @@ extern "C" {
264264
/// # Ok(())
265265
/// # }
266266
/// ```
267-
pub fn get_map_value<K>(map_type: MapType, key: K) -> Result<Option<HeaderValue>>
267+
pub fn get_map_value<K>(map_type: MapType, key: K) -> Result<Option<ByteString>>
268268
where
269269
K: AsRef<str>,
270270
{
@@ -281,7 +281,7 @@ where
281281
Status::Ok => {
282282
if !return_data.is_null() {
283283
Ok(Vec::from_raw_parts(return_data, return_size, return_size))
284-
.map(HeaderValue::from)
284+
.map(ByteString::from)
285285
.map(Option::from)
286286
} else {
287287
Ok(None)
@@ -420,7 +420,7 @@ extern "C" {
420420
/// # Ok(())
421421
/// # }
422422
/// ```
423-
pub fn get_property<P>(path: &[P]) -> Result<Option<Bytes>>
423+
pub fn get_property<P>(path: &[P]) -> Result<Option<ByteString>>
424424
where
425425
P: AsRef<str>,
426426
{
@@ -436,11 +436,9 @@ where
436436
) {
437437
Status::Ok => {
438438
if !return_data.is_null() {
439-
Ok(Some(Vec::from_raw_parts(
440-
return_data,
441-
return_size,
442-
return_size,
443-
)))
439+
Ok(Vec::from_raw_parts(return_data, return_size, return_size))
440+
.map(ByteString::from)
441+
.map(Option::from)
444442
} else {
445443
Ok(None)
446444
}
@@ -518,7 +516,7 @@ extern "C" {
518516
/// # Ok(())
519517
/// # }
520518
/// ```
521-
pub fn get_shared_data<K>(key: K) -> Result<(Option<Bytes>, Option<u32>)>
519+
pub fn get_shared_data<K>(key: K) -> Result<(Option<ByteString>, Option<u32>)>
522520
where
523521
K: AsRef<str>,
524522
{
@@ -540,7 +538,8 @@ where
540538
};
541539
if !return_data.is_null() {
542540
Ok((
543-
Some(Vec::from_raw_parts(return_data, return_size, return_size)),
541+
Some(Vec::from_raw_parts(return_data, return_size, return_size))
542+
.map(ByteString::from),
544543
cas,
545544
))
546545
} else {
@@ -654,18 +653,16 @@ extern "C" {
654653
}
655654

656655
/// Returns data from the end of a given queue.
657-
pub fn dequeue_shared_queue(queue_id: u32) -> Result<Option<Bytes>> {
656+
pub fn dequeue_shared_queue(queue_id: u32) -> Result<Option<ByteString>> {
658657
let mut return_data: *mut u8 = null_mut();
659658
let mut return_size: usize = 0;
660659
unsafe {
661660
match proxy_dequeue_shared_queue(queue_id, &mut return_data, &mut return_size) {
662661
Status::Ok => {
663662
if !return_data.is_null() {
664-
Ok(Some(Vec::from_raw_parts(
665-
return_data,
666-
return_size,
667-
return_size,
668-
)))
663+
Ok(Vec::from_raw_parts(return_data, return_size, return_size))
664+
.map(ByteString::from)
665+
.map(Option::from)
669666
} else {
670667
Ok(None)
671668
}
@@ -925,10 +922,10 @@ pub fn done() -> Result<()> {
925922

926923
mod utils {
927924
use crate::error::Result;
928-
use crate::types::{Bytes, HeaderValue};
925+
use crate::types::ByteString;
929926
use std::convert::TryFrom;
930927

931-
pub(super) fn serialize_property_path<P>(path: &[P]) -> Bytes
928+
pub(super) fn serialize_property_path<P>(path: &[P]) -> Vec<u8>
932929
where
933930
P: AsRef<str>,
934931
{
@@ -939,7 +936,7 @@ mod utils {
939936
for part in path {
940937
size += part.as_ref().len() + 1;
941938
}
942-
let mut bytes: Bytes = Vec::with_capacity(size);
939+
let mut bytes: Vec<u8> = Vec::with_capacity(size);
943940
for part in path {
944941
bytes.extend_from_slice(part.as_ref().as_bytes());
945942
bytes.push(0);
@@ -948,7 +945,7 @@ mod utils {
948945
bytes
949946
}
950947

951-
pub(super) fn serialize_map<K, V>(map: &[(K, V)]) -> Bytes
948+
pub(super) fn serialize_map<K, V>(map: &[(K, V)]) -> Vec<u8>
952949
where
953950
K: AsRef<str>,
954951
V: AsRef<[u8]>,
@@ -957,7 +954,7 @@ mod utils {
957954
for (name, value) in map {
958955
size += name.as_ref().len() + value.as_ref().len() + 10;
959956
}
960-
let mut bytes: Bytes = Vec::with_capacity(size);
957+
let mut bytes: Vec<u8> = Vec::with_capacity(size);
961958
bytes.extend_from_slice(&map.len().to_le_bytes());
962959
for (name, value) in map {
963960
bytes.extend_from_slice(&name.as_ref().len().to_le_bytes());
@@ -972,7 +969,7 @@ mod utils {
972969
bytes
973970
}
974971

975-
pub(super) fn deserialize_map(bytes: &[u8]) -> Result<Vec<(String, HeaderValue)>> {
972+
pub(super) fn deserialize_map(bytes: &[u8]) -> Result<Vec<(ByteString, ByteString)>> {
976973
let mut map = Vec::new();
977974
if bytes.is_empty() {
978975
return Ok(map);
@@ -987,7 +984,7 @@ mod utils {
987984
let size = u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[s + 4..s + 8])?) as usize;
988985
let value = bytes[p..p + size].to_vec();
989986
p += size + 1;
990-
map.push((String::from_utf8(key)?, value.into()));
987+
map.push((key.into(), value.into()));
991988
}
992989
Ok(map)
993990
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod traits;
2020
pub mod types;
2121

2222
mod allocator;
23+
mod bytestring;
2324
mod dispatcher;
2425
mod logger;
2526

src/traits.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ pub trait Context {
2323
hostcalls::get_current_time().unwrap()
2424
}
2525

26-
fn get_property(&self, path: Vec<&str>) -> Option<Bytes> {
26+
fn get_property(&self, path: Vec<&str>) -> Option<ByteString> {
2727
hostcalls::get_property(&path).unwrap()
2828
}
2929

3030
fn set_property(&self, path: Vec<&str>, value: Option<&[u8]>) {
3131
hostcalls::set_property(&path, value).unwrap()
3232
}
3333

34-
fn get_shared_data(&self, key: &str) -> (Option<Bytes>, Option<u32>) {
34+
fn get_shared_data(&self, key: &str) -> (Option<ByteString>, Option<u32>) {
3535
hostcalls::get_shared_data(key).unwrap()
3636
}
3737

@@ -47,7 +47,7 @@ pub trait Context {
4747
hostcalls::resolve_shared_queue(vm_id, name).unwrap()
4848
}
4949

50-
fn dequeue_shared_queue(&self, queue_id: u32) -> Result<Option<Bytes>> {
50+
fn dequeue_shared_queue(&self, queue_id: u32) -> Result<Option<ByteString>> {
5151
hostcalls::dequeue_shared_queue(queue_id)
5252
}
5353

@@ -75,15 +75,15 @@ pub trait Context {
7575
) {
7676
}
7777

78-
fn get_http_call_response_headers(&self) -> Vec<(String, HeaderValue)> {
78+
fn get_http_call_response_headers(&self) -> Vec<(ByteString, ByteString)> {
7979
hostcalls::get_map(MapType::HttpCallResponseHeaders).unwrap()
8080
}
8181

82-
fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
82+
fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<ByteString> {
8383
hostcalls::get_buffer(BufferType::HttpCallResponseBody, start, max_size).unwrap()
8484
}
8585

86-
fn get_http_call_response_trailers(&self) -> Vec<(String, HeaderValue)> {
86+
fn get_http_call_response_trailers(&self) -> Vec<(ByteString, ByteString)> {
8787
hostcalls::get_map(MapType::HttpCallResponseTrailers).unwrap()
8888
}
8989

@@ -111,7 +111,7 @@ pub trait RootContext: Context {
111111
true
112112
}
113113

114-
fn get_configuration(&self) -> Option<Bytes> {
114+
fn get_configuration(&self) -> Option<ByteString> {
115115
hostcalls::get_configuration().unwrap()
116116
}
117117

@@ -141,7 +141,7 @@ pub trait StreamContext: Context {
141141
Action::Continue
142142
}
143143

144-
fn get_downstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
144+
fn get_downstream_data(&self, start: usize, max_size: usize) -> Option<ByteString> {
145145
hostcalls::get_buffer(BufferType::DownstreamData, start, max_size).unwrap()
146146
}
147147

@@ -151,7 +151,7 @@ pub trait StreamContext: Context {
151151
Action::Continue
152152
}
153153

154-
fn get_upstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
154+
fn get_upstream_data(&self, start: usize, max_size: usize) -> Option<ByteString> {
155155
hostcalls::get_buffer(BufferType::UpstreamData, start, max_size).unwrap()
156156
}
157157

@@ -165,15 +165,15 @@ pub trait HttpContext: Context {
165165
Action::Continue
166166
}
167167

168-
fn get_http_request_headers(&self) -> Vec<(String, HeaderValue)> {
168+
fn get_http_request_headers(&self) -> Vec<(ByteString, ByteString)> {
169169
hostcalls::get_map(MapType::HttpRequestHeaders).unwrap()
170170
}
171171

172172
fn set_http_request_headers(&self, headers: Vec<(&str, &str)>) {
173173
hostcalls::set_map(MapType::HttpRequestHeaders, &headers).unwrap()
174174
}
175175

176-
fn get_http_request_header(&self, name: &str) -> Option<HeaderValue> {
176+
fn get_http_request_header(&self, name: &str) -> Option<ByteString> {
177177
hostcalls::get_map_value(MapType::HttpRequestHeaders, &name).unwrap()
178178
}
179179

@@ -189,23 +189,23 @@ pub trait HttpContext: Context {
189189
Action::Continue
190190
}
191191

192-
fn get_http_request_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
192+
fn get_http_request_body(&self, start: usize, max_size: usize) -> Option<ByteString> {
193193
hostcalls::get_buffer(BufferType::HttpRequestBody, start, max_size).unwrap()
194194
}
195195

196196
fn on_http_request_trailers(&mut self, _num_trailers: usize) -> Action {
197197
Action::Continue
198198
}
199199

200-
fn get_http_request_trailers(&self) -> Vec<(String, HeaderValue)> {
200+
fn get_http_request_trailers(&self) -> Vec<(ByteString, ByteString)> {
201201
hostcalls::get_map(MapType::HttpRequestTrailers).unwrap()
202202
}
203203

204204
fn set_http_request_trailers(&self, trailers: Vec<(&str, &str)>) {
205205
hostcalls::set_map(MapType::HttpRequestTrailers, &trailers).unwrap()
206206
}
207207

208-
fn get_http_request_trailer(&self, name: &str) -> Option<HeaderValue> {
208+
fn get_http_request_trailer(&self, name: &str) -> Option<ByteString> {
209209
hostcalls::get_map_value(MapType::HttpRequestTrailers, &name).unwrap()
210210
}
211211

@@ -225,15 +225,15 @@ pub trait HttpContext: Context {
225225
Action::Continue
226226
}
227227

228-
fn get_http_response_headers(&self) -> Vec<(String, HeaderValue)> {
228+
fn get_http_response_headers(&self) -> Vec<(ByteString, ByteString)> {
229229
hostcalls::get_map(MapType::HttpResponseHeaders).unwrap()
230230
}
231231

232232
fn set_http_response_headers(&self, headers: Vec<(&str, &str)>) {
233233
hostcalls::set_map(MapType::HttpResponseHeaders, &headers).unwrap()
234234
}
235235

236-
fn get_http_response_header(&self, name: &str) -> Option<HeaderValue> {
236+
fn get_http_response_header(&self, name: &str) -> Option<ByteString> {
237237
hostcalls::get_map_value(MapType::HttpResponseHeaders, &name).unwrap()
238238
}
239239

@@ -249,23 +249,23 @@ pub trait HttpContext: Context {
249249
Action::Continue
250250
}
251251

252-
fn get_http_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
252+
fn get_http_response_body(&self, start: usize, max_size: usize) -> Option<ByteString> {
253253
hostcalls::get_buffer(BufferType::HttpResponseBody, start, max_size).unwrap()
254254
}
255255

256256
fn on_http_response_trailers(&mut self, _num_trailers: usize) -> Action {
257257
Action::Continue
258258
}
259259

260-
fn get_http_response_trailers(&self) -> Vec<(String, HeaderValue)> {
260+
fn get_http_response_trailers(&self) -> Vec<(ByteString, ByteString)> {
261261
hostcalls::get_map(MapType::HttpResponseTrailers).unwrap()
262262
}
263263

264264
fn set_http_response_trailers(&self, headers: Vec<(&str, &str)>) {
265265
hostcalls::set_map(MapType::HttpResponseTrailers, &headers).unwrap()
266266
}
267267

268-
fn get_http_response_trailer(&self, name: &str) -> Option<HeaderValue> {
268+
fn get_http_response_trailer(&self, name: &str) -> Option<ByteString> {
269269
hostcalls::get_map_value(MapType::HttpResponseTrailers, &name).unwrap()
270270
}
271271

0 commit comments

Comments
 (0)