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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use super::error::Error;
use crate::*;
use std::{collections::HashMap, str::SplitN};
impl Default for Request {
#[inline]
fn default() -> Self {
Self {
method: String::new(),
host: String::new(),
path: String::new(),
query: HashMap::new(),
headers: HashMap::new(),
body: Vec::new(),
}
}
}
impl Request {
/// Creates a new `Request` object from a TCP stream.
///
/// # Parameters
/// - `reader`: A mut reference to a `&mut std::io::BufReader<&std::net::TcpStream>`
///
/// # Returns
/// - `Ok`: A `Request` object populated with the HTTP request data.
/// - `Err`: An `Error` if the request is invalid or cannot be read.
#[inline]
pub fn from_reader(reader: &mut std::io::BufReader<&std::net::TcpStream>) -> RequestNewResult {
let mut request_line: String = String::new();
std::io::BufRead::read_line(reader, &mut request_line).map_err(|_| Error::HttpReadError)?;
let parts: Vec<&str> = request_line.split_whitespace().collect();
if parts.len() < 3 {
return Err(Error::InvalidHttpRequest);
}
let method: RequestMethod = parts[0].to_string();
let full_path: String = parts[1].to_string();
let hash_index: Option<usize> = full_path.find(HASH_SYMBOL);
let query_index: Option<usize> = full_path.find(QUERY_SYMBOL);
let query_string: String = query_index.map_or(EMPTY_STR.to_owned(), |i| {
let temp: String = full_path[i + 1..].to_string();
if hash_index.is_none() || hash_index.unwrap() <= i {
return temp.into();
}
let data: String = temp
.split(HASH_SYMBOL)
.next()
.unwrap_or_default()
.to_string();
data.into()
});
let query: RequestQuery = Self::parse_query(&query_string);
let path: RequestPath = if let Some(i) = query_index.or(hash_index) {
full_path[..i].to_string()
} else {
full_path
};
let mut headers: RequestHeaders = HashMap::new();
let mut host: RequestHost = EMPTY_STR.to_owned();
let mut content_length: usize = 0;
loop {
let mut header_line: String = String::new();
std::io::BufRead::read_line(reader, &mut header_line)
.map_err(|_| Error::HttpReadError)?;
let header_line: &str = header_line.trim();
if header_line.is_empty() {
break;
}
let parts: Vec<&str> = header_line.splitn(2, COLON_SPACE_SYMBOL).collect();
if parts.len() != 2 {
continue;
}
let key: String = parts[0].trim().to_string();
let value: String = parts[1].trim().to_string();
if key.eq_ignore_ascii_case(HOST) {
host = value.to_string();
}
if key.eq_ignore_ascii_case(CONTENT_LENGTH) {
content_length = value.parse().unwrap_or(0);
}
headers.insert(key, value);
}
let mut body: RequestBody = Vec::new();
if content_length > 0 {
body.resize(content_length, 0);
std::io::Read::read_exact(reader, &mut body).map_err(|_| Error::HttpReadError)?;
}
Ok(Request {
method,
host,
path,
query,
headers,
body,
})
}
/// Creates a new `Request` object from a TCP stream.
///
/// # Parameters
/// - `reader`: A mut reference to a `&mut tokio::io::BufReader<&mut tokio::net::TcpStream>`
///
/// # Returns
/// - `Ok`: A `Request` object populated with the HTTP request data.
/// - `Err`: An `Error` if the request is invalid or cannot be read.
#[inline]
pub async fn from_tokio_reader(
reader: &mut tokio::io::BufReader<&mut tokio::net::TcpStream>,
) -> RequestNewResult {
let mut request_line: String = String::new();
let _ = tokio::io::AsyncBufReadExt::read_line(reader, &mut request_line).await;
let parts: Vec<&str> = request_line.split_whitespace().collect();
if parts.len() < 3 {
return Err(Error::InvalidHttpRequest);
}
let method: RequestMethod = parts[0].to_string();
let full_path: String = parts[1].to_string();
let hash_index: Option<usize> = full_path.find(HASH_SYMBOL);
let query_index: Option<usize> = full_path.find(QUERY_SYMBOL);
let query_string: String = query_index.map_or(EMPTY_STR.to_owned(), |i| {
let temp: String = full_path[i + 1..].to_string();
if hash_index.is_none() || hash_index.unwrap() <= i {
return temp.into();
}
let data: String = temp
.split(HASH_SYMBOL)
.next()
.unwrap_or_default()
.to_string();
data.into()
});
let query: RequestQuery = Self::parse_query(&query_string);
let path: RequestPath = if let Some(i) = query_index.or(hash_index) {
full_path[..i].to_string()
} else {
full_path
};
let mut headers: RequestHeaders = HashMap::new();
let mut host: RequestHost = EMPTY_STR.to_owned();
let mut content_length: usize = 0;
loop {
let mut header_line: String = String::new();
let _ = tokio::io::AsyncBufReadExt::read_line(reader, &mut header_line).await;
let header_line: &str = header_line.trim();
if header_line.is_empty() {
break;
}
let parts: Vec<&str> = header_line.splitn(2, COLON_SPACE_SYMBOL).collect();
if parts.len() != 2 {
continue;
}
let key: String = parts[0].trim().to_string();
let value: String = parts[1].trim().to_string();
if key.eq_ignore_ascii_case(HOST) {
host = value.to_string();
}
if key.eq_ignore_ascii_case(CONTENT_LENGTH) {
content_length = value.parse().unwrap_or(0);
}
headers.insert(key, value);
}
let mut body: RequestBody = Vec::new();
if content_length > 0 {
body.resize(content_length, 0);
let _ = tokio::io::AsyncReadExt::read_exact(reader, &mut body);
}
Ok(Request {
method,
host,
path,
query,
headers,
body,
})
}
/// Creates a new `Request` object from a TCP stream.
///
/// # Parameters
/// - `stream`: A reference to a `&std::net::TcpStream` representing the incoming connection.
///
/// # Returns
/// - `Ok`: A `Request` object populated with the HTTP request data.
/// - `Err`: An `Error` if the request is invalid or cannot be read.
#[inline]
pub fn from_stream(stream: &std::net::TcpStream) -> RequestNewResult {
let mut reader: std::io::BufReader<&std::net::TcpStream> = std::io::BufReader::new(stream);
Self::from_reader(&mut reader)
}
/// Creates a new `Request` object from a TCP stream.
///
/// # Parameters
/// - `stream`: A reference to a `&ArcRwLockStream` representing the incoming connection.
///
/// # Returns
/// - `Ok`: A `Request` object populated with the HTTP request data.
/// - `Err`: An `Error` if the request is invalid or cannot be read.
#[inline]
pub async fn from_tokio_stream(stream: &ArcRwLockStream) -> RequestNewResult {
let mut buf_stream: RwLockWriteGuard<'_, tokio::net::TcpStream> = stream.write().await;
let mut reader: tokio::io::BufReader<&mut tokio::net::TcpStream> =
tokio::io::BufReader::new(&mut buf_stream);
Self::from_tokio_reader(&mut reader).await
}
/// Parse query
///
/// # Parameters
/// - `query`: &str
///
/// # Returns
/// - RequestQuery
#[inline]
fn parse_query(query: &str) -> RequestQuery {
let mut query_map: RequestQuery = HashMap::new();
for pair in query.split(AND) {
let mut parts: SplitN<'_, &str> = pair.splitn(2, EQUAL);
let key: String = parts.next().unwrap_or_default().to_string();
let value: String = parts.next().unwrap_or_default().to_string();
if !key.is_empty() {
query_map.insert(key, value);
}
}
query_map
}
/// Adds a header to the request.
///
/// This function inserts a key-value pair into the request headers.
/// The key and value are converted into `String`, allowing for efficient handling of both owned and borrowed string data.
///
/// # Parameters
/// - `key`: The header key, which will be converted into a `String`.
/// - `value`: The value of the header, which will be converted into a `String`.
///
/// # Returns
/// - Returns a mutable reference to the current instance (`&mut Self`), allowing for method chaining.
#[inline]
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
where
K: Into<String>,
V: Into<String>,
{
self.headers.insert(key.into(), value.into());
self
}
/// Set the body of the response.
///
/// This method allows you to set the body of the response by converting the provided
/// value into a `RequestBody` type. The `body` is updated with the converted value,
/// and the method returns a mutable reference to the current instance for method chaining.
///
/// # Parameters
/// - `body`: The body of the response to be set. It can be any type that can be converted
/// into a `RequestBody` using the `Into` trait.
///
/// # Return Value
/// - Returns a mutable reference to the current instance of the struct, enabling method chaining.
/// Set the body of the response.
///
/// This method allows you to set the body of the response by converting the provided
/// value into a `RequestBody` type. The `body` is updated with the converted value,
/// and the method returns a mutable reference to the current instance for method chaining.
///
/// # Parameters
/// - `body`: The body of the response to be set. It can be any type that can be converted
/// into a `RequestBody` using the `Into` trait.
///
/// # Return Value
/// - Returns a mutable reference to the current instance of the struct, enabling method chaining.
#[inline]
pub fn set_body<T: Into<RequestBody>>(&mut self, body: T) -> &mut Self {
self.body = body.into();
self
}
/// Set the HTTP method of the request.
///
/// This method allows you to set the HTTP method (e.g., GET, POST) of the request
/// by converting the provided value into a `RequestMethod` type. The `method` is updated
/// with the converted value, and the method returns a mutable reference to the current
/// instance for method chaining.
///
/// # Parameters
/// - `method`: The HTTP method to be set for the request. It can be any type that can
/// be converted into a `RequestMethod` using the `Into` trait.
///
/// # Return Value
/// - Returns a mutable reference to the current instance of the struct, enabling method chaining.
#[inline]
pub fn set_method<T: Into<RequestMethod>>(&mut self, method: T) -> &mut Self {
self.method = method.into();
self
}
/// Set the host of the request.
///
/// This method allows you to set the host (e.g., www.example.com) for the request
/// by converting the provided value into a `RequestHost` type. The `host` is updated
/// with the converted value, and the method returns a mutable reference to the current
/// instance for method chaining.
///
/// # Parameters
/// - `host`: The host to be set for the request. It can be any type that can be converted
/// into a `RequestHost` using the `Into` trait.
///
/// # Return Value
/// - Returns a mutable reference to the current instance of the struct, enabling method chaining.
#[inline]
pub fn set_host<T: Into<RequestHost>>(&mut self, host: T) -> &mut Self {
self.host = host.into();
self
}
/// Set the path of the request.
///
/// This method allows you to set the path (e.g., /api/v1/resource) for the request
/// by converting the provided value into a `RequestPath` type. The `path` is updated
/// with the converted value, and the method returns a mutable reference to the current
/// instance for method chaining.
///
/// # Parameters
/// - `path`: The path to be set for the request. It can be any type that can be converted
/// into a `RequestPath` using the `Into` trait.
///
/// # Return Value
/// - Returns a mutable reference to the current instance of the struct, enabling method chaining.
#[inline]
pub fn set_path<T: Into<RequestPath>>(&mut self, path: T) -> &mut Self {
self.path = path.into();
self
}
/// Set the query string of the request.
///
/// This method allows you to set the query string (e.g., ?key=value) for the request
/// by converting the provided value into a `RequestQuery` type. The `query` is updated
/// with the converted value, and the method returns a mutable reference to the current
/// instance for method chaining.
///
/// # Parameters
/// - `query`: The query string to be set for the request. It can be any type that can
/// be converted into a `RequestQuery` using the `Into` trait.
///
/// # Return Value
/// - Returns a mutable reference to the current instance of the struct, enabling method chaining.
#[inline]
pub fn set_query<T: Into<RequestQuery>>(&mut self, query: T) -> &mut Self {
self.query = query.into();
self
}
}