Compare commits

..

3 Commits

Author SHA1 Message Date
meatballhat 5df6b9e233 Server responds "oh no" 2026-04-12 21:58:55 -04:00
meatballhat 3c5e92a61d That, too 2026-04-12 11:07:30 -04:00
meatballhat 7edf80da41 Let's build an HTTP server 2026-04-12 11:05:30 -04:00
6 changed files with 74 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
/target/
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "h8r"
version = "0.1.0"
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "h8r"
version = "0.1.0"
edition = "2024"
[dependencies]
+5
View File
@@ -0,0 +1,5 @@
# h8r gonna h8
h + len(ttpserve) + r
A li'l HTTP server for learning stuff.
+13
View File
@@ -0,0 +1,13 @@
- [x] server on fixed port responding "oh no"
- [ ] configurable port and working directory
- [ ] string responses
- [ ] error page paths
- [ ] serve "index.txt" file if exists, else 404
- [ ] serve relative paths with mime type text/plain
- [ ] guess mime type
- [ ] automatic directory index
- [ ] content ranges
- [ ] liquid template rendering
- [ ] upstream proxy via prefix
- [ ] upstream proxy path match
- [ ] upstream proxy header middleware rules
+42
View File
@@ -0,0 +1,42 @@
use std::io::Write;
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"));
}
}