Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 5f5a9e20 authored by henryak25's avatar henryak25
Browse files

(4) Simulation of slow request

parent ff202a8b
Branches
No related tags found
No related merge requests found
use std::{
fs,
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
};
use std::{fs, io::{prelude::*, BufReader}, net::{TcpListener, TcpStream}, thread};
use std::time::Duration;
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
......@@ -17,6 +15,15 @@ fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let request_line = buf_reader.lines().next().unwrap().unwrap();
let (status_line, filename) = match &request_line[..] {
"GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"),
"GET /sleep HTTP/1.1" => {
thread::sleep(Duration::from_secs(500));
("HTTP/1.1 200 OK", "hello.html")
}
_ => ("HTTP/1.1 404 NOT FOUND", "404.html"),
};
let (status_line, filename) = if request_line == "GET / HTTP/1.1" {
("HTTP/1.1 200 OK", "hello.html")
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment