Struct Headers

Source
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-Types 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

Source

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());
Source

pub fn content_length(&self) -> u32

Returns the content length of the body.

Source

pub fn chunked(&self) -> bool

Returns true if the transfer encoding is chunked.

Source

pub fn expect(&self) -> bool

Returns true if the client is expecting the code 100.

Source

pub fn accept(&self) -> MediaType

Returns the Accept header MediaType.

Source

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");
Source

pub fn set_accept(&mut self, media_type: MediaType)

Accept header setter.

Source

pub fn insert_custom_header( &mut self, key: String, value: String, ) -> Result<(), RequestError>

Insert a new custom header and value pair into the HashMap.

Source

pub fn custom_entries(&self) -> &HashMap<String, String>

Returns the custom header HashMap.

Trait Implementations§

Source§

impl Debug for Headers

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Headers

Source§

fn default() -> Self

By default Requests are created with no headers.

Source§

impl PartialEq for Headers

Source§

fn eq(&self, other: &Headers) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Headers

Source§

impl StructuralPartialEq for Headers

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.