Serving index.txt

This commit is contained in:
2026-04-12 22:23:30 -04:00
parent c2ceee564b
commit 55672c77c2
2 changed files with 32 additions and 17 deletions
+3 -3
View File
@@ -1,13 +1,13 @@
- [x] server on fixed port responding "oh no" - [x] server on fixed port responding "oh no"
- [x] configurable addr - [x] configurable addr
- [ ] string responses - [x] serve "index.txt" file if exists, else 404
- [ ] error page paths
- [ ] serve "index.txt" file if exists, else 404
- [ ] serve relative paths with mime type text/plain - [ ] serve relative paths with mime type text/plain
- [ ] guess mime type - [ ] guess mime type
- [ ] configurable working directory - [ ] configurable working directory
- [ ] string responses
- [ ] automatic directory index - [ ] automatic directory index
- [ ] content ranges - [ ] content ranges
- [ ] error page paths
- [ ] liquid template rendering - [ ] liquid template rendering
- [ ] upstream proxy via prefix - [ ] upstream proxy via prefix
- [ ] upstream proxy path match - [ ] upstream proxy path match
+29 -14
View File
@@ -1,8 +1,10 @@
use std::io::Write; use std::env;
use std::fs;
use std::io::{self, Write};
use std::net::{Shutdown, TcpListener}; use std::net::{Shutdown, TcpListener};
fn main() -> std::io::Result<()> { fn main() -> io::Result<()> {
let addr = std::env::var("H8R_ADDR").unwrap_or("127.0.0.1:17321".to_string()); let addr = env::var("H8R_ADDR").unwrap_or("127.0.0.1:17321".to_string());
let listener = TcpListener::bind(&addr).unwrap(); let listener = TcpListener::bind(&addr).unwrap();
eprintln!("h8r: listening at {}", addr); eprintln!("h8r: listening at {}", addr);
@@ -16,19 +18,32 @@ fn main() -> std::io::Result<()> {
Ok(()) Ok(())
} }
fn handle_conn(stream: &mut impl Write) -> std::io::Result<()> { fn handle_conn(stream: &mut impl Write) -> io::Result<()> {
eprintln!("attempting to respond"); eprintln!("h8r: attempting to respond");
let response = concat![ let index_res = fs::read_to_string("index.txt");
"HTTP/1.1 200 OK\r\n", if index_res.is_ok() {
"content-type: text/plain\r\n", let head = concat!["HTTP/1.1 200 OK\r\n", "content-type: text/plain\r\n"];
"\r\n", let index = index_res.unwrap();
"oh no\n", let index_len = index.len();
]; stream.write_all(head.as_bytes()).unwrap();
stream
.write_all(format!("content-length: {}\r\n", index_len).as_bytes())
.unwrap();
stream.write_all("\r\n".as_bytes()).unwrap();
stream.write_all(index.as_bytes()).unwrap();
} else {
let response = concat![
"HTTP/1.1 404 Not Found\r\n",
"content-type: text/plain\r\n",
"\r\n",
"oh no\n",
];
stream.write_all(response.as_bytes()).unwrap();
}
stream.write_all(response.as_bytes()).unwrap();
stream.flush()?; stream.flush()?;
Ok(()) Ok(())
} }
@@ -38,7 +53,7 @@ mod tests {
#[test] #[test]
fn handle_conn_writes_response() { fn handle_conn_writes_response() {
let mut stream = [100u8].repeat(64_000); let mut stream = [100u8].repeat(1_000);
let _ = handle_conn(&mut stream); let _ = handle_conn(&mut stream);
assert!(str::from_utf8(&stream).unwrap().contains("oh no")); assert!(str::from_utf8(&stream).unwrap().contains("oh no"));
} }