~/ learn/ comp-348/ cards/ The smallest server that speaks HTTP
1 of 5

The header block, assembled with String.join

The header block, assembled with String.join

Answer

String header = String.join("\r\n", "HTTP/1.1 200 OK", "Content-Type: " + mediaType + "; charset=" + charset.name(), "Content-Length: " + page.length, "Connection: close", "", ""); byte[] head = header.getBytes(StandardCharsets.US_ASCII);

`String.join` inserts the separator *between* elements, so the two empty elements at the end buy you two more CRLFs: one closes the last header, one closes the block. `page.length` is the byte count of the array you are about to concatenate, not the character count of any `String`, so the announced length is true by construction — and the header encodes as US-ASCII because a header is protocol, never platform text.

Harold 4e ch9 §A Single-File Server

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/c1522018-69e3-4cd0-84fd-8dbd48f458e5/flashcard utf-8 LF