use std::io::{BufRead, BufReader, Read, Write};
use std::os::unix::fs::PermissionsExt;
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use super::runner::Session;
const MAX_BODY: usize = 4 * 1024 * 1024;
fn reason(code: u16) -> &'static str {
match code {
200 => "OK",
204 => "No Content",
400 => "Bad Request",
404 => "Not Found",
409 => "Conflict",
502 => "Bad Gateway",
_ => "OK",
}
}
fn read_request(stream: &UnixStream) -> Result<(String, String, Vec<u8>), String> {
let mut r = BufReader::new(stream);
let mut line = String::new();
r.read_line(&mut line).map_err(|e| e.to_string())?;
let mut parts = line.split_whitespace();
let method = parts.next().ok_or("empty request")?.to_string();
let path = parts.next().ok_or("no path")?.to_string();
let mut len = 0usize;
loop {
let mut h = String::new();
let n = r.read_line(&mut h).map_err(|e| e.to_string())?;
if n == 0 || h == "\r\n" || h == "\n" {
break;
}
if let Some((k, v)) = h.split_once(':') {
if k.eq_ignore_ascii_case("content-length") {
len = v
.trim()
.parse()
.map_err(|_| "bad content-length".to_string())?;
}
}
}
if len > MAX_BODY {
return Err("body too large".into());
}
let mut body = vec![0u8; len];
r.read_exact(&mut body).map_err(|e| e.to_string())?;
Ok((method, path, body))
}
fn respond(mut stream: &UnixStream, code: u16, body: &str) {
let head = format!(
"HTTP/1.1 {code} {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
reason(code),
body.len()
);
let _ = stream.write_all(head.as_bytes());
let _ = stream.write_all(body.as_bytes());
let _ = stream.flush();
}
pub fn route(session: &Session, method: &str, path: &str, body: &[u8]) -> (u16, String) {
let path = path.split('?').next().unwrap_or(path);
match (method, path) {
("GET", "/next") => session.next(),
("POST", "/outputs") => session.outputs(body),
("POST", "/done") => session.done(body),
_ => (404, r#"{"error":"not found"}"#.to_string()),
}
}
fn handle(stream: UnixStream, session: Arc<Session>) {
let _ = stream.set_read_timeout(Some(Duration::from_secs(30)));
match read_request(&stream) {
Ok((m, p, b)) => {
let (code, body) = route(&session, &m, &p, &b);
respond(&stream, code, &body);
}
Err(e) => respond(&stream, 400, &format!(r#"{{"error":{:?}}}"#, e)),
}
}
pub fn serve(path: &Path, session: Arc<Session>, stop: Arc<AtomicBool>) -> Result<(), String> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir).map_err(|e| format!("create {}: {e}", dir.display()))?;
let _ = std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700));
}
let _ = std::fs::remove_file(path);
let listener = UnixListener::bind(path).map_err(|e| format!("bind {}: {e}", path.display()))?;
let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o666));
listener.set_nonblocking(true).map_err(|e| e.to_string())?;
std::thread::Builder::new()
.name("spaces-sock".into())
.spawn(move || {
while !stop.load(Ordering::SeqCst) {
match listener.accept() {
Ok((s, _)) => {
let _ = s.set_nonblocking(false);
let sess = session.clone();
let _ = std::thread::Builder::new()
.name("spaces-conn".into())
.spawn(move || handle(s, sess));
}
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
std::thread::sleep(Duration::from_millis(25));
}
Err(_) => std::thread::sleep(Duration::from_millis(100)),
}
}
})
.map_err(|e| e.to_string())?;
Ok(())
}
#[cfg(test)]
pub(crate) fn request(path: &Path, method: &str, uri: &str, body: &str) -> (u16, String) {
try_request(path, method, uri, body).expect("socket request")
}
#[cfg(test)]
pub(crate) fn try_request(
path: &Path,
method: &str,
uri: &str,
body: &str,
) -> Option<(u16, String)> {
let mut s = UnixStream::connect(path).ok()?;
let req = format!(
"{method} {uri} HTTP/1.1\r\nHost: x\r\nContent-Length: {}\r\n\r\n{body}",
body.len()
);
s.write_all(req.as_bytes()).ok()?;
let mut out = String::new();
s.read_to_string(&mut out).ok()?;
let (head, body) = out.split_once("\r\n\r\n").unwrap_or((&out, ""));
let code = head
.split_whitespace()
.nth(1)
.and_then(|c| c.parse().ok())
.unwrap_or(0);
Some((code, body.to_string()))
}