httpwire
nimony/lib/std/http/httpwire.nim
const WriteFull: int64Did not fit. Nothing past
iwas written; growdestand retry.proc writeByte(dest: var openArray; i: int64; c: char): int64proc writeBytes(dest: var openArray; i: int64; s: openArray): int64proc writeCrLf(dest: var openArray; i: int64): int64proc digitCount(x: int64): int64Digits in the decimal form of a non-negative
x. Multiplications, not divisions:headLenasks this for every integer header of every head just to size a buffer, andwriteIntthen does the one division pass that actually produces the digits.proc writeInt(dest: var openArray; i: int64; x: int64): int64Decimal, formatted in place. Negative values are not written: nothing in an HTTP head is signed, and a
-1reaching the wire would be a bug downstream rather than a value.Written back-to-front from the last digit, so the single
divloop lands the digits straight indest— no temporary to reverse out of.proc reasonPhrase(status: int64): stringThe conventional phrase. Clients ignore it and HTTP/1.1 allows it to be empty, but middleboxes are happier when it is there. Unlisted codes get their class's phrase rather than nothing.
proc writeChunkHeader(dest: var openArray; i: int64; size: int64): int64SIZE CRLF. The data follows, then its own CRLF — seewriteChunkEnd. No extensions are ever written: nothing reads them, and every byte in this line is a byte another parser has to agree with us about.proc writeChunkEnd(dest: var openArray; i: int64): int64The CRLF that closes a chunk's data.
proc writeLastChunk(dest: var openArray; i: int64): int640 CRLF CRLF— the zero-length chunk and an empty trailer section. Written together because a body that stops after the zero chunk is a body the peer is still waiting on.proc writeChunk(dest: var openArray; i: int64; data: openArray): int64SIZE CRLF data CRLF— a whole chunk, staged in one place so the caller can send it in one write. A chunk split across writes is a chunk a peer can be left half-way through.proc chunkOverhead(size: int64): int64Bytes
writeChunkHeader+writeChunkEndadd aroundsizebytes of data, so a caller can size a buffer without writing twice: one hex digit and the two CRLFs, plus a digit for every further nibble.proc writeRequestHead(dest: var openArray; i: int64; m: HttpMsg): int64METHOD SP target SP version CRLF, the headers, and the blank line.proc writeResponseHead(dest: var openArray; i: int64; m: HttpMsg): int64version SP status SP reason CRLF, the headers, and the blank line.proc writeHead(dest: var openArray; i: int64; m: HttpMsg): int64Whichever kind
mis.proc headLen(m: HttpMsg): int64Exactly how many bytes
writeHeadwill produce. One extra walk of a tree that is already in cache, and it means the common path sizes its buffer once instead of writing, failing and retrying.