An implementation of HPACK (Header Compression for HTTP/2). Based on rfc7541.
This lib is used by https://github.com/nitely/nim-hyperx
Nim +2
nimble install hpack import hpack proc toBytes(s: seq[uint16]): seq[byte] = result = newSeqOfCap[byte](s.len * 2) for b in s: result.add(byte(b shr 8)) result.add(byte(b and 0xff)) # First request let req1 = @[ 0x8286'u16, 0x8441, 0x8cf1, 0xe3c2, 0xe5f2, 0x3a6b, 0xa0ab, 0x90f4].toBytes var ss = "" var bb = newSeq[HBounds]() var dh = initDynHeaders(256) assert hdecodeAll(req1, dh, ss, bb) == req1.len assert(ss == ":method: GET\r\L" & ":scheme: http\r\L" & ":path: /\r\L" & ":authority: www.example.com\r\L") # Second request let req2 = @[ 0x8286'u16, 0x84be, 0x5886, 0xa8eb, 0x1064, 0x9cbf].toBytes ss.setLen 0 bb.setLen 0 assert hdecodeAll(req2, dh, ss, bb) == req2.len assert(ss == ":method: GET\r\L" & ":scheme: http\r\L" & ":path: /\r\L" & ":authority: www.example.com\r\L" & "cache-control: no-cache\r\L") # So on...import hpack proc toBytes(s: seq[uint16]): seq[byte] = result = newSeqOfCap[byte](s.len * 2) for b in s: result.add(byte(b shr 8)) result.add(byte(b and 0xff)) # First response var resp = newSeq[byte]() var dh = initDynHeaders(256) hencode(":method", "GET", dh, resp) hencode(":scheme", "http", dh, resp) hencode(":path", "/", dh, resp) hencode(":authority", "www.example.com", dh, resp) assert resp == @[ 0x8286'u16, 0x8441, 0x8cf1, 0xe3c2, 0xe5f2, 0x3a6b, 0xa0ab, 0x90f4].toBytes # Second response resp.setLen(0) hencode(":method", "GET", dh, resp) hencode(":scheme", "http", dh, resp) hencode(":path", "/", dh, resp) hencode(":authority", "www.example.com", dh, resp) hencode("cache-control", "no-cache", dh, resp) assert resp == @[ 0x8286'u16, 0x84be, 0x5886, 0xa8eb, 0x1064, 0x9cbf].toBytes # So on...nimble test MIT