Handle filesystem errors by presuming 404.
This commit is contained in:
parent
9d99683f3a
commit
1e377125f8
1 changed files with 14 additions and 12 deletions
24
src/main.rs
24
src/main.rs
|
@ -4,23 +4,25 @@ use std::fs;
|
|||
use std::string::{String};
|
||||
use std::str;
|
||||
|
||||
fn get_page() -> String {
|
||||
fn get_page(request: String) -> String {
|
||||
// To-do: Make this able to load other pages
|
||||
//
|
||||
// The loaded page should be left immutable as it does
|
||||
// not need to be modified by the server.
|
||||
|
||||
let filename = "index.html"; // BAD!!!
|
||||
let page = fs::read_to_string(filename)
|
||||
.expect("Page read failed!");
|
||||
let mut filename = "index.html";
|
||||
|
||||
return page;
|
||||
let page = fs::read_to_string(filename);
|
||||
match page {
|
||||
Ok(i) => i.to_string(),
|
||||
Err(e) => "<!DOCTYPE HTML><html><body><h1>404 Not Found</h1><p>The resource you are trying to locate cannot be accessed!</p></body></html>".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_stream(mut stream: TcpStream) -> Result<TcpStream> {
|
||||
let mut buffer = BufReader::new(stream.try_clone()?);
|
||||
fn read_stream(mut stream: TcpStream) {
|
||||
// To-do: move streaming reading here
|
||||
|
||||
Ok(stream)
|
||||
return;
|
||||
}
|
||||
|
||||
fn serve(mut stream: TcpStream) {
|
||||
|
@ -30,11 +32,11 @@ fn serve(mut stream: TcpStream) {
|
|||
let mut reader = BufReader::new(&mut stream);
|
||||
reader
|
||||
.read_until(b'\n', &mut request)
|
||||
.expect("Failed to read from stream");
|
||||
.expect("Failed to read from stream!");
|
||||
|
||||
println!("{}", String::from_utf8_lossy(&request));
|
||||
// println!("{}", String::from_utf8_lossy(&request));
|
||||
|
||||
let contents = get_page();
|
||||
let contents = get_page(String::from_utf8_lossy(&request).to_string());
|
||||
let header = "HTTP/1.1 200 OK\r\n\r\n";
|
||||
let response = format!("{}{}", header, contents);
|
||||
|
||||
|
|
Loading…
Reference in a new issue