mirror of https://github.com/fafhrd91/actix-web
rename req_data back to extensions
This commit is contained in:
parent
bb9d0e9cb9
commit
a122a40397
|
@ -1,8 +1,6 @@
|
||||||
use std::{convert::Infallible, io};
|
use std::{convert::Infallible, io};
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{body::EitherBody, HttpService, Request, Response, StatusCode};
|
||||||
body::EitherBody, HttpMessage as _, HttpService, Request, Response, StatusCode,
|
|
||||||
};
|
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
|
|
|
@ -25,10 +25,10 @@ pub trait HttpMessage: Sized {
|
||||||
/// Message payload stream
|
/// Message payload stream
|
||||||
fn take_payload(&mut self) -> Payload<Self::Stream>;
|
fn take_payload(&mut self) -> Payload<Self::Stream>;
|
||||||
|
|
||||||
/// Returns a reference to the request-local data container.
|
/// Returns a reference to the request-local data/extensions container.
|
||||||
fn extensions(&self) -> Ref<'_, Extensions>;
|
fn extensions(&self) -> Ref<'_, Extensions>;
|
||||||
|
|
||||||
/// Returns a mutable reference to the request-local data container.
|
/// Returns a mutable reference to the request-local data/extensions container.
|
||||||
fn extensions_mut(&self) -> RefMut<'_, Extensions>;
|
fn extensions_mut(&self) -> RefMut<'_, Extensions>;
|
||||||
|
|
||||||
/// Get a header.
|
/// Get a header.
|
||||||
|
|
|
@ -128,6 +128,18 @@ impl<B> Response<B> {
|
||||||
self.head.keep_alive()
|
self.head.keep_alive()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to the request-local data/extensions container.
|
||||||
|
#[inline]
|
||||||
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
||||||
|
self.extensions.borrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable reference to the request-local data/extensions container.
|
||||||
|
#[inline]
|
||||||
|
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
||||||
|
self.extensions.borrow_mut()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a reference to the body of this response.
|
/// Returns a reference to the body of this response.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn body(&self) -> &B {
|
pub fn body(&self) -> &B {
|
||||||
|
|
|
@ -18,7 +18,7 @@ use crate::{
|
||||||
AppServiceFactory, BoxedHttpService, BoxedHttpServiceFactory, ServiceRequest,
|
AppServiceFactory, BoxedHttpService, BoxedHttpServiceFactory, ServiceRequest,
|
||||||
ServiceResponse,
|
ServiceResponse,
|
||||||
},
|
},
|
||||||
Error, HttpMessage, HttpResponse,
|
Error, HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
type Guards = Vec<Box<dyn Guard>>;
|
type Guards = Vec<Box<dyn Guard>>;
|
||||||
|
|
10
src/guard.rs
10
src/guard.rs
|
@ -54,7 +54,7 @@ use std::{
|
||||||
|
|
||||||
use actix_http::{header, uri::Uri, Extensions, Method as HttpMethod, RequestHead};
|
use actix_http::{header, uri::Uri, Extensions, Method as HttpMethod, RequestHead};
|
||||||
|
|
||||||
use crate::{http::header::Header, service::ServiceRequest};
|
use crate::{http::header::Header, service::ServiceRequest, HttpMessage as _};
|
||||||
|
|
||||||
/// Provides access to request parts that are useful during routing.
|
/// Provides access to request parts that are useful during routing.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -71,14 +71,14 @@ impl<'a> GuardContext<'a> {
|
||||||
|
|
||||||
/// Returns reference to the request-local data container.
|
/// Returns reference to the request-local data container.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn req_data(&self) -> Ref<'a, Extensions> {
|
pub fn extensions(&self) -> Ref<'a, Extensions> {
|
||||||
self.req.req_data()
|
self.req.extensions()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns mutable reference to the request-local data container.
|
/// Returns mutable reference to the request-local data container.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn req_data_mut(&self) -> RefMut<'a, Extensions> {
|
pub fn extensions_mut(&self) -> RefMut<'a, Extensions> {
|
||||||
self.req.req_data_mut()
|
self.req.extensions_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts a typed header from the request.
|
/// Extracts a typed header from the request.
|
||||||
|
|
|
@ -2,7 +2,10 @@ use std::{any::type_name, ops::Deref};
|
||||||
|
|
||||||
use actix_utils::future::{err, ok, Ready};
|
use actix_utils::future::{err, ok, Ready};
|
||||||
|
|
||||||
use crate::{dev::Payload, error::ErrorInternalServerError, Error, FromRequest, HttpRequest};
|
use crate::{
|
||||||
|
dev::Payload, error::ErrorInternalServerError, Error, FromRequest, HttpMessage as _,
|
||||||
|
HttpRequest,
|
||||||
|
};
|
||||||
|
|
||||||
/// Request-local data extractor.
|
/// Request-local data extractor.
|
||||||
///
|
///
|
||||||
|
@ -23,7 +26,7 @@ use crate::{dev::Payload, error::ErrorInternalServerError, Error, FromRequest, H
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # use actix_web::{web, HttpResponse, HttpRequest, Responder};
|
/// # use actix_web::{web, HttpResponse, HttpRequest, Responder, HttpMessage as _};
|
||||||
///
|
///
|
||||||
/// #[derive(Debug, Clone, PartialEq)]
|
/// #[derive(Debug, Clone, PartialEq)]
|
||||||
/// struct FlagFromMiddleware(String);
|
/// struct FlagFromMiddleware(String);
|
||||||
|
@ -35,7 +38,7 @@ use crate::{dev::Payload, error::ErrorInternalServerError, Error, FromRequest, H
|
||||||
/// ) -> impl Responder {
|
/// ) -> impl Responder {
|
||||||
/// // use an option extractor if middleware is not guaranteed to add this type of req data
|
/// // use an option extractor if middleware is not guaranteed to add this type of req data
|
||||||
/// if let Some(flag) = opt_flag {
|
/// if let Some(flag) = opt_flag {
|
||||||
/// assert_eq!(&flag.into_inner(), req.req_data().get::<FlagFromMiddleware>().unwrap());
|
/// assert_eq!(&flag.into_inner(), req.extensions().get::<FlagFromMiddleware>().unwrap());
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// HttpResponse::Ok()
|
/// HttpResponse::Ok()
|
||||||
|
@ -67,7 +70,7 @@ impl<T: Clone + 'static> FromRequest for ReqData<T> {
|
||||||
type Future = Ready<Result<Self, Error>>;
|
type Future = Ready<Result<Self, Error>>;
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
if let Some(st) = req.req_data().get::<T>() {
|
if let Some(st) = req.extensions().get::<T>() {
|
||||||
ok(ReqData(st.clone()))
|
ok(ReqData(st.clone()))
|
||||||
} else {
|
} else {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
|
|
|
@ -22,7 +22,7 @@ use {
|
||||||
cookie::Cookie,
|
cookie::Cookie,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{error::Error, HttpMessage, HttpRequest, HttpResponseBuilder, Responder};
|
use crate::{error::Error, HttpRequest, HttpResponseBuilder, Responder};
|
||||||
|
|
||||||
/// An outgoing response.
|
/// An outgoing response.
|
||||||
pub struct HttpResponse<B = BoxBody> {
|
pub struct HttpResponse<B = BoxBody> {
|
||||||
|
@ -180,22 +180,25 @@ impl<B> HttpResponse<B> {
|
||||||
self.res.extensions_mut()
|
self.res.extensions_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get body of this response
|
/// Returns a reference to this response's body.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn body(&self) -> &B {
|
pub fn body(&self) -> &B {
|
||||||
self.res.body()
|
self.res.body()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a body
|
/// Sets new body.
|
||||||
pub fn set_body<B2>(self, body: B2) -> HttpResponse<B2> {
|
pub fn set_body<B2>(self, body: B2) -> HttpResponse<B2> {
|
||||||
HttpResponse {
|
HttpResponse {
|
||||||
res: self.res.set_body(body),
|
res: self.res.set_body(body),
|
||||||
error: None,
|
error: self.error,
|
||||||
// error: self.error, ??
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Split response and body
|
/// Returns split head and body.
|
||||||
|
///
|
||||||
|
/// # Implementation Notes
|
||||||
|
/// Due to internal performance optimizations, the first element of the returned tuple is an
|
||||||
|
/// `HttpResponse` as well but only contains the head of the response this was called on.
|
||||||
pub fn into_parts(self) -> (HttpResponse<()>, B) {
|
pub fn into_parts(self) -> (HttpResponse<()>, B) {
|
||||||
let (head, body) = self.res.into_parts();
|
let (head, body) = self.res.into_parts();
|
||||||
|
|
||||||
|
@ -208,7 +211,7 @@ impl<B> HttpResponse<B> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Drop request's body
|
/// Drops body and returns new response.
|
||||||
pub fn drop_body(self) -> HttpResponse<()> {
|
pub fn drop_body(self) -> HttpResponse<()> {
|
||||||
HttpResponse {
|
HttpResponse {
|
||||||
res: self.res.drop_body(),
|
res: self.res.drop_body(),
|
||||||
|
|
|
@ -257,18 +257,6 @@ impl ServiceRequest {
|
||||||
self.req.conn_data()
|
self.req.conn_data()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Counterpart to [`HttpRequest::req_data`].
|
|
||||||
#[inline]
|
|
||||||
pub fn req_data(&self) -> Ref<'_, Extensions> {
|
|
||||||
self.req.req_data()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Counterpart to [`HttpRequest::req_data_mut`].
|
|
||||||
#[inline]
|
|
||||||
pub fn req_data_mut(&self) -> RefMut<'_, Extensions> {
|
|
||||||
self.req.req_data_mut()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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> {
|
||||||
|
|
Loading…
Reference in New Issue