Allow custom HTTP headers to be set (#52)

This commit is contained in:
mdecimus
2023-08-08 19:21:01 +02:00
parent 132e86046e
commit ca29121ad9
5 changed files with 47 additions and 2 deletions

View File

@@ -21,7 +21,7 @@
* for more details.
*/
use std::time::Duration;
use std::{str::FromStr, time::Duration};
use store::{
fts::Language,
@@ -140,6 +140,32 @@ impl crate::Config {
.unwrap_or(true),
encrypt: settings.property_or_static("jmap.encryption.enable", "true")?,
encrypt_append: settings.property_or_static("jmap.encryption.append", "false")?,
http_headers: settings
.values("jmap.http.headers")
.map(|(_, v)| {
if let Some((k, v)) = v.split_once(':') {
Ok((
hyper::header::HeaderName::from_str(k.trim()).map_err(|err| {
format!(
"Invalid header found in property \"jmap.http.headers\": {}",
err
)
})?,
hyper::header::HeaderValue::from_str(v.trim()).map_err(|err| {
format!(
"Invalid header found in property \"jmap.http.headers\": {}",
err
)
})?,
))
} else {
Err(format!(
"Invalid header found in property \"jmap.http.headers\": {}",
v
))
}
})
.collect::<Result<Vec<_>, String>>()?,
};
config.add_capabilites(settings);
Ok(config)

View File

@@ -446,7 +446,18 @@ async fn handle_request<T: AsyncRead + AsyncWrite + Unpin + Send + 'static>(
uri = req.uri().to_string(),
);
let response = parse_jmap_request(jmap, req, session.remote_ip, instance).await;
// Parse JMAP request
let mut response =
parse_jmap_request(jmap.clone(), req, session.remote_ip, instance).await;
// Add custom headers
if !jmap.config.http_headers.is_empty() {
let headers = response.headers_mut();
for (header, value) in &jmap.config.http_headers {
headers.insert(header.clone(), value.clone());
}
}
Ok::<_, hyper::Error>(response)
}

View File

@@ -150,6 +150,8 @@ pub struct Config {
pub oauth_expiry_refresh_token_renew: u64,
pub oauth_max_auth_attempts: u32,
pub http_headers: Vec<(hyper::header::HeaderName, hyper::header::HeaderValue)>,
pub encrypt: bool,
pub encrypt_append: bool,