Add path handling

This commit is contained in:
g1n 2021-07-09 12:34:59 +00:00
parent 9e478116f8
commit d5b412aef8
2 changed files with 13 additions and 10 deletions

1
error.gmi Normal file
View File

@ -0,0 +1 @@
# Server Error

View File

@ -11,6 +11,7 @@ fn main() {
let mut verbose = false; let mut verbose = false;
let mut host = "localhost".to_string(); let mut host = "localhost".to_string();
let mut port = 3000; // TODO: change to 300 let mut port = 3000; // TODO: change to 300
//let mut root_dir = format!("/home/{}/public_spartan/", "username");
// Arguments // Arguments
{ {
let mut ap = ArgumentParser::new(); let mut ap = ArgumentParser::new();
@ -24,6 +25,9 @@ fn main() {
ap.refer(&mut port) ap.refer(&mut port)
.add_option(&["-p", "--port"], Store, .add_option(&["-p", "--port"], Store,
"Port"); "Port");
/*ap.refer(&mut root_dir)
.add_option(&["-d", "--root-dir"], Store,
"Root directory");*/
ap.parse_args_or_exit(); ap.parse_args_or_exit();
} }
if verbose { if verbose {
@ -57,7 +61,8 @@ fn handle_connection(mut stream: TcpStream) {
_post _post
);*/ );*/
let _echo = b"$hostname /echo 0\r\n"; // TODO: fix echo behaviour // let _echo = b"$hostname /echo 0\r\n"; // TODO: fix echo behaviour
let request = String::from_utf8_lossy(&buffer[..]); let request = String::from_utf8_lossy(&buffer[..]);
let parsed_request: Vec<&str> = request.split(' ').collect(); let parsed_request: Vec<&str> = request.split(' ').collect();
@ -76,24 +81,21 @@ fn handle_connection(mut stream: TcpStream) {
let get = format!( "{} {} {}\r\n {}\r\n", host, _path, _post_len, _post); let get = format!( "{} {} {}\r\n {}\r\n", host, _path, _post_len, _post);
let get_bytes = get.as_bytes(); let get_bytes = get.as_bytes();
let status_line = "2 text/gemini\r\n"; let (status_line, _filename) = if _path == "/" {
("2 text/gemini", format!(".{}index.gmi", _path))
/*
let (status_line, _filename) = if buffer.starts_with(get_bytes) {
("2 text/gemini\r\n", "index.gmi")
} else { } else {
("4 Not found\r\n", "") // TODO: fix Not Found behaviour ("4 Not found", format!(".{}error.gmi", "/")) // TODO: fix Not Found behaviour
}; };
*/
//let filename = "index.gmi"; //let filename = "index.gmi";
/* /*
let _contents = ""; let _contents = "";
if _filename != "" { if _filename != "" {
let _contents = fs::read_to_string(_filename).unwrap(); let _contents = fs::read_to_string(_filename).unwrap();
} else { */ } else { */
let _contents = fs::read_to_string("index.gmi").unwrap(); let _contents = fs::read_to_string(_filename).unwrap();
let response = format!( let response = format!(
"{}\r\n{}\r\n", "{}\r\n {}",
status_line, status_line,
_contents _contents
); );