pub struct Headers { /* private fields */ }
Expand description
Wrapper over the list of headers associated with a Request that we need in order to parse the request correctly and be able to respond to it.
The only Content-Type
s supported are text/plain
and application/json
, which are both
in plain text actually and don’t influence our parsing process.
All the other possible header fields are not necessary in order to serve this connection and, thus, are not of interest to us. However, we still look for header fields that might invalidate our request as we don’t support the full set of HTTP/1.1 specification. Such header entries are “Transfer-Encoding: identity; q=0”, which means a compression algorithm is applied to the body of the request, or “Expect: 103-checkpoint”.
Implementations§
Source§impl Headers
impl Headers
Sourcepub fn parse_header_line(
&mut self,
header_line: &[u8],
) -> Result<(), RequestError>
pub fn parse_header_line( &mut self, header_line: &[u8], ) -> Result<(), RequestError>
Expects one header line and parses it, updating the header structure or returning an error if the header is invalid.
§Errors
UnsupportedHeader
is returned when the parsed header line is not of interest
to us or when it is unrecognizable.
InvalidHeader
is returned when the parsed header is formatted incorrectly or suggests
that the client is using HTTP features that we do not support in this implementation,
which invalidates the request.
§Examples
use dbs_uhttp::Headers;
let mut request_header = Headers::default();
assert!(request_header.parse_header_line(b"Content-Length: 24").is_ok());
assert!(request_header.parse_header_line(b"Content-Length: 24: 2").is_err());
Sourcepub fn content_length(&self) -> u32
pub fn content_length(&self) -> u32
Returns the content length of the body.
Sourcepub fn try_from(bytes: &[u8]) -> Result<Headers, RequestError>
pub fn try_from(bytes: &[u8]) -> Result<Headers, RequestError>
Parses a byte slice into a Headers structure for a HTTP request.
The byte slice is expected to have the following format:
* Request Header Lines “<header_line> CRLF”- Optional
There can be any number of request headers, including none, followed by
an extra sequence of Carriage Return and Line Feed.
All header fields are parsed. However, only the ones present in the
Headers
struct are relevant to us and stored
for future use.
§Errors
The function returns InvalidHeader
when parsing the byte stream fails.
§Examples
use dbs_uhttp::Headers;
let request_headers = Headers::try_from(b"Content-Length: 55\r\n\r\n");
Sourcepub fn set_accept(&mut self, media_type: MediaType)
pub fn set_accept(&mut self, media_type: MediaType)
Accept header setter.
Sourcepub fn insert_custom_header(
&mut self,
key: String,
value: String,
) -> Result<(), RequestError>
pub fn insert_custom_header( &mut self, key: String, value: String, ) -> Result<(), RequestError>
Insert a new custom header and value pair into the HashMap
.
Sourcepub fn custom_entries(&self) -> &HashMap<String, String>
pub fn custom_entries(&self) -> &HashMap<String, String>
Returns the custom header HashMap
.