Skip to main content

WebRequest

Struct WebRequest 

Source
pub struct WebRequest<St = ()> { /* private fields */ }
Expand description

An service http request

WebRequest allows mutable access to request’s internal structures

Implementations§

Source§

impl<St> WebRequest<St>

Source

pub fn error_response<AppSt, E>(self, st: &AppSt, err: &E) -> WebResponse
where AppSt: State, E: WebResponseError<AppSt, AppSt::Error>,

Create web response for error

Source§

impl WebRequest<()>

Source

pub fn from_request(req: HttpRequest) -> Self

Construct request from request.

Source§

impl<St> WebRequest<St>

Source

pub fn into_parts(self) -> (HttpRequest, Payload, St)

Deconstruct request into parts

Source

pub fn from_parts(req: HttpRequest, payload: Payload, state: St) -> Self

Construct request from parts.

Source

pub fn into_response<R: Into<Response>>(self, res: R) -> WebResponse

Create web response

Source

pub fn io(&self) -> Option<&IoRef>

Io reference for current connection

Source

pub fn head(&self) -> &RequestHead

This method returns reference to the request head

Source

pub fn head_mut(&mut self) -> &mut RequestHead

This method returns mutable reference to the request head

Source

pub fn uri(&self) -> &Url

Request’s uri.

Source

pub fn method(&self) -> &Method

Read the Request method.

Source

pub fn version(&self) -> Version

Read the Request Version.

Source

pub fn headers(&self) -> &HeaderMap

Returns request’s headers.

Source

pub fn headers_mut(&mut self) -> &mut HeaderMap

Returns mutable request’s headers.

Source

pub fn path(&self) -> &str

The target path of this Request.

Source

pub fn query_string(&self) -> &str

The query string in the URL.

E.g., id=10

Source

pub fn peer_addr(&self) -> Option<SocketAddr>

Peer socket address

Peer address is actual socket address, if proxy is used in front of ntex http server, then peer address would be address of this proxy.

To get client connection information ConnectionInfo should be used.

Source

pub fn take_payload(&mut self) -> Payload

Take request’s payload.

The payload is replaced with an empty one, so subsequent calls return an empty payload.

Source

pub fn set_payload(&mut self, payload: Payload)

Set request payload.

Source

pub fn connection_info(&self) -> Ref<'_, ConnectionInfo>

Get ConnectionInfo for the current request.

Source

pub fn match_info(&self) -> &Path<Url>

Get a reference to the Path parameters.

Params is a container for url parameters. A variable segment is specified in the form {identifier}, where the identifier can be used later in a request handler to access the matched value for that segment.

Source

pub fn match_info_mut(&mut self) -> &mut Path<Url>

Get a mutable reference to the Path parameters.

Source

pub fn resource_map(&self) -> &ResourceMap

Get a reference to a ResourceMap of current application.

Source

pub fn app_config(&self) -> &WebAppConfig

Service configuration

Source

pub fn app_state<T: 'static>(&self) -> Option<&T>

Get an application state.

Source

pub fn extensions(&self) -> Ref<'_, Extensions>

Request extensions

Source

pub fn extensions_mut(&self) -> RefMut<'_, Extensions>

Mutable reference to a the request’s extensions

Source

pub fn st(&self) -> &St

Get request state ref.

Source

pub fn st_mut(&mut self) -> &mut St

Get request state mut ref

Source

pub fn map_state<F, NewSt>(self, f: F) -> WebRequest<NewSt>
where F: FnOnce(St) -> NewSt,

Transform the request state into a new type.

This method consumes the request and returns request with different state a WebRequest<NewSt>. The HTTP request and payload are preserved. It is useful in application and resource filters that add request-specific data before passing the request to the next service.

Application state is not affected; only the state stored in this WebRequest is replaced.

use std::convert::Infallible;
use ntex::web::{self, WebRequest};

async fn index(_: &(), user_id: usize) -> String {
    format!("User {user_id}")
}

web::App::default()
    .filter(async |req: WebRequest<()>| {
        Ok::<_, Infallible>(req.map_state(|()| 42usize))
    })
    .service(web::resource("/").to_with_state(index));

Trait Implementations§

Source§

impl<St> Debug for WebRequest<St>

Source§

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

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

impl<St> HttpMessage for WebRequest<St>

Source§

fn message_headers(&self) -> &HeaderMap

Returns Request’s headers.

Source§

fn message_extensions(&self) -> Ref<'_, Extensions>

Request extensions

Source§

fn message_extensions_mut(&self) -> RefMut<'_, Extensions>

Mutable reference to a the request’s extensions

Source§

fn content_type(&self) -> &str

