hotaru_http 0.8.3

HTTP/1.1 implementation for the Hotaru web framework
Documentation
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
use crate::message::http_value::{ContentDisposition, StatusCode};
use crate::security::safety::HttpSafety;

use crate::message::body::HttpBody;
use crate::util::cookie::Cookie;
use crate::message::http_value::HttpContentType;
use crate::message::meta::HttpMeta;
use crate::context::io;
use crate::message::start_line::{HttpStartLine, ResponseStartLine};
use std::collections::HashMap;
use hotaru_core::connection::{HotaruBufRead, HotaruWrite};

#[derive(Debug, Clone)]
pub struct HttpResponse {
    pub meta: HttpMeta,
    pub body: HttpBody,
}

impl HttpResponse {
    pub fn new(meta: HttpMeta, body: HttpBody) -> Self {
        Self { meta, body }
    }

    pub async fn parse_lazy<R: HotaruBufRead<Error = std::io::Error> + Unpin + Send>(
        stream: &mut R,
        config: &HttpSafety,
        print_raw: bool,
    ) -> Self {
        match io::parse_lazy(stream, config, false, print_raw).await {
            Ok((meta, body)) => Self::new(meta, body),
            Err(_) => Self::default(),
        }
    }

    /// Parses the HTTP Body from buffer
    pub async fn parse_body(&mut self, safety_setting: &HttpSafety) {
        let body = std::mem::take(&mut self.body);
        self.body = body.parse_buffer(safety_setting);
    }

    /// Get the parsed HTTP Body
    pub async fn get_parsed_body(&mut self, safety: HttpSafety) -> HttpBody {
        self.parse_body(&safety).await;
        self.body.clone()
    }

    /// Add a cookie into the response metadata.
    /// Insert an empty cookie to delete the cookie.
    pub fn add_cookie<T: Into<String>>(mut self, key: T, cookie: Cookie) -> Self {
        self.meta.add_cookie(key, cookie);
        self
    }

    /// Set content type for Http Response
    pub fn content_type(mut self, content_type: HttpContentType) -> Self {
        self.meta.set_content_type(content_type);
        self
    }

    /// Add a header for Http Response
    pub fn add_header<T: Into<String>, U: Into<String>>(mut self, key: T, value: U) -> Self {
        self.meta.set_attribute(key, value.into());
        self
    }

    /// Set the content disposition for the request.
    pub fn content_disposition(mut self, disposition: ContentDisposition) -> Self {
        self.meta.set_content_disposition(disposition);
        self
    }

    /// Send a status
    pub fn status<T: Into<StatusCode>>(mut self, status: T) -> Self {
        self.meta.start_line.set_status_code(status);
        self
    }

    /// Send the response
    /// When this method is changed, please also check Request::send()
    pub async fn send<W: HotaruWrite<Error = std::io::Error> + Unpin + Send>(self, writer: &mut W) -> std::io::Result<()> {
        io::send(self.meta, self.body, writer).await
    }

    // /// Converts this response into a Future that resolves to itself.
    // /// Useful for middleware functions that need to return a Future<Output = HttpResponse>.
    // pub fn future(self) -> impl Future<Output = HttpResponse> + Send {
    //     ready(self)
    // }

    // /// Creates a boxed future from this response (useful for trait objects).
    // pub fn boxed_future(self) -> Pin<Box<dyn Future<Output = HttpResponse> + Send>> {
    //     Box::pin(self.future())
    // }
}

impl Default for HttpResponse {
    fn default() -> Self {
        let meta = HttpMeta::new(
            HttpStartLine::Response(ResponseStartLine::default()),
            HashMap::new(),
        );
        let body = HttpBody::default(); // Default body is empty string
        HttpResponse::new(meta, body)
    }
}

/// Collection of helper functions to easily create common HTTP responses.
///
/// This module provides convenient functions to create standardized HTTP responses
/// such as text, HTML, JSON, redirects, status codes, and template-based responses.
/// All functions return an `HttpResponse` that can be further customized if needed.
pub mod response_templates {
    use std::collections::HashMap;
    use std::path::Path;

    use akari::TemplateManager;
    use akari::Value;

    use super::HttpResponse;
    use crate::message::body::HttpBody;
    use crate::message::http_value::{HttpContentType, HttpVersion, StatusCode};
    use crate::message::meta::HttpMeta;
    use crate::message::start_line::HttpStartLine;

