aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-03-27 09:37:36 -0600
committerMelody Horn <melody@boringcactus.com>2021-03-27 09:37:36 -0600
commit79698f03acc59e7c1131dd9a57f0c17d17b33b1a (patch)
treed5980dfcabc12c101e8cc3e873d6c228744dccf9
parent691b671560993081d2ba25f0cc7e5b6a370e1b4c (diff)
downloadwebget-79698f03acc59e7c1131dd9a57f0c17d17b33b1a.tar.gz
webget-79698f03acc59e7c1131dd9a57f0c17d17b33b1a.zip
follow redirects
-rw-r--r--src/download.rs14
1 files changed, 12 insertions, 2 deletions
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);
}