Returns the media type from the Content-Type header. Read more
Source§

fn encoding(&self) -> Result<&'static Encoding, ContentTypeError>

Returns the character encoding declared by Content-Type. Read more
Source§

fn mime_type(&self) -> Result<Option<Mime>, ContentTypeError>

Parses the Content-Type header as a MIME type. Read more
Source§

fn chunked(&self) -> Result<bool, DecodeError>

Returns whether Transfer-Encoding contains chunked. Read more
Source§

fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, ParseError>

Parses and caches cookies from the Cookie headers. Read more
Source§

fn cookie(&self, name: &str) -> Option<Cookie<'static>>

Returns the first request cookie with the given name.
Source§

impl<St> Resource<Url> for WebRequest<St>

Source§

fn path(&self) -> &str

Path to match.
Source§

fn resource_path(&mut self) -> &mut Path<Url>

Path state that stores match results.
Source§

impl<S, St, In> Service<St, WebRequest<In>> for WebMiddleware<S, St>
where S: Service<St, WebRequest<In>, Res = WebResponse>, S::Error: WebResponseError<St, St::Error>, St: State,

Source§

type Res = WebResponse

Response produced by the service.
Source§

type Error = WebError<St, <St as State>::Error>

Error produced while checking readiness or processing a request.
Source§

async fn call( &self, req: WebRequest<In>, ctx: Ctx<'_, Self, St>, ) -> Result<Self::Res, Self::Error>

Processes a request and asynchronously returns the response. Read more
Source§

async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), Self::Error>

Waits until the service is ready to process a request. Read more
Source§

