web-faith-encoding 1.0.1

HTTP content coding for request and response bodies: gzip, deflate, brotli, and zstd
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! HTTP content coding for request and response bodies.
//!
//! Currently supports:
//!
//! - gzip ([RFC 1952](https://www.rfc-editor.org/rfc/rfc1952))
//! - deflate, in its zlib-wrapped form ([RFC 1950](https://www.rfc-editor.org/rfc/rfc1950)), like
//!   browsers
//! - brotli ([RFC 7932](https://www.rfc-editor.org/rfc/rfc7932))
//! - zstd ([RFC 8878](https://www.rfc-editor.org/rfc/rfc8878))
//!
//! # Requests
//!
//! - Use [`encode`] or [`encode_stream`] to compress a request body and declare it, together.
//!
//! ```
//! use http::HeaderMap;
//! use web_faith_encoding::{Coding, request::encode};
//!
//! # async fn example() {
//! let mut headers = HeaderMap::new();
//! let body = b"the quick brown fox".repeat(8);
//!
//! let compressed = encode(&mut headers, &body, Coding::Gzip).await.expect("gzip compresses");
//! assert!(compressed.len() < body.len());
//! assert_eq!(headers["content-encoding"], "gzip");
//! # }
//! ```
//!
//! To drive the halves separately, [`compress_buffer`] and [`compress_stream`] do the body, and
//! [`ContentEncoding::layer`] with [`to_header_value`](ContentEncoding::to_header_value) does the
//! header.
//!
//! # Responses
//!
//! - Use [`AcceptEncoding`] to parse the advertised supported coding set from the request.
//! - Use [`decode`] to take one layer off a response: it decodes the body and updates the headers
//!   together.
//! - A body encoded more than once takes one call per layer.
//!
//! ```
//! use std::pin::Pin;
//!
//! use bytes::Bytes;
//! use futures::TryStreamExt as _;
//! use http::{HeaderMap, HeaderValue};
//! use web_faith_encoding::{
//!     Coding,
//!     request::encode,
//!     response::{AcceptEncoding, ByteStream, decode},
//! };
//!
//! # async fn example() {
//! let mut request = HeaderMap::new();
//! request.insert("accept-encoding", HeaderValue::from_static("gzip, br;q=0.5"));
//! let accept = AcceptEncoding::from(&request);
//!
//! // A body gzipped over a coding the caller applied themselves.
//! let mut response = HeaderMap::new();
//! response.insert("content-encoding", HeaderValue::from_static("custom-thing"));
//! let gzipped = encode(&mut response, b"pretend this is custom-thing", Coding::Gzip)
//!     .await
//!     .expect("gzip compresses");
//! assert_eq!(response["content-encoding"], "custom-thing, gzip");
//!
//! let body: Pin<Box<ByteStream>> =
//!     Box::pin(futures::stream::once(async move { Ok(Bytes::from(gzipped)) }));
//! let decoded: Vec<u8> = decode(&mut response, body, &accept)
//!     .try_fold(Vec::new(), |mut acc, chunk| async move {
//!         acc.extend_from_slice(&chunk);
//!         Ok(acc)
//!     })
//!     .await
//!     .expect("the gzip decodes");
//!
//! // gzip came off, and the header names what is still under it.
//! assert_eq!(decoded, b"pretend this is custom-thing");
//! assert_eq!(response["content-encoding"], "custom-thing");
//! # }
//! ```
//!
//! [`encode`]: request::encode
//! [`encode_stream`]: request::encode_stream
//! [`compress_buffer`]: request::compress_buffer
//! [`compress_stream`]: request::compress_stream
//! [`AcceptEncoding`]: response::AcceptEncoding
//! [`decode`]: response::decode
//! [`decode_stream`]: response::decode_stream

#![deny(missing_docs)]
// Lets docs.rs label each item with the feature or platform it needs.
#![cfg_attr(docsrs, feature(doc_cfg))]

pub mod request;
pub mod response;

use std::fmt;

use http::header::{CONTENT_ENCODING, CONTENT_LENGTH, HeaderMap, HeaderValue};

use crate::response::AcceptEncoding;

/// The codings a response says its body carries.
///
/// Read from every `Content-Encoding` line together: a representation encoded more than once may
/// arrive comma-joined on one line or split across several, and it is the same list either way.
#[derive(Clone, Debug, Default)]
pub struct ContentEncoding {
	codings: Vec<Coding>,
	/// A line that was not valid ASCII, so what the body carries is not knowable.
	unreadable: bool,
}

impl From<&str> for ContentEncoding {
	/// Read one `Content-Encoding` header value.
	fn from(value: &str) -> Self {
		let mut this = Self::default();
		this.merge(value);
		this
	}
}

