Reading the stream is successful.
This commit is contained in:
parent
9cbc19e3bf
commit
9d99683f3a
2 changed files with 20 additions and 2 deletions
|
@ -20,7 +20,9 @@ I need this stuff to actually have a functioning but basic web server.
|
||||||
### Next Chonklist
|
### Next Chonklist
|
||||||
The stuff I need to make it usable.
|
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
|
[ ] Detect missing files and return a 404 page
|
||||||
|
|
||||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -1,7 +1,8 @@
|
||||||
use std::io::{Write};
|
use std::io::{Write, Read, BufReader, BufRead, Result};
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::string::{String};
|
use std::string::{String};
|
||||||
|
use std::str;
|
||||||
|
|
||||||
fn get_page() -> String {
|
fn get_page() -> String {
|
||||||
// To-do: Make this able to load other pages
|
// To-do: Make this able to load other pages
|
||||||
|
@ -16,9 +17,23 @@ fn get_page() -> String {
|
||||||
return page;
|
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) {
|
fn serve(mut stream: TcpStream) {
|
||||||
// To-do: Make a header generator to feed into response.
|
// 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 contents = get_page();
|
||||||
let header = "HTTP/1.1 200 OK\r\n\r\n";
|
let header = "HTTP/1.1 200 OK\r\n\r\n";
|
||||||
let response = format!("{}{}", header, contents);
|
let response = format!("{}{}", header, contents);
|
||||||
|
@ -33,6 +48,7 @@ fn main() -> std::io::Result<()> {
|
||||||
let listen = TcpListener::bind("127.0.0.1:8080")?;
|
let listen = TcpListener::bind("127.0.0.1:8080")?;
|
||||||
|
|
||||||
for stream in listen.incoming() {
|
for stream in listen.incoming() {
|
||||||
|
println!("Serving incoming stream.");
|
||||||
serve(stream?);
|
serve(stream?);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue