feat: include user-agent in request headers

This commit is contained in:
Aravinth Manivannan 2024-11-19 21:54:50 +05:30
parent 169137be02
commit cf133283e4
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 18 additions and 4 deletions

View file

@ -12,7 +12,7 @@ use crate::{
};
use bytes::Bytes;
use futures::StreamExt;
use http::StatusCode;
use http::{header::USER_AGENT, StatusCode};
use httpdate::fmt_http_date;
use itertools::Itertools;
use reqwest::{
@ -39,6 +39,7 @@ pub struct SendActivityTask {
pub(crate) inbox: Url,
pub(crate) private_key: RsaPrivateKey,
pub(crate) http_signature_compat: bool,
pub(crate) user_agent: String,
}
impl Display for SendActivityTask {
@ -83,7 +84,7 @@ impl SendActivityTask {
let request_builder = client
.post(self.inbox.to_string())
.timeout(timeout)
.headers(generate_request_headers(&self.inbox));
.headers(generate_request_headers(&self.inbox, &self.user_agent));
let request = sign_request(
request_builder,
&self.actor_id,
@ -173,6 +174,7 @@ where
activity: activity_serialized.clone(),
private_key: private_key.clone(),
http_signature_compat: config.http_signature_compat,
user_agent: config.user_agent().to_owned(),
})
})
.collect()
@ -211,7 +213,7 @@ where
.map_err(|e| Error::Other(format!("cloned error: {e}")))
}
pub(crate) fn generate_request_headers(inbox_url: &Url) -> HeaderMap {
pub(crate) fn generate_request_headers(inbox_url: &Url, user_agent: &str) -> HeaderMap {
let mut host = inbox_url.domain().expect("read inbox domain").to_string();
if let Some(port) = inbox_url.port() {
host = format!("{}:{}", host, port);
@ -222,6 +224,11 @@ pub(crate) fn generate_request_headers(inbox_url: &Url) -> HeaderMap {
HeaderName::from_static("content-type"),
HeaderValue::from_static(FEDERATION_CONTENT_TYPE),
);
headers.insert(
USER_AGENT,
HeaderValue::from_str(user_agent).expect("Hostname is valid"),
);
headers.insert(
HeaderName::from_static("host"),
HeaderValue::from_str(&host).expect("Hostname is valid"),
@ -230,6 +237,7 @@ pub(crate) fn generate_request_headers(inbox_url: &Url) -> HeaderMap {
"date",
HeaderValue::from_str(&fmt_http_date(SystemTime::now())).expect("Date is valid"),
);
println!("Generating request headers: {:?}", headers);
headers
}

View file

@ -191,6 +191,11 @@ impl<T: Clone> FederationConfig<T> {
pub fn domain(&self) -> &str {
&self.domain
}
/// Returns user-agent
pub fn user_agent(&self) -> &str {
&self.domain
}
}
impl<T: Clone> FederationConfigBuilder<T> {

View file

@ -11,7 +11,7 @@ use crate::{
FEDERATION_CONTENT_TYPE,
};
use bytes::Bytes;
use http::{HeaderValue, StatusCode};
use http::{header::USER_AGENT, HeaderValue, StatusCode};
use serde::de::DeserializeOwned;
use std::sync::atomic::Ordering;
use tracing::info;
@ -115,6 +115,7 @@ async fn fetch_object_http_with_accept<T: Clone, Kind: DeserializeOwned>(
.client
.get(url.as_str())
.header("Accept", content_type)
.header(USER_AGENT, data.config.user_agent())
.timeout(config.request_timeout);
let res = if let Some((actor_id, private_key_pem)) = config.signed_fetch_actor.as_deref() {