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

Support HTTP protocol again #431

Merged
merged 1 commit into from
May 10, 2016
Merged
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
158 changes: 81 additions & 77 deletions src/rustup-utils/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,102 +173,106 @@ pub fn download_file<P: AsRef<Path>>(url: hyper::Url,
use std::net::{SocketAddr, Shutdown};
use std::sync::{Arc, Mutex};

// This is just a defensive measure to make sure I'm not sending
// anything through hyper I haven't tested.
if url.scheme() != "https" {
return Err(format!("unsupported URL scheme: '{}'", url.scheme()).into());
}
// The Hyper HTTP client
let client;

// All the following is adapter code to use native_tls with hyper.
if url.scheme() == "https" {

struct NativeSslClient;

impl<T: NetworkStream + Send + Clone> SslClient<T> for NativeSslClient {
type Stream = NativeSslStream<T>;
// All the following is adapter code to use native_tls with hyper.

fn wrap_client(&self, stream: T, host: &str) -> HyperResult<Self::Stream> {
use native_tls::ClientBuilder as TlsClientBuilder;
use hyper::error::Error as HyperError;
struct NativeSslClient;

let mut ssl_builder = try!(TlsClientBuilder::new()
.map_err(|e| HyperError::Ssl(Box::new(e))));
let ssl_stream = try!(ssl_builder.handshake(host, stream)
.map_err(|e| HyperError::Ssl(Box::new(e))));
impl<T: NetworkStream + Send + Clone> SslClient<T> for NativeSslClient {
type Stream = NativeSslStream<T>;

Ok(NativeSslStream(Arc::new(Mutex::new(ssl_stream))))
}
}
fn wrap_client(&self, stream: T, host: &str) -> HyperResult<Self::Stream> {
use native_tls::ClientBuilder as TlsClientBuilder;
use hyper::error::Error as HyperError;

let mut ssl_builder = try!(TlsClientBuilder::new()
.map_err(|e| HyperError::Ssl(Box::new(e))));
let ssl_stream = try!(ssl_builder.handshake(host, stream)
.map_err(|e| HyperError::Ssl(Box::new(e))));

#[derive(Clone)]
struct NativeSslStream<T>(Arc<Mutex<native_tls::TlsStream<T>>>);
Ok(NativeSslStream(Arc::new(Mutex::new(ssl_stream))))
}
}

#[derive(Debug)]
struct NativeSslPoisonError;
#[derive(Clone)]
struct NativeSslStream<T>(Arc<Mutex<native_tls::TlsStream<T>>>);

impl ::std::error::Error for NativeSslPoisonError {
fn description(&self) -> &str { "mutex poisoned during TLS operation" }
}
#[derive(Debug)]
struct NativeSslPoisonError;

impl ::std::fmt::Display for NativeSslPoisonError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
f.write_str(::std::error::Error::description(self))
impl ::std::error::Error for NativeSslPoisonError {
fn description(&self) -> &str { "mutex poisoned during TLS operation" }
}
}

impl<T> NetworkStream for NativeSslStream<T>
where T: NetworkStream
{
fn peer_addr(&mut self) -> IoResult<SocketAddr> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.get_mut().peer_addr())
}
fn set_read_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|t| t.get_ref().set_read_timeout(dur))
}
fn set_write_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|t| t.get_ref().set_write_timeout(dur))
}
fn close(&mut self, how: Shutdown) -> IoResult<()> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.get_mut().close(how))
impl ::std::fmt::Display for NativeSslPoisonError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
f.write_str(::std::error::Error::description(self))
}
}
}

impl<T> Read for NativeSslStream<T>
where T: Read + Write
{
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.read(buf))
impl<T> NetworkStream for NativeSslStream<T>
where T: NetworkStream
{
fn peer_addr(&mut self) -> IoResult<SocketAddr> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.get_mut().peer_addr())
}
fn set_read_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|t| t.get_ref().set_read_timeout(dur))
}
fn set_write_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|t| t.get_ref().set_write_timeout(dur))
}
fn close(&mut self, how: Shutdown) -> IoResult<()> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.get_mut().close(how))
}
}
}

impl<T> Write for NativeSslStream<T>
where T: Read + Write
{
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.write(buf))
impl<T> Read for NativeSslStream<T>
where T: Read + Write
{
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.read(buf))
}
}
fn flush(&mut self) -> IoResult<()> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.flush())

impl<T> Write for NativeSslStream<T>
where T: Read + Write
{
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.write(buf))
}
fn flush(&mut self) -> IoResult<()> {
self.0.lock()
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
.and_then(|mut t| t.flush())
}
}
}

maybe_init_certs();
maybe_init_certs();

// Connect with hyper + native_tls
let client = Client::with_connector(HttpsConnector::new(NativeSslClient));
// Connect with hyper + native_tls
client = Client::with_connector(HttpsConnector::new(NativeSslClient));
} else if url.scheme() == "http" {
client = Client::new();
} else {
return Err(format!("unsupported URL scheme: '{}'", url.scheme()).into());
}

let mut res = try!(client.get(url).send()
.chain_err(|| "failed to make network request"));
Expand Down