    /// Creates a plain text HTTP response with status 200 OK.
    ///
    /// # Arguments
    ///
    /// * `body` - The text content to be sent in the response.
    ///
    /// # Returns
    ///
    /// An `HttpResponse` with Content-Type set to text/plain.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crate::response::response_templates;
    ///
    /// let response = response_templates::text_response("Hello, world!");
    /// ```
    pub fn text_response(body: impl Into<String>) -> HttpResponse {
        let start_line = HttpStartLine::new_response(HttpVersion::Http11, StatusCode::OK);
        let mut meta = HttpMeta::new(start_line, HashMap::new());
        meta.set_content_type(HttpContentType::TextPlain());
        HttpResponse::new(meta, HttpBody::Text(body.into()))
    }

    /// Creates an HTML HTTP response with status 200 OK.
    ///
    /// # Arguments
    ///
    /// * `body` - The HTML content to be sent in the response.
    ///
    /// # Returns
    ///
    /// An `HttpResponse` with Content-Type set to text/html.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crate::response::response_templates;
    ///
    /// let html = "<html><body><h1>Hello, world!</h1></body></html>";
    /// let response = response_templates::html_response(html);
    /// ```
    pub fn html_response(body: impl Into<Vec<u8>>) -> HttpResponse {
        let start_line = HttpStartLine::new_response(HttpVersion::Http11, StatusCode::OK);
        let mut meta = HttpMeta::new(start_line, HashMap::new());
        meta.set_content_type(HttpContentType::TextHtml());
        HttpResponse::new(meta, HttpBody::Binary(body.into()))
    }

    /// Creates a redirect response (302 Found).
    ///
    /// # Arguments
    ///
    /// * `url` - The URL to redirect to.
    ///
    /// # Returns
    ///
    /// An `HttpResponse` with the Location header set and an empty body.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crate::response::response_templates;
    ///
    /// let response = response_templates::redirect_response("/login");
    /// ```
    pub fn redirect_response(url: &str) -> HttpResponse {
        let start_line = HttpStartLine::new_response(HttpVersion::Http11, StatusCode::FOUND);
        let mut meta = HttpMeta::new(start_line, HashMap::new());
        meta.set_location(Some(url.to_string()));
        HttpResponse::new(meta, HttpBody::Empty)
    }

    /// Creates an HTML response from a template file without any data binding.
    ///
    /// # Arguments
    ///
    /// * `file` - The filename of the template within the templates directory.
    /// Never use absolute path for file argument
    ///
    /// # Returns
    ///
    /// An `HttpResponse` with the template content or a 404 error if the file is not found.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crate::response::response_templates;
    ///
    /// let response = response_templates::plain_template_response("index.html");
    /// ```
    pub fn plain_template_response(file: &str) -> HttpResponse {
        let start_line = HttpStartLine::new_response(HttpVersion::Http11, StatusCode::OK);
        let mut meta = HttpMeta::new(start_line, HashMap::new());
        let file_path = Path::new("templates").join(file);
        // println!("[Response] Loading template: {}", file_path.display());
        let body = match std::fs::read(file_path) {
            Ok(content) => content,
            Err(_) => return return_status(StatusCode::NOT_FOUND),
        };
        meta.set_content_type(HttpContentType::TextHtml());
        HttpResponse::new(meta, HttpBody::Binary(body))
    }

    pub fn serve_static_file(file: &str) -> HttpResponse {
        let start_line = HttpStartLine::new_response(HttpVersion::Http11, StatusCode::OK);
        let mut meta = HttpMeta::new(start_line, HashMap::new());
        let file_path = Path::new("templates").join(file);

        // Set the response content type based on the file extension
        meta.set_content_type(HttpContentType::from_file_name(
            file_path
                .file_name()
                .unwrap_or_default()
                .to_str()
                .unwrap_or(""),
        ));

        let body = match std::fs::read(file_path) {
            Ok(content) => content,
            Err(_) => return return_status(StatusCode::NOT_FOUND),
        };
        HttpResponse::new(meta, HttpBody::Binary(body))
    }

    /// Creates an HTTP response with a specified status code and binary body.
    ///
    /// # Arguments
    ///
    /// * `status_code` - The HTTP status code for the response.
    /// * `body` - The binary content to be sent in the response.
    ///
    /// # Returns
    ///
    /// An `HttpResponse` with the specified status code and body.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crate::response::response_templates;
    /// use crate::message::http_value::StatusCode;
    ///
    /// let response = response_templates::normal_response(StatusCode::CREATED, "Resource created");
    /// ```
    pub fn normal_response<S: Into<StatusCode>, B: Into<Vec<u8>>>(
        status_code: S,
        body: B,
    ) -> HttpResponse {
        let start_line = HttpStartLine::new_response(HttpVersion::Http11, status_code.into());
        let meta = HttpMeta::new(start_line, HashMap::new());
        HttpResponse::new(meta, HttpBody::Binary(body.into())).content_type(HttpContentType::Text {
            subtype: "plain".to_string(),
            charset: Some("utf-8".to_string()),
        })
    }

