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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#![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 `example.org` via Connect proxy (`tokio`):
//! ```ignore
//! use async_http_proxy::http_connect_tokio;
//! use std::error::Error;
//! use tokio::net::TcpStream;
//! // Features "runtime-tokio" have to be activated
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//!     let mut stream = TcpStream::connect("127.0.0.1:8080").await?;
//!     http_connect_tokio(&mut stream, "example.org", 443).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_with_basic_auth;
//! use async_std::net::TcpStream;
//! use async_std::task;
//! use std::error::Error;
//! // 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?;
//!         http_connect_async_std_with_basic_auth(
//!             &mut stream,
//!             "example.org",
//!             443,
//!             "username",
//!             "password",
//!         )
//!         .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;

#[cfg(feature = "runtime-async-std")]
use async_std::io::{Read, Write};
use httparse::Error as HttpParseError;
#[cfg(feature = "runtime-async-std")]
use response::recv_and_check_response_async_std;
#[cfg(feature = "runtime-tokio")]
use response::recv_and_check_response_tokio;
use std::io::Error as IoError;
use thiserror::Error as ThisError;
#[cfg(feature = "runtime-tokio")]
use tokio::io::{AsyncRead, AsyncWrite, BufStream};

/// 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),
}

/// Connect to the server defined by the host and port 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;
/// // Features "runtime-tokio" have to be activated
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn Error>> {
///     let mut stream = TcpStream::connect("127.0.0.1:8080").await?;
///     http_connect_tokio(&mut stream, "example.org", 443).await?;
///     // stream is now connect to github.com
///     Ok(())
/// }
/// ```
#[cfg(feature = "runtime-tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "runtime-tokio")))]
pub async fn http_connect_tokio<IO>(io: &mut IO, host: &str, port: u16) -> Result<(), HttpError>
where
    IO: AsyncRead + AsyncWrite + Unpin,
{
    let mut stream = BufStream::new(io);

    request::send_request_tokio(&mut stream, host, port).await?;

    recv_and_check_response_tokio(&mut stream).await?;

    Ok(())
}

/// Connect to the server defined by the host and port with basic auth and check if the connection \
/// was established.
///
/// The functions will use HTTP CONNECT request and the tokio runtime.
///
/// # Example
/// use async_http_proxy::http_connect_tokio_with_basic_auth;
/// use std::error::Error;
/// use tokio::net::TcpStream;
/// // 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?;
///     http_connect_tokio_with_basic_auth(&mut stream, "example.org", 443, "username", "password")
///         .await?;
///     // stream is now connect to github.com
///     Ok(())
/// }
/// ```no_run
/// ```
#[cfg(all(feature = "runtime-tokio", feature = "basic-auth"))]
#[cfg_attr(
    docsrs,
    doc(cfg(all(feature = "runtime-tokio", feature = "basic-auth")))
)]
pub async fn http_connect_tokio_with_basic_auth<IO>(
    io: &mut IO,
    host: &str,
    port: u16,
    username: &str,
    password: &str,
) -> Result<(), HttpError>
where
    IO: AsyncRead + AsyncWrite + Unpin,
{
    let mut stream = BufStream::new(io);

    request::send_request_tokio_with_basic_auth(&mut stream, host, port, username, password)
        .await?;

    recv_and_check_response_tokio(&mut stream).await?;

    Ok(())
}

/// Connect to the server defined by the host and port 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;
/// // Features "runtime-async-std" 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?;
///         http_connect_async_std(&mut stream, "example.org", 443).await?;
///         // stream is now connect to github.com
///         Ok(())
///     })
/// }
/// ```
#[cfg(feature = "runtime-async-std")]
#[cfg_attr(docsrs, doc(cfg(feature = "runtime-async-std")))]
pub async fn http_connect_async_std<IO>(io: &mut IO, host: &str, port: u16) -> Result<(), HttpError>
where
    IO: Read + Write + Unpin,
{
    request::send_request_async_std(io, host, port).await?;

    recv_and_check_response_async_std(io).await?;

    Ok(())
}

/// Connect to the server defined by the host and port with basic auth and check if the connection \
/// was established.
///
/// The functions will use HTTP CONNECT request and the async std framework.
///
/// # Example
/// ```no_run
/// use async_http_proxy::http_connect_async_std_with_basic_auth;
/// use async_std::net::TcpStream;
/// use async_std::task;
/// use std::error::Error;
/// // 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?;
///         http_connect_async_std_with_basic_auth(
///             &mut stream,
///             "example.org",
///             443,
///             "username",
///             "password",
///         )
///         .await?;
///         // stream is now connect to github.com
///         Ok(())
///     })
/// }
/// ```
#[cfg(all(feature = "runtime-async-std", feature = "basic-auth"))]
#[cfg_attr(
    docsrs,
    doc(cfg(all(feature = "runtime-async-std", feature = "basic-auth")))
)]
pub async fn http_connect_async_std_with_basic_auth<IO>(
    io: &mut IO,
    host: &str,
    port: u16,
    username: &str,
    password: &str,
) -> Result<(), HttpError>
where
    IO: Read + Write + Unpin,
{
    request::send_request_async_std_with_basic_auth(io, host, port, username, password).await?;

    recv_and_check_response_async_std(io).await?;

    Ok(())
}