37 lines
1.1 KiB
Rust
37 lines
1.1 KiB
Rust
#![allow(non_snake_case)]
|
|
use std::net::{TcpListener, TcpStream};
|
|
use std::io::{BufReader, prelude::*};
|
|
use std::fs;
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
let TCPlistener = TcpListener::bind("127.0.0.1:8080").unwrap();
|
|
for Stream in TCPlistener.incoming() {
|
|
ServePage(Stream?);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn ServePage(mut Stream: TcpStream) {
|
|
// Identifying information.
|
|
let Buffer = BufReader::new(&Stream);
|
|
let HTTPrequest: Vec<_> = Buffer.lines().map(|result| result.unwrap()).take_while(|line| !line.is_empty()).collect();
|
|
println!("{HTTPrequest:#?}");
|
|
|
|
// Response.
|
|
#[allow(unused_assignments)]
|
|
let mut Status : &str = "";
|
|
#[allow(unused_assignments)]
|
|
let mut Contents : String = "".to_string();
|
|
|
|
if HTTPrequest[0] == "GET / HTTP/1.1" {
|
|
Status = "HTTP/1.1 200 OK";
|
|
Contents = fs::read_to_string("src/index.html").unwrap();
|
|
} else {
|
|
Status = "HTTP/1.1 404 Not Found";
|
|
Contents = fs::read_to_string("src/404.html").unwrap();
|
|
}
|
|
let Length = Contents.len();
|
|
let Response : String = format!("{Status}\r\nContent-Length: {Length}\r\n\r\n{Contents}");
|
|
|
|
Stream.write_all(Response.as_bytes()).unwrap();
|
|
}
|