1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#![cfg_attr(docsrs, feature(doc_cfg))] //! `async-http-proxy` is a lightweight asynchronous HTTP proxy client library, which can be used //! to connect a to a TCP port via HTTP Connect proxy. It can use [Tokio](https://tokio.rs/) and //! [async-std](https://async.rs/) as asynchronous runtime. //! # Example //! The following example shows how to connect to `github.com` via Connect proxy (`tokio`): //! ```ignore //! use async_http_proxy::http_connect_tokio; //! use std::error::Error; //! use tokio::net::TcpStream; //! use url::Url; //! // Features "runtime-tokio" and "basic-auth" have to be activated //! #[tokio::main] //! async fn main() -> Result<(), Box<dyn Error>> { //! let mut stream = TcpStream::connect("127.0.0.1:8080").await?; //! let url = Url::parse("https://github.com")?; //! //! http_connect_tokio(&mut stream, &url).await?; //! // stream is now connect to github.com //! Ok(()) //! } //! ``` //! //! The following example shows how to connect to `example.org` with Basic Authentication via //! Connect proxy (`async-std`): //! ```ignore //! use async_http_proxy::http_connect_async_std; //! use async_std::net::TcpStream; //! use async_std::task; //! use std::error::Error; //! use url::Url; //! // Features "async-std-tokio" and "basic-auth" have to be activated //! fn main() -> Result<(), Box<dyn Error>> { //! task::block_on(async { //! let mut stream = TcpStream::connect("127.0.0.1:8080").await?; //! // feature "basic-auth" have to be active //! let url = Url::parse("https://USER:PASSWORD@example.org")?; //! //! http_connect_async_std(&mut stream, &url).await?; //! // stream is now connect to github.com //! Ok(()) //! }) //! } //! ``` #[cfg(all( not(feature = "runtime-tokio"), not(feature = "runtime-async-std"), not(doc) ))] compile_error!( "An async runtime have to be specified by feature: \"runtime-tokio\" \"runtime-async-std\"" ); mod request; mod response; use httparse::Error as HttpParseError; use std::io::Error as IoError; use thiserror::Error as ThisError; /// The maximum length of the response header. pub const MAXIMUM_RESPONSE_HEADER_LENGTH: usize = 4096; /// The maximum HTTP Headers, which can be parsed. pub const MAXIMUM_RESPONSE_HEADERS: usize = 16; /// This enum contains all errors, which can occur during the HTTP `CONNECT`. #[derive(Debug, ThisError)] pub enum HttpError { #[error("IO Error: {0}")] IoError(#[from] IoError), #[error("HTTP parse error: {0}")] HttpParseError(#[from] HttpParseError), #[error("The maximum response header length is exceeded: {0}")] MaximumResponseHeaderLengthExceeded(String), #[error("The end of file is reached")] EndOfFile, #[error("No HTTP code was found in the response")] NoHttpCode, #[error("The HTTP code is not equal 200: {0}")] HttpCode200(u16), #[error("No HTTP reason was found in the response")] NoHttpReason, #[error("The HTTP reason is not equal 'ConnectionEstablished': {0}")] HttpReasonConnectionEstablished(String), #[error("The URL does not have a host defined")] NoHost, #[error("The URL does not have a port defined")] NoPort, } /// Connect to the server defined by the URL and check if the connection was established. /// /// The functions will use HTTP CONNECT request and the tokio runtime. /// /// # Example /// ```no_run /// use async_http_proxy::http_connect_tokio; /// use std::error::Error; /// use tokio::net::TcpStream; /// use url::Url; /// // Features "runtime-tokio" and "basic-auth" have to be activated /// #[tokio::main] /// async fn main() -> Result<(), Box<dyn Error>> { /// let mut stream = TcpStream::connect("127.0.0.1:8080").await?; /// let url = Url::parse("https://github.com")?; /// /// http_connect_tokio(&mut stream, &url).await?; /// // stream is now connect to github.com /// Ok(()) /// } /// ``` #[cfg(feature = "runtime-tokio")] #[cfg_attr(docsrs, doc(cfg(all(feature = "runtime-tokio"))))] pub async fn http_connect_tokio<IO>(io: &mut IO, url: &url::Url) -> Result<(), HttpError> where IO: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin, { let mut stream = tokio::io::BufStream::new(io); request::send_request_tokio(&mut stream, url).await?; response::recv_and_check_response_tokio(&mut stream).await?; Ok(()) } /// Connect to the server defined by the URL and check if the connection was established. /// /// The functions will use HTTP CONNECT request and the tokio framework. /// /// # Example /// ```no_run /// use async_http_proxy::http_connect_async_std; /// use async_std::net::TcpStream; /// use async_std::task; /// use std::error::Error; /// use url::Url; /// // Features "runtime-async-std" and "basic-auth" have to be activated /// fn main() -> Result<(), Box<dyn Error>> { /// task::block_on(async { /// let mut stream = TcpStream::connect("127.0.0.1:8080").await?; /// let url = Url::parse("https://github.com")?; /// /// http_connect_async_std(&mut stream, &url).await?; /// // stream is now connect to github.com /// Ok(()) /// }) /// } /// ``` #[cfg(feature = "runtime-async-std")] #[cfg_attr(docsrs, doc(cfg(all(feature = "runtime-async-std"))))] pub async fn http_connect_async_std<IO>(io: &mut IO, url: &url::Url) -> Result<(), HttpError> where IO: async_std::io::Read + async_std::io::Write + Unpin, { request::send_request_async_std(io, url).await?; response::recv_and_check_response_async_std(io).await?; Ok(()) }