-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e80a2f1
commit 147ba89
Showing
2 changed files
with
88 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// NOTE: This is not being used right now, please ignore | ||
use std::net::TcpListener; | ||
use std::net::TcpStream; | ||
use std::io::prelude::*; | ||
use std::fs; | ||
use std::thread; | ||
use std::time::Duration; | ||
use milady_pool_lib::fibonacci; | ||
|
||
fn main () { | ||
let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); | ||
|
||
let pool = ThreadPool::new(4); | ||
|
||
for stream in listener.incoming() { | ||
let stream = stream.unwrap(); | ||
|
||
pool.execute(|| { | ||
handle_connection(stream); | ||
}); | ||
} | ||
} | ||
|
||
fn handle_connection(mut stream: TcpStream) { | ||
let mut buffer = [0; 1024]; | ||
stream.read(&mut buffer).unwrap(); | ||
|
||
let get = b"GET / HTTP/1.1\r\n"; | ||
let sleep = b"GET /sleep HTTP/1.1\r\n"; | ||
let post = b"POST /log HTTP/1.1\r\n"; | ||
|
||
|
||
if buffer.starts_with(post) { | ||
let contents = String::from_utf8_lossy(&buffer[..]); | ||
println!("Received POST request with data: {}", contents); | ||
// TODO: Come back to this | ||
|
||
} else if buffer.starts_with(get) { | ||
send_response(stream, "HTTP/1.1 200 OK", "server/index.html"); | ||
} else if buffer.starts_with(sleep) { | ||
thread::sleep(Duration::from_secs(5)); | ||
send_response(stream, "HTTP/1.1 200 OK", "server/index.html"); | ||
} else if buffer.starts_with(get) { | ||
let id = 5; | ||
let (a, b) = call_fibonacci(id); | ||
let response = format!("HTTP/1.1 200 OK\r\n\r\nThe fibonacci number at position {} is: {} and {}", id, a, b); | ||
stream.write(response.as_bytes()).unwrap(); | ||
stream.flush().unwrap(); | ||
} else { | ||
send_response(stream, "HTTP/1.1 404 NOT FOUND", "server/404.html"); | ||
} | ||
} | ||
|
||
fn send_response(mut stream: TcpStream, status_line: &str, filename: &str) { | ||
let contents = fs::read_to_string(filename).unwrap(); | ||
let response = format!( | ||
"{}\r\nContent-Length: {}\r\n\r\n{}", | ||
status_line, | ||
contents.len(), | ||
contents | ||
); | ||
stream.write(response.as_bytes()).unwrap(); | ||
stream.flush().unwrap(); | ||
} | ||
|
||
fn call_fibonacci(n: u32) -> (u32, u32) { | ||
let (a, b)= fibonacci(n); | ||
(a, b) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters