No description
Find a file
nitely 75e7548f00 revert CI
keep using 2.0.0 because 2.2.0 is arm64
2024-10-21 14:09:28 -03:00
.github/workflows revert CI 2024-10-21 14:09:28 -03:00
docs remove docs 2024-02-17 05:26:56 -03:00
gen huffman decoder 2018-05-22 12:53:37 -03:00
src check fix 2024-10-21 13:20:10 -03:00
tests test table len 2024-10-15 15:39:16 -03:00
.gitignore http2jp tests + bug fix #3 2024-03-31 10:05:19 -03:00
CHANGELOG.md remove DecodedStr 2024-04-16 22:57:06 -03:00
hpack.nimble bump 2024-10-21 13:22:33 -03:00
LICENSE init 2018-04-01 08:43:06 -03:00
README.md remove DecodedStr 2024-04-16 22:57:06 -03:00

HPACK

An implementation of HPACK (Header Compression for HTTP/2). Based on rfc7541.

This lib is used by https://github.com/nitely/nim-hyperx

Compatibility

Nim +2

Install

nimble install hpack

Docs

nitely.github.io/nim-hpack

Usage

Decode

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...

Encode

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...

Tests

nimble test

LICENSE

MIT