Server responds "oh no"

This commit is contained in:
2026-04-12 21:58:55 -04:00
parent 3c5e92a61d
commit 5df6b9e233
2 changed files with 42 additions and 4 deletions
+1 -2
View File
@@ -1,5 +1,4 @@
- [ ] single-request server on fixed port responding "oh no" - [x] server on fixed port responding "oh no"
- [ ] multi-request server on fixed port responding "oh no"
- [ ] configurable port and working directory - [ ] configurable port and working directory
- [ ] string responses - [ ] string responses
- [ ] error page paths - [ ] error page paths
+41 -2
View File
@@ -1,3 +1,42 @@
fn main() { use std::io::Write;
println!("Hello, world!"); use std::net::{Shutdown, TcpListener};
fn handle_conn(stream: &mut impl Write) -> std::io::Result<()> {
eprintln!("attempting to respond");
let response = concat![
"HTTP/1.1 200 OK\r\n",
"content-type: text/plain\r\n",
"\r\n",
"oh no\n",
];
stream.write_all(response.as_bytes()).unwrap();
stream.flush()?;
Ok(())
}
fn main() -> std::io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:17321").unwrap();
for stream in listener.incoming() {
let mut stream = stream.unwrap();
handle_conn(&mut stream)?;
stream.shutdown(Shutdown::Both)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn handle_conn_writes_response() {
let mut stream = [100u8].repeat(64_000);
let _ = handle_conn(&mut stream);
assert!(str::from_utf8(&stream).unwrap().contains("oh no"));
}
} }