Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,43 +415,46 @@ fn cmd_serve() {

for stream in listener.incoming() {
let Ok(mut stream) = stream else { continue };
let mut buf = [0u8; 4096];
let n = stream.read(&mut buf).unwrap_or(0);
let request = String::from_utf8_lossy(&buf[..n]);

let path = request
.lines()
.next()
.and_then(|line| line.split_whitespace().nth(1))
.unwrap_or("/");

match resolve_site_file(&site_canon, path) {
ResolveResult::File(file_path) => {
let body = fs::read(&file_path).unwrap_or_default();

// Clone the pathbuf so we can move it into the thread
let site_canon = site_canon.clone();

std::thread::spawn(move || {
let mut buf = [0u8; 4096];
let n = stream.read(&mut buf).unwrap_or(0);
let request = String::from_utf8_lossy(&buf[..n]);

let path = request
.lines()
.next()
.and_then(|line| line.split_whitespace().nth(1))
.unwrap_or("/");

if let Some(file_path) = resolve_site_file(&site_canon, path) {
let body = std::fs::read(&file_path).unwrap_or_default();
let mime = guess_mime(&file_path);

// ADDED: Connection: close
let header = format!(
"HTTP/1.1 200 OK\r\nContent-Type: {mime}\r\nContent-Length: {}\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Type: {mime}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
);

let _ = stream.write_all(header.as_bytes());
let _ = stream.write_all(&body);
}
ResolveResult::Redirect(new_path) => {
let header = format!(
"HTTP/1.1 301 Moved Permanently\r\nLocation: {new_path}\r\nContent-Length: 0\r\n\r\n"
);
let _ = stream.write_all(header.as_bytes());
}
ResolveResult::NotFound => {
} else {
let body = b"404 Not Found";

// ADDED: Connection: close
let header = format!(
"HTTP/1.1 404 Not Found\r\nContent-Length: {}\r\n\r\n",
"HTTP/1.1 404 Not Found\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
);

let _ = stream.write_all(header.as_bytes());
let _ = stream.write_all(body);
}
}
});
}
}

Expand Down