Skip to main content

ruma_common/
http_headers.rs

1//! Helpers for HTTP headers.
2
3use std::borrow::Cow;
4
5use http::{HeaderValue, header::HeaderName};
6use web_time::{Duration, SystemTime, UNIX_EPOCH};
7
8mod content_disposition;
9mod rfc8187;
10
11pub use self::content_disposition::{
12    ContentDisposition, ContentDispositionParseError, ContentDispositionType, TokenString,
13    TokenStringParseError,
14};
15use crate::api::error::{HeaderDeserializationError, HeaderSerializationError};
16
17/// The `application/json` media type as a [`HeaderValue`].
18pub const APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json");
19
20/// The `application/octet-stream` media type as a [`HeaderValue`].
21pub const APPLICATION_OCTET_STREAM: HeaderValue =
22    HeaderValue::from_static("application/octet-stream");
23
24/// The [`Cross-Origin-Resource-Policy`] HTTP response header.
25///
26/// [`Cross-Origin-Resource-Policy`]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy
27pub const CROSS_ORIGIN_RESOURCE_POLICY: HeaderName =
28    HeaderName::from_static("cross-origin-resource-policy");
29
30/// Whether the given byte is a [`token` char].
31///
32/// [`token` char]: https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.2
33pub const fn is_tchar(b: u8) -> bool {
34    b.is_ascii_alphanumeric()
35        || matches!(
36            b,
37            b'!' | b'#'
38                | b'$'
39                | b'%'
40                | b'&'
41                | b'\''
42                | b'*'
43                | b'+'
44                | b'-'
45                | b'.'
46                | b'^'
47                | b'_'
48                | b'`'
49                | b'|'
50                | b'~'
51        )
52}
53
54/// Whether the given bytes slice is a [`token`].
55///
56/// [`token`]: https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.2
57pub fn is_token(bytes: &[u8]) -> bool {
58    bytes.iter().all(|b| is_tchar(*b))
59}
60
61/// Whether the given string is a [`token`].
62///
63/// [`token`]: https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.2
64pub fn is_token_string(s: &str) -> bool {
65    is_token(s.as_bytes())
66}
67
68/// Whether the given char is a [visible US-ASCII char].
69///
70/// [visible US-ASCII char]: https://datatracker.ietf.org/doc/html/rfc5234#appendix-B.1
71pub const fn is_vchar(c: char) -> bool {
72    matches!(c, '\x21'..='\x7E')
73}
74
75/// Whether the given char is in the US-ASCII character set and allowed inside a [quoted string].
76///
77/// Contrary to the definition of quoted strings, this doesn't allow `obs-text` characters, i.e.
78/// non-US-ASCII characters, as we usually deal with UTF-8 strings rather than ISO-8859-1 strings.
79///
80/// [quoted string]: https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.4
81pub const fn is_ascii_string_quotable(c: char) -> bool {
82    is_vchar(c) || matches!(c, '\x09' | '\x20')
83}
84
85/// Remove characters that do not pass [`is_ascii_string_quotable()`] from the given string.
86///
87/// [quoted string]: https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.4
88pub fn sanitize_for_ascii_quoted_string(value: &str) -> Cow<'_, str> {
89    if value.chars().all(is_ascii_string_quotable) {
90        return Cow::Borrowed(value);
91    }
92
93    Cow::Owned(value.chars().filter(|c| is_ascii_string_quotable(*c)).collect())
94}
95
96/// If the US-ASCII field value does not contain only token chars, convert it to a [quoted string].
97///
98/// The string should be sanitized with [`sanitize_for_ascii_quoted_string()`] or should only
99/// contain characters that pass [`is_ascii_string_quotable()`].
100///
101/// [quoted string]: https://datatracker.ietf.org/doc/html/rfc9110#section-5.6.4
102pub fn quote_ascii_string_if_required(value: &str) -> Cow<'_, str> {
103    if !value.is_empty() && is_token_string(value) {
104        return Cow::Borrowed(value);
105    }
106
107    let value = value.replace('\\', r#"\\"#).replace('"', r#"\""#);
108    Cow::Owned(format!("\"{value}\""))
109}
110
111/// Removes the escape backslashes in the given string.
112pub fn unescape_string(s: &str) -> String {
113    let mut is_escaped = false;
114
115    s.chars()
116        .filter(|c| {
117            is_escaped = *c == '\\' && !is_escaped;
118            !is_escaped
119        })
120        .collect()
121}
122
123/// Convert as `SystemTime` to a HTTP date header value.
124pub fn system_time_to_http_date(
125    time: &SystemTime,
126) -> Result<HeaderValue, HeaderSerializationError> {
127    let mut buffer = [0; 29];
128
129    let duration =
130        time.duration_since(UNIX_EPOCH).map_err(|_| HeaderSerializationError::InvalidHttpDate)?;
131    date_header::format(duration.as_secs(), &mut buffer)
132        .map_err(|_| HeaderSerializationError::InvalidHttpDate)?;
133
134    Ok(HeaderValue::from_bytes(&buffer).expect("date_header should produce a valid header value"))
135}
136
137/// Convert a header value representing a HTTP date to a `SystemTime`.
138pub fn http_date_to_system_time(
139    value: &HeaderValue,
140) -> Result<SystemTime, HeaderDeserializationError> {
141    let bytes = value.as_bytes();
142
143    let ts = date_header::parse(bytes).map_err(|_| HeaderDeserializationError::InvalidHttpDate)?;
144
145    UNIX_EPOCH
146        .checked_add(Duration::from_secs(ts))
147        .ok_or(HeaderDeserializationError::InvalidHttpDate)
148}