Skip to content

Commit

Permalink
Add User-Agent header to fetch request
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Jul 6, 2024
1 parent 530c516 commit 2f72cbe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/bsky.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::client::{ClientInfo, FetchClient};
use crate::fetch;
use crate::hatebu::Entry;
use atrium_api::app::bsky::embed::external;
use atrium_api::app::bsky::feed::post::RecordEmbedRefs;
Expand All @@ -12,7 +13,7 @@ use atrium_api::types::Union;
use http::Uri;
use std::sync::{Arc, RwLock};
use webpage::HTML;
use worker::{Fetch, Url};
use worker::Url;

pub(crate) struct BskyAgent {
client: AtpServiceClient<FetchClient>,
Expand Down Expand Up @@ -98,7 +99,7 @@ impl BskyAgent {
}
other => other,
}?;
let data = Fetch::Url(url).send().await?.bytes().await?;
let data = fetch(url.as_str()).await?.bytes().await?;
let uploaded = self
.client
.service
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use markup5ever_rcdom::{Handle, NodeData, RcDom};
use mime::Mime;
use std::io::Cursor;
use webpage::HTML;
use worker::Env;
use worker::{console_error, console_log, event, Fetch};
use worker::{console_error, console_log, event, Fetch, Method, Response};
use worker::{Env, Request};
use worker::{ScheduleContext, ScheduledEvent};

const KV_NAMESPACE: &str = "kv";
Expand Down Expand Up @@ -69,7 +69,7 @@ async fn post2bsky(
}

async fn get_webpage(url: &str) -> Result<HTML, Box<dyn std::error::Error>> {
let mut res = Fetch::Url(url.parse()?).send().await?;
let mut res = fetch(url).await?;
let content_type = res
.headers()
.get(http::header::CONTENT_TYPE.as_str())?
Expand Down Expand Up @@ -131,3 +131,9 @@ fn extract_charset(handle: &Handle) -> Option<String> {
}
None
}

pub async fn fetch(uri: &str) -> Result<Response, Box<dyn std::error::Error>> {
let mut req = Request::new(uri, Method::Get)?;
req.headers_mut()?.set("User-Agent", "Cloudflare Workers")?;
Ok(Fetch::Request(req).send().await?)
}

0 comments on commit 2f72cbe

Please sign in to comment.