Reading the stream is successful.

This commit is contained in:
abbie 2021-08-30 17:18:22 +01:00
parent 9cbc19e3bf
commit 9d99683f3a
2 changed files with 20 additions and 2 deletions

View file

@ -20,7 +20,9 @@ I need this stuff to actually have a functioning but basic web server.
### Next Chonklist
The stuff I need to make it usable.
[ ] Read the stream to detect what resource it wants to access
[x] Read the stream
[ ] Detect which resource the client wants to access
[ ] Detect missing files and return a 404 page

View file

@ -1,7 +1,8 @@
use std::io::{Write};
use std::io::{Write, Read, BufReader, BufRead, Result};
use std::net::{TcpListener, TcpStream};
use std::fs;
use std::string::{String};
use std::str;
fn get_page() -> String {
// To-do: Make this able to load other pages
@ -16,9 +17,23 @@ fn get_page() -> String {
return page;
}
fn read_stream(mut stream: TcpStream) -> Result<TcpStream> {
let mut buffer = BufReader::new(stream.try_clone()?);
Ok(stream)
}
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();
let header = "HTTP/1.1 200 OK\r\n\r\n";
let response = format!("{}{}", header, contents);
@ -33,6 +48,7 @@ fn main() -> std::io::Result<()> {
let listen = TcpListener::bind("127.0.0.1:8080")?;
for stream in listen.incoming() {
println!("Serving incoming stream.");
serve(stream?);
}
Ok(())