impl From<&HeaderMap> for ContentEncoding {
	fn from(headers: &HeaderMap) -> Self {
		let mut this = Self::default();
		for value in headers.get_all(CONTENT_ENCODING) {
			let Ok(value) = value.to_str() else {
				this.unreadable = true;
				continue;
			};
			this.merge(value);
		}
		this
	}
}

impl ContentEncoding {
	/// Fold one header value's codings into what is already here.
	fn merge(&mut self, value: &str) {
		self.codings.extend(
			value
				.split(',')
				.map(str::trim)
				.filter(|token| !token.is_empty())
				.map(Coding::from_token),
		);
	}

	/// The codings the header carried, in the order it applied them.
	///
	/// Empty for a response that declared none, and for one whose header could not be read.
	pub fn codings(&self) -> &[Coding] {
		&self.codings
	}

	/// Add a coding on top of the ones already here.
	///
	/// Applied last, so it is last in the header: the codings are listed in the order they were
	/// applied, and a reader unwinds them in reverse.
	pub fn layer(&self, coding: Coding) -> Self {
		let mut layered = self.clone();
		layered.codings.push(coding);
		layered
	}

	/// The header value these codings make, or `None` when there are none to declare.
	pub fn to_header_value(&self) -> Option<HeaderValue> {
		if self.codings.is_empty() {
			return None;
		}

		HeaderValue::from_str(&self.to_string()).ok()
	}

	/// The coding the next layer of the body is under, given what the request accepted.
	///
	/// The last coding, being the last applied and so the first to unwind. `None` leaves the body
	/// as it arrived, which covers a response that declared no coding, one whose outermost coding
	/// this crate cannot decode (`identity` among them) or the request did not accept, and one
	/// whose header was not readable.
	pub fn can_decode_as(&self, accept: &AcceptEncoding) -> Option<Coding> {
		if self.unreadable {
			return None;
		}

		let outermost = self.codings.last()?;
		(outermost.is_supported() && accept.accepts(outermost)).then(|| outermost.clone())
	}

	/// These codings with the outermost removed, as the body stands once it is decoded.
	pub fn peeled(&self) -> Self {
		let mut peeled = self.clone();
		peeled.codings.pop();
		peeled
	}

	/// Take one layer off `headers`, returning the coding its body is under.
	///
	/// The headers are left describing the body once that coding has been decoded, which
	/// [`response::decode`] does in the same call. Reach for this only to
	/// drive the two halves separately.
	///
	/// `Content-Encoding` keeps whatever layers remain and goes when none do; `Content-Length`
	/// goes either way, no longer describing what the caller reads. `None` leaves `headers` as
	/// they are.
	pub fn peel_one_header(headers: &mut HeaderMap, accept: &AcceptEncoding) -> Option<Coding> {
		let encoding = Self::from(&*headers);
		let coding = encoding.can_decode_as(accept)?;

		match encoding.peeled().to_header_value() {
			Some(value) => headers.insert(CONTENT_ENCODING, value),
			None => headers.remove(CONTENT_ENCODING),
		};
		headers.remove(CONTENT_LENGTH);

		Some(coding)
	}
}

impl fmt::Display for ContentEncoding {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		for (n, coding) in self.codings.iter().enumerate() {
			if n > 0 {
				f.write_str(", ")?;
			}
			f.write_str(coding.token())?;
		}
		Ok(())
	}
}

/// A content coding.
///
/// The four this crate decodes, and [`Other`](Self::Other) for any token it does not. Marked
/// non-exhaustive: a coding that becomes standard should not be a breaking change here.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Coding {
	/// [RFC 1952](https://www.rfc-editor.org/rfc/rfc1952).
	Gzip,
	/// In its zlib-wrapped form ([RFC 1950](https://www.rfc-editor.org/rfc/rfc1950)), like browsers.
	Deflate,
	/// [RFC 7932](https://www.rfc-editor.org/rfc/rfc7932).
	Brotli,
	/// [RFC 8878](https://www.rfc-editor.org/rfc/rfc8878).
	Zstd,
	/// A coding named on the wire that this crate does not decode, lowercased.
	///
	/// Includes `identity`, which means the absence of a coding rather than one to apply.
	Other(String),
}

impl Coding {
	/// Match the `compress` option's value, given as a coding's wire token.
	///
	/// Matches the four documented tokens exactly. [`Self::from_token`] reads off the wire and so
	/// takes a token as loosely as HTTP writes it.
	// spec:ENC#compressing-a-request-body
	pub fn from_option(value: &str) -> Option<Self> {
		match value {
			"gzip" => Some(Self::Gzip),
			"deflate" => Some(Self::Deflate),
			"br" => Some(Self::Brotli),
			"zstd" => Some(Self::Zstd),
			_ => None,
		}
	}

	/// The wire token for this coding in a `Content-Encoding`.
	pub fn token(&self) -> &str {
		match self {
			Self::Gzip => "gzip",
			Self::Deflate => "deflate",
			Self::Brotli => "br",
			Self::Zstd => "zstd",
			Self::Other(token) => token,
		}
	}

