use cookie::{Cookie, CookieJar};
use error::Error;
use pool::ConnectionPool;
use response::{self, Response};
use std::sync::Mutex;
use header::{add_header, get_all_headers, get_header, has_header, Header};
include!("request.rs");
include!("unit.rs");
#[derive(Debug, Default, Clone)]
pub struct Agent {
headers: Vec<Header>,
state: Arc<Mutex<Option<AgentState>>>,
}
#[derive(Debug)]
pub struct AgentState {
pool: ConnectionPool,
jar: CookieJar,
}
impl AgentState {
fn new() -> Self {
AgentState {
pool: ConnectionPool::new(),
jar: CookieJar::new(),
}
}
pub fn pool(&mut self) -> &mut ConnectionPool {
&mut self.pool
}
}
impl Agent {
pub fn new() -> Agent {
Default::default()
}
pub fn build(&self) -> Self {
Agent {
headers: self.headers.clone(),
state: Arc::new(Mutex::new(Some(AgentState::new()))),
}
}
pub fn set<K, V>(&mut self, header: K, value: V) -> &mut Agent
where
K: Into<String>,
V: Into<String>,
{
add_header(
&mut self.headers,
Header::new(&header.into(), &value.into()),
);
self
}
pub fn set_map<K, V, I>(&mut self, headers: I) -> &mut Agent
where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
{
for (k, v) in headers.into_iter() {
self.set(k, v);
}
self
}
pub fn auth<S, T>(&mut self, user: S, pass: T) -> &mut Agent
where
S: Into<String>,
T: Into<String>,
{
let u = user.into();
let p = pass.into();
let pass = basic_auth(&u, &p);
self.auth_kind("Basic", pass)
}
pub fn auth_kind<S, T>(&mut self, kind: S, pass: T) -> &mut Agent
where
S: Into<String>,
T: Into<String>,
{
let value = format!("{} {}", kind.into(), pass.into());
self.set("Authorization", value);
self
}
pub fn request<M, S>(&self, method: M, path: S) -> Request
where
M: Into<String>,
S: Into<String>,
{
Request::new(&self, method.into(), path.into())
}
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
let state = self.state.lock().unwrap();
state
.as_ref()
.and_then(|state| state.jar.get(name))
.map(|c| c.clone())
}
pub fn set_cookie(&self, cookie: Cookie<'static>) {
let mut state = self.state.lock().unwrap();
match state.as_mut() {
None => (),
Some(state) => {
state.jar.add_original(cookie);
}
}
}
pub fn get<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("GET", path)
}
pub fn head<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("HEAD", path)
}
pub fn post<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("POST", path)
}
pub fn put<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("PUT", path)
}
pub fn delete<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("DELETE", path)
}
pub fn trace<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("TRACE", path)
}
pub fn options<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("OPTIONS", path)
}
pub fn connect<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("CONNECT", path)
}
pub fn patch<S>(&self, path: S) -> Request
where
S: Into<String>,
{
self.request("PATCH", path)
}
#[cfg(test)]
pub fn state(&self) -> &Arc<Mutex<Option<AgentState>>> {
&self.state
}
}
fn basic_auth(user: &str, pass: &str) -> String {
let safe = match user.find(":") {
Some(idx) => &user[..idx],
None => user,
};
::base64::encode(&format!("{}:{}", safe, pass))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn agent_implements_send() {
let mut agent = Agent::new();
::std::thread::spawn(move || {
agent.set("Foo", "Bar");
});
}
#[test]
fn request_implements_send() {
let agent = Agent::new();
let mut request = Request::new(&agent, "GET".to_string(), "/foo".to_string());
::std::thread::spawn(move || {
request.set("Foo", "Bar");
});
}
}