http1/request/
mod.rs

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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::fmt::Display;

use crate::{
    extensions::Extensions,
    headers::{InvalidHeaderName, InvalidHeaderValue},
};

use super::{
    headers::{HeaderName, HeaderValue, Headers},
    method::Method,
    uri::{
        path_query::PathAndQuery,
        uri::{InvalidUri, Uri},
    },
    version::Version,
};

/// Request parts.
#[derive(Debug, Clone)]
pub struct Parts {
    pub headers: Headers,
    pub method: Method,
    pub version: Version,
    pub uri: Uri,
    pub extensions: Extensions,
}

impl Default for Parts {
    fn default() -> Self {
        Self {
            headers: Default::default(),
            method: Method::GET,
            version: Default::default(),
            uri: Uri::new(None, None, PathAndQuery::new(String::from("/"), None, None)),
            extensions: Default::default(),
        }
    }
}

/// Represents an HTTP request.
///
/// This struct holds the HTTP method, version, URL, headers, and body of the request.
/// The body is generic, allowing flexibility in request content.
#[derive(Debug)]
pub struct Request<T> {
    body: T,
    parts: Parts,
}

impl<T> Request<T> {
    /// Creates a new `Request` with the specified method, version, URL, and body.
    ///
    /// # Parameters
    /// - `method`: The HTTP method of the request (e.g., `GET`, `POST`).
    /// - `uri`: The URL of the request.
    /// - `body`: The content of the request body.
    ///
    /// # Returns
    /// A new `Request` instance with empty headers.
    pub fn new(method: Method, uri: Uri, body: T) -> Self {
        Request {
            body,
            parts: Parts {
                method,
                uri,
                version: Version::Http1_1,
                headers: Headers::default(),
                extensions: Extensions::new(),
            },
        }
    }

    /// Creates a request from the given parts and body.
    pub fn from_parts(parts: Parts, body: T) -> Self {
        Request { body, parts }
    }

    /// Returns a reference to the HTTP method of the request.
    pub fn method(&self) -> &Method {
        &self.parts.method
    }

    /// Returns a mutable reference to the HTTP method of the request.
    pub fn method_mut(&mut self) -> &mut Method {
        &mut self.parts.method
    }

    /// Returns a reference to the HTTP version of the request.
    pub fn version(&self) -> &Version {
        &self.parts.version
    }

    /// Returns a mutable reference to the HTTP version of the request.
    pub fn version_mut(&mut self) -> &mut Version {
        &mut self.parts.version
    }

    /// Returns a reference to the URL of the request.
    pub fn uri(&self) -> &Uri {
        &self.parts.uri
    }

    /// Returns a mutable reference to the URL of the request.
    pub fn uri_mut(&mut self) -> &mut Uri {
        &mut self.parts.uri
    }

    /// Returns a reference to the headers of the request.
    pub fn headers(&self) -> &Headers {
        &self.parts.headers
    }

    /// Returns a mutable reference to the headers of the request.
    pub fn headers_mut(&mut self) -> &mut Headers {
        &mut self.parts.headers
    }

    /// Returns this request extensions.
    pub fn extensions(&self) -> &Extensions {
        &self.parts.extensions
    }

    /// Returns a mutable reference to the request extensions.
    pub fn extensions_mut(&mut self) -> &mut Extensions {
        &mut self.parts.extensions
    }

    /// Returns a reference to the body of the request.
    pub fn body(&self) -> &T {
        &self.body
    }

    /// Returns a mutable reference to the body of the request.
    pub fn body_mut(&mut self) -> &mut T {
        &mut self.body
    }

    /// Returns a reference to the request parts.
    pub fn parts(&self) -> &Parts {
        &self.parts
    }

    /// Returns a mutable reference to the request parts.
    pub fn parts_mut(&mut self) -> &mut Parts {
        &mut self.parts
    }

    /// Returns the body
    pub fn into_body(self) -> T {
        self.body
    }

