ruma_common/
http_headers.rs1use 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
17pub const APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json");
19
20pub const APPLICATION_OCTET_STREAM: HeaderValue =
22 HeaderValue::from_static("application/octet-stream");
23
24pub const CROSS_ORIGIN_RESOURCE_POLICY: HeaderName =
28 HeaderName::from_static("cross-origin-resource-policy");
29
30pub 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
54pub fn is_token(bytes: &[u8]) -> bool {
58 bytes.iter().all(|b| is_tchar(*b))
59}
60
61pub fn is_token_string(s: &str) -> bool {
65 is_token(s.as_bytes())
66}
67
68pub const fn is_vchar(c: char) -> bool {
72 matches!(c, '\x21'..='\x7E')
73}
74
75pub const fn is_ascii_string_quotable(c: char) -> bool {
82 is_vchar(c) || matches!(c, '\x09' | '\x20')
83}
84
85pub 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
96pub 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
111pub 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
123pub 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
137pub 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}