mirror of https://github.com/fafhrd91/actix-web
remove cookies method from httpmessage trait
This commit is contained in:
parent
2522bc0147
commit
0c6ec535e0
|
@ -9,11 +9,6 @@ use crate::error::{ContentTypeError, ParseError};
|
||||||
use crate::extensions::Extensions;
|
use crate::extensions::Extensions;
|
||||||
use crate::header::{Header, HeaderMap};
|
use crate::header::{Header, HeaderMap};
|
||||||
use crate::payload::Payload;
|
use crate::payload::Payload;
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use crate::{cookie::Cookie, error::CookieParseError};
|
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
struct Cookies(Vec<Cookie<'static>>);
|
|
||||||
|
|
||||||
/// Trait that implements general purpose operations on HTTP messages.
|
/// Trait that implements general purpose operations on HTTP messages.
|
||||||
pub trait HttpMessage: Sized {
|
pub trait HttpMessage: Sized {
|
||||||
|
@ -104,28 +99,6 @@ pub trait HttpMessage: Sized {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load request cookies.
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
|
||||||
if self.extensions().get::<Cookies>().is_none() {
|
|
||||||
let mut cookies = Vec::new();
|
|
||||||
for hdr in self.headers().get_all(header::COOKIE) {
|
|
||||||
let s =
|
|
||||||
str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
|
||||||
for cookie_str in s.split(';').map(|s| s.trim()) {
|
|
||||||
if !cookie_str.is_empty() {
|
|
||||||
cookies.push(Cookie::parse_encoded(cookie_str)?.into_owned());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.extensions_mut().insert(Cookies(cookies));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Ref::map(self.extensions(), |ext| {
|
|
||||||
&ext.get::<Cookies>().unwrap().0
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> HttpMessage for &'a mut T
|
impl<'a, T> HttpMessage for &'a mut T
|
||||||
|
|
|
@ -2,10 +2,11 @@
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Ref, RefMut},
|
cell::{Ref, RefMut},
|
||||||
fmt, net,
|
fmt, net, str,
|
||||||
};
|
};
|
||||||
|
|
||||||
use cookie::Cookie;
|
#[cfg(feature = "cookies")]
|
||||||
|
use cookie::{Cookie, ParseError as CookieParseError};
|
||||||
use http::{header, Method, Uri, Version};
|
use http::{header, Method, Uri, Version};
|
||||||
|
|
||||||
use crate::extensions::Extensions;
|
use crate::extensions::Extensions;
|
||||||
|
@ -14,6 +15,9 @@ use crate::message::{Message, RequestHead};
|
||||||
use crate::payload::{Payload, PayloadStream};
|
use crate::payload::{Payload, PayloadStream};
|
||||||
use crate::HttpMessage;
|
use crate::HttpMessage;
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
struct Cookies(Vec<Cookie<'static>>);
|
||||||
|
|
||||||
/// Request
|
/// Request
|
||||||
pub struct Request<P = PayloadStream> {
|
pub struct Request<P = PayloadStream> {
|
||||||
pub(crate) payload: Payload<P>,
|
pub(crate) payload: Payload<P>,
|
||||||
|
@ -170,6 +174,28 @@ impl<P> Request<P> {
|
||||||
self.head().peer_addr
|
self.head().peer_addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load request cookies.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
||||||
|
if self.extensions().get::<Cookies>().is_none() {
|
||||||
|
let mut cookies = Vec::new();
|
||||||
|
for hdr in self.headers().get_all(header::COOKIE) {
|
||||||
|
let s =
|
||||||
|
str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
||||||
|
for cookie_str in s.split(';').map(|s| s.trim()) {
|
||||||
|
if !cookie_str.is_empty() {
|
||||||
|
cookies.push(Cookie::parse_encoded(cookie_str)?.into_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.extensions_mut().insert(Cookies(cookies));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Ref::map(self.extensions(), |ext| {
|
||||||
|
&ext.get::<Cookies>().unwrap().0
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
/// Return request cookie.
|
/// Return request cookie.
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
||||||
|
|
|
@ -892,9 +892,9 @@ mod tests {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::body::Body;
|
use crate::body::Body;
|
||||||
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE};
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
use crate::{http::header::SET_COOKIE, HttpMessage};
|
use crate::http::header::SET_COOKIE;
|
||||||
|
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_debug() {
|
fn test_debug() {
|
||||||
|
|
|
@ -20,8 +20,7 @@ use futures_core::{ready, Stream};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
use {crate::cookie::Cookie, actix_http::error::CookieParseError};
|
use crate::cookie::{Cookie, ParseError as CookieParseError};
|
||||||
|
|
||||||
use crate::error::JsonPayloadError;
|
use crate::error::JsonPayloadError;
|
||||||
|
|
||||||
/// Client Response
|
/// Client Response
|
||||||
|
@ -80,24 +79,6 @@ impl<S> HttpMessage for ClientResponse<S> {
|
||||||
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
||||||
self.head.extensions_mut()
|
self.head.extensions_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load request cookies.
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
|
||||||
struct Cookies(Vec<Cookie<'static>>);
|
|
||||||
|
|
||||||
if self.extensions().get::<Cookies>().is_none() {
|
|
||||||
let mut cookies = Vec::new();
|
|
||||||
for hdr in self.headers().get_all(&header::SET_COOKIE) {
|
|
||||||
let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
|
||||||
cookies.push(Cookie::parse_encoded(s)?.into_owned());
|
|
||||||
}
|
|
||||||
self.extensions_mut().insert(Cookies(cookies));
|
|
||||||
}
|
|
||||||
Ok(Ref::map(self.extensions(), |ext| {
|
|
||||||
&ext.get::<Cookies>().unwrap().0
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> ClientResponse<S> {
|
impl<S> ClientResponse<S> {
|
||||||
|
@ -181,6 +162,24 @@ impl<S> ClientResponse<S> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load request cookies.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
||||||
|
struct Cookies(Vec<Cookie<'static>>);
|
||||||
|
|
||||||
|
if self.extensions().get::<Cookies>().is_none() {
|
||||||
|
let mut cookies = Vec::new();
|
||||||
|
for hdr in self.headers().get_all(&header::SET_COOKIE) {
|
||||||
|
let s = std::str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
||||||
|
cookies.push(Cookie::parse_encoded(s)?.into_owned());
|
||||||
|
}
|
||||||
|
self.extensions_mut().insert(Cookies(cookies));
|
||||||
|
}
|
||||||
|
Ok(Ref::map(self.extensions(), |ext| {
|
||||||
|
&ext.get::<Cookies>().unwrap().0
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
/// Return request cookie.
|
/// Return request cookie.
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
||||||
|
|
|
@ -37,9 +37,9 @@ use actix_service::Service;
|
||||||
|
|
||||||
pub use actix_http::ws::{CloseCode, CloseReason, Codec, Frame, Message};
|
pub use actix_http::ws::{CloseCode, CloseReason, Codec, Frame, Message};
|
||||||
|
|
||||||
|
use crate::connect::{BoxedSocket, ConnectRequest};
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
use crate::cookie::{Cookie, CookieJar};
|
use crate::cookie::{Cookie, CookieJar};
|
||||||
use crate::connect::{BoxedSocket, ConnectRequest};
|
|
||||||
use crate::error::{InvalidUrl, SendRequestError, WsClientError};
|
use crate::error::{InvalidUrl, SendRequestError, WsClientError};
|
||||||
use crate::http::header::{self, HeaderName, HeaderValue, IntoHeaderValue, AUTHORIZATION};
|
use crate::http::header::{self, HeaderName, HeaderValue, IntoHeaderValue, AUTHORIZATION};
|
||||||
use crate::http::{ConnectionType, Error as HttpError, Method, StatusCode, Uri, Version};
|
use crate::http::{ConnectionType, Error as HttpError, Method, StatusCode, Uri, Version};
|
||||||
|
|
|
@ -1,20 +1,27 @@
|
||||||
use std::cell::{Ref, RefCell, RefMut};
|
use std::{
|
||||||
use std::rc::Rc;
|
cell::{Ref, RefCell, RefMut},
|
||||||
use std::{fmt, net};
|
fmt, net,
|
||||||
|
rc::Rc,
|
||||||
|
str,
|
||||||
|
};
|
||||||
|
|
||||||
use actix_http::http::{HeaderMap, Method, Uri, Version};
|
use actix_http::{
|
||||||
use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead};
|
http::{HeaderMap, Method, Uri, Version},
|
||||||
|
Error, Extensions, HttpMessage, Message, Payload, RequestHead,
|
||||||
|
};
|
||||||
use actix_router::{Path, Url};
|
use actix_router::{Path, Url};
|
||||||
use actix_utils::future::{ok, Ready};
|
use actix_utils::future::{ok, Ready};
|
||||||
use cookie::Cookie;
|
#[cfg(feature = "cookies")]
|
||||||
|
use cookie::{Cookie, ParseError as CookieParseError};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
use crate::app_service::AppInitServiceState;
|
use crate::{
|
||||||
use crate::config::AppConfig;
|
app_service::AppInitServiceState, config::AppConfig, error::UrlGenerationError,
|
||||||
use crate::error::UrlGenerationError;
|
extract::FromRequest, http::header, info::ConnectionInfo, rmap::ResourceMap,
|
||||||
use crate::extract::FromRequest;
|
};
|
||||||
use crate::info::ConnectionInfo;
|
|
||||||
use crate::rmap::ResourceMap;
|
#[cfg(feature = "cookies")]
|
||||||
|
struct Cookies(Vec<Cookie<'static>>);
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
/// An HTTP Request
|
/// An HTTP Request
|
||||||
|
@ -262,6 +269,27 @@ impl HttpRequest {
|
||||||
&*self.inner.app_state
|
&*self.inner.app_state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load request cookies.
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
pub fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
|
||||||
|
if self.extensions().get::<Cookies>().is_none() {
|
||||||
|
let mut cookies = Vec::new();
|
||||||
|
for hdr in self.headers().get_all(header::COOKIE) {
|
||||||
|
let s = str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
||||||
|
for cookie_str in s.split(';').map(|s| s.trim()) {
|
||||||
|
if !cookie_str.is_empty() {
|
||||||
|
cookies.push(Cookie::parse_encoded(cookie_str)?.into_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.extensions_mut().insert(Cookies(cookies));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Ref::map(self.extensions(), |ext| {
|
||||||
|
&ext.get::<Cookies>().unwrap().0
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
/// Return request cookie.
|
/// Return request cookie.
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
||||||
|
|
|
@ -826,7 +826,7 @@ mod plus_rustls {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_server_cookies() {
|
async fn test_server_cookies() {
|
||||||
use actix_web::{http, HttpMessage};
|
use actix_web::http;
|
||||||
|
|
||||||
let srv = actix_test::start(|| {
|
let srv = actix_test::start(|| {
|
||||||
App::new().default_service(web::to(|| {
|
App::new().default_service(web::to(|| {
|
||||||
|
|
Loading…
Reference in New Issue