From 02561b614f80ea318563f7ad8cff3efe8c161bf7 Mon Sep 17 00:00:00 2001 From: Peter Rice Date: Sun, 1 Sep 2019 07:55:45 -0400 Subject: [PATCH] Parse url login info into basic auth header --- podcasts-data/Cargo.toml | 1 + podcasts-data/src/models/source.rs | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/podcasts-data/Cargo.toml b/podcasts-data/Cargo.toml index 13cd60f3..7ade9cec 100644 --- a/podcasts-data/Cargo.toml +++ b/podcasts-data/Cargo.toml @@ -25,6 +25,7 @@ native-tls = "0.2.2" num_cpus = "1.10.0" failure = "0.1.5" failure_derive = "0.1.5" +base64 = "0.10.1" [dependencies.diesel] features = ["sqlite", "r2d2"] diff --git a/podcasts-data/src/models/source.rs b/podcasts-data/src/models/source.rs index a7d5c546..3dba37e1 100644 --- a/podcasts-data/src/models/source.rs +++ b/podcasts-data/src/models/source.rs @@ -27,7 +27,7 @@ use hyper::{Body, Client}; use hyper_tls::HttpsConnector; use http::header::{ - HeaderValue, ETAG, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED, LOCATION, + HeaderValue, AUTHORIZATION, ETAG, IF_MODIFIED_SINCE, IF_NONE_MATCH, LAST_MODIFIED, LOCATION, USER_AGENT as USER_AGENT_HEADER, }; use http::{Request, Response, StatusCode, Uri}; @@ -35,6 +35,8 @@ use http::{Request, Response, StatusCode, Uri}; use futures::future::{loop_fn, Future, Loop}; use futures::prelude::*; +use base64::{encode_config, URL_SAFE}; + use crate::database::connection; use crate::errors::*; use crate::feed::{Feed, FeedBuilder}; @@ -276,6 +278,18 @@ impl Source { let uri = Uri::from_str(self.uri()).unwrap(); let mut req = Request::get(uri).body(Body::empty()).unwrap(); + if let Ok(url) = Url::parse(self.uri()) { + if let Some(password) = url.password() { + let mut auth = "Basic ".to_owned(); + auth.push_str(&encode_config( + &format!("{}:{}", url.username(), password), + URL_SAFE, + )); + req.headers_mut() + .insert(AUTHORIZATION, HeaderValue::from_str(&auth).unwrap()); + } + } + // Set the UserAgent cause ppl still seem to check it for some reason... req.headers_mut() .insert(USER_AGENT_HEADER, HeaderValue::from_static(USER_AGENT)); -- GitLab