    /// Split this request into its body and rest of the parts.
    pub fn into_parts(self) -> (T, Parts) {
        let Self { body, parts } = self;
        (body, parts)
    }

    /// Separates the request and the body.
    pub fn drop_body(self) -> (Request<()>, T) {
        let Request { body, parts } = self;
        (Request::from_parts(parts, ()), body)
    }

    /// Maps this request body to other type.
    pub fn map_body<F, U>(self, f: F) -> Request<U>
    where
        F: FnOnce(T) -> U,
    {
        Request {
            body: f(self.body),
            parts: self.parts,
        }
    }
}

impl Request<()> {
    /// Creates a new request from other with no body.
    pub fn with_body<T>(req: Request<()>, body: T) -> Request<T> {
        Request {
            body,
            parts: req.parts,
        }
    }

    /// Returns a `Builder` for constructing a new `Request`.
    ///
    /// # Example
    /// ```
    /// use http1::{method::Method,request::Request};
    ///
    /// let request = Request::builder()
    ///     .method(Method::POST)
    ///     .uri("/submit")
    ///     .body("body content")
    ///     .unwrap();
    /// ```
    pub fn builder() -> Builder {
        Builder::new()
    }
}

#[derive(Debug)]
pub enum InvalidRequest {
    InvalidUri(InvalidUri),
    InvalidHeaderName(InvalidHeaderName),
    InvalidHeaderValue(InvalidHeaderValue),
}

impl Display for InvalidRequest {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            InvalidRequest::InvalidUri(invalid_uri) => write!(f, "{invalid_uri}"),
            InvalidRequest::InvalidHeaderName(invalid_header_name) => {
                write!(f, "invalid request, {invalid_header_name}")
            }
            InvalidRequest::InvalidHeaderValue(invalid_header_value) => {
                write!(f, "invalid request, {invalid_header_value}")
            }
        }
    }
}

impl std::error::Error for InvalidRequest {}

/// A builder for constructing `Request` objects.
///
/// The `Builder` allows setting the HTTP method, version, URL, and headers before
/// building the final `Request` with a body.
pub struct Builder {
    inner: Result<Parts, InvalidRequest>,
}

impl Builder {
    /// Creates a new `Builder` with default values:
    /// - `GET` method
    /// - `HTTP/1.1` version
    /// - URL set to "/"
    /// - Empty headers
    pub fn new() -> Self {
        let parts = Parts {
            headers: Headers::new(),
            method: Method::GET,
            uri: Uri::new(None, None, PathAndQuery::new("/".to_owned(), None, None)),
            version: Version::Http1_1,
            extensions: Default::default(),
        };

        Builder { inner: Ok(parts) }
    }

    fn update<F: FnOnce(&mut Parts) -> Result<(), InvalidRequest>>(mut self, f: F) -> Self {
        self.inner = self.inner.and_then(move |mut parts| {
            f(&mut parts)?;
            Ok(parts)
        });

        self
    }

    /// Sets the HTTP version of the request.
    ///
    /// # Parameters
    /// - `version`: The HTTP version to set (e.g., `Version::Http1_1`).
    ///
    /// # Returns
    /// The updated `Builder`.
    pub fn version(self, version: Version) -> Self {
        self.update(|parts| {
            parts.version = version;
            Ok(())
        })
    }

    /// Returns a mutable reference to the HTTP version.
    pub fn version_mut(&mut self) -> Option<&mut Version> {
        self.inner.as_mut().ok().map(|parts| &mut parts.version)
    }

    /// Sets the HTTP method of the request.
    ///
    /// # Parameters
    /// - `method`: The HTTP method to set (e.g., `Method::POST`).
    ///
    /// # Returns
    /// The updated `Builder`.
    pub fn method(self, method: Method) -> Self {
        self.update(|parts| {
            parts.method = method;
            Ok(())
        })
    }

