Server responds "oh no"
This commit is contained in:
+1
-2
@@ -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
@@ -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"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user