Skip to content

Commit 96d2d50

Browse files
authored
Improve CORS Middleware: Correct Preflight Handling & Precomputed Headers (#137)
* cors middleware improvements * cors logic and performance improvements * granular CORS setup * added explicit default policy setter * added grouped cors * added gate for cors * small polishing and bugfix
1 parent 05818a3 commit 96d2d50

File tree

18 files changed

+1353
-461
lines changed

18 files changed

+1353
-461
lines changed

examples/tracing_example/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ publish = false
88
tokio = "1.46.1"
99
tracing = "0.1.41"
1010
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
11-
volga = { path = "../../volga", features = ["tracing"] }
11+
volga = { path = "../../volga", features = ["tracing", "middleware"] }
1212

1313
[lints]
1414
workspace = true

volga/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ serde_urlencoded = "0.7.1"
3232
smallvec = "1.15.1"
3333

3434
# optional
35-
async-compression = { version = "0.4.36", features = ["tokio"], optional = true }
35+
async-compression = { version = "0.4.37", features = ["tokio"], optional = true }
3636
base64 = { version = "0.22.1", optional = true }
37-
chrono = { version = "0.4.42", optional = true }
37+
chrono = { version = "0.4.43", optional = true }
3838
cookie = { version = "0.18.1", features = ["percent-encode"], optional = true }
3939
jsonwebtoken = { version = "10.2.0", features = ["rust_crypto"], optional = true }
4040
handlebars = { version = "6.4.0", optional = true }

volga/src/app.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use {
3939
use crate::tracing::TracingConfig;
4040

4141
#[cfg(feature = "middleware")]
42-
use crate::http::CorsConfig;
42+
use crate::http::cors::CorsRegistry;
4343

4444
#[cfg(feature = "jwt-auth")]
4545
use crate::auth::bearer::{BearerAuthConfig, BearerTokenService};
@@ -107,7 +107,7 @@ pub struct App {
107107

108108
/// CORS configuration options
109109
#[cfg(feature = "middleware")]
110-
pub(super) cors_config: Option<CorsConfig>,
110+
pub(super) cors: CorsRegistry,
111111

112112
/// Web Server's Hosting Environment
113113
#[cfg(feature = "static-files")]
@@ -132,7 +132,7 @@ pub struct App {
132132
/// HTTP/2 resource and backpressure limits.
133133
#[cfg(feature = "http2")]
134134
pub(super) http2_limits: Http2Limits,
135-
135+
136136
/// TCP connection parameters
137137
connection: Connection,
138138

@@ -261,6 +261,10 @@ pub(crate) struct AppInstance {
261261
/// Default `Cache-Control` header value
262262
pub(super) cache_control: Option<HeaderValue>,
263263

264+
/// CORS registry
265+
#[cfg(feature = "middleware")]
266+
pub(super) cors: CorsRegistry,
267+
264268
/// Request/Middleware pipeline
265269
pipeline: Pipeline,
266270
}
@@ -297,6 +301,8 @@ impl TryFrom<App> for AppInstance {
297301
max_header_count: app.max_header_count,
298302
max_header_size: app.max_header_size,
299303
cache_control: default_cache_control,
304+
#[cfg(feature = "middleware")]
305+
cors: app.cors,
300306
#[cfg(feature = "http2")]
301307
http2_limits: app.http2_limits,
302308
#[cfg(feature = "static-files")]
@@ -360,7 +366,7 @@ impl App {
360366
#[cfg(feature = "tracing")]
361367
tracing_config: None,
362368
#[cfg(feature = "middleware")]
363-
cors_config: None,
369+
cors: Default::default(),
364370
#[cfg(feature = "static-files")]
365371
host_env: HostEnv::default(),
366372
#[cfg(feature = "jwt-auth")]
@@ -781,7 +787,7 @@ impl App {
781787

782788
println!();
783789
println!("\x1b[1;34m╭───────────────────────────────────────────────╮");
784-
println!("│ 🚀 Welcome to Volga v{version:<5} │");
790+
println!("│ >> Volga v{version:<5} │");
785791
println!("│ Listening on: {url:<28}│");
786792
println!("╰───────────────────────────────────────────────╯\x1b[0m");
787793

0 commit comments

Comments
 (0)