Handle URL rewriting for subdirectories containing index.html
This commit is contained in:
parent
00ebb833aa
commit
04e019b00e
1 changed files with 47 additions and 8 deletions
55
src/main.rs
55
src/main.rs
|
@ -1,23 +1,45 @@
|
||||||
se std::io::{Write, BufReader, BufRead};
|
use std::io::{Write, BufReader, BufRead};
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::string::{String};
|
use std::string::{String};
|
||||||
|
|
||||||
|
fn check_if_path_exists(path: String) -> bool {
|
||||||
|
|
||||||
|
// This is probably not the best way of checking if a path
|
||||||
|
// exists but I don't care :)
|
||||||
|
|
||||||
|
let file = fs::metadata(path);
|
||||||
|
|
||||||
|
match file {
|
||||||
|
Ok(_) => true,
|
||||||
|
Err(_) => false,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_if_dir(directory: String) -> bool {
|
||||||
|
let path = directory + "/index.html";
|
||||||
|
let result = check_if_path_exists(path);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
fn get_page(filename: String) -> String {
|
fn get_page(filename: String) -> String {
|
||||||
|
|
||||||
// The loaded page should be left immutable as it does
|
// The loaded page should be left immutable as it does
|
||||||
// not need to be modified by the server.
|
// not need to be modified by the server.
|
||||||
|
|
||||||
if filename == ".503" {
|
if filename == ".error_server_404" {
|
||||||
|
let result = "<!DOCTYPE HTML><html><body><h1>404 Not Found</h1><p>The resource you are trying to locate cannot be accessed!</p></body></html>";
|
||||||
|
return result.to_string();
|
||||||
|
} else if filename == ".error_server_503" {
|
||||||
let result = "<!DOCTYPE HTML><html><body><h1>503 Not Implemented</h1><p>The request sent by your web browser cannot be handled by this server software.</p></body></html>";
|
let result = "<!DOCTYPE HTML><html><body><h1>503 Not Implemented</h1><p>The request sent by your web browser cannot be handled by this server software.</p></body></html>";
|
||||||
|
|
||||||
return result.to_string();
|
return result.to_string();
|
||||||
} else {
|
} else {
|
||||||
let result = fs::read_to_string(filename);
|
let result = fs::read_to_string(filename);
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(i) => i.to_string(),
|
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(),
|
Err(_e) => "<!DOCTYPE HTML><html><body><h1>403 Forbidden</h1><p>The resource you are trying to access cannot be read by the server.</p></body></html>".to_string(),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,6 +47,8 @@ fn get_page(filename: String) -> String {
|
||||||
|
|
||||||
fn process_request(request: Vec<u8>) -> String {
|
fn process_request(request: Vec<u8>) -> String {
|
||||||
let mut input = String::from_utf8_lossy(&request).to_string();
|
let mut input = String::from_utf8_lossy(&request).to_string();
|
||||||
|
|
||||||
|
let mut index = String::new();
|
||||||
let output;
|
let output;
|
||||||
if input.contains("GET") {
|
if input.contains("GET") {
|
||||||
// To-do: find a less atrocious way to do this.
|
// To-do: find a less atrocious way to do this.
|
||||||
|
@ -41,7 +65,22 @@ fn process_request(request: Vec<u8>) -> String {
|
||||||
if input != "/" {
|
if input != "/" {
|
||||||
let mut chars = input.chars();
|
let mut chars = input.chars();
|
||||||
chars.next();
|
chars.next();
|
||||||
output = chars.as_str();
|
let path = chars.as_str().to_string();
|
||||||
|
let exists = check_if_path_exists(path);
|
||||||
|
if exists == false {
|
||||||
|
output = ".error_server_404";
|
||||||
|
} else {
|
||||||
|
let path = chars.as_str().to_string();
|
||||||
|
let dir = check_if_dir(path);
|
||||||
|
if dir == true {
|
||||||
|
let path = chars.as_str().to_string();
|
||||||
|
index += &path;
|
||||||
|
index += "/index.html";
|
||||||
|
output = index.as_str().clone();
|
||||||
|
} else {
|
||||||
|
output = chars.as_str();
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
output = "index.html";
|
output = "index.html";
|
||||||
}
|
}
|
||||||
|
@ -49,8 +88,8 @@ fn process_request(request: Vec<u8>) -> String {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// It's get_page()'s problem now.
|
// It's get_page()'s problem now.
|
||||||
println!("Stream sent unhandlable request type.");
|
println!("Stream sent unhandlable request.");
|
||||||
output = ".503";
|
output = ".error_server_503";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Did you want to see chars.as_str().to_string()?
|
// Did you want to see chars.as_str().to_string()?
|
||||||
|
|
Loading…
Reference in a new issue