Skip to content

Commit 23cbbac

Browse files
committed
Aloooooo
Added returning a reat html return when the request is submited
1 parent ba6045a commit 23cbbac

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

html/alo.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Alooooo!</title>
6+
</head>
7+
<body>
8+
<h1>Aloooop!</h1>
9+
<p>Alooo from rusty web-servey web-serverr</p>
10+
</body>
11+
</html>
12+

src/main.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
//! The HTTP Request
2+
//! Format : Method Request-URI HTTP-Version CRLF
3+
//! headers CRLF
4+
//! message-body
5+
//! The GET request : the client is asking for info
6+
//!
7+
//! Now onto the Writting a response
8+
//! HTTP-Version Status-Code Reason-Phrase CRLF
9+
//! headers CRLF
10+
//! message-body
11+
//! HTTP/1.1 200 OK\r\n\r\n -> Example of a response an "Ok" type one
112
//! user imports
213
use std::{
14+
fs, //file system stuff
315
io::{prelude::*, BufReader}, // the std lib offers a module to listen to a TCP connection
416
net::{TcpListener, TcpStream},
517
};
@@ -19,11 +31,30 @@ fn main() {
1931
}
2032

2133
fn handle_connection(mut stream: TcpStream) {
22-
let buf_reader = BufReader::new(&stream);
23-
let http_request: Vec<_> = buf_reader
24-
.lines()
34+
let buf_reader = BufReader::new(&stream); //wraps a reference to
35+
//the stream
36+
let http_request: Vec<_> = buf_reader // lines colected in a
37+
// vector
38+
.lines() //method that returns an iterator of type
39+
//result<string, std::io:.Error>
2540
.map(|result| result.unwrap())
2641
.take_while(|line| !line.is_empty())
2742
.collect();
2843
println!("Request: {http_request:#?}");
44+
45+
let status_line = "HTTP/1.1 200 OK"; //to say its "ok"
46+
47+
let contents = fs::read_to_string("html/alo.html").unwrap();
48+
let length = contents.len();
49+
50+
let response = format!(
51+
"{}\r\n\
52+
Content-Length: {}\r\n\
53+
Content-Type: text/html\r\n\
54+
\r\n\
55+
{}",
56+
status_line, length, contents
57+
);
58+
59+
stream.write_all(response.as_bytes()).unwrap();
2960
}

0 commit comments

Comments
 (0)