separate out ConnectionPool

This commit is contained in:
Martin Algesten
2018-06-30 13:41:52 +02:00
parent b54f747d16
commit 3b249e0313
4 changed files with 34 additions and 32 deletions

View File

@@ -1,4 +1,5 @@
use cookie::{Cookie, CookieJar};
use conn::ConnectionPool;
use error::Error;
use response::{self, Response};
use std::sync::Mutex;
@@ -7,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!("conn.rs");
include!("stream.rs");
include!("unit.rs");

View File

@@ -1,8 +1,4 @@
use std::io::Write;
use url::Url;
use chunked_transfer;
const CHUNK_SIZE: usize = 1024 * 1024;
//
#[derive(Debug, Default, Clone)]
pub struct ConnectionPool {}
@@ -12,29 +8,3 @@ impl ConnectionPool {
ConnectionPool {}
}
}
fn send_body(body: SizedReader, do_chunk: bool, stream: &mut Stream) -> IoResult<()> {
if do_chunk {
pipe(body.reader, chunked_transfer::Encoder::new(stream))?;
} else {
pipe(body.reader, stream)?;
}
Ok(())
}
fn pipe<R, W>(mut reader: R, mut writer: W) -> IoResult<()>
where
R: Read,
W: Write,
{
let mut buf = [0_u8; CHUNK_SIZE];
loop {
let len = reader.read(&mut buf)?;
if len == 0 {
break;
}
writer.write_all(&buf[0..len])?;
}
Ok(())
}

View File

@@ -107,6 +107,7 @@ extern crate native_tls;
extern crate serde_json;
mod agent;
mod conn;
mod error;
mod header;
mod macros;

View File

@@ -3,12 +3,17 @@ 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;
#[cfg(feature = "tls")]
use native_tls::TlsStream;
const CHUNK_SIZE: usize = 1024 * 1024;
pub enum Stream {
Http(TcpStream),
#[cfg(feature = "tls")]
@@ -131,3 +136,29 @@ fn connect_test(unit: &Unit) -> Result<Stream, Error> {
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<()> {
if do_chunk {
pipe(body.reader, chunked_transfer::Encoder::new(stream))?;
} else {
pipe(body.reader, stream)?;
}
Ok(())
}
fn pipe<R, W>(mut reader: R, mut writer: W) -> IoResult<()>
where
R: Read,
W: Write,
{
let mut buf = [0_u8; CHUNK_SIZE];
loop {
let len = reader.read(&mut buf)?;
if len == 0 {
break;
}
writer.write_all(&buf[0..len])?;
}
Ok(())
}