Merge pull request #93 from hyperium/split-networkstream
split NetworkStream::connect to NetworkConnector
This commit is contained in:
@@ -55,6 +55,9 @@ impl hyper::header::Header for Foo {
|
||||
fn parse_header(_: &[Vec<u8>]) -> Option<Foo> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl hyper::header::HeaderFormat for Foo {
|
||||
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
"Bar".fmt(fmt)
|
||||
}
|
||||
|
||||
@@ -77,20 +77,25 @@ impl hyper::header::Header for Foo {
|
||||
fn parse_header(_: &[Vec<u8>]) -> Option<Foo> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl hyper::header::HeaderFormat for Foo {
|
||||
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
"Bar".fmt(fmt)
|
||||
}
|
||||
}
|
||||
|
||||
impl net::NetworkStream for MockStream {
|
||||
fn peer_name(&mut self) -> IoResult<SocketAddr> {
|
||||
Ok(from_str("127.0.0.1:1337").unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl net::NetworkConnector for MockStream {
|
||||
fn connect(_host: &str, _port: u16, _scheme: &str) -> IoResult<MockStream> {
|
||||
Ok(MockStream::new())
|
||||
}
|
||||
|
||||
fn peer_name(&mut self) -> IoResult<SocketAddr> {
|
||||
Ok(from_str("127.0.0.1:1337").unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
||||
@@ -6,7 +6,7 @@ use url::Url;
|
||||
use method::{mod, Get, Post, Delete, Put, Patch, Head, Options};
|
||||
use header::Headers;
|
||||
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 version;
|
||||
use {HttpResult, HttpUriError};
|
||||
@@ -43,7 +43,7 @@ impl Request<Fresh> {
|
||||
}
|
||||
|
||||
/// 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);
|
||||
let host = match url.serialize_host() {
|
||||
Some(host) => host,
|
||||
@@ -56,8 +56,8 @@ impl Request<Fresh> {
|
||||
};
|
||||
debug!("port={}", port);
|
||||
|
||||
let stream: S = try_io!(NetworkStream::connect(host.as_slice(), port, url.scheme.as_slice()));
|
||||
let stream = ThroughWriter(BufferedWriter::new(stream.dynamic()));
|
||||
let stream: S = try_io!(NetworkConnector::connect(host.as_slice(), port, url.scheme.as_slice()));
|
||||
let stream = ThroughWriter(BufferedWriter::new(box stream as Box<NetworkStream + Send>));
|
||||
|
||||
let mut headers = Headers::new();
|
||||
headers.set(Host {
|
||||
|
||||
@@ -25,7 +25,7 @@ impl Response {
|
||||
|
||||
/// Creates a new response from a server.
|
||||
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 headers = try!(header::Headers::from_raw(&mut stream));
|
||||
|
||||
|
||||
@@ -408,7 +408,7 @@ mod tests {
|
||||
use std::hash::sip::hash;
|
||||
use mime::{Mime, Text, Plain};
|
||||
use super::CaseInsensitive;
|
||||
use super::{Headers, Header};
|
||||
use super::{Headers, Header, HeaderFormat};
|
||||
use super::common::{ContentLength, ContentType, Accept, Host};
|
||||
|
||||
fn mem(s: &str) -> MemReader {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#![feature(macro_rules, phase, default_type_params, if_let, slicing_syntax)]
|
||||
#![deny(missing_doc)]
|
||||
#![deny(missing_docs)]
|
||||
#![deny(warnings)]
|
||||
#![experimental]
|
||||
|
||||
|
||||
11
src/mock.rs
11
src/mock.rs
@@ -1,7 +1,7 @@
|
||||
use std::io::IoResult;
|
||||
use std::io::net::ip::SocketAddr;
|
||||
|
||||
use net::NetworkStream;
|
||||
use net::{NetworkStream, NetworkConnector};
|
||||
|
||||
#[deriving(Clone, PartialEq, Show)]
|
||||
pub struct MockStream;
|
||||
@@ -19,11 +19,14 @@ impl Writer 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> {
|
||||
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.
|
||||
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)]
|
||||
#[inline]
|
||||
// Hack to work around lack of Clone impl for Box<Clone>
|
||||
fn clone_box(&self) -> Box<NetworkStream + Send> { self.clone().dynamic() }
|
||||
fn clone_box(&self) -> Box<NetworkStream + Send> { box self.clone() }
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
@@ -214,6 +210,15 @@ impl Writer 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> {
|
||||
match scheme {
|
||||
"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 {
|
||||
@@ -276,7 +274,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_downcast_box_stream() {
|
||||
let stream = MockStream.dynamic();
|
||||
let stream = box MockStream as Box<NetworkStream + Send>;
|
||||
|
||||
let mock = stream.downcast::<MockStream>().unwrap();
|
||||
assert_eq!(mock, box MockStream);
|
||||
@@ -285,7 +283,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
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>() };
|
||||
assert_eq!(mock, box MockStream);
|
||||
|
||||
@@ -38,7 +38,7 @@ impl Request {
|
||||
pub fn new<S: NetworkStream>(mut stream: S) -> HttpResult<Request> {
|
||||
let remote_addr = try_io!(stream.peer_name());
|
||||
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 headers = try!(Headers::from_raw(&mut stream));
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ impl Response<Fresh> {
|
||||
status: status::Ok,
|
||||
version: version::Http11,
|
||||
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