Skip to main content

rust_web_server/app/controller/form/get_method/
mod.rs

1use std::collections::HashMap;
2use crate::controller::Controller;
3use crate::mime_type::MimeType;
4use crate::range::{ContentRange, Range};
5use crate::request::{METHOD, Request};
6use crate::response::{Response, STATUS_CODE_REASON_PHRASE};
7use crate::server::ConnectionInfo;
8use crate::symbol::SYMBOL;
9use crate::url::URL;
10
11pub struct FormGetMethodController;
12
13impl Controller for FormGetMethodController {
14    fn is_matching(request: &Request, _connection: &ConnectionInfo) -> bool {
15        // Asked for every request: method first, then the path without a URL parse.
16        request.method == METHOD.get
17            && URL::request_target_path(&request.request_uri) == Some("/form-get-method")
18    }
19
20    fn process(_request: &Request, mut response: Response, _connection: &ConnectionInfo) -> Response {
21        response.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
22        response.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
23
24        // here is the form data, as an example here it is printed in the response body
25        let boxed_query_option = _request.get_uri_query();
26        if boxed_query_option.is_err() {
27            let error_message = boxed_query_option.clone().err().unwrap().to_string();
28            eprintln!("unable to extract query from url: {}", error_message)
29        }
30        let query_option = boxed_query_option.unwrap();
31        if query_option.is_some() {
32            let form: HashMap<String, String> = query_option.unwrap();
33
34
35            let mut formatted_list : Vec<String> = vec![];
36            for (key, value) in form.into_iter() {
37                let formatted_output = format!("{} is {}{}", key, value, SYMBOL.new_line_carriage_return);
38                formatted_list.push(formatted_output);
39            }
40
41            let response_body = formatted_list.join(SYMBOL.empty_string);
42            response.content_range_list = vec![
43                ContentRange{
44                    unit: Range::BYTES.to_string(),
45                    range: Range { start: 0, end: response_body.len() as u64 },
46                    size: response_body.len().to_string(),
47                    body: Vec::from(response_body.as_bytes()),
48                    content_type: MimeType::TEXT_PLAIN.to_string(),
49                }
50            ];
51        }
52
53        response
54    }
55}
56
57impl FormGetMethodController {
58
59    pub fn is_matching_request(request: &Request) -> bool {
60        let boxed_path = request.get_uri_path();
61        if boxed_path.is_err() {
62            let message = format!("unable to get path {}", boxed_path.err().unwrap());
63            eprintln!("{}", message);
64            return false
65        }
66
67        let path = boxed_path.unwrap();
68        path == "/form-get-method" && request.method == METHOD.get
69
70    }
71
72    pub fn process_request(_request: &Request, mut response: Response) -> Response {
73        response.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
74        response.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
75
76        // here is the form data, as an example here it is printed in the response body
77        let boxed_query_option = _request.get_uri_query();
78        if boxed_query_option.is_err() {
79            let error_message = boxed_query_option.clone().err().unwrap().to_string();
80            eprintln!("unable to extract query from url: {}", error_message)
81        }
82        let query_option = boxed_query_option.unwrap();
83        if query_option.is_some() {
84            let form: HashMap<String, String> = query_option.unwrap();
85
86
87            let mut formatted_list : Vec<String> = vec![];
88            for (key, value) in form.into_iter() {
89                let formatted_output = format!("{} is {}{}", key, value, SYMBOL.new_line_carriage_return);
90                formatted_list.push(formatted_output);
91            }
92
93            let response_body = formatted_list.join(SYMBOL.empty_string);
94            response.content_range_list = vec![
95                ContentRange{
96                    unit: Range::BYTES.to_string(),
97                    range: Range { start: 0, end: response_body.len() as u64 },
98                    size: response_body.len().to_string(),
99                    body: Vec::from(response_body.as_bytes()),
100                    content_type: MimeType::TEXT_PLAIN.to_string(),
101                }
102            ];
103        }
104
105        response
106    }
107}