One error responder, with a truthful byte count
One error responder, with a truthful byte count
Answer
String page = "<!doctype html><html><head><title>" + status + "</title></head>" + "<body><h1>" + status + "</h1><p>" + detail + "</p></body></html>"; byte[] bytes = page.getBytes(StandardCharsets.UTF_8); out.write(String.join("\r\n", "HTTP/1.1 " + status, "Content-Type: text/html; charset=UTF-8", "Content-Length: " + bytes.length, "", "")); out.write(page); out.flush();
`bytes.length`, not `page.length()`. The moment the sentence contains one non-ASCII character the two disagree, and a `Content-Length` short by two bytes leaves the client waiting for a body it has already received in full. The header assembles exactly as it did in lesson 1 — one `String.join`, two empty elements to close the block — so an error response and a success response are built by the same rule.
Harold 4e ch9 §A Full-Fledged HTTP Server; ch6 §The Protocol