- Add `unicode` feature to switch between `regex` and `regex-lite`

as a trade-off between full unicode support and binary size.
This commit is contained in:
LoveSy 2024-02-16 00:02:08 +08:00
parent 8c31d137aa
commit a9738b24c5
8 changed files with 46 additions and 25 deletions

View File

@ -2,6 +2,7 @@
## Unreleased ## Unreleased
- Add `unicode` feature to switch between `regex` and `regex-lite` as a trade-off between full unicode support and binary size.
- Minimum supported Rust version (MSRV) is now 1.72. - Minimum supported Rust version (MSRV) is now 1.72.
## 0.5.2 ## 0.5.2

View File

@ -17,12 +17,14 @@ name = "actix_router"
path = "src/lib.rs" path = "src/lib.rs"
[features] [features]
default = ["http"] default = ["http", "unicode"]
unicode = ["dep:regex"]
[dependencies] [dependencies]
bytestring = ">=0.1.5, <2" bytestring = ">=0.1.5, <2"
http = { version = "0.2.7", optional = true } http = { version = "0.2.7", optional = true }
regex = "1.5" regex = { version = "1.5", optional = true }
regex-lite = "0.1"
serde = "1" serde = "1"
tracing = { version = "0.1.30", default-features = false, features = ["log"] } tracing = { version = "0.1.30", default-features = false, features = ["log"] }

View File