	/// Read a content-coding token, case-insensitively.
	///
	/// Anything this crate does not decode, `identity` included, becomes
	/// [`Other`](Self::Other); see [`is_supported`](Self::is_supported).
	pub fn from_token(token: &str) -> Self {
		let token = token.trim();
		if token.eq_ignore_ascii_case("gzip") || token.eq_ignore_ascii_case("x-gzip") {
			Self::Gzip
		} else if token.eq_ignore_ascii_case("deflate") {
			Self::Deflate
		} else if token.eq_ignore_ascii_case("br") {
			Self::Brotli
		} else if token.eq_ignore_ascii_case("zstd") {
			Self::Zstd
		} else {
			Self::Other(token.to_ascii_lowercase())
		}
	}

	/// Whether this crate can decode this coding.
	pub fn is_supported(&self) -> bool {
		!matches!(self, Self::Other(_))
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	use http::header::HeaderValue;

	fn headers(value: &str) -> HeaderMap {
		let mut headers = HeaderMap::new();
		headers.insert(CONTENT_ENCODING, HeaderValue::from_str(value).unwrap());
		headers
	}

	#[test]
	fn a_layered_coding_is_added_last() {
		let existing = ContentEncoding::from(&headers("gzip"));
		let layered = existing.layer(Coding::Zstd);
		assert_eq!(layered.to_string(), "gzip, zstd");
		assert_eq!(
			ContentEncoding::from(&headers("gzip, br"))
				.layer(Coding::Deflate)
				.to_string(),
			"gzip, br, deflate"
		);

		// Layering leaves what it was called on alone.
		assert_eq!(existing.to_string(), "gzip");
	}

	#[test]
	fn a_request_declaring_nothing_carries_only_the_layered_coding() {
		assert_eq!(
			ContentEncoding::default().layer(Coding::Brotli).to_string(),
			"br"
		);
		// An empty or blank header declares nothing.
		for value in ["", "  "] {
			assert_eq!(
				ContentEncoding::from(&headers(value))
					.layer(Coding::Gzip)
					.to_string(),
				"gzip"
			);
		}
	}

	#[test]
	fn a_coding_this_crate_cannot_apply_is_still_declarable() {
		// The caller compressed in a coding of their own and declared it; Faith layers gzip over
		// the top, and the header names both in the order they were applied.
		let layered = ContentEncoding::from(&headers("custom-thing")).layer(Coding::Gzip);
		assert_eq!(layered.to_string(), "custom-thing, gzip");
		assert_eq!(
			layered.codings(),
			[Coding::Other("custom-thing".into()), Coding::Gzip]
		);

		// Applying it is another matter: this crate has no encoder for it.
		assert!(
			futures::executor::block_on(crate::request::compress_buffer(
				b"x",
				Coding::Other("custom-thing".into())
			))
			.is_err()
		);
	}

	#[test]
	fn nothing_to_declare_makes_no_header() {
		assert!(ContentEncoding::default().to_header_value().is_none());
		assert_eq!(
			ContentEncoding::default()
				.layer(Coding::Gzip)
				.to_header_value()
				.unwrap(),
			"gzip"
		);
	}

	#[test]
	fn the_compress_option_names_a_coding_by_its_wire_token() {
		assert_eq!(Coding::from_option("gzip"), Some(Coding::Gzip));
		assert_eq!(Coding::from_option("deflate"), Some(Coding::Deflate));
		assert_eq!(Coding::from_option("br"), Some(Coding::Brotli));
		assert_eq!(Coding::from_option("zstd"), Some(Coding::Zstd));
	}
	#[test]
	fn the_compress_option_matches_its_tokens_exactly() {
		// Loose on the wire, exact as an API: `x-gzip` and a shouted token are read off a
		// `Content-Encoding` but refused as option values.
		assert_eq!(Coding::from_token("x-gzip"), Coding::Gzip);
		assert_eq!(Coding::from_option("x-gzip"), None);
		assert_eq!(Coding::from_token("GZIP"), Coding::Gzip);
		assert_eq!(Coding::from_option("GZIP"), None);
		assert_eq!(Coding::from_option(" gzip"), None);
		assert_eq!(Coding::from_option("brotli"), None);
		assert_eq!(Coding::from_option("identity"), None);
		assert_eq!(Coding::from_option(""), None);
	}

	#[test]
	fn a_coding_this_crate_cannot_decode_is_still_named() {
		let identity = Coding::from_token("identity");
		assert_eq!(identity, Coding::Other("identity".into()));
		assert!(!identity.is_supported());
		assert_eq!(identity.token(), "identity");

		// Lowercased, so two spellings of one coding are one value.
		assert_eq!(Coding::from_token("LZMA"), Coding::from_token("lzma"));
		assert!(Coding::Gzip.is_supported());
	}
}