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