    /// Creates a JSON HTTP response with status 200 OK.
    ///
    /// # Arguments
    ///
    /// * `body` - The JSON object to be sent in the response.
    ///
    /// # Returns
    ///
    /// An `HttpResponse` with Content-Type set to application/json.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crate::response::response_templates;
    /// use akari::{Value, object};
    ///
    /// let mut data = object!({});
    /// data.set("message", "Success");
    /// data.set("status", 200);
    ///
    /// let response = response_templates::json_response(data);
    /// ```
    pub fn json_response(body: Value) -> HttpResponse {
        let start_line = HttpStartLine::new_response(HttpVersion::Http11, StatusCode::OK);
        let mut meta = HttpMeta::new(start_line, HashMap::new());
        meta.set_content_type(HttpContentType::ApplicationJson());
        HttpResponse::new(meta, HttpBody::Json(body))
    }

    /// Creates an HTML response from a template with data binding.
    ///
    /// # Arguments
    ///
    /// * `file` - The filename of the template within the templates directory.
    /// * `data` - A hashmap of values to be bound to the template.
    ///
    /// # Returns
    ///
    /// An `HttpResponse` with the rendered template or an error message if rendering fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crate::response::response_templates;
    /// use akari::Object;
    /// use std::collections::HashMap;
    ///
    /// let mut data = HashMap::new();
    /// let mut user = Object::new();
    /// user.insert("name", "John Doe");
    /// data.insert("user", user);
    ///
    /// let response = response_templates::template_response("user_profile.html", data);
    /// ```
    pub fn template_response(file: &str, data: HashMap<String, Value>) -> HttpResponse {
        let template_manager = TemplateManager::new("templates");
        let result = match template_manager.render(file, &data) {
            Ok(content) => content,
            Err(err) => return text_response(err.to_string()),
        };

        let start_line = HttpStartLine::new_response(HttpVersion::Http11, StatusCode::OK);
        let mut meta = HttpMeta::new(start_line, HashMap::new());
        meta.set_content_type(HttpContentType::TextHtml());

        let body = result.into_bytes();
        HttpResponse::new(meta, HttpBody::Binary(body))
    }

    /// Creates an HTTP response with only a status code and an empty body.
    ///
    /// # Arguments
    ///
    /// * `status_code` - The HTTP status code for the response.
    ///
    /// # Returns
    ///
    /// An `HttpResponse` with the specified status code and an empty body.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use crate::response::response_templates;
    /// use crate::message::http_value::StatusCode;
    ///
    /// // Return a 404 Not Found response
    /// let response = response_templates::return_status(StatusCode::NOT_FOUND);
    /// ```
    pub fn return_status(status_code: StatusCode) -> HttpResponse {
        normal_response(status_code, Vec::<u8>::new())
    }
}

// pub mod akari_templates {
//     /// This macro is used to create a template response with the given path and key-value pairs.
//     /// It renders a template within specified path
//     /// and inserts the key-value pairs into the template context.
//     /// It is a convenient way to generate dynamic HTML responses.
//     /// # Examples
//     /// ```rust
//     /// // akari_render!("/path/to/template", key1 = "value1", key2 = "value2");
//     /// // This will fail because the template does not exist.
//     /// ```
//     #[macro_export]
//     macro_rules! akari_render {
//         ($path:expr) => {{
//             template_response($path, std::collections::HashMap::new())
//         }};
//         ($path:expr, $($key:ident = $value:tt),* $(,)?) => {{
//             let mut map = std::collections::HashMap::new();
//             $(
//                 akari_render!(@insert map, $key = $value);
//             )*
//             template_response($path, map)
//         }};
//         (@insert $map:expr, $key:ident = $value:literal) => {
//             $map.insert(stringify!($key).to_string(), object!($value));
//         };
//         (@insert $map:expr, $key:ident = $value:expr) => {
//             $map.insert(stringify!($key).to_string(), $value);
//         };
//     }
// }

// pub mod akari_object {
//     /// This macro is used to create a JSON response with the given key-value pairs.
//     /// It is a convenient way to generate JSON responses.
//     #[macro_export]
//     macro_rules! akari_json {
//         // Forward any input to the object! macro and wrap the result in json_response
//         ($($tokens:tt)*) => {{
//             let obj = object!($($tokens)*);
//             json_response(obj)
//         }};
//     }
// }