mirror of
https://github.com/guilhermewerner/http
synced 2025-06-16 11:44:17 +00:00
Update cargo project
This commit is contained in:
30
Source/lib.rs
Normal file
30
Source/lib.rs
Normal file
@ -0,0 +1,30 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::fs;
|
||||
use std::io::prelude::*;
|
||||
use std::net::TcpStream;
|
||||
|
||||
pub fn HandleConnection(mut stream: TcpStream) {
|
||||
let mut buffer = [0; 1024];
|
||||
stream.read(&mut buffer).unwrap();
|
||||
|
||||
let get = b"GET / HTTP/1.1\r\n";
|
||||
|
||||
let (status_line, filename) = if buffer.starts_with(get) {
|
||||
("HTTP/1.1 200 OK", "Hello.html")
|
||||
} else {
|
||||
("HTTP/1.1 404 NOT FOUND", "404.html")
|
||||
};
|
||||
|
||||
let contents = fs::read_to_string(format!("Resources/{}", 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();
|
||||
}
|
Reference in New Issue
Block a user