mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-25 22:49:21 +02:00
add session and cookie session backend
This commit is contained in:
@ -64,12 +64,6 @@ impl HttpRequest {
|
||||
self.head().uri.path()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns Request's headers.
|
||||
pub fn headers(&self) -> &HeaderMap {
|
||||
&self.head().headers
|
||||
}
|
||||
|
||||
/// The query string in the URL.
|
||||
///
|
||||
/// E.g., id=10
|
||||
@ -93,18 +87,6 @@ impl HttpRequest {
|
||||
&self.path
|
||||
}
|
||||
|
||||
/// Request extensions
|
||||
#[inline]
|
||||
pub fn extensions(&self) -> Ref<Extensions> {
|
||||
self.head.extensions()
|
||||
}
|
||||
|
||||
/// Mutable reference to a the request's extensions
|
||||
#[inline]
|
||||
pub fn extensions_mut(&self) -> RefMut<Extensions> {
|
||||
self.head.extensions_mut()
|
||||
}
|
||||
|
||||
/// Application extensions
|
||||
#[inline]
|
||||
pub fn app_extensions(&self) -> &Extensions {
|
||||
@ -130,8 +112,26 @@ impl HttpMessage for HttpRequest {
|
||||
type Stream = ();
|
||||
|
||||
#[inline]
|
||||
/// Returns Request's headers.
|
||||
fn headers(&self) -> &HeaderMap {
|
||||
self.headers()
|
||||
&self.head().headers
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||
&mut self.head.headers
|
||||
}
|
||||
|
||||
/// Request extensions
|
||||
#[inline]
|
||||
fn extensions(&self) -> Ref<Extensions> {
|
||||
self.head.extensions()
|
||||
}
|
||||
|
||||
/// Mutable reference to a the request's extensions
|
||||
#[inline]
|
||||
fn extensions_mut(&self) -> RefMut<Extensions> {
|
||||
self.head.extensions_mut()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -2,7 +2,7 @@ use std::borrow::Cow;
|
||||
use std::cell::{Ref, RefMut};
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_http::body::{Body, MessageBody, ResponseBody};
|
||||
use actix_http::body::{Body, ResponseBody};
|
||||
use actix_http::http::{HeaderMap, Method, Uri, Version};
|
||||
use actix_http::{
|
||||
Error, Extensions, HttpMessage, Payload, PayloadStream, Request, RequestHead,
|
||||
@ -84,12 +84,6 @@ impl<P> ServiceRequest<P> {
|
||||
self.head().uri.path()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns Request's headers.
|
||||
pub fn headers(&self) -> &HeaderMap {
|
||||
&self.head().headers
|
||||
}
|
||||
|
||||
/// The query string in the URL.
|
||||
///
|
||||
/// E.g., id=10
|
||||
@ -118,18 +112,6 @@ impl<P> ServiceRequest<P> {
|
||||
&mut self.req.path
|
||||
}
|
||||
|
||||
/// Request extensions
|
||||
#[inline]
|
||||
pub fn extensions(&self) -> Ref<Extensions> {
|
||||
self.req.head.extensions()
|
||||
}
|
||||
|
||||
/// Mutable reference to a the request's extensions
|
||||
#[inline]
|
||||
pub fn extensions_mut(&self) -> RefMut<Extensions> {
|
||||
self.req.head.extensions_mut()
|
||||
}
|
||||
|
||||
/// Application extensions
|
||||
#[inline]
|
||||
pub fn app_extensions(&self) -> &Extensions {
|
||||
@ -147,8 +129,27 @@ impl<P> HttpMessage for ServiceRequest<P> {
|
||||
type Stream = P;
|
||||
|
||||
#[inline]
|
||||
/// Returns Request's headers.
|
||||
fn headers(&self) -> &HeaderMap {
|
||||
self.req.headers()
|
||||
&self.head().headers
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Mutable reference to the request's headers.
|
||||
fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||
&mut self.head_mut().headers
|
||||
}
|
||||
|
||||
/// Request extensions
|
||||
#[inline]
|
||||
fn extensions(&self) -> Ref<Extensions> {
|
||||
self.req.head.extensions()
|
||||
}
|
||||
|
||||
/// Mutable reference to a the request's extensions
|
||||
#[inline]
|
||||
fn extensions_mut(&self) -> RefMut<Extensions> {
|
||||
self.req.head.extensions_mut()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -229,6 +230,23 @@ impl<P> HttpMessage for ServiceFromRequest<P> {
|
||||
self.req.headers()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||
self.req.headers_mut()
|
||||
}
|
||||
|
||||
/// Request extensions
|
||||
#[inline]
|
||||
fn extensions(&self) -> Ref<Extensions> {
|
||||
self.req.head.extensions()
|
||||
}
|
||||
|
||||
/// Mutable reference to a the request's extensions
|
||||
#[inline]
|
||||
fn extensions_mut(&self) -> RefMut<Extensions> {
|
||||
self.req.head.extensions_mut()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn take_payload(&mut self) -> Payload<Self::Stream> {
|
||||
std::mem::replace(&mut self.payload, Payload::None)
|
||||
@ -275,11 +293,26 @@ impl<B> ServiceResponse<B> {
|
||||
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||
self.response.headers_mut()
|
||||
}
|
||||
|
||||
/// Execute closure and in case of error convert it to response.
|
||||
pub fn checked_expr<F, E>(mut self, f: F) -> Self
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(), E>,
|
||||
E: Into<Error>,
|
||||
{
|
||||
match f(&mut self) {
|
||||
Ok(_) => self,
|
||||
Err(err) => {
|
||||
let res: Response = err.into().into();
|
||||
ServiceResponse::new(self.request, res.into_body())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: MessageBody> ServiceResponse<B> {
|
||||
impl<B> ServiceResponse<B> {
|
||||
/// Set a new body
|
||||
pub fn map_body<F, B2: MessageBody>(self, f: F) -> ServiceResponse<B2>
|
||||
pub fn map_body<F, B2>(self, f: F) -> ServiceResponse<B2>
|
||||
where
|
||||
F: FnOnce(&mut ResponseHead, ResponseBody<B>) -> ResponseBody<B2>,
|
||||
{
|
||||
@ -292,7 +325,7 @@ impl<B: MessageBody> ServiceResponse<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: MessageBody> std::ops::Deref for ServiceResponse<B> {
|
||||
impl<B> std::ops::Deref for ServiceResponse<B> {
|
||||
type Target = Response<B>;
|
||||
|
||||
fn deref(&self) -> &Response<B> {
|
||||
@ -300,19 +333,19 @@ impl<B: MessageBody> std::ops::Deref for ServiceResponse<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: MessageBody> std::ops::DerefMut for ServiceResponse<B> {
|
||||
impl<B> std::ops::DerefMut for ServiceResponse<B> {
|
||||
fn deref_mut(&mut self) -> &mut Response<B> {
|
||||
self.response_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: MessageBody> Into<Response<B>> for ServiceResponse<B> {
|
||||
impl<B> Into<Response<B>> for ServiceResponse<B> {
|
||||
fn into(self) -> Response<B> {
|
||||
self.response
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: MessageBody> IntoFuture for ServiceResponse<B> {
|
||||
impl<B> IntoFuture for ServiceResponse<B> {
|
||||
type Item = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type Future = FutureResult<ServiceResponse<B>, Error>;
|
||||
|
47
src/test.rs
47
src/test.rs
@ -8,11 +8,12 @@ use actix_http::test::TestRequest as HttpTestRequest;
|
||||
use actix_http::{Extensions, PayloadStream, Request};
|
||||
use actix_router::{Path, Url};
|
||||
use actix_rt::Runtime;
|
||||
use actix_service::{IntoNewService, NewService, Service};
|
||||
use bytes::Bytes;
|
||||
use futures::Future;
|
||||
|
||||
use crate::request::HttpRequest;
|
||||
use crate::service::{ServiceFromRequest, ServiceRequest};
|
||||
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
||||
|
||||
thread_local! {
|
||||
static RT: RefCell<Runtime> = {
|
||||
@ -37,6 +38,34 @@ where
|
||||
RT.with(move |rt| rt.borrow_mut().block_on(f))
|
||||
}
|
||||
|
||||
/// This method accepts application builder instance, and constructs
|
||||
/// service.
|
||||
///
|
||||
/// ```rust
|
||||
/// use actix_http::http::{test, App, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = test::init_service(
|
||||
/// App::new()
|
||||
/// .resource("/test", |r| r.to(|| HttpResponse::Ok()))
|
||||
/// )
|
||||
///
|
||||
/// let req = TestRequest::with_uri("/test").to_request();
|
||||
/// let resp = block_on(srv.call(req)).unwrap();
|
||||
/// assert_eq!(resp.status(), StatusCode::OK);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn init_service<R, S, B, E>(
|
||||
app: R,
|
||||
) -> impl Service<Request, Response = ServiceResponse<B>, Error = E>
|
||||
where
|
||||
R: IntoNewService<S, Request, ()>,
|
||||
S: NewService<Request, Response = ServiceResponse<B>, Error = E>,
|
||||
S::InitError: std::fmt::Debug,
|
||||
{
|
||||
block_on(app.into_new_service().new_service(&())).unwrap()
|
||||
}
|
||||
|
||||
/// Test `Request` builder.
|
||||
///
|
||||
/// For unit testing, actix provides a request builder type and a simple handler runner. TestRequest implements a builder-like pattern.
|
||||
@ -112,6 +141,22 @@ impl TestRequest {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create TestRequest and set method to `Method::GET`
|
||||
pub fn get() -> TestRequest {
|
||||
TestRequest {
|
||||
req: HttpTestRequest::default().method(Method::GET).take(),
|
||||
extensions: Extensions::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create TestRequest and set method to `Method::POST`
|
||||
pub fn post() -> TestRequest {
|
||||
TestRequest {
|
||||
req: HttpTestRequest::default().method(Method::POST).take(),
|
||||
extensions: Extensions::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set HTTP version of this request
|
||||
pub fn version(mut self, ver: Version) -> Self {
|
||||
self.req.version(ver);
|
||||
|
Reference in New Issue
Block a user