2021-09-02 16:16:38 +01:00
use std ::io ::{ Write , BufReader , BufRead } ;
2021-08-29 16:07:05 +01:00
use std ::net ::{ TcpListener , TcpStream } ;
2021-08-30 16:17:11 +01:00
use std ::fs ;
use std ::string ::{ String } ;
2021-09-02 17:25:53 +01:00
fn get_page ( filename : String ) -> String {
2021-09-02 16:16:38 +01:00
2021-08-30 16:17:11 +01:00
// The loaded page should be left immutable as it does
// not need to be modified by the server.
2021-09-02 17:25:53 +01:00
if filename = = " .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> " ;
return result . to_string ( ) ;
} else {
println! ( " Read " ) ;
let result = fs ::read_to_string ( filename ) ;
2021-08-30 16:17:11 +01:00
2021-09-02 17:25:53 +01:00
match result {
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 ( ) ,
}
2021-08-30 17:32:44 +01:00
}
2021-08-30 16:17:11 +01:00
}
2021-08-29 16:07:05 +01:00
2021-09-02 16:16:38 +01:00
fn process_request ( request : Vec < u8 > ) -> String {
let mut input = String ::from_utf8_lossy ( & request ) . to_string ( ) ;
2021-09-02 16:18:23 +01:00
let output ;
2021-09-02 16:16:38 +01:00
if input . contains ( " GET " ) {
// To-do: find a less atrocious way to do this.
println! ( " Stream sent GET request. " ) ;
input = input . replace ( " GET " , " " ) ;
2021-09-02 17:25:53 +01:00
input = input . replace ( " HTTP/1.1 \r \n " , " " ) ;
2021-09-02 16:16:38 +01:00
// 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.
2021-09-02 17:25:53 +01:00
if input ! = " / " {
2021-09-02 16:25:13 +01:00
let mut chars = input . chars ( ) ;
chars . next ( ) ;
output = chars . as_str ( ) ;
2021-09-02 17:25:53 +01:00
println! ( " CHARS " ) ;
} else {
output = " index.html " ;
2021-09-02 16:16:38 +01:00
}
2021-09-02 17:25:53 +01:00
2021-09-02 16:16:38 +01:00
} else {
// It's get_page()'s problem now.
println! ( " Stream sent unhandlable request type. " ) ;
2021-09-02 17:25:53 +01:00
output = " .503 " ;
2021-09-02 16:16:38 +01:00
}
2021-09-02 16:25:13 +01:00
println! ( " {} " , output . to_string ( ) ) ;
// Did you want to see chars.as_str().to_string()?
return output . to_string ( ) ;
2021-08-30 17:32:44 +01:00
2021-08-30 17:18:22 +01:00
}
2021-08-29 16:07:05 +01:00
fn serve ( mut stream : TcpStream ) {
2021-08-30 17:18:22 +01:00
let mut request = Vec ::new ( ) ;
let mut reader = BufReader ::new ( & mut stream ) ;
reader
. read_until ( b '\n' , & mut request )
2021-08-30 17:32:44 +01:00
. expect ( " Failed to read from stream! " ) ;
2021-08-30 17:18:22 +01:00
2021-09-02 16:16:38 +01:00
let resource = process_request ( request ) ;
let contents = get_page ( resource ) ;
2021-08-30 16:17:11 +01:00
let header = " HTTP/1.1 200 OK \r \n \r \n " ;
let response = format! ( " {} {} " , header , contents ) ;
2021-08-29 16:07:05 +01:00
stream . write ( response . as_bytes ( ) ) . unwrap ( ) ;
stream . flush ( ) . unwrap ( ) ;
}
fn main ( ) -> std ::io ::Result < ( ) > {
let listen = TcpListener ::bind ( " 127.0.0.1:8080 " ) ? ;
for stream in listen . incoming ( ) {
2021-08-30 17:18:22 +01:00
println! ( " Serving incoming stream. " ) ;
2021-08-29 16:07:05 +01:00
serve ( stream ? ) ;
}
Ok ( ( ) )
2021-08-29 15:18:20 +01:00
}