pub struct Fields { /* private fields */ }
Expand description
A common representation for headers and trailers.
§Examples
Initialise response headers from a list:
let response_headers = Fields::from_list(&[
("content-type".to_owned(), "text/plain".into())
]).unwrap();
let response = OutgoingResponse::new(response_headers);
Build response headers up dynamically:
let accepts_json = req.headers()
.get(&"accept".to_owned())
.iter()
.flat_map(|v| String::from_utf8(v.clone()).ok())
.any(|s| s == "application/json");
let response_headers = Fields::new();
if accepts_json {
response_headers.set(&"content-type".to_owned(), &["application/json".into()]).unwrap();
};
§WASI resource documentation
This following block defines the fields
resource which corresponds to
HTTP standard Fields. Fields are a common representation used for both
Headers and Trailers.
A fields
may be mutable or immutable. A fields
created using the
constructor, from-list
, or clone
will be mutable, but a fields
resource given by other means (including, but not limited to,
incoming-request.headers
, outgoing-request.headers
) might be be
immutable. In an immutable fields, the set
, append
, and delete
operations will fail with header-error.immutable
.
Implementations§
Source§impl Fields
impl Fields
Sourcepub fn from_list(
entries: &[(FieldKey, FieldValue)],
) -> Result<Fields, HeaderError>
pub fn from_list( entries: &[(FieldKey, FieldValue)], ) -> Result<Fields, HeaderError>
Construct an HTTP Fields.
The resulting fields
is mutable.
The list represents each key-value pair in the Fields. Keys which have multiple values are represented by multiple entries in this list with the same key.
The tuple is a pair of the field key, represented as a string, and Value, represented as a list of bytes. In a valid Fields, all keys and values are valid UTF-8 strings. However, values are not always well-formed, so they are represented as a raw list of bytes.
An error result will be returned if any header or value was syntactically invalid, or if a header was forbidden.
Source§impl Fields
impl Fields
Sourcepub fn entries(&self) -> Vec<(FieldKey, FieldValue)>
pub fn entries(&self) -> Vec<(FieldKey, FieldValue)>
Retrieve the full set of keys and values in the Fields. Like the constructor, the list represents each key-value pair.
The outer list represents each key-value pair in the Fields. Keys which have multiple values are represented by multiple entries in this list with the same key.