mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-26 23:17:42 +02:00
optional cookies features (#1981)
This commit is contained in:
@ -97,7 +97,9 @@ use std::convert::TryFrom;
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
|
||||
pub use actix_http::{client::Connector, cookie, http};
|
||||
#[cfg(feature = "cookies")]
|
||||
pub use actix_http::cookie;
|
||||
pub use actix_http::{client::Connector, http};
|
||||
|
||||
use actix_http::http::{Error as HttpError, HeaderMap, Method, Uri};
|
||||
use actix_http::RequestHead;
|
||||
|
@ -8,6 +8,7 @@ use futures_core::Stream;
|
||||
use serde::Serialize;
|
||||
|
||||
use actix_http::body::Body;
|
||||
#[cfg(feature = "cookies")]
|
||||
use actix_http::cookie::{Cookie, CookieJar};
|
||||
use actix_http::http::header::{self, IntoHeaderPair};
|
||||
use actix_http::http::{
|
||||
@ -54,10 +55,12 @@ pub struct ClientRequest {
|
||||
pub(crate) head: RequestHead,
|
||||
err: Option<HttpError>,
|
||||
addr: Option<net::SocketAddr>,
|
||||
cookies: Option<CookieJar>,
|
||||
response_decompress: bool,
|
||||
timeout: Option<Duration>,
|
||||
config: Rc<ClientConfig>,
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: Option<CookieJar>,
|
||||
}
|
||||
|
||||
impl ClientRequest {
|
||||
@ -72,6 +75,7 @@ impl ClientRequest {
|
||||
head: RequestHead::default(),
|
||||
err: None,
|
||||
addr: None,
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: None,
|
||||
timeout: None,
|
||||
response_decompress: true,
|
||||
@ -290,6 +294,7 @@ impl ClientRequest {
|
||||
/// println!("Response: {:?}", resp);
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(feature = "cookies")]
|
||||
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
|
||||
if self.cookies.is_none() {
|
||||
let mut jar = CookieJar::new();
|
||||
@ -472,7 +477,8 @@ impl ClientRequest {
|
||||
)
|
||||
}
|
||||
|
||||
fn prep_for_sending(mut self) -> Result<Self, PrepForSendingError> {
|
||||
// allow unused mut when cookies feature is disabled
|
||||
fn prep_for_sending(#[allow(unused_mut)] mut self) -> Result<Self, PrepForSendingError> {
|
||||
if let Some(e) = self.err {
|
||||
return Err(e.into());
|
||||
}
|
||||
@ -493,6 +499,7 @@ impl ClientRequest {
|
||||
}
|
||||
|
||||
// set cookies
|
||||
#[cfg(feature = "cookies")]
|
||||
if let Some(ref mut jar) = self.cookies {
|
||||
let cookie: String = jar
|
||||
.delta()
|
||||
|
@ -1,20 +1,25 @@
|
||||
use std::cell::{Ref, RefMut};
|
||||
use std::fmt;
|
||||
use std::future::Future;
|
||||
use std::marker::PhantomData;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::{
|
||||
cell::{Ref, RefMut},
|
||||
mem,
|
||||
};
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures_core::{ready, Stream};
|
||||
|
||||
use actix_http::cookie::Cookie;
|
||||
use actix_http::error::{CookieParseError, PayloadError};
|
||||
use actix_http::http::header::{CONTENT_LENGTH, SET_COOKIE};
|
||||
use actix_http::error::PayloadError;
|
||||
use actix_http::http::header;
|
||||
use actix_http::http::{HeaderMap, StatusCode, Version};
|
||||
use actix_http::{Extensions, HttpMessage, Payload, PayloadStream, ResponseHead};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
use actix_http::{cookie::Cookie, error::CookieParseError};
|
||||
|
||||
use crate::error::JsonPayloadError;
|
||||
|
||||
/// Client Response
|
||||
@ -39,17 +44,17 @@ impl<S> HttpMessage for ClientResponse<S> {
|
||||
}
|
||||
|
||||
fn take_payload(&mut self) -> Payload<S> {
|
||||
std::mem::replace(&mut self.payload, Payload::None)
|
||||
mem::replace(&mut self.payload, Payload::None)
|
||||
}
|
||||
|
||||
/// Load request cookies.
|
||||
#[inline]
|
||||
#[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(&SET_COOKIE) {
|
||||
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());
|
||||
}
|
||||
@ -161,7 +166,7 @@ where
|
||||
/// Create `MessageBody` for request.
|
||||
pub fn new(res: &mut ClientResponse<S>) -> MessageBody<S> {
|
||||
let mut len = None;
|
||||
if let Some(l) = res.headers().get(&CONTENT_LENGTH) {
|
||||
if let Some(l) = res.headers().get(&header::CONTENT_LENGTH) {
|
||||
if let Ok(s) = l.to_str() {
|
||||
if let Ok(l) = s.parse::<usize>() {
|
||||
len = Some(l)
|
||||
@ -256,7 +261,7 @@ where
|
||||
}
|
||||
|
||||
let mut len = None;
|
||||
if let Some(l) = req.headers().get(&CONTENT_LENGTH) {
|
||||
if let Some(l) = req.headers().get(&header::CONTENT_LENGTH) {
|
||||
if let Ok(s) = l.to_str() {
|
||||
if let Ok(l) = s.parse::<usize>() {
|
||||
len = Some(l)
|
||||
|
@ -1,9 +1,13 @@
|
||||
//! Test helpers for actix http client to use during testing.
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use actix_http::cookie::{Cookie, CookieJar};
|
||||
use actix_http::http::header::{self, Header, HeaderValue, IntoHeaderValue};
|
||||
use actix_http::http::header::{Header, IntoHeaderValue};
|
||||
use actix_http::http::{Error as HttpError, HeaderName, StatusCode, Version};
|
||||
#[cfg(feature = "cookies")]
|
||||
use actix_http::{
|
||||
cookie::{Cookie, CookieJar},
|
||||
http::header::{self, HeaderValue},
|
||||
};
|
||||
use actix_http::{h1, Payload, ResponseHead};
|
||||
use bytes::Bytes;
|
||||
|
||||
@ -12,6 +16,7 @@ use crate::ClientResponse;
|
||||
/// Test `ClientResponse` builder
|
||||
pub struct TestResponse {
|
||||
head: ResponseHead,
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: CookieJar,
|
||||
payload: Option<Payload>,
|
||||
}
|
||||
@ -20,6 +25,7 @@ impl Default for TestResponse {
|
||||
fn default() -> TestResponse {
|
||||
TestResponse {
|
||||
head: ResponseHead::new(StatusCode::OK),
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: CookieJar::new(),
|
||||
payload: None,
|
||||
}
|
||||
@ -69,6 +75,7 @@ impl TestResponse {
|
||||
}
|
||||
|
||||
/// Set cookie for this response
|
||||
#[cfg(feature = "cookies")]
|
||||
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
|
||||
self.cookies.add(cookie.into_owned());
|
||||
self
|
||||
@ -84,8 +91,11 @@ impl TestResponse {
|
||||
|
||||
/// Complete response creation and generate `ClientResponse` instance
|
||||
pub fn finish(self) -> ClientResponse {
|
||||
// allow unused mut when cookies feature is disabled
|
||||
#[allow(unused_mut)]
|
||||
let mut head = self.head;
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
for cookie in self.cookies.delta() {
|
||||
head.headers.insert(
|
||||
header::SET_COOKIE,
|
||||
|
@ -32,6 +32,7 @@ use std::rc::Rc;
|
||||
use std::{fmt, str};
|
||||
|
||||
use actix_codec::Framed;
|
||||
#[cfg(feature = "cookies")]
|
||||
use actix_http::cookie::{Cookie, CookieJar};
|
||||
use actix_http::{ws, Payload, RequestHead};
|
||||
use actix_rt::time::timeout;
|
||||
@ -54,8 +55,10 @@ pub struct WebsocketsRequest {
|
||||
addr: Option<SocketAddr>,
|
||||
max_size: usize,
|
||||
server_mode: bool,
|
||||
cookies: Option<CookieJar>,
|
||||
config: Rc<ClientConfig>,
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: Option<CookieJar>,
|
||||
}
|
||||
|
||||
impl WebsocketsRequest {
|
||||
@ -89,6 +92,7 @@ impl WebsocketsRequest {
|
||||
protocols: None,
|
||||
max_size: 65_536,
|
||||
server_mode: false,
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: None,
|
||||
}
|
||||
}
|
||||
@ -117,6 +121,7 @@ impl WebsocketsRequest {
|
||||
}
|
||||
|
||||
/// Set a cookie
|
||||
#[cfg(feature = "cookies")]
|
||||
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
|
||||
if self.cookies.is_none() {
|
||||
let mut jar = CookieJar::new();
|
||||
@ -270,6 +275,7 @@ impl WebsocketsRequest {
|
||||
}
|
||||
|
||||
// set cookies
|
||||
#[cfg(feature = "cookies")]
|
||||
if let Some(ref mut jar) = self.cookies {
|
||||
let cookie: String = jar
|
||||
.delta()
|
||||
|
Reference in New Issue
Block a user