Skip to content

Commit

Permalink
introduce HeaderValue abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
yskopets committed Aug 10, 2020
1 parent 95bcde5 commit 1030893
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 19 deletions.
6 changes: 3 additions & 3 deletions examples/http_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ impl Context for HttpHeaders {}
impl HttpContext for HttpHeaders {
fn on_http_request_headers(&mut self, _: usize) -> Action {
for (name, value) in &self.get_http_request_headers() {
trace!("#{} -> {}: {:?}", self.context_id, name, value);
trace!("#{} -> {}: {}", self.context_id, name, value);
}

match self.get_http_request_header(":path") {
Some(path) if path == b"/hello" => {
Some(path) if path == "/hello" => {
self.send_http_response(
200,
vec![("Hello", "World"), ("Powered-By", "proxy-wasm")],
Expand All @@ -53,7 +53,7 @@ impl HttpContext for HttpHeaders {

fn on_http_response_headers(&mut self, _: usize) -> Action {
for (name, value) in &self.get_http_response_headers() {
trace!("#{} <- {}: {:?}", self.context_id, name, value);
trace!("#{} <- {}: {}", self.context_id, name, value);
}
Action::Continue
}
Expand Down
14 changes: 8 additions & 6 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ extern "C" {
}

/// Returns all key-value pairs from a given map.
pub fn get_map(map_type: MapType) -> Result<Vec<(String, Bytes)>> {
pub fn get_map(map_type: MapType) -> Result<Vec<(String, HeaderValue)>> {
unsafe {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;
Expand Down Expand Up @@ -264,7 +264,7 @@ extern "C" {
/// # Ok(())
/// # }
/// ```
pub fn get_map_value<K>(map_type: MapType, key: K) -> Result<Option<Bytes>>
pub fn get_map_value<K>(map_type: MapType, key: K) -> Result<Option<HeaderValue>>
where
K: AsRef<str>,
{
Expand All @@ -280,7 +280,9 @@ where
) {
Status::Ok => {
if !return_data.is_null() {
Ok(Vec::from_raw_parts(return_data, return_size, return_size)).map(Option::from)
Ok(Vec::from_raw_parts(return_data, return_size, return_size))
.map(HeaderValue::from)
.map(Option::from)
} else {
Ok(None)
}
Expand Down Expand Up @@ -923,7 +925,7 @@ pub fn done() -> Result<()> {

mod utils {
use crate::error::Result;
use crate::types::Bytes;
use crate::types::{Bytes, HeaderValue};
use std::convert::TryFrom;

pub(super) fn serialize_property_path<P>(path: &[P]) -> Bytes
Expand Down Expand Up @@ -970,7 +972,7 @@ mod utils {
bytes
}

pub(super) fn deserialize_map(bytes: &[u8]) -> Result<Vec<(String, Bytes)>> {
pub(super) fn deserialize_map(bytes: &[u8]) -> Result<Vec<(String, HeaderValue)>> {
let mut map = Vec::new();
if bytes.is_empty() {
return Ok(map);
Expand All @@ -985,7 +987,7 @@ mod utils {
let size = u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[s + 4..s + 8])?) as usize;
let value = bytes[p..p + size].to_vec();
p += size + 1;
map.push((String::from_utf8(key)?, value));
map.push((String::from_utf8(key)?, value.into()));
}
Ok(map)
}
Expand Down
20 changes: 10 additions & 10 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ pub trait Context {
) {
}

fn get_http_call_response_headers(&self) -> Vec<(String, Bytes)> {
fn get_http_call_response_headers(&self) -> Vec<(String, HeaderValue)> {
hostcalls::get_map(MapType::HttpCallResponseHeaders).unwrap()
}

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

fn get_http_call_response_trailers(&self) -> Vec<(String, Bytes)> {
fn get_http_call_response_trailers(&self) -> Vec<(String, HeaderValue)> {
hostcalls::get_map(MapType::HttpCallResponseTrailers).unwrap()
}

Expand Down Expand Up @@ -165,15 +165,15 @@ pub trait HttpContext: Context {
Action::Continue
}

fn get_http_request_headers(&self) -> Vec<(String, Bytes)> {
fn get_http_request_headers(&self) -> Vec<(String, HeaderValue)> {
hostcalls::get_map(MapType::HttpRequestHeaders).unwrap()
}

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

fn get_http_request_header(&self, name: &str) -> Option<Bytes> {
fn get_http_request_header(&self, name: &str) -> Option<HeaderValue> {
hostcalls::get_map_value(MapType::HttpRequestHeaders, &name).unwrap()
}

Expand All @@ -197,15 +197,15 @@ pub trait HttpContext: Context {
Action::Continue
}

fn get_http_request_trailers(&self) -> Vec<(String, Bytes)> {
fn get_http_request_trailers(&self) -> Vec<(String, HeaderValue)> {
hostcalls::get_map(MapType::HttpRequestTrailers).unwrap()
}

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

fn get_http_request_trailer(&self, name: &str) -> Option<Bytes> {
fn get_http_request_trailer(&self, name: &str) -> Option<HeaderValue> {
hostcalls::get_map_value(MapType::HttpRequestTrailers, &name).unwrap()
}

Expand All @@ -225,15 +225,15 @@ pub trait HttpContext: Context {
Action::Continue
}

fn get_http_response_headers(&self) -> Vec<(String, Bytes)> {
fn get_http_response_headers(&self) -> Vec<(String, HeaderValue)> {
hostcalls::get_map(MapType::HttpResponseHeaders).unwrap()
}

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

fn get_http_response_header(&self, name: &str) -> Option<Bytes> {
fn get_http_response_header(&self, name: &str) -> Option<HeaderValue> {
hostcalls::get_map_value(MapType::HttpResponseHeaders, &name).unwrap()
}

Expand All @@ -257,15 +257,15 @@ pub trait HttpContext: Context {
Action::Continue
}

fn get_http_response_trailers(&self) -> Vec<(String, Bytes)> {
fn get_http_response_trailers(&self) -> Vec<(String, HeaderValue)> {
hostcalls::get_map(MapType::HttpResponseTrailers).unwrap()
}

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

fn get_http_response_trailer(&self, name: &str) -> Option<Bytes> {
fn get_http_response_trailer(&self, name: &str) -> Option<HeaderValue> {
hostcalls::get_map_value(MapType::HttpResponseTrailers, &name).unwrap()
}

Expand Down
Loading

0 comments on commit 1030893

Please sign in to comment.