@ -5,7 +5,10 @@ use std::{
mem, mem,
}; };
use regex::{escape, Regex, RegexSet}; #[cfg(feature = "unicode")]
use regex::{escape, Regex};
#[cfg(not(feature = "unicode"))]
use regex_lite::{escape, Regex};
use tracing::error; use tracing::error;
use crate::{path::PathItem, IntoPatterns, Patterns, Resource, ResourcePath}; use crate::{path::PathItem, IntoPatterns, Patterns, Resource, ResourcePath};
@ -243,7 +246,7 @@ enum PatternType {
Dynamic(Regex, Vec<&'static str>), Dynamic(Regex, Vec<&'static str>),
/// Regular expression set and list of component expressions plus dynamic segment names. /// Regular expression set and list of component expressions plus dynamic segment names.
DynamicSet(RegexSet, Vec<(Regex, Vec<&'static str>)>), DynamicSet(Vec<Regex>, Vec<(Regex, Vec<&'static str>)>),
} }
impl ResourceDef { impl ResourceDef {
@ -557,7 +560,7 @@ impl ResourceDef {
match &self.pat_type { match &self.pat_type {
PatternType::Static(pattern) => self.static_match(pattern, path).is_some(), PatternType::Static(pattern) => self.static_match(pattern, path).is_some(),
PatternType::Dynamic(re, _) => re.is_match(path), PatternType::Dynamic(re, _) => re.is_match(path),
PatternType::DynamicSet(re, _) => re.is_match(path), PatternType::DynamicSet(re, _) => re.iter().any(|re| re.is_match(path)),
} }
} }
@ -603,7 +606,7 @@ impl ResourceDef {
PatternType::Dynamic(re, _) => Some(re.captures(path)?[1].len()), PatternType::Dynamic(re, _) => Some(re.captures(path)?[1].len()),
PatternType::DynamicSet(re, params) => { PatternType::DynamicSet(re, params) => {
let idx = re.matches(path).into_iter().next()?; let idx = re.iter().enumerate().find(|(_, re)| re.is_match(path))?.0;
let (ref pattern, _) = params[idx]; let (ref pattern, _) = params[idx];
Some(pattern.captures(path)?[1].len()) Some(pattern.captures(path)?[1].len())
} }
@ -706,8 +709,9 @@ impl ResourceDef {
PatternType::DynamicSet(re, params) => { PatternType::DynamicSet(re, params) => {
let path = path.unprocessed(); let path = path.unprocessed();
let (pattern, names) = match re.matches(path).into_iter().next() { let (pattern, names) = match re.iter().enumerate().find(|(_, re)| re.is_match(path))
Some(idx) => &params[idx], {
Some((idx, _)) => &params[idx],
_ => return false, _ => return false,
}; };
@ -849,10 +853,9 @@ impl ResourceDef {
// since zero length pattern sets are possible // since zero length pattern sets are possible
// just return a useless `ResourceDef` // just return a useless `ResourceDef`
Patterns::List(patterns) if patterns.is_empty() => ( Patterns::List(patterns) if patterns.is_empty() => {
PatternType::DynamicSet(RegexSet::empty(), Vec::new()), (PatternType::DynamicSet(Vec::new(), Vec::new()), Vec::new())
Vec::new(), }
),
Patterns::List(patterns) => { Patterns::List(patterns) => {
let mut re_set = Vec::with_capacity(patterns.len()); let mut re_set = Vec::with_capacity(patterns.len());
@ -870,7 +873,7 @@ impl ResourceDef {
} }
} }
let pattern_re_set = RegexSet::new(re_set).unwrap(); let pattern_re_set = re_set.iter().map(|re| Regex::new(re).unwrap()).collect();
let segments = segments.unwrap_or_default(); let segments = segments.unwrap_or_default();
( (

View File

@ -4,6 +4,7 @@
### Changed ### Changed
- Add `unicode` feature to switch between `regex` and `regex-lite` as a trade-off between full unicode support and binary size.
- Minimum supported Rust version (MSRV) is now 1.72. - Minimum supported Rust version (MSRV) is now 1.72.
## 4.5.1 ## 4.5.1

View File

@ -40,7 +40,7 @@ name = "actix_web"
path = "src/lib.rs" path = "src/lib.rs"
[features] [features]
default = ["macros", "compress-brotli", "compress-gzip", "compress-zstd", "cookies", "http2"] default = ["macros", "compress-brotli", "compress-gzip", "compress-zstd", "cookies", "http2", "unicode"]
# Brotli algorithm content-encoding support # Brotli algorithm content-encoding support
compress-brotli = ["actix-http/compress-brotli", "__compress"] compress-brotli = ["actix-http/compress-brotli", "__compress"]
@ -72,6 +72,9 @@ rustls-0_21 = ["http2", "actix-http/rustls-0_21", "actix-tls/accept", "actix-tls
# TLS via Rustls v0.22 # TLS via Rustls v0.22
rustls-0_22 = ["http2", "actix-http/rustls-0_22", "actix-tls/accept", "actix-tls/rustls-0_22"] rustls-0_22 = ["http2", "actix-http/rustls-0_22", "actix-tls/accept", "actix-tls/rustls-0_22"]
# Full unicode support
unicode = ["dep:regex", "actix-router/unicode"]
# Internal (PRIVATE!) features used to aid testing and checking feature status. # Internal (PRIVATE!) features used to aid testing and checking feature status.
# Don't rely on these whatsoever. They may disappear at anytime. # Don't rely on these whatsoever. They may disappear at anytime.
__compress = [] __compress = []
@ -89,7 +92,7 @@ actix-utils = "3"
actix-tls = { version = "3.3", default-features = false, optional = true } actix-tls = { version = "3.3", default-features = false, optional = true }
actix-http = { version = "3.6", features = ["ws"] } actix-http = { version = "3.6", features = ["ws"] }
actix-router = "0.5" actix-router = { path = "../actix-router", default-features = false, features = ["http"] }
actix-web-codegen = { version = "4.2", optional = true } actix-web-codegen = { version = "4.2", optional = true }
ahash = "0.8" ahash = "0.8"
@ -107,7 +110,8 @@ log = "0.4"
mime = "0.3" mime = "0.3"
once_cell = "1.5" once_cell = "1.5"
pin-project-lite = "0.2.7" pin-project-lite = "0.2.7"
regex = "1.5.5" regex = { version = "1.5.5", optional = true }
regex-lite = "0.1"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
serde_urlencoded = "0.7" serde_urlencoded = "0.7"

View File

@ -13,7 +13,10 @@
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
#[cfg(feature = "unicode")]
use regex::Regex; use regex::Regex;
#[cfg(not(feature = "unicode"))]
use regex_lite::Regex;
use super::{ExtendedValue, Header, TryIntoHeaderValue, Writer}; use super::{ExtendedValue, Header, TryIntoHeaderValue, Writer};
use crate::http::header; use crate::http::header;

View File

@ -18,7 +18,10 @@ use bytes::Bytes;
use futures_core::ready; use futures_core::ready;
use log::{debug, warn}; use log::{debug, warn};
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use regex::{Regex, RegexSet}; #[cfg(feature = "unicode")]
use regex::Regex;
#[cfg(not(feature = "unicode"))]
use regex_lite::Regex;
use time::{format_description::well_known::Rfc3339, OffsetDateTime}; use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use crate::{ use crate::{
@ -87,7 +90,7 @@ pub struct Logger(Rc<Inner>);
struct Inner { struct Inner {
format: Format, format: Format,
exclude: HashSet<String>, exclude: HashSet<String>,
exclude_regex: RegexSet, exclude_regex: Vec<Regex>,
log_target: Cow<'static, str>, log_target: Cow<'static, str>,
} }
@ -97,7 +100,7 @@ impl Logger {
Logger(Rc::new(Inner { Logger(Rc::new(Inner {
format: Format::new(format), format: Format::new(format),
exclude: HashSet::new(), exclude: HashSet::new(),
exclude_regex: RegexSet::empty(), exclude_regex: Vec::new(),
log_target: Cow::Borrowed(module_path!()), log_target: Cow::Borrowed(module_path!()),
})) }))
} }
@ -114,10 +117,7 @@ impl Logger {
/// Ignore and do not log access info for paths that match regex. /// Ignore and do not log access info for paths that match regex.
pub fn exclude_regex<T: Into<String>>(mut self, path: T) -> Self { pub fn exclude_regex<T: Into<String>>(mut self, path: T) -> Self {
let inner = Rc::get_mut(&mut self.0).unwrap(); let inner = Rc::get_mut(&mut self.0).unwrap();
let mut patterns = inner.exclude_regex.patterns().to_vec(); inner.exclude_regex.push(Regex::new(&path.into()).unwrap());
patterns.push(path.into());
let regex_set = RegexSet::new(patterns).unwrap();
inner.exclude_regex = regex_set;
self self
} }
@ -240,7 +240,7 @@ impl Default for Logger {
Logger(Rc::new(Inner { Logger(Rc::new(Inner {
format: Format::default(), format: Format::default(),
exclude: HashSet::new(), exclude: HashSet::new(),
exclude_regex: RegexSet::empty(), exclude_regex: Vec::new(),
log_target: Cow::Borrowed(module_path!()), log_target: Cow::Borrowed(module_path!()),
})) }))
} }
@ -300,7 +300,11 @@ where
fn call(&self, req: ServiceRequest) -> Self::Future { fn call(&self, req: ServiceRequest) -> Self::Future {
let excluded = self.inner.exclude.contains(req.path()) let excluded = self.inner.exclude.contains(req.path())
|| self.inner.exclude_regex.is_match(req.path()); || self
.inner
.exclude_regex
.iter()
.any(|r| r.is_match(req.path()));
if excluded { if excluded {
LoggerResponse { LoggerResponse {

View File

@ -4,7 +4,10 @@ use actix_http::uri::{PathAndQuery, Uri};
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use actix_utils::future::{ready, Ready}; use actix_utils::future::{ready, Ready};
use bytes::Bytes; use bytes::Bytes;
#[cfg(feature = "unicode")]
use regex::Regex; use regex::Regex;
#[cfg(not(feature = "unicode"))]
use regex_lite::Regex;
use crate::{ use crate::{
service::{ServiceRequest, ServiceResponse}, service::{ServiceRequest, ServiceResponse},