Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional cookies features #1981

Merged
merged 7 commits into from Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
@@ -1,6 +1,10 @@
# Changes

## Unreleased - 2021-xx-xx
### Changed
* Feature `cookies` is now optional and enabled by default. [#1981]

[#1981]: https://github.com/actix/actix-web/pull/1981


## 4.0.0-beta.3 - 2021-02-10
Expand Down
17 changes: 9 additions & 8 deletions Cargo.toml
Expand Up @@ -15,6 +15,7 @@ license = "MIT OR Apache-2.0"
edition = "2018"

[package.metadata.docs.rs]
# features that docs.rs will build with
features = ["openssl", "rustls", "compress", "secure-cookies"]

[badges]
Expand All @@ -38,12 +39,15 @@ members = [
]

[features]
default = ["compress"]
default = ["compress", "cookies"]

# content-encoding support
compress = ["actix-http/compress", "awc/compress"]

# sessions feature
# support for cookies
cookies = ["actix-http/cookies", "awc/cookies"]

# secure cookies feature
secure-cookies = ["actix-http/secure-cookies"]

# openssl
Expand Down Expand Up @@ -95,17 +99,17 @@ futures-core = { version = "0.3.7", default-features = false }
futures-util = { version = "0.3.7", default-features = false }
log = "0.4"
mime = "0.3"
socket2 = "0.3.16"
pin-project = "1.0.0"
regex = "1.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_urlencoded = "0.7"
smallvec = "1.6"
socket2 = "0.3.16"
time = { version = "0.2.23", default-features = false, features = ["std"] }
url = "2.1"
tls-openssl = { package = "openssl", version = "0.10.9", optional = true }
tls-rustls = { package = "rustls", version = "0.19.0", optional = true }
smallvec = "1.6"
url = "2.1"

[target.'cfg(windows)'.dependencies.tls-openssl]
version = "0.10.9"
Expand All @@ -122,9 +126,6 @@ brotli2 = "0.3.2"
flate2 = "1.0.13"
criterion = "0.3"

[profile.dev]
debug = false

[profile.release]
lto = true
opt-level = 3
Expand Down
4 changes: 4 additions & 0 deletions actix-http/CHANGES.md
@@ -1,6 +1,10 @@
# Changes

## Unreleased - 2021-xx-xx
### Changed
* Feature `cookies` is now optional and disabled by default. [#1981]

[#1981]: https://github.com/actix/actix-web/pull/1981


## 3.0.0-beta.3 - 2021-02-10
Expand Down
21 changes: 13 additions & 8 deletions actix-http/Cargo.toml
Expand Up @@ -15,7 +15,8 @@ license = "MIT OR Apache-2.0"
edition = "2018"

[package.metadata.docs.rs]
features = ["openssl", "rustls", "compress", "secure-cookies"]
# features that docs.rs will build with
features = ["openssl", "rustls", "compress", "cookies", "secure-cookies"]

[lib]
name = "actix_http"
Expand All @@ -30,11 +31,14 @@ openssl = ["actix-tls/openssl"]
# rustls support
rustls = ["actix-tls/rustls"]

# enable compressison support
# enable compression support
compress = ["flate2", "brotli2"]

# support for cookies
cookies = ["cookie"]

# support for secure cookies
secure-cookies = ["cookie/secure"]
secure-cookies = ["cookies", "cookie/secure"]

# trust-dns as client dns resolver
trust-dns = ["trust-dns-resolver"]
Expand All @@ -46,24 +50,25 @@ actix-utils = "3.0.0-beta.2"
actix-rt = "2"
actix-tls = "3.0.0-beta.2"

ahash = "0.7"
base64 = "0.13"
bitflags = "1.2"
bytes = "1"
bytestring = "1"
cookie = { version = "0.14.1", features = ["percent-encode"] }
cfg-if = "1"
cookie = { version = "0.14.1", features = ["percent-encode"], optional = true }
derive_more = "0.99.5"
encoding_rs = "0.8"
futures-channel = { version = "0.3.7", default-features = false, features = ["alloc"] }
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
futures-util = { version = "0.3.7", default-features = false, features = ["alloc", "sink"] }
ahash = "0.7"
h2 = "0.3.0"
http = "0.2.2"
httparse = "1.3"
indexmap = "1.3"
itoa = "0.4"
lazy_static = "1.4"
language-tags = "0.2"
lazy_static = "1.4"
log = "0.4"
mime = "0.3"
percent-encoding = "2.1"
Expand All @@ -72,10 +77,10 @@ rand = "0.8"
regex = "1.3"
serde = "1.0"
serde_json = "1.0"
serde_urlencoded = "0.7"
sha-1 = "0.9"
smallvec = "1.6"
slab = "0.4"
serde_urlencoded = "0.7"
smallvec = "1.6"
time = { version = "0.2.23", default-features = false, features = ["std"] }

# compression
Expand Down
5 changes: 4 additions & 1 deletion actix-http/src/error.rs
Expand Up @@ -19,10 +19,12 @@ use serde_json::error::Error as JsonError;
use serde_urlencoded::ser::Error as FormError;

use crate::body::Body;
pub use crate::cookie::ParseError as CookieParseError;
use crate::helpers::Writer;
use crate::response::{Response, ResponseBuilder};

#[cfg(feature = "cookies")]
pub use crate::cookie::ParseError as CookieParseError;

/// A specialized [`std::result::Result`]
/// for actix web operations
///
Expand Down Expand Up @@ -397,6 +399,7 @@ impl ResponseError for PayloadError {
}

/// Return `BadRequest` for `cookie::ParseError`
#[cfg(feature = "cookies")]
impl ResponseError for crate::cookie::ParseError {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
Expand Down
1 change: 0 additions & 1 deletion actix-http/src/h1/encoder.rs
Expand Up @@ -549,7 +549,6 @@ mod tests {
);
let data =
String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
eprintln!("{}", &data);

assert!(data.contains("Content-Length: 0\r\n"));
assert!(data.contains("Connection: close\r\n"));
Expand Down
10 changes: 7 additions & 3 deletions actix-http/src/http_message.rs
Expand Up @@ -5,12 +5,14 @@ use encoding_rs::{Encoding, UTF_8};
use http::header;
use mime::Mime;

use crate::cookie::Cookie;
use crate::error::{ContentTypeError, CookieParseError, ParseError};
use crate::error::{ContentTypeError, ParseError};
use crate::extensions::Extensions;
use crate::header::{Header, HeaderMap};
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.
Expand Down Expand Up @@ -104,7 +106,7 @@ pub trait HttpMessage: Sized {
}

/// Load request cookies.
#[inline]
#[cfg(feature = "cookies")]
fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> {
if self.extensions().get::<Cookies>().is_none() {
let mut cookies = Vec::new();
Expand All @@ -119,12 +121,14 @@ pub trait HttpMessage: Sized {
}
self.extensions_mut().insert(Cookies(cookies));
}

Ok(Ref::map(self.extensions(), |ext| {
&ext.get::<Cookies>().unwrap().0
}))
}

/// Return request cookie.
#[cfg(feature = "cookies")]
fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
if let Ok(cookies) = self.cookies() {
for cookie in cookies.iter() {
Expand Down
20 changes: 19 additions & 1 deletion actix-http/src/lib.rs
@@ -1,4 +1,19 @@
//! HTTP primitives for the Actix ecosystem.
//!
//! ## Crate Features
//! | Feature | Functionality |
//! | ---------------- | ----------------------------------------------------- |
//! | `openssl` | TLS support via [OpenSSL]. |
//! | `rustls` | TLS support via [rustls]. |
//! | `compress` | Payload compression support. (Deflate, Gzip & Brotli) |
//! | `cookies` | Support for cookies backed by the [cookie] crate. |
//! | `secure-cookies` | Adds for secure cookies. Enables `cookies` feature. |
//! | `trust-dns` | Use [trust-dns] as the client DNS resolver. |
//!
//! [OpenSSL]: https://crates.io/crates/openssl
//! [rustls]: https://crates.io/crates/rustls
//! [cookie]: https://crates.io/crates/cookie
//! [trust-dns]: https://crates.io/crates/trust-dns

#![deny(rust_2018_idioms, nonstandard_style)]
#![allow(
Expand Down Expand Up @@ -34,13 +49,15 @@ mod response;
mod service;
mod time_parser;

pub use cookie;
pub mod error;
pub mod h1;
pub mod h2;
pub mod test;
pub mod ws;

#[cfg(feature = "cookies")]
pub use cookie;

pub use self::builder::HttpServiceBuilder;
pub use self::config::{KeepAlive, ServiceConfig};
pub use self::error::{Error, ResponseError, Result};
Expand All @@ -61,6 +78,7 @@ pub mod http {
pub use http::{uri, Error, Uri};
pub use http::{Method, StatusCode, Version};

#[cfg(feature = "cookies")]
pub use crate::cookie::{Cookie, CookieBuilder};
pub use crate::header::HeaderMap;

Expand Down