nghttp2/examples/deflate.c

207 lines
5.9 KiB
C
Raw Permalink Normal View History

2014-06-29 16:45:49 +02:00
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2014 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* !HAVE_CONFIG_H */
2014-06-29 16:45:49 +02:00
#include <stdio.h>
#include <string.h>
#include <nghttp2/nghttp2.h>
2014-11-27 15:39:04 +01:00
#define MAKE_NV(K, V) \
{ \
(uint8_t *) K, (uint8_t *)V, sizeof(K) - 1, sizeof(V) - 1, \
NGHTTP2_NV_FLAG_NONE \
}
2014-06-29 16:45:49 +02:00
static void deflate(nghttp2_hd_deflater *deflater,
2014-11-27 15:39:04 +01:00
nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
size_t nvlen);
2014-06-29 16:45:49 +02:00
2014-11-27 15:39:04 +01:00
static int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
size_t inlen, int final);
2014-06-29 16:45:49 +02:00
2014-11-27 15:39:04 +01:00
int main(int argc _U_, char **argv _U_) {
2014-06-29 16:45:49 +02:00
int rv;
nghttp2_hd_deflater *deflater;
nghttp2_hd_inflater *inflater;
/* Define 1st header set. This is looks like a HTTP request. */
nghttp2_nv nva1[] = {
2014-11-27 15:39:04 +01:00
MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"),
MAKE_NV(":path", "/"), MAKE_NV("user-agent", "libnghttp2"),
MAKE_NV("accept-encoding", "gzip, deflate")};
2014-06-29 16:45:49 +02:00
/* Define 2nd header set */
2014-11-27 15:39:04 +01:00
nghttp2_nv nva2[] = {MAKE_NV(":scheme", "https"),
MAKE_NV(":authority", "example.org"),
MAKE_NV(":path", "/stylesheet/style.css"),
MAKE_NV("user-agent", "libnghttp2"),
MAKE_NV("accept-encoding", "gzip, deflate"),
MAKE_NV("referer", "https://example.org")};
2014-06-29 16:45:49 +02:00
rv = nghttp2_hd_deflate_new(&deflater, 4096);
2014-11-27 15:39:04 +01:00
if (rv != 0) {
2014-06-29 16:45:49 +02:00
fprintf(stderr, "nghttp2_hd_deflate_init failed with error: %s\n",
nghttp2_strerror(rv));
exit(EXIT_FAILURE);
}
rv = nghttp2_hd_inflate_new(&inflater);
2014-11-27 15:39:04 +01:00
if (rv != 0) {
2014-06-29 16:45:49 +02:00
fprintf(stderr, "nghttp2_hd_inflate_init failed with error: %s\n",
nghttp2_strerror(rv));
exit(EXIT_FAILURE);
}
/* Encode and decode 1st header set */
deflate(deflater, inflater, nva1, sizeof(nva1) / sizeof(nva1[0]));
/* Encode and decode 2nd header set, using differential encoding
using state after encoding 1st header set. */
deflate(deflater, inflater, nva2, sizeof(nva2) / sizeof(nva2[0]));
nghttp2_hd_inflate_del(inflater);
nghttp2_hd_deflate_del(deflater);
return 0;
}
static void deflate(nghttp2_hd_deflater *deflater,
2014-11-27 15:39:04 +01:00
nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
size_t nvlen) {
2014-06-29 16:45:49 +02:00
ssize_t rv;
uint8_t *buf;
size_t buflen;
size_t outlen;
size_t i;
size_t sum;
sum = 0;
2014-11-27 15:39:04 +01:00
for (i = 0; i < nvlen; ++i) {
2014-06-29 16:45:49 +02:00
sum += nva[i].namelen + nva[i].valuelen;
}
printf("Input (%zu byte(s)):\n\n", sum);
2014-11-27 15:39:04 +01:00
for (i = 0; i < nvlen; ++i) {
fwrite(nva[i].name, 1, nva[i].namelen, stdout);
2014-06-29 16:45:49 +02:00
printf(": ");
fwrite(nva[i].value, 1, nva[i].valuelen, stdout);
2014-06-29 16:45:49 +02:00
printf("\n");
}
buflen = nghttp2_hd_deflate_bound(deflater, nva, nvlen);
buf = malloc(buflen);
rv = nghttp2_hd_deflate_hd(deflater, buf, buflen, nva, nvlen);
2014-11-27 15:39:04 +01:00
if (rv < 0) {
2014-06-29 16:45:49 +02:00
fprintf(stderr, "nghttp2_hd_deflate_hd() failed with error: %s\n",
nghttp2_strerror((int)rv));
free(buf);
exit(EXIT_FAILURE);
}
2015-09-23 07:41:53 +02:00
outlen = (size_t)rv;
2014-06-29 16:45:49 +02:00
2014-11-27 15:39:04 +01:00
printf("\nDeflate (%zu byte(s), ratio %.02f):\n\n", outlen,
2015-09-23 11:07:38 +02:00
sum == 0 ? 0 : (double)outlen / (double)sum);
2014-06-29 16:45:49 +02:00
2014-11-27 15:39:04 +01:00
for (i = 0; i < outlen; ++i) {
if ((i & 0x0fu) == 0) {
2014-06-29 16:45:49 +02:00
printf("%08zX: ", i);
}
printf("%02X ", buf[i]);
2014-11-27 15:39:04 +01:00
if (((i + 1) & 0x0fu) == 0) {
2014-06-29 16:45:49 +02:00
printf("\n");
}
}
printf("\n\nInflate:\n\n");
/* We pass 1 to final parameter, because buf contains whole deflated
header data. */
rv = inflate_header_block(inflater, buf, outlen, 1);
2014-11-27 15:39:04 +01:00
if (rv != 0) {
2014-06-29 16:45:49 +02:00
free(buf);
exit(EXIT_FAILURE);
}
printf("\n-----------------------------------------------------------"
"--------------------\n");
free(buf);
}
2014-11-27 15:39:04 +01:00
int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
size_t inlen, int final) {
2014-06-29 16:45:49 +02:00
ssize_t rv;
2014-11-27 15:39:04 +01:00
for (;;) {
2014-06-29 16:45:49 +02:00
nghttp2_nv nv;
int inflate_flags = 0;
size_t proclen;
2014-11-27 15:39:04 +01:00
rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, in, inlen, final);
2014-06-29 16:45:49 +02:00
2014-11-27 15:39:04 +01:00
if (rv < 0) {
2014-06-29 16:45:49 +02:00
fprintf(stderr, "inflate failed with error code %zd", rv);
return -1;
}
2015-09-23 07:41:53 +02:00
proclen = (size_t)rv;
2014-06-29 16:45:49 +02:00
in += proclen;
inlen -= proclen;
2014-11-27 15:39:04 +01:00
if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
fwrite(nv.name, 1, nv.namelen, stderr);
2014-06-29 16:45:49 +02:00
fprintf(stderr, ": ");
fwrite(nv.value, 1, nv.valuelen, stderr);
2014-06-29 16:45:49 +02:00
fprintf(stderr, "\n");
}
2014-11-27 15:39:04 +01:00
if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
2014-06-29 16:45:49 +02:00
nghttp2_hd_inflate_end_headers(inflater);
break;
}
2014-11-27 15:39:04 +01:00
if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {
2014-06-29 16:45:49 +02:00
break;
}
}
return 0;
}