Last active
May 1, 2024 10:21
-
-
Save masakielastic/91e11d1508b0cda50e3517e37f024dda to your computer and use it in GitHub Desktop.
nghttp2 の HPACK エンコードの例です
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <nghttp2/nghttp2.h> | |
// https://nghttp2.org/documentation/tutorial-hpack.html | |
#define MAKE_NV(K, V) \ | |
{ \ | |
(uint8_t *)K, (uint8_t *)V, sizeof(K) - 1, sizeof(V) - 1, \ | |
NGHTTP2_NV_FLAG_NONE \ | |
} | |
int main() { | |
size_t i; | |
size_t sum; | |
size_t nvlen; | |
uint8_t *buf; | |
size_t buflen; | |
size_t outlen; | |
int rv; | |
nghttp2_hd_deflater *deflater; | |
nghttp2_nv nva[] = { | |
MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"), | |
MAKE_NV(":path", "/"), MAKE_NV("user-agent", "libnghttp2"), | |
MAKE_NV("accept-encoding", "gzip, deflate")}; | |
nvlen = sizeof(nva) / sizeof(nva[0]); | |
sum = 0; | |
for (i = 0; i < nvlen; ++i) { | |
sum += nva[i].namelen + nva[i].valuelen; | |
} | |
printf("Input (%zu byte(s)):\n\n", sum); | |
for (i = 0; i < nvlen; ++i) { | |
fwrite(nva[i].name, 1, nva[i].namelen, stdout); | |
printf(": "); | |
fwrite(nva[i].value, 1, nva[i].valuelen, stdout); | |
printf("\n"); | |
} | |
rv = nghttp2_hd_deflate_new(&deflater, 4096); | |
buflen = nghttp2_hd_deflate_bound(deflater, nva, nvlen); | |
buf = malloc(buflen); | |
rv = nghttp2_hd_deflate_hd(deflater, buf, buflen, nva, nvlen); | |
outlen = (size_t)rv; | |
printf("\nDeflate (%zu byte(s), ratio %.02f):\n\n", outlen, | |
sum == 0 ? 0 : (double)outlen / (double)sum); | |
for (i = 0; i < outlen; ++i) { | |
if ((i & 0x0fu) == 0) { | |
printf("%08zX: ", i); | |
} | |
printf("%02X ", buf[i]); | |
if (((i + 1) & 0x0fu) == 0) { | |
printf("\n"); | |
} | |
} | |
printf("\n\nInflate:\n\n"); | |
free(buf); | |
nghttp2_hd_deflate_del(deflater); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zig run test.c -lc -lnghttp2 | |
Input (87 byte(s)): | |
:scheme: https | |
:authority: example.org | |
:path: / | |
user-agent: libnghttp2 | |
accept-encoding: gzip, deflate | |
Deflate (22 byte(s), ratio 0.25): | |
00000000: 87 41 88 2F 91 D3 5D 05 5C F6 4D 84 7A 87 A0 D1 | |
00000010: D5 34 E9 4D 62 90 | |
Inflate: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment