Skip to content

Commit

Permalink
Add web push headers
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Sep 13, 2024
1 parent 3775b18 commit 8d5c550
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn anonymize_url(url_in: &str) -> String {
}

pub async fn ping(url: Url) -> Result<reqwest::Response> {
let res = post_allowed::post_allowed(url, &json!({"test":true})).await?;
let res = post_allowed::post_allowed(url, &json!({"test":true}), Some("test")).await?;
res.error_for_status_ref()?;
Ok(res)
}
20 changes: 17 additions & 3 deletions src/utils/post_allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ impl Resolve for ResolveNothing {
}
}

pub async fn post_allowed<T: Serialize + ?Sized>(url: Url, body: &T) -> Result<reqwest::Response> {
pub async fn post_allowed<T: Serialize + ?Sized>(
url: Url,
body: &T,
topic: Option<&str>,
) -> Result<reqwest::Response> {
let port = match url.port() {
Some(p) => p,
None if url.scheme() == "http" => 80,
Expand Down Expand Up @@ -80,8 +84,17 @@ pub async fn post_allowed<T: Serialize + ?Sized>(url: Url, body: &T) -> Result<r
}
.build()
.unwrap();

Ok(client.post(url).json(&body).send().await?)
let mut builder = client
.post(url)
.header("TTL", "2592000") // 30 days
.header("Content-Encoding", "aes128gcm") // Fake this encoding to be web push compliant
.header("Urgency", "high");
builder = if let Some(topic) = topic {
builder.header("Topic", topic) // Should override previous push messages with same topic
} else {
builder
};
Ok(builder.json(&body).send().await?)
}

#[async_trait]
Expand Down Expand Up @@ -151,6 +164,7 @@ mod tests {
post_allowed(
Url::from_str("https://httpbin.org/post").unwrap(),
&json!({"urgent": true}),
None,
)
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/ws/signalwebsocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl SignalWebSocket {
}

let url = self.push_endpoint.clone();
let _ = post_allowed(url, &json!({"urgent": true})).await;
let _ = post_allowed(url, &json!({"urgent": true}), Some("mollysocket")).await;
if let Some(tx) = &self.channels.on_push_tx {
let _ = tx.unbounded_send(1);
}
Expand Down

0 comments on commit 8d5c550

Please sign in to comment.