use pub(crate) instead of include!()

This commit is contained in:
Chris West (Faux)
2019-05-27 17:44:14 +01:00
parent 73d1c89e90
commit ceb7c3ac14
8 changed files with 86 additions and 63 deletions

View File

@@ -1,14 +1,11 @@
use crate::error::Error;
use crate::pool::ConnectionPool;
use crate::response::{self, Response};
use cookie::{Cookie, CookieJar};
use std::sync::Arc;
use std::sync::Mutex;
use crate::header::{add_header, get_all_headers, get_header, has_header, Header};
use cookie::{Cookie, CookieJar};
// to get to share private fields
include!("request.rs");
include!("unit.rs");
use crate::header::{self, Header};
use crate::pool::ConnectionPool;
use crate::request::Request;
/// Agents keep state between requests.
///
@@ -41,9 +38,9 @@ include!("unit.rs");
#[derive(Debug, Default, Clone)]
pub struct Agent {
/// Copied into each request of this agent.
headers: Vec<Header>,
pub(crate) headers: Vec<Header>,
/// Reused agent state for repeated requests from this agent.
state: Arc<Mutex<Option<AgentState>>>,
pub(crate) state: Arc<Mutex<Option<AgentState>>>,
}
/// Container of the state
@@ -52,9 +49,9 @@ pub struct Agent {
#[derive(Debug)]
pub(crate) struct AgentState {
/// Reused connections between requests.
pool: ConnectionPool,
pub(crate) pool: ConnectionPool,
/// Cookies saved between requests.
jar: CookieJar,
pub(crate) jar: CookieJar,
}
impl AgentState {
@@ -113,7 +110,7 @@ impl Agent {
/// }
/// ```
pub fn set(&mut self, header: &str, value: &str) -> &mut Agent {
add_header(&mut self.headers, Header::new(header, value));
header::add_header(&mut self.headers, Header::new(header, value));
self
}
@@ -255,7 +252,7 @@ impl Agent {
}
}
fn basic_auth(user: &str, pass: &str) -> String {
pub(crate) fn basic_auth(user: &str, pass: &str) -> String {
let safe = match user.find(':') {
Some(idx) => &user[..idx],
None => user,

View File

@@ -97,8 +97,10 @@ mod body;
mod error;
mod header;
mod pool;
mod request;
mod response;
mod stream;
mod unit;
#[cfg(feature = "json")]
mod serde_macros;
@@ -106,9 +108,10 @@ mod serde_macros;
#[cfg(test)]
mod test;
pub use crate::agent::{Agent, Request};
pub use crate::agent::Agent;
pub use crate::error::Error;
pub use crate::header::Header;
pub use crate::request::Request;
pub use crate::response::Response;
// re-export

View File

@@ -1,7 +1,9 @@
use crate::agent::Unit;
use crate::stream::Stream;
use std::collections::HashMap;
use std::io::{Read, Result as IoResult};
use crate::stream::Stream;
use crate::unit::Unit;
use url::Url;
pub const DEFAULT_HOST: &str = "localhost";

View File

@@ -1,7 +1,17 @@
use std::io::Read;
use std::sync::{Arc, Mutex};
use lazy_static::lazy_static;
use qstring::QString;
use std::io::Read;
use std::sync::Arc;
use url::Url;
use crate::agent::{self, Agent, AgentState};
use crate::body::Payload;
use crate::error::Error;
use crate::header::{self, Header};
use crate::pool;
use crate::unit::{self, Unit};
use crate::Response;
#[cfg(feature = "json")]
use super::SerdeValue;
@@ -22,19 +32,19 @@ lazy_static! {
/// ```
#[derive(Clone, Default)]
pub struct Request {
agent: Arc<Mutex<Option<AgentState>>>,
pub(crate) agent: Arc<Mutex<Option<AgentState>>>,
// via agent
method: String,
pub(crate) method: String,
path: String,
// from request itself
headers: Vec<Header>,
query: QString,
timeout_connect: u64,
timeout_read: u64,
timeout_write: u64,
redirects: u32,
pub(crate) headers: Vec<Header>,
pub(crate) query: QString,
pub(crate) timeout_connect: u64,
pub(crate) timeout_read: u64,
pub(crate) timeout_write: u64,
pub(crate) redirects: u32,
}
impl ::std::fmt::Debug for Request {
@@ -42,7 +52,7 @@ impl ::std::fmt::Debug for Request {
let (path, query) = self
.to_url()
.map(|u| {
let query = combine_query(&u, &self.query, true);
let query = unit::combine_query(&u, &self.query, true);
(u.path().to_string(), query)
})
.unwrap_or_else(|_| ("BAD_URL".to_string(), "BAD_URL".to_string()));
@@ -55,7 +65,7 @@ impl ::std::fmt::Debug for Request {
}
impl Request {
fn new(agent: &Agent, method: String, path: String) -> Request {
pub(crate) fn new(agent: &Agent, method: String, path: String) -> Request {
Request {
agent: Arc::clone(&agent.state),
method,
@@ -99,7 +109,7 @@ impl Request {
.and_then(|url| {
let reader = payload.into_read();
let unit = Unit::new(&self, &url, true, &reader);
connect(&self, unit, true, 0, reader, false)
unit::connect(&self, unit, true, 0, reader, false)
})
.unwrap_or_else(|e| e.into())
}
@@ -148,7 +158,8 @@ impl Request {
/// ```
pub fn send_string(&mut self, data: &str) -> Response {
let text = data.into();
let charset = response::charset_from_content_type(self.header("content-type")).to_string();
let charset =
crate::response::charset_from_content_type(self.header("content-type")).to_string();
self.do_call(Payload::Text(text, charset))
}
@@ -185,7 +196,7 @@ impl Request {
/// }
/// ```
pub fn set(&mut self, header: &str, value: &str) -> &mut Request {
add_header(&mut self.headers, Header::new(header, value));
header::add_header(&mut self.headers, Header::new(header, value));
self
}
@@ -198,7 +209,7 @@ impl Request {
/// assert_eq!("foobar", req.header("x-api-Key").unwrap());
/// ```
pub fn header<'a>(&self, name: &'a str) -> Option<&str> {
get_header(&self.headers, name)
header::get_header(&self.headers, name)
}
/// A list of the set header names in this request. Lowercased to be uniform.
@@ -226,7 +237,7 @@ impl Request {
/// assert_eq!(true, req.has("x-api-Key"));
/// ```
pub fn has<'a>(&self, name: &'a str) -> bool {
has_header(&self.headers, name)
header::has_header(&self.headers, name)
}
/// All headers corresponding values for the give name, or empty vector.
@@ -242,7 +253,7 @@ impl Request {
/// ]);
/// ```
pub fn all<'a>(&self, name: &'a str) -> Vec<&str> {
get_all_headers(&self.headers, name)
header::get_all_headers(&self.headers, name)
}
/// Set a query parameter.
@@ -336,7 +347,7 @@ impl Request {
/// println!("{:?}", r2);
/// ```
pub fn auth(&mut self, user: &str, pass: &str) -> &mut Request {
let pass = basic_auth(user, pass);
let pass = agent::basic_auth(user, pass);
self.auth_kind("Basic", &pass)
}
@@ -440,7 +451,7 @@ impl Request {
/// ```
pub fn get_host(&self) -> Result<String, Error> {
self.to_url()
.map(|u| u.host_str().unwrap_or(DEFAULT_HOST).to_string())
.map(|u| u.host_str().unwrap_or(pool::DEFAULT_HOST).to_string())
}
/// Returns the scheme for this request.
@@ -465,7 +476,8 @@ impl Request {
/// assert_eq!(req.get_query().unwrap(), "?foo=bar&format=json");
/// ```
pub fn get_query(&self) -> Result<String, Error> {
self.to_url().map(|u| combine_query(&u, &self.query, true))
self.to_url()
.map(|u| unit::combine_query(&u, &self.query, true))
}
/// The normalized path of this request.

View File

@@ -1,11 +1,14 @@
use crate::agent::Unit;
use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult};
use std::str::FromStr;
use ascii::AsciiString;
use chunked_transfer::Decoder as ChunkDecoder;
use crate::error::Error;
use crate::header::Header;
use crate::pool::PoolReturnRead;
use crate::stream::Stream;
use ascii::AsciiString;
use chunked_transfer::Decoder as ChunkDecoder;
use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult};
use std::str::FromStr;
use crate::unit::Unit;
#[cfg(feature = "json")]
use serde_json;
@@ -15,8 +18,6 @@ use encoding::label::encoding_from_whatwg_label;
#[cfg(feature = "charset")]
use encoding::DecoderTrap;
use crate::error::Error;
pub const DEFAULT_CONTENT_TYPE: &str = "text/plain";
pub const DEFAULT_CHARACTER_SET: &str = "utf-8";

View File

@@ -1,11 +1,12 @@
use crate::agent::Unit;
use crate::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 crate::error::Error;
use crate::unit::Unit;
#[allow(clippy::large_enum_variant)]
pub enum Stream {
Http(TcpStream),

View File

@@ -1,6 +1,6 @@
use crate::agent::Unit;
use crate::error::Error;
use crate::stream::Stream;
use crate::unit::Unit;
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::io::{Cursor, Write};

View File

@@ -1,9 +1,16 @@
use crate::body::{send_body, Payload, SizedReader};
use crate::stream::{connect_http, connect_https, connect_test, Stream};
use base64;
use std::io::{Result as IoResult, Write};
use std::sync::{Arc, Mutex};
use base64;
use cookie::{Cookie, CookieJar};
use qstring::QString;
use url::Url;
//
use crate::agent::AgentState;
use crate::body::{self, Payload, SizedReader};
use crate::header;
use crate::stream::{self, connect_https, connect_test, Stream};
use crate::{Error, Header, Request, Response};
use crate::pool::DEFAULT_HOST;
@@ -26,7 +33,7 @@ pub(crate) struct Unit {
impl Unit {
//
fn new(req: &Request, url: &Url, mix_queries: bool, body: &SizedReader) -> Self {
pub(crate) fn new(req: &Request, url: &Url, mix_queries: bool, body: &SizedReader) -> Self {
//
let is_chunked = req
@@ -96,15 +103,15 @@ impl Unit {
#[cfg(test)]
pub fn header<'a>(&self, name: &'a str) -> Option<&str> {
get_header(&self.headers, name)
header::get_header(&self.headers, name)
}
#[cfg(test)]
pub fn has<'a>(&self, name: &'a str) -> bool {
has_header(&self.headers, name)
header::has_header(&self.headers, name)
}
#[cfg(test)]
pub fn all<'a>(&self, name: &'a str) -> Vec<&str> {
get_all_headers(&self.headers, name)
header::get_all_headers(&self.headers, name)
}
}
@@ -136,7 +143,7 @@ pub(crate) fn connect(
}
// send the body (which can be empty now depending on redirects)
send_body(body, unit.is_chunked, &mut stream)?;
body::send_body(body, unit.is_chunked, &mut stream)?;
// start reading the response to process cookies and redirects.
let mut resp = Response::from_read(&mut stream);
@@ -182,7 +189,7 @@ pub(crate) fn connect(
// since it is not a redirect, or we're not following redirects,
// give away the incoming stream to the response object
response::set_stream(&mut resp, unit.url.to_string(), Some(unit), stream);
crate::response::set_stream(&mut resp, unit.url.to_string(), Some(unit), stream);
// release the response
Ok(resp)
@@ -219,7 +226,7 @@ fn match_cookies<'a>(jar: &'a CookieJar, domain: &str, path: &str, is_secure: bo
}
/// Combine the query of the url and the query options set on the request object.
fn combine_query(url: &Url, query: &QString, mix_queries: bool) -> String {
pub(crate) fn combine_query(url: &Url, query: &QString, mix_queries: bool) -> String {
match (url.query(), !query.is_empty() && mix_queries) {
(Some(urlq), true) => format!("?{}&{}", urlq, query),
(Some(urlq), false) => format!("?{}", urlq),
@@ -239,7 +246,7 @@ fn connect_socket(unit: &Unit, use_pooled: bool) -> Result<(Stream, bool), Error
}
}
let stream = match unit.url.scheme() {
"http" => connect_http(&unit),
"http" => stream::connect_http(&unit),
"https" => connect_https(&unit),
"test" => connect_test(&unit),
_ => Err(Error::UnknownScheme(unit.url.scheme().to_string())),
@@ -264,13 +271,13 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, redir: bool) -> IoResult<()> {
)?;
// host header if not set by user.
if !has_header(&unit.headers, "host") {
if !header::has_header(&unit.headers, "host") {
write!(prelude, "Host: {}\r\n", unit.url.host().unwrap())?;
}
if !has_header(&unit.headers, "user-agent") {
if !header::has_header(&unit.headers, "user-agent") {
write!(prelude, "User-Agent: ureq\r\n")?;
}
if !has_header(&unit.headers, "accept") {
if !header::has_header(&unit.headers, "accept") {
write!(prelude, "Accept: */*\r\n")?;
}