Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
fe7fab6c9f
|
|||
|
55672c77c2
|
|||
|
c2ceee564b
|
|||
|
5df6b9e233
|
|||
|
3c5e92a61d
|
|||
|
7edf80da41
|
@@ -0,0 +1 @@
|
|||||||
|
/target/
|
||||||
Generated
+7
@@ -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"
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "h8r"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# h8r gonna h8
|
||||||
|
|
||||||
|
h + len(ttpserve) + r
|
||||||
|
|
||||||
|
A li'l HTTP server for learning stuff.
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
- [x] server on fixed port responding "oh no"
|
||||||
|
- [x] configurable addr
|
||||||
|
- [x] serve "index.txt" file if exists, else 404
|
||||||
|
- [ ] serve relative paths with mime type text/plain
|
||||||
|
- [ ] guess mime type
|
||||||
|
- [ ] configurable working directory
|
||||||
|
- [ ] string responses
|
||||||
|
- [ ] automatic directory index
|
||||||
|
- [ ] content ranges
|
||||||
|
- [ ] error page paths
|
||||||
|
- [ ] liquid template rendering
|
||||||
|
- [ ] upstream proxy via prefix
|
||||||
|
- [ ] upstream proxy path match
|
||||||
|
- [ ] upstream proxy header middleware rules
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
|
use std::io::{self, Write};
|
||||||
|
use std::net;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let addr = env::var("H8R_ADDR").unwrap_or("127.0.0.1:17321".to_string());
|
||||||
|
|
||||||
|
run_server(addr)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_server(addr: String) -> Result<(), io::Error> {
|
||||||
|
let listener = net::TcpListener::bind(&addr).unwrap();
|
||||||
|
|
||||||
|
eprintln!("h8r: listening at {}", addr);
|
||||||
|
|
||||||
|
for stream in listener.incoming() {
|
||||||
|
let mut stream = stream.unwrap();
|
||||||
|
handle_conn(&mut stream)?;
|
||||||
|
stream.shutdown(net::Shutdown::Both)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_conn(outstream: &mut net::TcpStream) -> io::Result<()> {
|
||||||
|
eprintln!("h8r: attempting to respond");
|
||||||
|
|
||||||
|
let relpath = "index.txt";
|
||||||
|
|
||||||
|
let index_res = fs::read_to_string(relpath);
|
||||||
|
if index_res.is_ok() {
|
||||||
|
let head = concat!["HTTP/1.1 200 OK\r\n", "content-type: text/plain\r\n"];
|
||||||
|
let index = index_res.unwrap();
|
||||||
|
let index_len = index.len();
|
||||||
|
outstream.write_all(head.as_bytes()).unwrap();
|
||||||
|
outstream
|
||||||
|
.write_all(format!("content-length: {}\r\n", index_len).as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
outstream.write_all("\r\n".as_bytes()).unwrap();
|
||||||
|
outstream.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",
|
||||||
|
];
|
||||||
|
|
||||||
|
outstream.write_all(response.as_bytes()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
outstream.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn handle_conn_writes_response() {
|
||||||
|
let test_addr = "127.0.0.1:27321".to_string();
|
||||||
|
|
||||||
|
let _l = net::TcpListener::bind(&test_addr);
|
||||||
|
let mut stream = match net::TcpStream::connect(&("127.0.0.1", 27321)) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => panic!("cannot connect to test server: {}", e),
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = handle_conn(&mut stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user