Parse url login info into basic auth header

This commit is contained in:
Peter Rice 2019-09-01 07:55:45 -04:00
parent 6ca2d02c69
commit 02561b614f
2 changed files with 16 additions and 1 deletions

View File

@ -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"]

View File

@ -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));