Requests can now be parsed and compiler warnings are fewer.
This commit is contained in:
parent
1e377125f8
commit
2612f1d679
1 changed files with 37 additions and 14 deletions
49
src/main.rs
49
src/main.rs
|
@ -1,42 +1,65 @@
|
|||
use std::io::{Write, Read, BufReader, BufRead, Result};
|
||||
use std::io::{Write, BufReader, BufRead};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::fs;
|
||||
use std::string::{String};
|
||||
use std::str;
|
||||
|
||||
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 mut filename = "index.html";
|
||||
let filename = "index.html";
|
||||
|
||||
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(),
|
||||
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) {
|
||||
// To-do: move streaming reading here
|
||||
fn process_request(request: Vec<u8>) -> String {
|
||||
let mut input = String::from_utf8_lossy(&request).to_string();
|
||||
let mut output = String::new();
|
||||
if input.contains("GET") {
|
||||
// To-do: find a less atrocious way to do this.
|
||||
println!("Stream sent GET request.");
|
||||
input = input.replace("GET ", "");
|
||||
input = input.replace(" HTTP/1.1", "");
|
||||
|
||||
// Theoretically by this point, the request
|
||||
// will have been cut down to just the
|
||||
// requested resource, but in my experience
|
||||
// this code is gonna result in like 50k errors
|
||||
// and I'm gonna have to rewrite it to get it
|
||||
// to actually work the way I want it to.
|
||||
|
||||
if input == "/" {
|
||||
// And now for ignoring the entire variable!
|
||||
output = "index.html".to_string();
|
||||
} else {
|
||||
output = "index.html".to_string();
|
||||
}
|
||||
|
||||
} else {
|
||||
// It's get_page()'s problem now.
|
||||
println!("Stream sent unhandlable request type.");
|
||||
output = ".503".to_string();
|
||||
}
|
||||
println!("{}", output);
|
||||
return output;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
fn serve(mut stream: TcpStream) {
|
||||
// To-do: Make a header generator to feed into response.
|
||||
|
||||
let mut request = Vec::new();
|
||||
let mut reader = BufReader::new(&mut stream);
|
||||
reader
|
||||
.read_until(b'\n', &mut request)
|
||||
.expect("Failed to read from stream!");
|
||||
|
||||
// println!("{}", String::from_utf8_lossy(&request));
|
||||
|
||||
let contents = get_page(String::from_utf8_lossy(&request).to_string());
|
||||
let resource = process_request(request);
|
||||
let contents = get_page(resource);
|
||||
let header = "HTTP/1.1 200 OK\r\n\r\n";
|
||||
let response = format!("{}{}", header, contents);
|
||||
|
||||
|
|
Loading…
Reference in a new issue