From 3e1e10450caeca253629c80cca9cb731a98c9621 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 9 May 2016 23:35:04 -0700 Subject: [PATCH] Support HTTP protocol again This means that setting RUSTUP_DIST_ROOT=http://static.rust-lang.org/dist and RUSTUP_UPDATE_ROOT=http://static.rust-lang.org/rustup/dist will workaround current Windows TLS problems. --- src/rustup-utils/src/raw.rs | 158 ++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 77 deletions(-) diff --git a/src/rustup-utils/src/raw.rs b/src/rustup-utils/src/raw.rs index 71614289f6..5ce2e1f4c5 100644 --- a/src/rustup-utils/src/raw.rs +++ b/src/rustup-utils/src/raw.rs @@ -173,102 +173,106 @@ pub fn download_file>(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 SslClient for NativeSslClient { - type Stream = NativeSslStream; + // All the following is adapter code to use native_tls with hyper. - fn wrap_client(&self, stream: T, host: &str) -> HyperResult { - 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 SslClient for NativeSslClient { + type Stream = NativeSslStream; - Ok(NativeSslStream(Arc::new(Mutex::new(ssl_stream)))) - } - } + fn wrap_client(&self, stream: T, host: &str) -> HyperResult { + 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(Arc>>); + Ok(NativeSslStream(Arc::new(Mutex::new(ssl_stream)))) + } + } - #[derive(Debug)] - struct NativeSslPoisonError; + #[derive(Clone)] + struct NativeSslStream(Arc>>); - 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 NetworkStream for NativeSslStream - where T: NetworkStream - { - fn peer_addr(&mut self) -> IoResult { - 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) -> 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) -> 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 Read for NativeSslStream - where T: Read + Write - { - fn read(&mut self, buf: &mut [u8]) -> IoResult { - self.0.lock() - .map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError)) - .and_then(|mut t| t.read(buf)) + impl NetworkStream for NativeSslStream + where T: NetworkStream + { + fn peer_addr(&mut self) -> IoResult { + 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) -> 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) -> 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 Write for NativeSslStream - where T: Read + Write - { - fn write(&mut self, buf: &[u8]) -> IoResult { - self.0.lock() - .map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError)) - .and_then(|mut t| t.write(buf)) + impl Read for NativeSslStream + where T: Read + Write + { + fn read(&mut self, buf: &mut [u8]) -> IoResult { + 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 Write for NativeSslStream + where T: Read + Write + { + fn write(&mut self, buf: &[u8]) -> IoResult { + 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"));