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
use std::any::Any;
use bytes::Bytes;
use http::{HeaderName, HeaderValue, Method, Request, Uri, Version};
use http::header::{CONNECTION, HOST, TE, TRANSFER_ENCODING, UPGRADE};
use http::request::Builder;
#[cfg(not(feature = "hyper-tls"))]
use monoio_http::common::body::HttpBody;
#[cfg(any(feature = "hyper", feature = "pool-hyper", feature = "hyper-tls"))]
use crate::hyper::client::MonoioHyperClient;
#[cfg(any(feature = "hyper", feature = "pool-hyper", feature = "hyper-tls"))]
use crate::hyper::hyper_body::HyperBody;
#[cfg(not(feature = "hyper-tls"))]
use super::http::{client::MonoioClient, monoio_body::MonoioBody};
use super::{response::HttpResponse, error::Error};
const PROHIBITED_HEADERS: [HeaderName; 5] = [
CONNECTION,
HeaderName::from_static("keep-alive"),
TE,
TRANSFER_ENCODING,
UPGRADE,
];
pub trait RequestBody {
type Body;
fn create_body(bytes: Option<Bytes>) -> Self::Body;
}
pub struct HttpRequest<C> {
client: C,
builder: Builder,
}
impl<C> HttpRequest<C> {
pub(crate) fn new(client: C) -> HttpRequest<C> {
HttpRequest {
client,
builder: Builder::default(),
}
}
/// Sets the URI for the HTTP request.
/// Accepts any type that can be converted into a `Uri`.
/// # Examples
/// ```
/// request.set_uri("https://example.com/path");
/// request.set_uri(Uri::from_static("https://example.com/path"));
/// ```
pub fn set_uri<T>(mut self, uri: T) -> Self
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<http::Error>,
{
self.builder = self.builder.uri(uri);
self
}
/// Sets the HTTP method for the request (GET, POST, PUT, etc.).
/// Accepts any type that can be converted into a `Method`.
/// # Examples
/// ```
/// request.set_method("POST");
/// request.set_method(Method::POST);
/// ```
pub fn set_method<T>(mut self, method: T) -> Self
where
Method: TryFrom<T>,
<Method as TryFrom<T>>::Error: Into<http::Error>,
{
self.builder = self.builder.method(method);
self
}
/// Sets a header in the HTTP request.
/// Note: For HTTP/2 requests, connection-specific headers will be automatically removed.
/// The 'host' header is mandatory in HTTP/1.1 and will be added by default if not set.
/// # Examples
/// ```
/// request.set_header("content-type", "application/json");
/// request.set_header(HeaderName::from_static("authorization"), "Bearer token");
/// ```
pub fn set_header<K, T>(mut self, key: K, value: T) -> Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
HeaderValue: TryFrom<T>,
<HeaderValue as TryFrom<T>>::Error: Into<http::Error>,
{
self.builder = self.builder.header(key, value);
self
}
/// Sets the HTTP version for the request.
/// Default version is HTTP/1.1 if not specified.
/// # Examples
/// ```
/// request.set_version(Version::HTTP_11);
/// request.set_version(Version::HTTP_2);
/// ```
pub fn set_version(mut self, version: Version) -> Self {
self.builder = self.builder.version(version);
self
}
/// Adds a type-based extension to the request.
/// Extensions can be used to store extra information that travels along with the request.
/// The extension type must be `Clone + Any + 'static`.
pub fn set_extension<T>(mut self, extension: T) -> Self
where
T: Clone + Any + Send + Sync + 'static,
{
self.builder = self.builder.extension(extension);
self
}
fn build_request<B: RequestBody>(
builder: Builder,
body: Option<Bytes>,
) -> Result<(Request<B::Body>, Uri), Error> {
let mut request = builder
.body(B::create_body(body))
.map_err(Error::HttpRequestBuilder)?;
let uri = request.uri().clone();
// Remove any connection specific headers to Http/2 requests
// Avoid adding host header to Http/2 based requests but not Http/1.1
// unless you are sending request to a proxy which downgrade the connection
match request.version() {
Version::HTTP_2 | Version::HTTP_3 => {
let headers = request.headers_mut();
for header in PROHIBITED_HEADERS.iter() {
headers.remove(header);
}
}
_ => {
if let Some(host) = uri.host() {
let host = HeaderValue::try_from(host).map_err(Error::InvalidHeaderValue)?;
let headers = request.headers_mut();
if !headers.contains_key(HOST) {
headers.insert(HOST, host);
}
}
}
}
Ok((request, uri))
}
}
#[cfg(not(feature = "hyper-tls"))]
impl HttpRequest<MonoioClient> {
/// Sends the HTTP request without a body.
/// Returns a Result containing either the HTTP response or an error.
pub async fn send(self) -> Result<HttpResponse<HttpBody>, Error> {
self.send_body(None).await
}
/// Sends the HTTP request with an optional body.
/// The body can be provided as any type that can be converted into `Option<Bytes>`.
/// Returns a Result containing either the HTTP response or an error.
/// # Examples
/// ```
/// let response = request.send_body(Some(Bytes::from("request body"))).await?;
/// let response = request.send_body(None).await?; // No body
/// ```
pub async fn send_body(self, body: impl Into<Option<Bytes>>) -> Result<HttpResponse<HttpBody>, Error> {
let (req, uri) = Self::build_request::<MonoioBody>(self.builder, body.into())?;
let response = self.client.send_request(req, uri).await?;
Ok(HttpResponse::new(response))
}
}
#[cfg(any(feature = "hyper", feature = "pool-hyper", feature = "hyper-tls"))]
impl HttpRequest<MonoioHyperClient> {
/// Sends the HTTP request without a body.
/// Returns a Result containing either the HTTP response or an error.
pub async fn send(self) -> Result<HttpResponse<Bytes>, Error> { self.send_body(None).await }
/// Sends the HTTP request with an optional body.
/// The body can be provided as any type that can be converted into `Option<Bytes>`.
/// Returns a Result containing either the HTTP response or an error.
/// # Examples
/// ```
/// let response = request.send_body(Some(Bytes::from("request body"))).await?;
/// let response = request.send_body(None).await?; // No body
/// ```
pub async fn send_body(self, body: impl Into<Option<Bytes>>) -> Result<HttpResponse<Bytes>, Error> {
let (req, uri) = Self::build_request::<HyperBody>(self.builder, body.into())?;
let response = self.client.send_request(req, uri).await?;
HttpResponse::hyper_new(response).await
}
}