    /// Returns a mutable reference to the HTTP method.
    pub fn method_mut(&mut self) -> Option<&mut Method> {
        self.inner.as_mut().ok().map(|parts| &mut parts.method)
    }

    /// Returns a mutable reference to the extensions.
    pub fn extensions_mut(&mut self) -> Option<&mut Extensions> {
        self.inner.as_mut().ok().map(|parts| &mut parts.extensions)
    }

    /// Sets the URL of the request.
    ///
    /// # Parameters
    /// - `url`: The URL to set.
    ///
    /// # Returns
    /// The updated `Builder`.
    pub fn uri<T>(self, uri: T) -> Self
    where
        T: TryInto<Uri>,
        T::Error: Into<InvalidUri>,
    {
        self.update(|parts| {
            parts.uri = uri
                .try_into()
                .map_err(|err| InvalidRequest::InvalidUri(err.into()))?;
            Ok(())
        })
    }

    /// Returns a mutable reference to the URL.
    pub fn uri_mut(&mut self) -> Option<&mut Uri> {
        self.inner.as_mut().ok().map(|parts| &mut parts.uri)
    }

    /// Returns a reference to the headers being set in the `Builder`.
    pub fn headers(&self) -> Option<&Headers> {
        self.inner.as_ref().ok().map(|parts| &parts.headers)
    }

    /// Returns a mutable reference to the headers being set in the `Builder`.
    pub fn headers_mut(&mut self) -> Option<&mut Headers> {
        self.inner.as_mut().ok().map(|parts| &mut parts.headers)
    }

    /// Inserts a header into the request.
    ///
    /// If the header already exists, it is replaced.
    ///
    /// # Parameters
    /// - `key`: The name of the header.
    /// - `value`: The value of the header.
    ///
    /// # Returns
    /// The updated `Builder`.
    pub fn insert_header<K, V>(self, name: K, value: V) -> Self
    where
        K: TryInto<HeaderName>,
        K::Error: Into<InvalidHeaderName>,
        V: TryInto<HeaderValue>,
        V::Error: Into<InvalidHeaderValue>,
    {
        self.update(|parts| {
            let key = name
                .try_into()
                .map_err(|err| InvalidRequest::InvalidHeaderName(err.into()))?;
            let value = value
                .try_into()
                .map_err(|err| InvalidRequest::InvalidHeaderValue(err.into()))?;

            parts.headers.insert(key, value);
            Ok(())
        })
    }

    /// Appends a value to an existing header in the request.
    ///
    /// If the header does not exist, it is created.
    ///
    /// # Parameters
    /// - `key`: The name of the header.
    /// - `value`: The value to append.
    ///
    /// # Returns
    /// The updated `Builder`.
    pub fn append_header<K, V>(self, name: K, value: V) -> Self
    where
        K: TryInto<HeaderName>,
        K::Error: Into<InvalidHeaderName>,
        V: TryInto<HeaderValue>,
        V::Error: Into<InvalidHeaderValue>,
    {
        self.update(|parts| {
            let key = name
                .try_into()
                .map_err(|err| InvalidRequest::InvalidHeaderName(err.into()))?;
            let value = value
                .try_into()
                .map_err(|err| InvalidRequest::InvalidHeaderValue(err.into()))?;

            parts.headers.append(key, value);
            Ok(())
        })
    }

    /// Builds the final `Request` with the provided body.
    ///
    /// # Parameters
    /// - `body`: The content to include in the request body.
    ///
    /// # Returns
    /// A `Request` with the method, version, URL, headers, and body.
    pub fn body<T>(self, body: T) -> Result<Request<T>, InvalidRequest> {
        let inner = self.inner?;
        let Parts {
            headers,
            method,
            version,
            uri,
            extensions,
        } = inner;

        Ok(Request {
            body,
            parts: Parts {
                method,
                version,
                uri,
                headers,
                extensions,
            },
        })
    }
}

impl Default for Builder {
    fn default() -> Self {
        Self::new()
    }
}