stage 4 attempt 2

This commit is contained in:
YK 2024-05-10 21:37:00 +03:00
parent 5fb0b9478e
commit a86efcf494

View File

@ -1,6 +1,6 @@
#![feature(if_let_guard)] // #![feature(if_let_guard)]
use std::{ fmt::Display, io::{ BufRead, BufReader, Write }, net::TcpListener }; use std::{ io::{ BufRead, BufReader, Write }, net::TcpListener };
use anyhow::Result; use anyhow::Result;
use thiserror::Error; use thiserror::Error;
@ -30,15 +30,16 @@ fn main() -> Result<()> {
let start_line = data.next().ok_or(E::InvalidRequest)?; // should be 500; let start_line = data.next().ok_or(E::InvalidRequest)?; // should be 500;
let mut parts = start_line.split_whitespace().map(ToOwned::to_owned); let mut parts = start_line.split_whitespace().map(ToOwned::to_owned);
let method = parts.next().ok_or(E::InvalidRequest)?; let method = parts.next().ok_or(E::InvalidRequest)?;
let path = parts.next().ok_or(E::InvalidRequest)?; let path = parts.next().ok_or(E::InvalidRequest)?;
let ver = parts.next().ok_or(E::InvalidRequest)?; let ver = parts.next().ok_or(E::InvalidRequest)?;
(method, path, ver) (method, path, ver)
}; };
let response = match path.as_str() { let response = match path.as_str() {
"/" => Response::Empty, "/" => Response::Empty,
p if let Some(echo) = p.strip_prefix("/echo/") => Response::TextPlain(echo), // p if let Some(echo) = p.strip_prefix("/echo/") => Response::TextPlain(echo), // a nicer way to do that, not available in stable yet
p if p.starts_with("/echo/") => Response::TextPlain(p.trim_start_matches("/echo/")),
_ => Response::_404, _ => Response::_404,
}; };