split NetworkStream::connect to NetworkConnector
This commit is contained in:
		| @@ -6,7 +6,7 @@ use url::Url; | |||||||
| use method::{mod, Get, Post, Delete, Put, Patch, Head, Options}; | use method::{mod, Get, Post, Delete, Put, Patch, Head, Options}; | ||||||
| use header::Headers; | use header::Headers; | ||||||
| use header::common::{mod, Host}; | use header::common::{mod, Host}; | ||||||
| use net::{NetworkStream, HttpStream, Fresh, Streaming}; | use net::{NetworkStream, NetworkConnector, HttpStream, Fresh, Streaming}; | ||||||
| use http::{HttpWriter, ThroughWriter, ChunkedWriter, SizedWriter, LINE_ENDING}; | use http::{HttpWriter, ThroughWriter, ChunkedWriter, SizedWriter, LINE_ENDING}; | ||||||
| use version; | use version; | ||||||
| use {HttpResult, HttpUriError}; | use {HttpResult, HttpUriError}; | ||||||
| @@ -43,7 +43,7 @@ impl Request<Fresh> { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Create a new client request with a specific underlying NetworkStream. |     /// Create a new client request with a specific underlying NetworkStream. | ||||||
|     pub fn with_stream<S: NetworkStream>(method: method::Method, url: Url) -> HttpResult<Request<Fresh>> { |     pub fn with_stream<S: NetworkConnector>(method: method::Method, url: Url) -> HttpResult<Request<Fresh>> { | ||||||
|         debug!("{} {}", method, url); |         debug!("{} {}", method, url); | ||||||
|         let host = match url.serialize_host() { |         let host = match url.serialize_host() { | ||||||
|             Some(host) => host, |             Some(host) => host, | ||||||
| @@ -56,8 +56,8 @@ impl Request<Fresh> { | |||||||
|         }; |         }; | ||||||
|         debug!("port={}", port); |         debug!("port={}", port); | ||||||
|  |  | ||||||
|         let stream: S = try_io!(NetworkStream::connect(host.as_slice(), port, url.scheme.as_slice())); |         let stream: S = try_io!(NetworkConnector::connect(host.as_slice(), port, url.scheme.as_slice())); | ||||||
|         let stream = ThroughWriter(BufferedWriter::new(stream.dynamic())); |         let stream = ThroughWriter(BufferedWriter::new(box stream as Box<NetworkStream + Send>)); | ||||||
|  |  | ||||||
|         let mut headers = Headers::new(); |         let mut headers = Headers::new(); | ||||||
|         headers.set(Host { |         headers.set(Host { | ||||||
|   | |||||||
| @@ -25,7 +25,7 @@ impl Response { | |||||||
|  |  | ||||||
|     /// Creates a new response from a server. |     /// Creates a new response from a server. | ||||||
|     pub fn new(stream: Box<NetworkStream + Send>) -> HttpResult<Response> { |     pub fn new(stream: Box<NetworkStream + Send>) -> HttpResult<Response> { | ||||||
|         let mut stream = BufferedReader::new(stream.dynamic()); |         let mut stream = BufferedReader::new(stream); | ||||||
|         let (version, status) = try!(read_status_line(&mut stream)); |         let (version, status) = try!(read_status_line(&mut stream)); | ||||||
|         let headers = try!(header::Headers::from_raw(&mut stream)); |         let headers = try!(header::Headers::from_raw(&mut stream)); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -408,7 +408,7 @@ mod tests { | |||||||
|     use std::hash::sip::hash; |     use std::hash::sip::hash; | ||||||
|     use mime::{Mime, Text, Plain}; |     use mime::{Mime, Text, Plain}; | ||||||
|     use super::CaseInsensitive; |     use super::CaseInsensitive; | ||||||
|     use super::{Headers, Header}; |     use super::{Headers, Header, HeaderFormat}; | ||||||
|     use super::common::{ContentLength, ContentType, Accept, Host}; |     use super::common::{ContentLength, ContentType, Accept, Host}; | ||||||
|  |  | ||||||
|     fn mem(s: &str) -> MemReader { |     fn mem(s: &str) -> MemReader { | ||||||
|   | |||||||
							
								
								
									
										11
									
								
								src/mock.rs
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/mock.rs
									
									
									
									
									
								
							| @@ -1,7 +1,7 @@ | |||||||
| use std::io::IoResult; | use std::io::IoResult; | ||||||
| use std::io::net::ip::SocketAddr; | use std::io::net::ip::SocketAddr; | ||||||
|  |  | ||||||
| use net::NetworkStream; | use net::{NetworkStream, NetworkConnector}; | ||||||
|  |  | ||||||
| #[deriving(Clone, PartialEq, Show)] | #[deriving(Clone, PartialEq, Show)] | ||||||
| pub struct MockStream; | pub struct MockStream; | ||||||
| @@ -19,11 +19,14 @@ impl Writer for MockStream { | |||||||
| } | } | ||||||
|  |  | ||||||
| impl NetworkStream for MockStream { | impl NetworkStream for MockStream { | ||||||
|     fn connect(_host: &str, _port: u16, _scheme: &str) -> IoResult<MockStream> { |  | ||||||
|         Ok(MockStream) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     fn peer_name(&mut self) -> IoResult<SocketAddr> { |     fn peer_name(&mut self) -> IoResult<SocketAddr> { | ||||||
|         Ok(from_str("127.0.0.1:1337").unwrap()) |         Ok(from_str("127.0.0.1:1337").unwrap()) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | impl NetworkConnector for MockStream { | ||||||
|  |     fn connect(_host: &str, _port: u16, _scheme: &str) -> IoResult<MockStream> { | ||||||
|  |         Ok(MockStream) | ||||||
|  |     } | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										38
									
								
								src/net.rs
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								src/net.rs
									
									
									
									
									
								
							| @@ -45,19 +45,15 @@ pub trait NetworkStream: Stream + Any + Clone + Send { | |||||||
|     /// Get the remote address of the underlying connection. |     /// Get the remote address of the underlying connection. | ||||||
|     fn peer_name(&mut self) -> IoResult<SocketAddr>; |     fn peer_name(&mut self) -> IoResult<SocketAddr>; | ||||||
|  |  | ||||||
|     /// Connect to a remote address. |  | ||||||
|     fn connect(host: &str, Port, scheme: &str) -> IoResult<Self>; |  | ||||||
|  |  | ||||||
|     /// Turn this into an appropriately typed trait object. |  | ||||||
|     #[inline] |  | ||||||
|     fn dynamic(self) -> Box<NetworkStream + Send> { |  | ||||||
|         box self as Box<NetworkStream + Send> |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     #[doc(hidden)] |     #[doc(hidden)] | ||||||
|     #[inline] |     #[inline] | ||||||
|     // Hack to work around lack of Clone impl for Box<Clone> |     fn clone_box(&self) -> Box<NetworkStream + Send> { box self.clone() } | ||||||
|     fn clone_box(&self) -> Box<NetworkStream + Send> { self.clone().dynamic() } | } | ||||||
|  |  | ||||||
|  | /// A connector creates a NetworkStream. | ||||||
|  | pub trait NetworkConnector: NetworkStream { | ||||||
|  |     /// Connect to a remote address. | ||||||
|  |     fn connect(host: &str, Port, scheme: &str) -> IoResult<Self>; | ||||||
| } | } | ||||||
|  |  | ||||||
| impl fmt::Show for Box<NetworkStream + Send> { | impl fmt::Show for Box<NetworkStream + Send> { | ||||||
| @@ -214,6 +210,15 @@ impl Writer for HttpStream { | |||||||
|  |  | ||||||
|  |  | ||||||
| impl NetworkStream for HttpStream { | impl NetworkStream for HttpStream { | ||||||
|  |     fn peer_name(&mut self) -> IoResult<SocketAddr> { | ||||||
|  |         match *self { | ||||||
|  |             Http(ref mut inner) => inner.peer_name(), | ||||||
|  |             Https(_, addr) => Ok(addr) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | impl NetworkConnector for HttpStream { | ||||||
|     fn connect(host: &str, port: Port, scheme: &str) -> IoResult<HttpStream> { |     fn connect(host: &str, port: Port, scheme: &str) -> IoResult<HttpStream> { | ||||||
|         match scheme { |         match scheme { | ||||||
|             "http" => { |             "http" => { | ||||||
| @@ -239,13 +244,6 @@ impl NetworkStream for HttpStream { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn peer_name(&mut self) -> IoResult<SocketAddr> { |  | ||||||
|         match *self { |  | ||||||
|             Http(ref mut inner) => inner.peer_name(), |  | ||||||
|             Https(_, addr) => Ok(addr) |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| fn lift_ssl_error(ssl: SslError) -> IoError { | fn lift_ssl_error(ssl: SslError) -> IoError { | ||||||
| @@ -276,7 +274,7 @@ mod tests { | |||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_downcast_box_stream() { |     fn test_downcast_box_stream() { | ||||||
|         let stream = MockStream.dynamic(); |         let stream = box MockStream as Box<NetworkStream + Send>; | ||||||
|  |  | ||||||
|         let mock = stream.downcast::<MockStream>().unwrap(); |         let mock = stream.downcast::<MockStream>().unwrap(); | ||||||
|         assert_eq!(mock, box MockStream); |         assert_eq!(mock, box MockStream); | ||||||
| @@ -285,7 +283,7 @@ mod tests { | |||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_downcast_unchecked_box_stream() { |     fn test_downcast_unchecked_box_stream() { | ||||||
|         let stream = MockStream.dynamic(); |         let stream = box MockStream as Box<NetworkStream + Send>; | ||||||
|  |  | ||||||
|         let mock = unsafe { stream.downcast_unchecked::<MockStream>() }; |         let mock = unsafe { stream.downcast_unchecked::<MockStream>() }; | ||||||
|         assert_eq!(mock, box MockStream); |         assert_eq!(mock, box MockStream); | ||||||
|   | |||||||
| @@ -38,7 +38,7 @@ impl Request { | |||||||
|     pub fn new<S: NetworkStream>(mut stream: S) -> HttpResult<Request> { |     pub fn new<S: NetworkStream>(mut stream: S) -> HttpResult<Request> { | ||||||
|         let remote_addr = try_io!(stream.peer_name()); |         let remote_addr = try_io!(stream.peer_name()); | ||||||
|         debug!("remote addr = {}", remote_addr); |         debug!("remote addr = {}", remote_addr); | ||||||
|         let mut stream = BufferedReader::new(stream.dynamic()); |         let mut stream = BufferedReader::new(box stream as Box<NetworkStream + Send>); | ||||||
|         let (method, uri, version) = try!(read_request_line(&mut stream)); |         let (method, uri, version) = try!(read_request_line(&mut stream)); | ||||||
|         let headers = try!(Headers::from_raw(&mut stream)); |         let headers = try!(Headers::from_raw(&mut stream)); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -55,7 +55,7 @@ impl Response<Fresh> { | |||||||
|             status: status::Ok, |             status: status::Ok, | ||||||
|             version: version::Http11, |             version: version::Http11, | ||||||
|             headers: header::Headers::new(), |             headers: header::Headers::new(), | ||||||
|             body: ThroughWriter(BufferedWriter::new(stream.dynamic())) |             body: ThroughWriter(BufferedWriter::new(box stream as Box<NetworkStream + Send>)) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user