From 79698f03acc59e7c1131dd9a57f0c17d17b33b1a Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 27 Mar 2021 09:37:36 -0600 Subject: follow redirects --- src/download.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/download.rs b/src/download.rs index 537dae3..9e126dd 100644 --- a/src/download.rs +++ b/src/download.rs @@ -1,8 +1,11 @@ use std::path::{Path, PathBuf}; -use anyhow::Error; +use anyhow::{Error, Context}; use fehler::throws; -use hyper::{body::HttpBody, Client, header::CONTENT_TYPE, Uri}; +use hyper::Uri; +use hyper::body::HttpBody; +use hyper::client::Client; +use hyper::header::{CONTENT_TYPE, LOCATION}; use tokio::fs; use tokio::io::AsyncWriteExt; @@ -22,6 +25,13 @@ pub(crate) async fn download(url: Uri, args: &Args) { let client = Client::new(); let mut response = client.get(url.clone()).await?; + while response.status().is_redirection() { + let location = response.headers() + .get(LOCATION).context("no Location header in redirect")? + .to_str().context("malformed Location header in redirect")? + .parse().context("non-URL Location header in redirect")?; + response = client.get(location).await?; + } if !response.status().is_success() { panic!("non-success response code {} in URL {}", response.status(), url); } -- cgit v1.2.3