Skip to content

Commit 9c256e8

Browse files
committed
pass 4th stage
1 parent d644b59 commit 9c256e8

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

Diff for: Cargo.lock

+41-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ thiserror = "1.0.38" # error handling
2525
tokio = { version = "1.23.0", features = ["full"] } # async networking
2626
nom = "7.1.3" # parser combinators
2727
itertools = "0.11.0" # General iterator helpers
28+
regex = "1.10.4"
2829

2930
[dev-dependencies]
3031
pretty_assertions = "1.3.0" # nicer looking assertions

Diff for: src/main.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use std::io::{Read, Write};
22
use std::net::{TcpListener, TcpStream};
3-
use std::str::FromStr;
43

54
use nom::branch::alt;
65
use nom::bytes::streaming::{tag_no_case as tag, take_until};
7-
use nom::character::complete::one_of;
86
use nom::IResult;
97
use thiserror::Error;
108

@@ -32,6 +30,16 @@ pub enum ConnectionError {
3230
ParsingError(String), // TODO: improve parsing error
3331
}
3432

33+
fn send_text_content(stream: &mut TcpStream, txt: &str) -> Result<(), std::io::Error> {
34+
stream.write("HTTP/1.1 200 OK\r\n".as_bytes())?;
35+
stream.write("Content-Type: text/plain\r\n".as_bytes())?;
36+
stream.write(format!("Content-Length: {}\r\n", txt.len()).as_bytes())?;
37+
stream.write("\r\n".as_bytes())?;
38+
stream.write(txt.as_bytes())?;
39+
stream.flush()?;
40+
Ok(())
41+
}
42+
3543
fn handle_connection(mut stream: TcpStream) -> Result<(), ConnectionError> {
3644
let mut buffer = [0; 1024];
3745
stream.read(&mut buffer)?; // read 1K bytes for now
@@ -43,6 +51,9 @@ fn handle_connection(mut stream: TcpStream) -> Result<(), ConnectionError> {
4351

4452
if req.path == "/" {
4553
stream.write("HTTP/1.1 200 OK\r\n\r\n".as_bytes()).unwrap();
54+
} else if req.path.starts_with("/echo/") && req.path[6..].find("/").is_none() {
55+
let txt = &req.path[6..];
56+
send_text_content(&mut stream, txt).unwrap();
4657
} else {
4758
stream
4859
.write("HTTP/1.1 404 Not Found\r\n\r\n".as_bytes())

0 commit comments

Comments
 (0)