async fn shutdown(&self, ctx: Ctx<'_, Self, St>)

Shuts down the service. Read more
Source§

fn map<F, Res>(self, f: F) -> ServiceChain<Map<F, Self, Res>, St, Req>
where Self: Sized, F: Fn(Self::Res) -> Res,

Maps this service’s output to a different type, returning a new service. Read more
Source§

fn map_err<F, E>(self, f: F) -> ServiceChain<MapErr<F, Self, E>, St, Req>
where Self: Sized, F: Fn(Self::Error) -> E,

Maps this service’s error to a different type, returning a new service. Read more
Source§

fn and_then<Next, F>(self, f: F) -> ServiceChain<AndThen<Self, Next>, St, Req>
where Self: Sized, Next: Service<St, Self::Res, Error = Self::Error>, F: IntoService<Next, St, Self::Res>,

Calls another service after this service completes successfully. Read more
Source§

fn pipeline(self, st: St) -> Pipeline<Req, Self::Res, Self::Error>
where Self: Sized + 'static, St: 'static, Req: 'static,

Wraps this service and its state in a Pipeline.
Source§

impl<St: State, In> Service<St, WebRequest<In>> for Filter<St, In>

Source§

type Res = WebRequest<In>

Response produced by the service.
Source§

type Error = WebError<St, <St as State>::Error>

Error produced while checking readiness or processing a request.
Source§

async fn call( &self, req: WebRequest<In>, _: Ctx<'_, Self, St>, ) -> Result<Self::Res, Self::Error>

Processes a request and asynchronously returns the response. Read more
Source§

async fn ready(&self, ctx: Ctx<'_, Self, St>) -> Result<(), Self::Error>

Waits until the service is ready to process a request. Read more
Source§

async fn shutdown(&self, ctx: Ctx<'_, Self, St>)

Shuts down the service. Read more
Source§

fn map<F, Res>(self, f: F) -> ServiceChain<Map<F, Self, Res>, St, Req>
where Self: Sized, F: Fn(Self::Res) -> Res,

Maps this service’s output to a different type, returning a new service. Read more
Source§

fn map_err<F, E>(self, f: F) -> ServiceChain<MapErr<F, Self, E>, St, Req>
where Self: Sized, F: Fn(Self::Error) -> E,

Maps this service’s error to a different type, returning a new service. Read more
Source§

fn and_then<Next, F>(self, f: F) -> ServiceChain<AndThen<Self, Next>, St, Req>
where Self: Sized, Next: Service<St, Self::Res, Error = Self::Error>, F: IntoService<Next, St, Self::Res>,

Calls another service after this service completes successfully. Read more
Source§

fn pipeline(self, st: St) -> Pipeline<Req, Self::Res, Self::Error>
where Self: Sized + 'static, St: 'static, Req: 'static,

Wraps this service and its state in a Pipeline.
Source§

impl<St: State, In: 'static> ServiceFactory<St, WebRequest<In>> for Route<St, In>

Source§

type Res = WebResponse

Response produced by the created services.
Source§

type Error = WebError<St, <St as State>::Error>

Error produced by the created services.
Source§

type Service = RouteService<St, In>

The type of Service produced by this factory.
Source§

type InitError = Failure

Error that can occur while constructing a service.
Source§

async fn create(&self, _: &St) -> Result<Self::Service, Self::InitError>

Asynchronously creates a service using the supplied state.
Source§

async fn pipeline( &self, st: St, ) -> Result<Pipeline<Req, Self::Res, Self::Error>, Self::InitError>
where Self: 'static, St: 'static, Req: 'static,

Creates a service and wraps it with its state in a Pipeline.
Source§

fn map<F, Res>( self, f: F, ) -> ServiceChainFactory<MapFactory<F, Self, Res>, St, Req>
where Self: Sized, F: Fn(Self::Res) -> Res + Clone,

Returns a factory whose services map responses to a different type.
Source§

fn map_err<F, E>( self, f: F, ) -> ServiceChainFactory<MapErrFactory<F, Self, E>, St, Req>
where Self: Sized, F: Fn(Self::Error) -> E + Clone,

Returns a factory whose services map errors to a different type.
Source§

fn map_init_err<F, E>( self, f: F, ) -> ServiceChainFactory<MapInitErr<F, Self, E>, St, Req>
where Self: Sized, F: Fn(Self::InitError) -> E + Clone,

Maps this factory’s initialization error to a different error, returning a new service factory.
Source§

fn and_then<U, F>( self, f: F, ) -> ServiceChainFactory<AndThenFactory<Self, U>, St, Req>
where Self: Sized, U: ServiceFactory<St, Self::Res, Error = Self::Error, InitError = Self::InitError>, F: IntoServiceFactory<U, St, Self::Res>,

Chains another factory after this factory’s services. Read more
Source§

fn boxed( self, ) -> BoxServiceFactory<St, Req, Self::Res, Self::Error, Self::InitError>
where St: 'static, Req: 'static, Self: Sized + 'static,

Creates a boxed service factory.
Source§

impl<St: State, In> ServiceFactory<St, WebRequest<In>> for Filter<St, In>

Source§

type Res = WebRequest<In>

Response produced by the created services.
Source§

type Error = WebError<St, <St as State>::Error>

Error produced by the created services.
Source§

type Service = Filter<St, In>

The type of Service produced by this factory.
Source§

type InitError = Failure

Error that can occur while constructing a service.
Source§

async fn create(&self, _: &St) -> Result<Self::Service, Self::InitError>

Asynchronously creates a service using the supplied state.
Source§

async fn pipeline( &self, st: St, ) -> Result<Pipeline<Req, Self::Res, Self::Error>, Self::InitError>
where Self: 'static, St: 'static, Req: 'static,

Creates a service and wraps it with its state in a Pipeline.
Source§

fn map<F, Res>( self, f: F, ) -> ServiceChainFactory<MapFactory<F, Self, Res>, St, Req>
where Self: Sized, F: Fn(Self::Res) -> Res + Clone,

Returns a factory whose services map responses to a different type.
Source§

fn map_err<F, E>( self, f: F, ) -> ServiceChainFactory<MapErrFactory<F, Self, E>, St, Req>
where Self: Sized, F: Fn(Self::Error) -> E + Clone,

Returns a factory whose services map errors to a different type.
Source§

fn map_init_err<F, E>( self, f: F, ) -> ServiceChainFactory<MapInitErr<F, Self, E>, St, Req>
where Self: Sized, F: Fn(Self::InitError) -> E + Clone,

Maps this factory’s initialization error to a different error, returning a new service factory.
Source§

fn and_then<U, F>( self, f: F, ) -> ServiceChainFactory<AndThenFactory<Self, U>, St, Req>
where Self: Sized, U: ServiceFactory<St, Self::Res, Error = Self::Error, InitError = Self::InitError>, F: IntoServiceFactory<U, St, Self::Res>,

Chains another factory after this factory’s services. Read more
Source§

fn boxed( self, ) -> BoxServiceFactory<St, Req, Self::Res, Self::Error, Self::InitError>
where St: 'static, Req: 'static, Self: Sized + 'static,

Creates a boxed service factory.

Auto Trait Implementations§

§

impl<St = ()> !RefUnwindSafe for WebRequest<St>

§

impl<St = ()> !Send for WebRequest<St>

§

impl<St = ()> !Sync for WebRequest<St>

§

impl<St = ()> !UnwindSafe for WebRequest<St>

§

impl<St> Freeze for WebRequest<St>
where St: Freeze,

§

impl<St> Unpin for WebRequest<St>
where St: Unpin,

§

impl<St> UnsafeUnpin for WebRequest<St>
where St: UnsafeUnpin,

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.