Struct hawk::Request

source ·
pub struct Request<'a> { /* private fields */ }
Expand description

Request represents a single HTTP request.

The structure is created using (RequestBuilder)[struct.RequestBuilder.html]. Most uses of this library will hold several of the fields in this structure fixed. Cloning the structure with these fields applied is a convenient way to avoid repeating those fields. Most fields are references, since in common use the values already exist and will outlive the request.

A request can be used on the client, to generate a header or a bewit, or on the server, to validate the same.

§Examples

use hawk::RequestBuilder;
let bldr = RequestBuilder::new("GET", "mysite.com", 443, "/");
let request1 = bldr.clone().method("POST").path("/api/user").request();
let request2 = bldr.path("/api/users").request();

See the documentation in the crate root for examples of creating and validating headers.

Implementations§

source§

impl<'a> Request<'a>

source

pub fn make_header(&self, credentials: &Credentials) -> Result<Header>

Create a new Header for this request, inventing a new nonce and setting the timestamp to the current time.

source

pub fn make_header_full<S>( &self, credentials: &Credentials, ts: SystemTime, nonce: S ) -> Result<Header>
where S: Into<String>,

Similar to make_header, but allowing specification of the timestamp and nonce.

source

pub fn make_bewit( &self, credentials: &'a Credentials, exp: SystemTime ) -> Result<Bewit<'a>>

Make a “bewit” that can be attached to a URL to authenticate GET access.

The ttl gives the time for which this bewit is valid, starting now.

source

pub fn make_bewit_with_ttl( &self, credentials: &'a Credentials, ttl: Duration ) -> Result<Bewit<'a>>

Variant of make_bewit that takes a Duration (starting from now) instead of a SystemTime, provided for convenience.

source

pub fn validate_header( &self, header: &Header, key: &Key, ts_skew: Duration ) -> bool

Validate the given header. This validates that the mac field matches that calculated using the other header fields and the given request information.

The header’s timestamp is verified to be within ts_skew of the current time. If any of the required header fields are missing, the method will return false.

It is up to the caller to examine the header’s id field and supply the corresponding key.

If desired, it is up to the caller to validate that nonce has not been used before.

If a hash has been supplied, then the header must contain a matching hash. Note that this hash must be calculated based on the request body, not copied from the request header!

source

pub fn validate_bewit(&self, bewit: &Bewit<'_>, key: &Key) -> bool

Validate the given bewit matches this request.

It is up to the caller to consult the Bewit’s id and look up the corresponding key.

Nonces and hashes do not apply when using bewits.

source

pub fn make_response_builder( &'a self, req_header: &'a Header ) -> ResponseBuilder<'a>

Get a Response instance for a response to this request. This is a convenience wrapper around Response::from_request_header.

Trait Implementations§

source§

impl<'a> Clone for Request<'a>

source§

fn clone(&self) -> Request<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for Request<'a>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Request<'a>

§

impl<'a> Send for Request<'a>

§

impl<'a> Sync for Request<'a>

§

impl<'a> Unpin for Request<'a>

§

impl<'a> UnwindSafe for Request<'a>

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.