Skip to content

Commit

Permalink
simplify racing gateway selection
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Oct 24, 2022
1 parent 0bdd9d5 commit df5f9f1
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions iroh-resolver/src/racing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ impl RacingLoader {
pub fn try_raw_gateway(&self) -> Result<&String> {
match self.http_resolvers.len() {
0 => Err(anyhow!("no gateway configured to fetch raw CIDs")),
1 => self
.http_resolvers
.first()
.ok_or_else(|| anyhow!("no gateway configured to fetch raw CIDs")),
_ => {
let mut rng = rand::thread_rng();
let gw = self.http_resolvers.choose(&mut rng).unwrap();
Expand All @@ -50,10 +46,13 @@ impl RacingLoader {
async fn fetch_http(&self, cid: &Cid) -> Result<(Bytes, String), anyhow::Error> {
let gateway = self.try_raw_gateway()?;
let cid_str = multibase::encode(multibase::Base::Base32Lower, cid.to_bytes().as_slice());
let mut gateway_url = format!("https://{}.ipfs.{}?format=raw", cid_str, gateway);
if gateway.starts_with("https://") || gateway.starts_with("http://") {
gateway_url = format!("{}/ipfs/{}?format=raw", gateway, cid_str);
}
// support two gateway URL formats: subdomain gateways (eg: dweb.link)
// and full URL (eg: https://ipfs.io)
let gateway_url = if gateway.starts_with("https://") || gateway.starts_with("http://") {
format!("{}/ipfs/{}?format=raw", gateway, cid_str)
} else {
format!("https://{}.ipfs.{}?format=raw", cid_str, gateway)
};
debug!("Will fetch {}", gateway_url);
let response = reqwest::get(gateway_url).await?;
response
Expand Down

0 comments on commit df5f9f1

Please sign in to comment.