rust_web_server/log/
mod.rs

1use std::net::SocketAddr;
2use std::thread;
3use file_ext::FileExt;
4use crate::entry_point::command_line_args::CommandLineArgument;
5use crate::request::Request;
6use crate::response::Response;
7
8#[cfg(test)]
9mod tests;
10
11pub struct Log;
12
13impl Log {
14    pub fn request_response(request: &Request, response: &Response, peer_addr: &SocketAddr) -> String {
15        let mut request_headers = "".to_string();
16        for header in &request.headers {
17            if &header.name.chars().count() > &0 {
18                request_headers = [
19                    request_headers,
20                    "\n  ".to_string(),
21                    header.name.to_string(),
22                    ": ".to_string(),
23                    header.value.to_string()
24                ].join("");
25            }
26        }
27
28        let mut response_headers = "".to_string();
29        for header in &response.headers {
30            if &header.name.chars().count() > &0 {
31                response_headers = [
32                    response_headers,
33                    "\n  ".to_string(),
34                    header.name.to_string(),
35                    ": ".to_string(),
36                    header.value.to_string()
37                ].join("");
38            }
39        }
40
41        let mut response_body_length = 0;
42        let mut response_body_parts_number = 0;
43        for content_range in &response.content_range_list {
44            let boxed_parse = content_range.size.parse::<i32>();
45            if boxed_parse.is_ok() {
46                response_body_length += boxed_parse.unwrap();
47                response_body_parts_number += 1;
48            }
49        }
50
51        let current_thread = thread::current();
52        let thread_id = current_thread.name().unwrap();
53
54        let log_request_response = format!("\n\nRequest (thread id: {} peer address is {}):\n  {} {} {}  {}\n  Body: {} byte(s) total (including default initialization vector)\nEnd of Request\nResponse:\n  {} {} {}\n\n  Body: {} part(s), {} byte(s) total\nEnd of Response",
55                                           thread_id,
56                                           peer_addr,
57                                           &request.http_version,
58                                           &request.method,
59                                           &request.request_uri,
60                                           request_headers,
61                                           request.body.len(),
62
63                                           &response.status_code,
64                                           &response.reason_phrase,
65                                           response_headers,
66                                           response_body_parts_number,
67                                           response_body_length);
68
69        log_request_response
70    }
71
72    pub fn usage_information() -> String {
73        let mut log = "Usage:\n\n".to_string();
74        let command_line_arg_list = CommandLineArgument::get_command_line_arg_list();
75        for arg in command_line_arg_list {
76            let argument_info = format!("  {} environment variable\n  -{} or --{} as command line line argument\n  {}\n\n", arg.environment_variable, arg.short_form, arg.long_form, arg._hint.unwrap());
77            log = [log, argument_info].join("");
78        }
79        log = [log, "End of usage section\n\n".to_string()].join("");
80        log
81    }
82
83    pub fn info(name: &str) -> String {
84        let mut log = format!("{}\n", name).to_string();
85        const VERSION: &str = env!("CARGO_PKG_VERSION");
86        const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
87        const REPOSITORY: &str = env!("CARGO_PKG_REPOSITORY");
88        const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
89        const RUST_VERSION: &str = env!("CARGO_PKG_RUST_VERSION");
90        const LICENSE: &str = env!("CARGO_PKG_LICENSE");
91        let boxed_user = FileExt::get_current_user();
92        if boxed_user.is_err() {
93            let message = boxed_user.as_ref().err().unwrap();
94            eprintln!("{}", message)
95        }
96        let user: String = boxed_user.unwrap();
97
98        let boxed_working_directory = FileExt::get_static_filepath("");
99        if boxed_working_directory.is_err() {
100            let message = boxed_working_directory.as_ref().err().unwrap();
101            eprintln!("{}", message)
102        }
103
104        let working_directory: String = boxed_working_directory.unwrap();
105
106        let version = format!("Version:           {}\n", VERSION);
107        log = [log, version].join("");
108
109        let authors = format!("Authors:           {}\n", AUTHORS);
110        log = [log, authors].join("");
111
112        let repository = format!("Repository:        {}\n", REPOSITORY);
113        log = [log, repository].join("");
114
115        let description = format!("Desciption:        {}\n", DESCRIPTION);
116        log = [log, description].join("");
117
118        let rust_version = format!("Rust Version:      {}\n", RUST_VERSION);
119        log = [log, rust_version].join("");
120
121        let license = format!("License:           {}\n", LICENSE);
122        log = [log, license].join("");
123
124        let license = format!("User:              {}\n", user);
125        log = [log, license].join("");
126
127        let license = format!("Working Directory: {}\n", working_directory);
128        log = [log, license].join("");
129
130        log
131    }
132
133    pub fn server_url_thread_count(protocol: &str, bind_addr: &String, thread_count: i32) -> String {
134        let url = format!("Server is up and running at: {}://{}\n", protocol, &bind_addr);
135        let thread_count = format!("Spawned {} thread(s) to handle incoming requests\n", thread_count);
136        [url, thread_count].join("")
137    }
138}