Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use auth credentials if specified in URL #112

Merged
merged 2 commits into from
Apr 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
tokio-timer = "0.1"
url = "1.7"
# Optional deps
hyper = { version = "0.11", optional = true }
hyper-tls = { version = "0.1", optional = true }
Expand Down
23 changes: 23 additions & 0 deletions src/transports/http.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! HTTP Transport

extern crate hyper;
extern crate url;

#[cfg(feature = "tls")]
extern crate hyper_tls;
Expand All @@ -19,6 +20,7 @@ use serde_json;
use transports::Result;
use transports::shared::{EventLoopHandle, Response};
use transports::tokio_core::reactor;
use self::url::Url;
use {BatchTransport, Error, ErrorKind, RequestId, Transport};

impl From<hyper::Error> for Error {
Expand Down Expand Up @@ -53,6 +55,7 @@ pub type FetchTask<F> = Response<F, hyper::Chunk>;
pub struct Http {
id: Arc<AtomicUsize>,
url: hyper::Uri,
basic_auth: Option<hyper::header::Basic>,
write_sender: mpsc::UnboundedSender<(hyper::client::Request, Pending)>,
}

Expand Down Expand Up @@ -109,9 +112,24 @@ impl Http {
}),
);

let basic_auth = {
let url = Url::parse(url)?;
let user = url.username();

if user.len() > 0 {
Some(hyper::header::Basic {
username: user.into(),
password: url.password().map(Into::into),
})
} else {
None
}
};

Ok(Http {
id: Default::default(),
url: url.parse()?,
basic_auth,
write_sender,
})
}
Expand All @@ -133,6 +151,11 @@ impl Http {
req.headers_mut()
.set(hyper::header::ContentLength(len as u64));
}
// Send basic auth header
if let Some(ref basic_auth) = self.basic_auth {
req.headers_mut()
.set(hyper::header::Authorization(basic_auth.clone()));
}
req.set_body(request);

let (tx, rx) = futures::oneshot();
Expand Down