#7 reading from a file and building the response

This commit is contained in:
Shautvast 2026-02-06 15:50:00 +01:00
parent 1fc91880d8
commit 793daa9617
2 changed files with 16 additions and 2 deletions

9
src/hello.html Normal file
View file

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Rustacean!</title>
</head>
<body>
<h1>Hello Rustacean!</h1>
</html>

View file

@ -15,7 +15,12 @@ fn handle_connection(mut stream: TcpStream) -> std::io::Result<()>{
stream.read(&mut buffer)?; stream.read(&mut buffer)?;
println!("Request: {}", String::from_utf8_lossy(&buffer[..])); println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
let response = "HTTP/1.1 200 OK\r\n\r\n"; let contents = include_str!("hello.html");
let response = format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{}",
contents.len(),
contents
);
stream.write(response.as_bytes())?; stream.write(response.as_bytes())?;
stream.flush()?; stream.flush()?;
Ok(()) Ok(())