main.rs
1 use std::io::{BufRead, BufReader, Read, Write}; 2 use std::net::TcpListener; 3 use std::thread; 4 5 use http::http_init::parser; 6 7 use crate::http::http_code_handler::handle_error_codes; 8 use crate::http::http_init::HttpVersion; 9 use crate::http::{http_0_9_parser, http_1_0_parser}; 10 use crate::needed::Polar; 11 12 mod needed; 13 14 mod http; 15 mod content_fetcher; 16 //#[path= "http/http.rs"] 17 //mod http; 18 19 //fn connection_listener_https() -> anyhow::Result<()> { 20 // let listener = TcpListener::bind("localhost:4443")?; 21 // 22 // for stream in listener.incoming() { 23 // match stream { 24 // Ok(mut stream) => { 25 // let mut buffer = Vec::new(); 26 // stream.read_to_end(&mut buffer)?; 27 // 28 // tls_handler(buffer); 29 // } 30 // Err(e) => eprintln_red!("HTTP Connection failed: {:?}", e), 31 // } 32 // } 33 // Ok(()) 34 //} 35 36 fn main() { 37 println_cyan!("Initializing Polar Bear!"); 38 39 thread::spawn(|| { 40 match connection_listener("localhost:4443".to_string()) { 41 Ok(_) => println_cyan!("Polar Bear Stopped!"), 42 Err(err) => { 43 eprintln_red!("Failed to setup ports. Do you have the necessary permissions?\nError: {:?}", err.root_cause()); 44 std::process::exit(1); 45 } 46 }; 47 }); 48 49 //thread::spawn(|| { 50 match connection_listener("localhost:8080".to_string()) { 51 Ok(_) => println_cyan!("Polar Bear Stopped!"), 52 Err(err) => { 53 eprintln_red!("Failed to setup ports. Do you have the necessary permissions?\nError: {:?}", err.root_cause()); 54 std::process::exit(1); 55 } 56 }; 57 //}); 58 59 //println_cyan!("Polar Bear Initialized!"); 60 } 61 62 fn connection_listener(addr: String) -> anyhow::Result<()> { 63 let listener = TcpListener::bind(addr)?; 64 65 for stream in listener.incoming() { 66 match stream { 67 Ok(mut stream) => { 68 //let start_time = Instant::now(); 69 70 let mut buf_reader = BufReader::new(&mut stream); 71 let mut buffer: Vec<String> = vec![]; 72 73 loop { 74 let mut temp_buffer: String = String::new(); 75 76 match buf_reader.read_line(&mut temp_buffer) { 77 Ok(0) => { 78 // If read_line returns 0, it means the stream has reached EOF 79 println!("\nConnection closed."); 80 81 //// browser doesn't like playing ping pong? hmm 82 break; 83 } 84 Ok(_) => { 85 // check if the current buffer contains /r/n/r/n (which indicates that the http header is done) 86 if temp_buffer == "\r\n" { 87 // stawp reading 88 buffer.push(temp_buffer); 89 break; 90 } 91 92 buffer.push(temp_buffer); 93 } 94 Err(e) => { 95 // Handle the error. 96 eprintln!("Error reading from stream: {}", e); 97 break; 98 } 99 } 100 } 101 102 103 if buffer.is_empty() { 104 eprintln_red!("that is an uh.. empty request? what do ya want son? || {:?}\n", stream); 105 //stream.shutdown(Shutdown::Both).unwrap_or_default(); 106 } else { 107 println!("{:?}", buffer); 108 109 // does the parsing, if it isn't successful we just give an error page! 110 match parser(buffer.first().unwrap_or(&String::new())) { 111 Polar::Silly(error_code) => { 112 let html = handle_error_codes(error_code); 113 114 stream.write_all(&html)?; 115 stream.flush()?; 116 } 117 118 Polar::Some(request_line) => { 119 println_cyan!("output: [{:?} | {:?} | {:?}]", request_line.method, request_line.uri, request_line.version); 120 121 match request_line.version { 122 HttpVersion::HTTP_0_9 => { 123 println_green!("HTTP 0.9 request received!"); 124 125 let response = http_0_9_parser::return_response(request_line); 126 127 match response { 128 Polar::Some(response) => { 129 stream.write_all(&response.body)?; 130 stream.flush()?; 131 } 132 Polar::Silly(error_code) => { 133 let html = handle_error_codes(error_code); 134 135 stream.write_all(&html)?; 136 stream.flush()?; 137 } 138 } 139 } 140 HttpVersion::HTTP_1_0 => { 141 let response = http_1_0_parser::return_response(buffer, request_line); 142 143 match response { 144 Polar::Some(response) => { 145 stream.write_all(&response.into_response())?; 146 stream.flush()?; 147 } 148 Polar::Silly(code) => { 149 let html = handle_error_codes(code); 150 151 stream.write_all(&html)?; 152 stream.flush()?; 153 } 154 } 155 } 156 //HttpVersion::HTTP_1_1 => {} 157 //HttpVersion::HTTP_2_0 => {} 158 //HttpVersion::HTTP_3_0 => {} 159 _ => { 160 //let html = handle_error_codes(505); 161 // 162 //stream.write_all(&html)?; 163 //stream.flush()?; 164 let response = http_1_0_parser::return_response(buffer, request_line); 165 match response { 166 Polar::Some(response) => { 167 stream.write_all(&response.into_response())?; 168 stream.flush()?; 169 } 170 Polar::Silly(code) => { 171 let html = handle_error_codes(code); 172 173 stream.write_all(&html)?; 174 stream.flush()?; 175 } 176 } 177 } 178 } 179 } 180 } 181 } 182 } 183 Err(e) => eprintln_red!("HTTP Connection failed: {:?}", e), 184 } 185 } 186 Ok(()) 187 }