separate out stream
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use cookie::{Cookie, CookieJar};
|
|
||||||
use conn::ConnectionPool;
|
use conn::ConnectionPool;
|
||||||
|
use cookie::{Cookie, CookieJar};
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use response::{self, Response};
|
use response::{self, Response};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
@@ -8,7 +8,6 @@ use header::{add_header, get_all_headers, get_header, has_header, Header};
|
|||||||
|
|
||||||
// to get to share private fields
|
// to get to share private fields
|
||||||
include!("request.rs");
|
include!("request.rs");
|
||||||
include!("stream.rs");
|
|
||||||
include!("unit.rs");
|
include!("unit.rs");
|
||||||
|
|
||||||
/// Agents keep state between requests.
|
/// Agents keep state between requests.
|
||||||
|
|||||||
@@ -112,6 +112,7 @@ mod error;
|
|||||||
mod header;
|
mod header;
|
||||||
mod macros;
|
mod macros;
|
||||||
mod response;
|
mod response;
|
||||||
|
mod stream;
|
||||||
|
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
mod serde_macros;
|
mod serde_macros;
|
||||||
|
|||||||
@@ -74,8 +74,8 @@ impl Default for Payload {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct SizedReader {
|
pub struct SizedReader {
|
||||||
size: Option<usize>,
|
pub size: Option<usize>,
|
||||||
reader: Box<Read + 'static>,
|
pub reader: Box<Read + 'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SizedReader {
|
impl SizedReader {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use agent::Stream;
|
|
||||||
use ascii::AsciiString;
|
use ascii::AsciiString;
|
||||||
use chunked_transfer;
|
use chunked_transfer;
|
||||||
use header::Header;
|
use header::Header;
|
||||||
@@ -8,6 +7,7 @@ use std::io::ErrorKind;
|
|||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::Result as IoResult;
|
use std::io::Result as IoResult;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use stream::Stream;
|
||||||
|
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
|
use agent::{SizedReader, Unit};
|
||||||
|
use chunked_transfer;
|
||||||
|
use error::Error;
|
||||||
|
use std::io::{Cursor, Read, Result as IoResult, Write};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::net::ToSocketAddrs;
|
use std::net::ToSocketAddrs;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::io::Result as IoResult;
|
|
||||||
use std::io::Write;
|
|
||||||
use url::Url;
|
|
||||||
use chunked_transfer;
|
|
||||||
|
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
use native_tls::TlsConnector;
|
use native_tls::TlsConnector;
|
||||||
@@ -66,7 +66,7 @@ impl Write for Stream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn connect_http(unit: &Unit) -> Result<Stream, Error> {
|
pub fn connect_http(unit: &Unit) -> Result<Stream, Error> {
|
||||||
//
|
//
|
||||||
let hostname = unit.url.host_str().unwrap();
|
let hostname = unit.url.host_str().unwrap();
|
||||||
let port = unit.url.port().unwrap_or(80);
|
let port = unit.url.port().unwrap_or(80);
|
||||||
@@ -75,7 +75,7 @@ fn connect_http(unit: &Unit) -> Result<Stream, Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
fn connect_https(unit: &Unit) -> Result<Stream, Error> {
|
pub fn connect_https(unit: &Unit) -> Result<Stream, Error> {
|
||||||
//
|
//
|
||||||
let hostname = unit.url.host_str().unwrap();
|
let hostname = unit.url.host_str().unwrap();
|
||||||
let port = unit.url.port().unwrap_or(443);
|
let port = unit.url.port().unwrap_or(443);
|
||||||
@@ -87,9 +87,10 @@ fn connect_https(unit: &Unit) -> Result<Stream, Error> {
|
|||||||
Ok(Stream::Https(stream))
|
Ok(Stream::Https(stream))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<TcpStream, Error> {
|
pub fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<TcpStream, Error> {
|
||||||
//
|
//
|
||||||
let ips: Vec<SocketAddr> = format!("{}:{}", hostname, port).to_socket_addrs()
|
let ips: Vec<SocketAddr> = format!("{}:{}", hostname, port)
|
||||||
|
.to_socket_addrs()
|
||||||
.map_err(|e| Error::DnsFailed(format!("{}", e)))?
|
.map_err(|e| Error::DnsFailed(format!("{}", e)))?
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@@ -103,7 +104,10 @@ fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<TcpStream, Err
|
|||||||
// connect with a configured timeout.
|
// connect with a configured timeout.
|
||||||
let stream = match unit.timeout_connect {
|
let stream = match unit.timeout_connect {
|
||||||
0 => TcpStream::connect(&sock_addr),
|
0 => TcpStream::connect(&sock_addr),
|
||||||
_ => TcpStream::connect_timeout(&sock_addr, Duration::from_millis(unit.timeout_connect as u64)),
|
_ => TcpStream::connect_timeout(
|
||||||
|
&sock_addr,
|
||||||
|
Duration::from_millis(unit.timeout_connect as u64),
|
||||||
|
),
|
||||||
}.map_err(|err| Error::ConnectionFailed(format!("{}", err)))?;
|
}.map_err(|err| Error::ConnectionFailed(format!("{}", err)))?;
|
||||||
|
|
||||||
// rust's absurd api returns Err if we set 0.
|
// rust's absurd api returns Err if we set 0.
|
||||||
@@ -122,22 +126,22 @@ fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<TcpStream, Err
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn connect_test(unit: &Unit) -> Result<Stream, Error> {
|
pub fn connect_test(unit: &Unit) -> Result<Stream, Error> {
|
||||||
use test;
|
use test;
|
||||||
test::resolve_handler(unit)
|
test::resolve_handler(unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
fn connect_test(unit: &Unit) -> Result<Stream, Error> {
|
pub fn connect_test(unit: &Unit) -> Result<Stream, Error> {
|
||||||
Err(Error::UnknownScheme(unit.url.scheme().to_string()))
|
Err(Error::UnknownScheme(unit.url.scheme().to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "tls"))]
|
#[cfg(not(feature = "tls"))]
|
||||||
fn connect_https(unit: &Unit) -> Result<Stream, Error> {
|
pub fn connect_https(unit: &Unit) -> Result<Stream, Error> {
|
||||||
Err(Error::UnknownScheme(unit.url.scheme().to_string()))
|
Err(Error::UnknownScheme(unit.url.scheme().to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_body(body: SizedReader, do_chunk: bool, stream: &mut Stream) -> IoResult<()> {
|
pub fn send_body(body: SizedReader, do_chunk: bool, stream: &mut Stream) -> IoResult<()> {
|
||||||
if do_chunk {
|
if do_chunk {
|
||||||
pipe(body.reader, chunked_transfer::Encoder::new(stream))?;
|
pipe(body.reader, chunked_transfer::Encoder::new(stream))?;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
use agent::Stream;
|
|
||||||
use agent::Unit;
|
use agent::Unit;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use header::Header;
|
use header::Header;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::Cursor;
|
use std::io::{Write, Cursor};
|
||||||
use std::io::Write;
|
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
use stream::Stream;
|
||||||
|
|
||||||
mod agent_test;
|
mod agent_test;
|
||||||
mod auth;
|
mod auth;
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
use url::Url;
|
||||||
|
use stream::{connect_http, connect_https, connect_test, send_body};
|
||||||
|
use std::io::Write;
|
||||||
//
|
//
|
||||||
|
|
||||||
pub struct Unit {
|
pub struct Unit {
|
||||||
|
|||||||
Reference in New Issue
Block a user