Skip to content

Commit

Permalink
Make fields.get return an option
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed Dec 2, 2023
1 parent 03a14ac commit b0ccc7b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions crates/test-programs/src/bin/api_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ impl bindings::exports::wasi::http::incoming_handler::Guest for T {
let req_hdrs = request.headers();

assert!(
req_hdrs.get(&header).is_empty(),
req_hdrs.get(&header).is_none(),
"forbidden `custom-forbidden-header` found in request"
);

assert!(req_hdrs.delete(&header).is_err());
assert!(req_hdrs.append(&header, &b"no".to_vec()).is_err());

assert!(
req_hdrs.get(&header).is_empty(),
req_hdrs.get(&header).is_none(),
"append of forbidden header succeeded"
);

Expand Down
17 changes: 11 additions & 6 deletions crates/wasi-http/src/types_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,24 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFields for T {
&mut self,
fields: Resource<HostFields>,
name: String,
) -> wasmtime::Result<Vec<Vec<u8>>> {
) -> wasmtime::Result<Option<Vec<Vec<u8>>>> {
let fields = get_fields(self.table(), &fields).context("[fields_get] getting fields")?;

let header = match hyper::header::HeaderName::from_bytes(name.as_bytes()) {
Ok(header) => header,
Err(_) => return Ok(vec![]),
Err(_) => return Ok(None),
};

let res = get_fields(self.table(), &fields)
.context("[fields_get] getting fields")?
.get_all(header)
if !fields.contains_key(&header) {
return Ok(None);
}

let res = fields
.get_all(&header)
.into_iter()
.map(|val| val.as_bytes().to_owned())
.collect();
Ok(res)
Ok(Some(res))
}

fn set(
Expand Down
6 changes: 4 additions & 2 deletions crates/wasi-http/wit/deps/http/types.wit
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ interface types {
entries: list<tuple<field-key,field-value>>
) -> result<fields, header-error>;

/// Get all of the values corresponding to a key.
get: func(name: field-key) -> list<field-value>;
/// Get all of the values corresponding to a key. Returns a `none` value
/// when the key isn't present, to distinguish from the case where it's
/// present but empty.
get: func(name: field-key) -> option<list<field-value>>;

/// Set all of the values for a key. Clears any existing values for that
/// key, if they have been set.
Expand Down
6 changes: 4 additions & 2 deletions crates/wasi/wit/deps/http/types.wit
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ interface types {
entries: list<tuple<field-key,field-value>>
) -> result<fields, header-error>;

/// Get all of the values corresponding to a key.
get: func(name: field-key) -> list<field-value>;
/// Get all of the values corresponding to a key. Returns a `none` value
/// when the key isn't present, to distinguish from the case where it's
/// present but empty.
get: func(name: field-key) -> option<list<field-value>>;

/// Set all of the values for a key. Clears any existing values for that
/// key, if they have been set.
Expand Down

0 comments on commit b0ccc7b

Please sign in to comment.