separate out stream

This commit is contained in:
Martin Algesten
2018-06-30 13:47:37 +02:00
parent 3b249e0313
commit ff582b8c6f
7 changed files with 27 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
use cookie::{Cookie, CookieJar};
use conn::ConnectionPool;
use cookie::{Cookie, CookieJar};
use error::Error;
use response::{self, Response};
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
include!("request.rs");
include!("stream.rs");
include!("unit.rs");
/// Agents keep state between requests.

View File

@@ -112,6 +112,7 @@ mod error;
mod header;
mod macros;
mod response;
mod stream;
#[cfg(feature = "json")]
mod serde_macros;

View File

@@ -74,8 +74,8 @@ impl Default for Payload {
}
pub struct SizedReader {
size: Option<usize>,
reader: Box<Read + 'static>,
pub size: Option<usize>,
pub reader: Box<Read + 'static>,
}
impl SizedReader {

View File

@@ -1,4 +1,3 @@
use agent::Stream;
use ascii::AsciiString;
use chunked_transfer;
use header::Header;
@@ -8,6 +7,7 @@ use std::io::ErrorKind;
use std::io::Read;
use std::io::Result as IoResult;
use std::str::FromStr;
use stream::Stream;
#[cfg(feature = "json")]
use serde_json;

View File

@@ -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::TcpStream;
use std::net::ToSocketAddrs;
use std::time::Duration;
use std::io::Result as IoResult;
use std::io::Write;
use url::Url;
use chunked_transfer;
#[cfg(feature = "tls")]
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 port = unit.url.port().unwrap_or(80);
@@ -75,7 +75,7 @@ fn connect_http(unit: &Unit) -> Result<Stream, Error> {
}
#[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 port = unit.url.port().unwrap_or(443);
@@ -87,9 +87,10 @@ fn connect_https(unit: &Unit) -> Result<Stream, Error> {
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)))?
.collect();
@@ -103,7 +104,10 @@ fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<TcpStream, Err
// connect with a configured timeout.
let stream = match unit.timeout_connect {
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)))?;
// 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)]
fn connect_test(unit: &Unit) -> Result<Stream, Error> {
pub fn connect_test(unit: &Unit) -> Result<Stream, Error> {
use test;
test::resolve_handler(unit)
}
#[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()))
}
#[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()))
}
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 {
pipe(body.reader, chunked_transfer::Encoder::new(stream))?;
} else {

View File

@@ -1,11 +1,10 @@
use agent::Stream;
use agent::Unit;
use error::Error;
use header::Header;
use std::collections::HashMap;
use std::io::Cursor;
use std::io::Write;
use std::io::{Write, Cursor};
use std::sync::{Arc, Mutex};
use stream::Stream;
mod agent_test;
mod auth;

View File

@@ -1,3 +1,6 @@
use url::Url;
use stream::{connect_http, connect_https, connect_test, send_body};
use std::io::Write;
//
pub struct Unit {