streaming http server

This commit is contained in:
Jake Hillion 2022-05-24 12:47:20 +01:00
parent 44380ba5be
commit 5d707bdde4

View File

@ -1,5 +1,5 @@
use std::fs; use std::fs::OpenOptions;
use std::io::{Read, Write}; use std::io::{self, ErrorKind, Read, Write};
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::path::PathBuf; use std::path::PathBuf;
@ -29,33 +29,53 @@ pub(super) fn handler(mut stream: UnixStream) -> i32 {
req.path req.path
}; };
let status_line = if filename.is_some() { if let Some(filename) = filename {
"HTTP/1.1 200 OK" if try_serve_file(&mut stream, filename).unwrap() {
} else { return exitcode::OK;
"HTTP/1.1 404 NOT FOUND" }
}; }
let contents = if let Some(filename) = filename { let status_line = "HTTP/1.1 404 NOT FOUND";
fs::read_to_string( let contents = "file not found\n";
PathBuf::from("/var/www/html/")
.join(filename.strip_prefix('/').unwrap_or(filename)),
)
.unwrap()
} else {
"content not found\n".to_string()
};
let response_header = format!( let response = format!(
"{}\r\nContent-Length: {}\r\n\r\n", "{}\r\nContent-Length: {}\r\n\r\n{}",
status_line, status_line,
contents.len(), contents.len(),
contents
); );
stream.write_all(response_header.as_bytes()).unwrap(); stream.write_all(response.as_bytes()).unwrap();
stream.write_all(contents.as_bytes()).unwrap();
break; break;
} }
exitcode::OK exitcode::OK
} }
fn try_serve_file(stream: &mut impl io::Write, filename: &str) -> io::Result<bool> {
let mut fd = match OpenOptions::new()
.read(true)
.open(PathBuf::from("/var/www/html/").join(filename.strip_prefix('/').unwrap_or(filename)))
{
Ok(fd) => fd,
Err(e) => {
if e.kind() == ErrorKind::NotFound {
return Ok(false);
}
return Err(e);
}
};
let status_line = "HTTP/1.1 200 OK";
let response_header = format!(
"{}\r\nContent-Length: {}\r\n\r\n",
status_line,
fd.metadata()?.len(),
);
stream.write_all(response_header.as_bytes())?;
io::copy(&mut fd, stream)?;
Ok(true)
}