Skip to content

Commit bd9fd2a

Browse files
committed
Merge pull request #431 from brson/http
Support HTTP protocol again
2 parents c8f6b09 + 3e1e104 commit bd9fd2a

File tree

1 file changed

+81
-77
lines changed

1 file changed

+81
-77
lines changed

src/rustup-utils/src/raw.rs

Lines changed: 81 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -173,102 +173,106 @@ pub fn download_file<P: AsRef<Path>>(url: hyper::Url,
173173
use std::net::{SocketAddr, Shutdown};
174174
use std::sync::{Arc, Mutex};
175175

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

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

184-
struct NativeSslClient;
185-
186-
impl<T: NetworkStream + Send + Clone> SslClient<T> for NativeSslClient {
187-
type Stream = NativeSslStream<T>;
181+
// All the following is adapter code to use native_tls with hyper.
188182

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

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

198-
Ok(NativeSslStream(Arc::new(Mutex::new(ssl_stream))))
199-
}
200-
}
188+
fn wrap_client(&self, stream: T, host: &str) -> HyperResult<Self::Stream> {
189+
use native_tls::ClientBuilder as TlsClientBuilder;
190+
use hyper::error::Error as HyperError;
191+
192+
let mut ssl_builder = try!(TlsClientBuilder::new()
193+
.map_err(|e| HyperError::Ssl(Box::new(e))));
194+
let ssl_stream = try!(ssl_builder.handshake(host, stream)
195+
.map_err(|e| HyperError::Ssl(Box::new(e))));
201196

202-
#[derive(Clone)]
203-
struct NativeSslStream<T>(Arc<Mutex<native_tls::TlsStream<T>>>);
197+
Ok(NativeSslStream(Arc::new(Mutex::new(ssl_stream))))
198+
}
199+
}
204200

205-
#[derive(Debug)]
206-
struct NativeSslPoisonError;
201+
#[derive(Clone)]
202+
struct NativeSslStream<T>(Arc<Mutex<native_tls::TlsStream<T>>>);
207203

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

212-
impl ::std::fmt::Display for NativeSslPoisonError {
213-
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
214-
f.write_str(::std::error::Error::description(self))
207+
impl ::std::error::Error for NativeSslPoisonError {
208+
fn description(&self) -> &str { "mutex poisoned during TLS operation" }
215209
}
216-
}
217210

218-
impl<T> NetworkStream for NativeSslStream<T>
219-
where T: NetworkStream
220-
{
221-
fn peer_addr(&mut self) -> IoResult<SocketAddr> {
222-
self.0.lock()
223-
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
224-
.and_then(|mut t| t.get_mut().peer_addr())
225-
}
226-
fn set_read_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
227-
self.0.lock()
228-
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
229-
.and_then(|t| t.get_ref().set_read_timeout(dur))
230-
}
231-
fn set_write_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
232-
self.0.lock()
233-
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
234-
.and_then(|t| t.get_ref().set_write_timeout(dur))
235-
}
236-
fn close(&mut self, how: Shutdown) -> IoResult<()> {
237-
self.0.lock()
238-
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
239-
.and_then(|mut t| t.get_mut().close(how))
211+
impl ::std::fmt::Display for NativeSslPoisonError {
212+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
213+
f.write_str(::std::error::Error::description(self))
214+
}
240215
}
241-
}
242216

243-
impl<T> Read for NativeSslStream<T>
244-
where T: Read + Write
245-
{
246-
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
247-
self.0.lock()
248-
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
249-
.and_then(|mut t| t.read(buf))
217+
impl<T> NetworkStream for NativeSslStream<T>
218+
where T: NetworkStream
219+
{
220+
fn peer_addr(&mut self) -> IoResult<SocketAddr> {
221+
self.0.lock()
222+
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
223+
.and_then(|mut t| t.get_mut().peer_addr())
224+
}
225+
fn set_read_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
226+
self.0.lock()
227+
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
228+
.and_then(|t| t.get_ref().set_read_timeout(dur))
229+
}
230+
fn set_write_timeout(&self, dur: Option<Duration>) -> IoResult<()> {
231+
self.0.lock()
232+
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
233+
.and_then(|t| t.get_ref().set_write_timeout(dur))
234+
}
235+
fn close(&mut self, how: Shutdown) -> IoResult<()> {
236+
self.0.lock()
237+
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
238+
.and_then(|mut t| t.get_mut().close(how))
239+
}
250240
}
251-
}
252241

253-
impl<T> Write for NativeSslStream<T>
254-
where T: Read + Write
255-
{
256-
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
257-
self.0.lock()
258-
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
259-
.and_then(|mut t| t.write(buf))
242+
impl<T> Read for NativeSslStream<T>
243+
where T: Read + Write
244+
{
245+
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
246+
self.0.lock()
247+
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
248+
.and_then(|mut t| t.read(buf))
249+
}
260250
}
261-
fn flush(&mut self) -> IoResult<()> {
262-
self.0.lock()
263-
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
264-
.and_then(|mut t| t.flush())
251+
252+
impl<T> Write for NativeSslStream<T>
253+
where T: Read + Write
254+
{
255+
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
256+
self.0.lock()
257+
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
258+
.and_then(|mut t| t.write(buf))
259+
}
260+
fn flush(&mut self) -> IoResult<()> {
261+
self.0.lock()
262+
.map_err(|_| io::Error::new(io::ErrorKind::Other, NativeSslPoisonError))
263+
.and_then(|mut t| t.flush())
264+
}
265265
}
266-
}
267266

268-
maybe_init_certs();
267+
maybe_init_certs();
269268

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

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

0 commit comments

Comments
 (0)