2021-02-11 22:39:54 +00:00
|
|
|
//! HTTP client.
|
|
|
|
|
2023-07-02 01:09:15 +01:00
|
|
|
use std::{rc::Rc, time::Duration};
|
2021-12-25 02:23:22 +00:00
|
|
|
|
|
|
|
use actix_http::{error::HttpError, header::HeaderMap, Method, RequestHead, Uri};
|
|
|
|
use actix_rt::net::TcpStream;
|
|
|
|
use actix_service::Service;
|
|
|
|
pub use actix_tls::connect::{
|
|
|
|
ConnectError as TcpConnectError, ConnectInfo, Connection as TcpConnection,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{ws, BoxConnectorService, ClientBuilder, ClientRequest};
|
2019-04-19 18:03:44 -07:00
|
|
|
|
2020-03-07 04:09:31 +02:00
|
|
|
mod config;
|
2018-11-11 23:12:54 -08:00
|
|
|
mod connection;
|
|
|
|
mod connector;
|
|
|
|
mod error;
|
2019-01-28 20:41:09 -08:00
|
|
|
mod h1proto;
|
|
|
|
mod h2proto;
|
2018-11-11 23:12:54 -08:00
|
|
|
mod pool;
|
2018-10-22 18:18:05 -07:00
|
|
|
|
2023-07-17 02:38:12 +01:00
|
|
|
pub use self::{
|
|
|
|
connection::{Connection, ConnectionIo},
|
|
|
|
connector::{Connector, ConnectorService},
|
|
|
|
error::{ConnectError, FreezeRequestError, InvalidUrl, SendRequestError},
|
|
|
|
};
|
2019-04-19 18:03:44 -07:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Connect {
|
|
|
|
pub uri: Uri,
|
|
|
|
pub addr: Option<std::net::SocketAddr>,
|
|
|
|
}
|
2021-12-25 02:23:22 +00:00
|
|
|
|
|
|
|
/// An asynchronous HTTP and WebSocket client.
|
|
|
|
///
|
|
|
|
/// You should take care to create, at most, one `Client` per thread. Otherwise, expect higher CPU
|
|
|
|
/// and memory usage.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// use awc::Client;
|
|
|
|
///
|
|
|
|
/// #[actix_rt::main]
|
|
|
|
/// async fn main() {
|
|
|
|
/// let mut client = Client::default();
|
|
|
|
///
|
|
|
|
/// let res = client.get("http://www.rust-lang.org")
|
|
|
|
/// .insert_header(("User-Agent", "my-app/1.2"))
|
|
|
|
/// .send()
|
|
|
|
/// .await;
|
|
|
|
///
|
|
|
|
/// println!("Response: {:?}", res);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Client(pub(crate) ClientConfig);
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct ClientConfig {
|
|
|
|
pub(crate) connector: BoxConnectorService,
|
|
|
|
pub(crate) default_headers: Rc<HeaderMap>,
|
|
|
|
pub(crate) timeout: Option<Duration>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Client {
|
|
|
|
fn default() -> Self {
|
|
|
|
ClientBuilder::new().finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2022-01-19 21:36:14 +00:00
|
|
|
/// Constructs new client instance with default settings.
|
2021-12-25 02:23:22 +00:00
|
|
|
pub fn new() -> Client {
|
|
|
|
Client::default()
|
|
|
|
}
|
|
|
|
|
2022-01-19 21:36:14 +00:00
|
|
|
/// Constructs new `Client` builder.
|
|
|
|
///
|
2021-12-25 02:23:22 +00:00
|
|
|
/// This function is equivalent of `ClientBuilder::new()`.
|
|
|
|
pub fn builder() -> ClientBuilder<
|
|
|
|
impl Service<
|
|
|
|
ConnectInfo<Uri>,
|
|
|
|
Response = TcpConnection<Uri, TcpStream>,
|
|
|
|
Error = TcpConnectError,
|
|
|
|
> + Clone,
|
|
|
|
> {
|
|
|
|
ClientBuilder::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP request.
|
|
|
|
pub fn request<U>(&self, method: Method, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
let mut req = ClientRequest::new(method, url, self.0.clone());
|
|
|
|
|
|
|
|
for header in self.0.default_headers.iter() {
|
2022-01-23 23:26:35 +00:00
|
|
|
req = req.append_header(header);
|
2021-12-25 02:23:22 +00:00
|
|
|
}
|
2022-01-23 23:26:35 +00:00
|
|
|
|
2021-12-25 02:23:22 +00:00
|
|
|
req
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `ClientRequest` from `RequestHead`
|
|
|
|
///
|
|
|
|
/// It is useful for proxy requests. This implementation
|
|
|
|
/// copies all headers and the method.
|
|
|
|
pub fn request_from<U>(&self, url: U, head: &RequestHead) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
let mut req = self.request(head.method.clone(), url);
|
|
|
|
for header in head.headers.iter() {
|
|
|
|
req = req.insert_header_if_none(header);
|
|
|
|
}
|
|
|
|
req
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP *GET* request.
|
|
|
|
pub fn get<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
self.request(Method::GET, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP *HEAD* request.
|
|
|
|
pub fn head<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
self.request(Method::HEAD, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP *PUT* request.
|
|
|
|
pub fn put<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
self.request(Method::PUT, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP *POST* request.
|
|
|
|
pub fn post<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
self.request(Method::POST, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP *PATCH* request.
|
|
|
|
pub fn patch<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
self.request(Method::PATCH, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP *DELETE* request.
|
|
|
|
pub fn delete<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
self.request(Method::DELETE, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP *OPTIONS* request.
|
|
|
|
pub fn options<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
self.request(Method::OPTIONS, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize a WebSocket connection.
|
|
|
|
/// Returns a WebSocket connection builder.
|
|
|
|
pub fn ws<U>(&self, url: U) -> ws::WebsocketsRequest
|
|
|
|
where
|
|
|
|
Uri: TryFrom<U>,
|
|
|
|
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
|
|
|
{
|
|
|
|
let mut req = ws::WebsocketsRequest::new(url, self.0.clone());
|
|
|
|
for (key, value) in self.0.default_headers.iter() {
|
|
|
|
req.head.headers.insert(key.clone(), value.clone());
|
|
|
|
}
|
|
|
|
req
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get default HeaderMap of Client.
|
|
|
|
///
|
|
|
|
/// Returns Some(&mut HeaderMap) when Client object is unique
|
|
|
|
/// (No other clone of client exists at the same time).
|
|
|
|
pub fn headers(&mut self) -> Option<&mut HeaderMap> {
|
|
|
|
Rc::get_mut(&mut self.0.default_headers)
|
|
|
|
}
|
|
|
|
}
|