Skip to content

Commit 64a984d

Browse files
committed
Disable compression for text/event-stream responses by default
* Improve docs * Make disabled content types configurable
1 parent 285a946 commit 64a984d

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed

.gitpod.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rocket_async_compression"
3-
version = "0.5.1"
3+
version = "0.6.0"
44
edition = "2021"
55
repository = "https://github.com/Ameobea/rocket_async_compression"
66
description = "Response compression in both gzip and brotli formats for the Rocket webserver using the `async-compression` library"

src/fairing.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ lazy_static! {
2727
MediaType::parse_flexible("video/*").unwrap(),
2828
MediaType::parse_flexible("application/wasm").unwrap(),
2929
MediaType::parse_flexible("application/octet-stream").unwrap(),
30+
MediaType::parse_flexible("text/event-stream").unwrap(),
3031
];
3132
static ref CACHED_FILES: RwLock<HashMap<(String, CachedEncoding), &'static [u8]>> = {
3233
let m = HashMap::new();
@@ -48,14 +49,14 @@ lazy_static! {
4849
/// - `video/*`
4950
/// - `application/wasm`
5051
/// - `application/octet-stream`
52+
/// - `text/event-stream`
5153
///
5254
/// # Usage
5355
///
5456
/// Attach the compression [fairing](/rocket/fairing/) to your Rocket
5557
/// application:
5658
///
5759
/// ```rust
58-
///
5960
/// use rocket_async_compression::Compression;
6061
///
6162
///
@@ -66,27 +67,28 @@ lazy_static! {
6667
/// # ;
6768
///
6869
/// ```
69-
pub struct Compression(pub Level);
70+
pub struct Compression {
71+
pub level: Level,
72+
pub excluded_content_types: Vec<MediaType>,
73+
}
7074

7175
impl Compression {
72-
/// Returns a fairing that compresses outgoing requests.
76+
/// Returns a fairing that compresses outgoing requests. Uses default compression level and excluded content types.
7377
///
7478
/// ## Example
7579
/// To attach this fairing, simply call `attach` on the application's
7680
/// `Rocket` instance with `Compression::fairing()`:
7781
///
7882
/// ```rust
79-
///
8083
/// use rocket_async_compression::Compression;
8184
///
8285
/// rocket::build()
8386
/// // ...
8487
/// .attach(Compression::fairing())
8588
/// // ...
86-
/// # ;
8789
/// ```
8890
pub fn fairing() -> Compression {
89-
Compression(Level::Default)
91+
Compression::with_level(Level::Default)
9092
}
9193

9294
/// Returns a fairing that compresses outgoing requests with the specified
@@ -95,17 +97,31 @@ impl Compression {
9597
/// ## Example
9698
///
9799
/// ```rust
98-
///
99100
/// use rocket_async_compression::{Compression, Level};
100101
///
101102
/// rocket::build()
102103
/// // ...
103104
/// .attach(Compression::with_level(Level::Fastest))
104105
/// // ...
105-
/// # ;
106106
/// ```
107107
pub fn with_level(level: Level) -> Compression {
108-
Compression(level)
108+
Compression {
109+
level,
110+
excluded_content_types: EXCLUSIONS.clone(),
111+
}
112+
}
113+
114+
/// Replaces the default list of excluded content types with the provided list.
115+
pub fn exlude_content_types(self, excluded_content_types: Vec<MediaType>) -> Self {
116+
Compression {
117+
excluded_content_types,
118+
..self
119+
}
120+
}
121+
122+
/// Returns a mutable reference to the list of excluded content types.
123+
pub fn excluded_content_types(&mut self) -> &mut Vec<MediaType> {
124+
&mut self.excluded_content_types
109125
}
110126
}
111127

@@ -119,7 +135,12 @@ impl Fairing for Compression {
119135
}
120136

121137
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) {
122-
super::CompressionUtils::compress_response(request, response, &EXCLUSIONS, self.0);
138+
super::CompressionUtils::compress_response(
139+
request,
140+
response,
141+
&self.excluded_content_types,
142+
self.level,
143+
);
123144
}
124145
}
125146

@@ -137,7 +158,6 @@ impl Fairing for Compression {
137158
/// application:
138159
///
139160
/// ```rust
140-
///
141161
/// use rocket_async_compression::CachedCompression;
142162
///
143163
/// rocket::build()
@@ -157,7 +177,6 @@ impl Fairing for Compression {
157177
/// ..Default::default()
158178
/// })
159179
/// // ...
160-
/// # ;
161180
/// ```
162181
///
163182
///
@@ -204,7 +223,7 @@ impl CachedCompression {
204223
}
205224
}
206225

207-
/// Caches Vec<&str> to Vec<String>.
226+
/// Caches `Vec<&str>` to `Vec<String>`.
208227
pub fn static_paths(paths: Vec<&str>) -> Vec<String> {
209228
paths.into_iter().map(Into::into).collect()
210229
}

src/lib.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1-
//! Gzip and Brotli response compression.
1+
//! Gzip and Brotli response compression for Rocket
22
//!
33
//! See the [`Compression`] and [`Compress`] types for further details.
44
//!
5-
//! # Security Implications
5+
//! ## Usage
6+
//!
7+
//! ```rust
8+
//! #[macro_use]
9+
//! extern crate rocket;
10+
//!
11+
//! use rocket_async_compression::Compression;
12+
//!
13+
//! #[launch]
14+
//! async fn rocket() -> _ {
15+
//! let server = rocket::build()
16+
//! .mount("/", routes![...]);
17+
//!
18+
//! if cfg!(debug_assertions) {
19+
//! server
20+
//! } else {
21+
//! server.attach(Compression::fairing())
22+
//! }
23+
//! }
24+
//! ```
25+
//!
26+
//! ## Security Implications
627
//!
728
//! In some cases, HTTP compression on a site served over HTTPS can make a web
829
//! application vulnerable to attacks including BREACH. These risks should be
930
//! evaluated in the context of your application before enabling compression.
10-
//!
1131
1232
#[macro_use]
1333
extern crate log;

0 commit comments

Comments
 (0)