pub struct Request<'a> { /* private fields */ }Expand description
A borrowed view of one HTTP request, with a body limit.
§Examples
use davidrs::http::Request;
use serde::Deserialize;
#[derive(Deserialize)]
struct Page {
page: u32,
}
let native = lambda_http::http::Request::builder()
.uri("https://example.com/orders?page=2")
.body(lambda_http::Body::Empty)
.unwrap();
let page: Page = Request::new(&native).query().unwrap();
assert_eq!(page.page, 2);Implementations§
Source§impl<'a> Request<'a>
impl<'a> Request<'a>
Sourcepub fn new(inner: &'a Request) -> Self
pub fn new(inner: &'a Request) -> Self
Wraps a native request with DEFAULT_BODY_LIMIT.
Sourcepub fn with_body_limit(self, limit: usize) -> Self
pub fn with_body_limit(self, limit: usize) -> Self
Sets the body limit every bounded read checks.
Sourcepub fn native(&self) -> &'a Request
pub fn native(&self) -> &'a Request
The native request, for anything this view does not expose.
Sourcepub fn header(&self, name: &str) -> Option<&'a str>
pub fn header(&self, name: &str) -> Option<&'a str>
One header as text, if present and valid UTF-8.
Sourcepub fn raw_body(&self) -> &'a [u8] ⓘ
pub fn raw_body(&self) -> &'a [u8] ⓘ
The exact body bytes, for signatures and hashing; empty when there is no body.
This read is not bounded: the pipeline has already checked the body limit before any decoder runs.
Sourcepub fn query_string(&self) -> &'a str
pub fn query_string(&self) -> &'a str
The raw query string, without the leading ?.
Sourcepub fn query_pairs(&self) -> Vec<(String, String)>
pub fn query_pairs(&self) -> Vec<(String, String)>
The query parameters the gateway parsed, as (name, value) pairs with
repetitions kept.
Use it for lists that arrive as ?tag=a&tag=b.
Sourcepub fn path_parameters(&self) -> HashMap<String, String>
pub fn path_parameters(&self) -> HashMap<String, String>
The path parameters the gateway matched, by name.
Sourcepub fn path<T: DeserializeOwned>(&self) -> Result<T, Failure>
pub fn path<T: DeserializeOwned>(&self) -> Result<T, Failure>
Deserializes the path parameters into T.
§Errors
Returns a 400 (codes::INVALID_PATH) when a parameter is missing
or has the wrong shape.
Sourcepub fn query<T: DeserializeOwned>(&self) -> Result<T, Failure>
pub fn query<T: DeserializeOwned>(&self) -> Result<T, Failure>
Deserializes the query string into T.
§Errors
Returns a 400 (codes::INVALID_QUERY) when a parameter is missing
or has the wrong shape.
Sourcepub fn source_ip(&self) -> String
pub fn source_ip(&self) -> String
The caller’s IP address as the gateway saw it, or "unknown".
Reads sourceIp from an HTTP API or, with the apigw-rest
feature, a REST API request context. A request with no context,
an empty address or another gateway flavour gives "unknown".
Forwarded headers are otherwise ignored on purpose: trusting them needs
a proxy policy of the application’s own, and without one any caller
could choose its own rate-limit key.
With the alb feature, an Application Load Balancer request, whose
context has no address, gives the last address of X-Forwarded-For:
the one the load balancer appends in its default append mode. With
multi-value headers the header can arrive as several values, one per
line the client sent, and the address is the last one of the last
value, never an earlier line a client could choose. With the
preserve or remove modes that address is not the caller’s, so
keep append wherever this address is a rate-limit key.
Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T, Failure>
pub fn json<T: DeserializeOwned>(&self) -> Result<T, Failure>
Deserializes a JSON body, bounded by the body limit.
§Errors
Returns a 413 when the body exceeds the limit, a 415 when it
declares a media type other than JSON, and a 400 when it is empty or
malformed.
Sourcepub fn json_bounded<T: DeserializeOwned>(
&self,
limit: usize,
) -> Result<T, Failure>
pub fn json_bounded<T: DeserializeOwned>( &self, limit: usize, ) -> Result<T, Failure>
Deserializes a JSON body under a tighter byte limit.
The effective limit is the smaller of limit and the request’s own,
and it is checked before parsing, so an oversized payload never
reaches serde_json.
§Errors
Returns a 413 when the body exceeds the limit, a 415 when it
declares a media type other than JSON, and a 400 when it is empty or
malformed.
Sourcepub fn form<T: DeserializeOwned>(&self) -> Result<T, Failure>
pub fn form<T: DeserializeOwned>(&self) -> Result<T, Failure>
Deserializes an application/x-www-form-urlencoded body, bounded by
the body limit: what an HTML form, a webhook or an OAuth client posts.
Names and values are percent-decoded, and + reads as a space. A
missing Content-Type is accepted, as for JSON. Each field is read
once; for a name that repeats, parse Request::raw_body yourself.
§Errors
Returns a 413 when the body exceeds the limit, a 415 when it
declares another media type, and a 400 when it does not deserialize
into T.
§Examples
use davidrs::http::{Body, Request};
#[derive(serde::Deserialize)]
struct Signup {
email: String,
plan: Option<String>,
}
let native = lambda_http::http::Request::builder()
.method("POST")
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::Text("email=ada%40example.com&plan=team+plus".to_owned()))
.unwrap();
let signup: Signup = Request::new(&native).form().unwrap();
assert_eq!(signup.email, "ada@example.com");
assert_eq!(signup.plan.as_deref(), Some("team plus"));Sourcepub fn check_body_limit(&self) -> Result<(), Failure>
pub fn check_body_limit(&self) -> Result<(), Failure>
Checks the body against the limit without reading it.
The pipeline calls this before any decoder, so a decoder that reads
Request::raw_body is bounded too.
§Errors
Returns a 413 (codes::BODY_TOO_LARGE) when the body is larger
than the limit; the limit is in the failure’s detail.
Sourcepub fn json_text(&self) -> Result<&'a str, Failure>
pub fn json_text(&self) -> Result<&'a str, Failure>
The body as strict UTF-8 text, for an application’s own JSON decoder.
Size and media type are checked as in Request::json; the caller
still validates the JSON syntax and the input contract.
§Errors
Returns a 413 when the body exceeds the limit, a 415 when it
declares a media type other than JSON, and a 400 when it is not
UTF-8.
Sourcepub fn media_type(&self) -> Option<Cow<'a, str>>
pub fn media_type(&self) -> Option<Cow<'a, str>>
The Content-Type, lowercased and without parameters.
Source§impl Request<'_>
impl Request<'_>
Sourcepub fn validated_json<T>(&self) -> Result<T, Failure>
pub fn validated_json<T>(&self) -> Result<T, Failure>
Deserializes a JSON body and validates it with Garde.
Validates the decoded value only. For rules that need a context, decode
with Request::json and call validate_with in the handler.
§Errors
Returns the failures of Request::json, or a 400
(codes::INVALID_BODY) when a constraint fails.
Trait Implementations§
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Request<'a>
impl<'a> !UnwindSafe for Request<'a>
impl<'a> Freeze for Request<'a>
impl<'a> Send for Request<'a>
impl<'a> Sync for Request<'a>
impl<'a> Unpin for Request<'a>
impl<'a> UnsafeUnpin for Request<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more