#7 reading from a file and building the response
This commit is contained in:
parent
1fc91880d8
commit
793daa9617
2 changed files with 16 additions and 2 deletions
9
src/hello.html
Normal file
9
src/hello.html
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Hello Rustacean!</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Hello Rustacean!</h1>
|
||||||
|
</html>
|
||||||
|
|
@ -10,12 +10,17 @@ fn main() -> std::io::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_connection(mut stream: TcpStream) -> std::io::Result<()>{
|
fn handle_connection(mut stream: TcpStream) -> std::io::Result<()> {
|
||||||
let mut buffer = [0; 1024];
|
let mut buffer = [0; 1024];
|
||||||
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(())
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue