1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 11:02:08 +01:00

improve servicerequest docs

This commit is contained in:
Rob Ede 2022-03-07 16:48:04 +00:00
parent 03456b8a33
commit 87f627cd5d
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 26 additions and 28 deletions

View File

@ -144,7 +144,7 @@ impl ResponseBuilder {
self self
} }
/// Set connection type to Upgrade /// Set connection type to `Upgrade`.
#[inline] #[inline]
pub fn upgrade<V>(&mut self, value: V) -> &mut Self pub fn upgrade<V>(&mut self, value: V) -> &mut Self
where where
@ -161,7 +161,7 @@ impl ResponseBuilder {
self self
} }
/// Force close connection, even if it is marked as keep-alive /// Force-close connection, even if it is marked as keep-alive.
#[inline] #[inline]
pub fn force_close(&mut self) -> &mut Self { pub fn force_close(&mut self) -> &mut Self {
if let Some(parts) = self.inner() { if let Some(parts) = self.inner() {

View File

@ -78,18 +78,18 @@ pub struct ServiceRequest {
} }
impl ServiceRequest { impl ServiceRequest {
/// Construct service request /// Construct `ServiceRequest` from parts.
pub(crate) fn new(req: HttpRequest, payload: Payload) -> Self { pub(crate) fn new(req: HttpRequest, payload: Payload) -> Self {
Self { req, payload } Self { req, payload }
} }
/// Deconstruct request into parts /// Deconstruct `ServiceRequest` into inner parts.
#[inline] #[inline]
pub fn into_parts(self) -> (HttpRequest, Payload) { pub fn into_parts(self) -> (HttpRequest, Payload) {
(self.req, self.payload) (self.req, self.payload)
} }
/// Get mutable access to inner `HttpRequest` and `Payload` /// Returns mutable accessors to inner parts.
#[inline] #[inline]
pub fn parts_mut(&mut self) -> (&mut HttpRequest, &mut Payload) { pub fn parts_mut(&mut self) -> (&mut HttpRequest, &mut Payload) {
(&mut self.req, &mut self.payload) (&mut self.req, &mut self.payload)
@ -105,9 +105,7 @@ impl ServiceRequest {
Self { req, payload } Self { req, payload }
} }
/// Construct request from request. /// Construct `ServiceRequest` with no payload from given `HttpRequest`.
///
/// The returned `ServiceRequest` would have no payload.
#[inline] #[inline]
pub fn from_request(req: HttpRequest) -> Self { pub fn from_request(req: HttpRequest) -> Self {
ServiceRequest { ServiceRequest {
@ -116,63 +114,63 @@ impl ServiceRequest {
} }
} }
/// Create service response /// Create `ServiceResponse` from this request and given response.
#[inline] #[inline]
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> { pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
let res = HttpResponse::from(res.into()); let res = HttpResponse::from(res.into());
ServiceResponse::new(self.req, res) ServiceResponse::new(self.req, res)
} }
/// Create service response for error /// Create `ServiceResponse` from this request and given error.
#[inline] #[inline]
pub fn error_response<E: Into<Error>>(self, err: E) -> ServiceResponse { pub fn error_response<E: Into<Error>>(self, err: E) -> ServiceResponse {
let res = HttpResponse::from_error(err.into()); let res = HttpResponse::from_error(err.into());
ServiceResponse::new(self.req, res) ServiceResponse::new(self.req, res)
} }
/// This method returns reference to the request head /// Returns a reference to the request head.
#[inline] #[inline]
pub fn head(&self) -> &RequestHead { pub fn head(&self) -> &RequestHead {
self.req.head() self.req.head()
} }
/// This method returns reference to the request head /// Returns a mutable reference to the request head.
#[inline] #[inline]
pub fn head_mut(&mut self) -> &mut RequestHead { pub fn head_mut(&mut self) -> &mut RequestHead {
self.req.head_mut() self.req.head_mut()
} }
/// Request's uri. /// Returns the request URI.
#[inline] #[inline]
pub fn uri(&self) -> &Uri { pub fn uri(&self) -> &Uri {
&self.head().uri &self.head().uri
} }
/// Read the Request method. /// Returns the request method.
#[inline] #[inline]
pub fn method(&self) -> &Method { pub fn method(&self) -> &Method {
&self.head().method &self.head().method
} }
/// Read the Request Version. /// Returns the request version.
#[inline] #[inline]
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
self.head().version self.head().version
} }
/// Returns a reference to request headers.
#[inline] #[inline]
/// Returns request's headers.
pub fn headers(&self) -> &HeaderMap { pub fn headers(&self) -> &HeaderMap {
&self.head().headers &self.head().headers
} }
/// Returns a mutable reference to request headers.
#[inline] #[inline]
/// Returns mutable request's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap { pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.head_mut().headers &mut self.head_mut().headers
} }
/// The target path of this Request. /// Returns request path.
#[inline] #[inline]
pub fn path(&self) -> &str { pub fn path(&self) -> &str {
self.head().uri.path() self.head().uri.path()
@ -184,7 +182,7 @@ impl ServiceRequest {
self.req.query_string() self.req.query_string()
} }
/// Peer socket address. /// Returns peer's socket address.
/// ///
/// Peer address is the directly connected peer's socket address. If a proxy is used in front of /// Peer address is the directly connected peer's socket address. If a proxy is used in front of
/// the Actix Web server, then it would be address of this proxy. /// the Actix Web server, then it would be address of this proxy.
@ -197,24 +195,23 @@ impl ServiceRequest {
self.head().peer_addr self.head().peer_addr
} }
/// Get *ConnectionInfo* for the current request. /// Returns a reference to connection info.
#[inline] #[inline]
pub fn connection_info(&self) -> Ref<'_, ConnectionInfo> { pub fn connection_info(&self) -> Ref<'_, ConnectionInfo> {
self.req.connection_info() self.req.connection_info()
} }
/// Returns a reference to the Path parameters. /// Returns reference to the Path parameters.
/// ///
/// Params is a container for URL parameters. /// Params is a container for URL parameters. A variable segment is specified in the form
/// A variable segment is specified in the form `{identifier}`, /// `{identifier}`, where the identifier can be used later in a request handler to access the
/// where the identifier can be used later in a request handler to /// matched value for that segment.
/// access the matched value for that segment.
#[inline] #[inline]
pub fn match_info(&self) -> &Path<Url> { pub fn match_info(&self) -> &Path<Url> {
self.req.match_info() self.req.match_info()
} }
/// Returns a mutable reference to the Path parameters. /// Returns a mutable reference to the path match information.
#[inline] #[inline]
pub fn match_info_mut(&mut self) -> &mut Path<Url> { pub fn match_info_mut(&mut self) -> &mut Path<Url> {
self.req.match_info_mut() self.req.match_info_mut()
@ -232,13 +229,13 @@ impl ServiceRequest {
self.req.match_pattern() self.req.match_pattern()
} }
/// Get a reference to a `ResourceMap` of current application. /// Returns a reference to the application's resource map.
#[inline] #[inline]
pub fn resource_map(&self) -> &ResourceMap { pub fn resource_map(&self) -> &ResourceMap {
self.req.resource_map() self.req.resource_map()
} }
/// Service configuration /// Returns a reference to the application's configuration.
#[inline] #[inline]
pub fn app_config(&self) -> &AppConfig { pub fn app_config(&self) -> &AppConfig {
self.req.app_config() self.req.app_config()
@ -262,6 +259,7 @@ impl ServiceRequest {
self.req.conn_data() self.req.conn_data()
} }
/// Return request cookies.
#[cfg(feature = "cookies")] #[cfg(feature = "cookies")]
#[inline] #[inline]
pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> { pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {