ruma_client_api/
http_headers.rs1#![allow(clippy::declare_interior_mutable_const)]
3
4use http::{header::HeaderName, HeaderValue};
5use ruma_common::api::error::{HeaderDeserializationError, HeaderSerializationError};
6pub use ruma_common::http_headers::{
7 ContentDisposition, ContentDispositionParseError, ContentDispositionType, TokenString,
8 TokenStringParseError,
9};
10use web_time::{Duration, SystemTime, UNIX_EPOCH};
11
12pub const CROSS_ORIGIN_RESOURCE_POLICY: HeaderName =
16 HeaderName::from_static("cross-origin-resource-policy");
17
18pub fn system_time_to_http_date(
20 time: &SystemTime,
21) -> Result<HeaderValue, HeaderSerializationError> {
22 let mut buffer = [0; 29];
23
24 let duration =
25 time.duration_since(UNIX_EPOCH).map_err(|_| HeaderSerializationError::InvalidHttpDate)?;
26 date_header::format(duration.as_secs(), &mut buffer)
27 .map_err(|_| HeaderSerializationError::InvalidHttpDate)?;
28
29 Ok(HeaderValue::from_bytes(&buffer).expect("date_header should produce a valid header value"))
30}
31
32pub fn http_date_to_system_time(
34 value: &HeaderValue,
35) -> Result<SystemTime, HeaderDeserializationError> {
36 let bytes = value.as_bytes();
37
38 let ts = date_header::parse(bytes).map_err(|_| HeaderDeserializationError::InvalidHttpDate)?;
39
40 UNIX_EPOCH
41 .checked_add(Duration::from_secs(ts))
42 .ok_or(HeaderDeserializationError::InvalidHttpDate)
43}