| | varnish-cache/bin/varnishtest/vtest2/src/vtc_gzip.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2008-2019 Varnish Software AS |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 5 |
|
* |
| 6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 7 |
|
* |
| 8 |
|
* Redistribution and use in source and binary forms, with or without |
| 9 |
|
* modification, are permitted provided that the following conditions |
| 10 |
|
* are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright |
| 12 |
|
* notice, this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
|
* SUCH DAMAGE. |
| 28 |
|
*/ |
| 29 |
|
|
| 30 |
|
#include "config.h" |
| 31 |
|
|
| 32 |
|
#include <stdio.h> |
| 33 |
|
#include <stdlib.h> |
| 34 |
|
#include <string.h> |
| 35 |
|
|
| 36 |
|
#include "vtc.h" |
| 37 |
|
#include "vtc_http.h" |
| 38 |
|
#if defined(WITH_ZLIB) |
| 39 |
|
#include <zlib.h> |
| 40 |
|
#else |
| 41 |
|
#include "vgz.h" |
| 42 |
|
#endif |
| 43 |
|
|
| 44 |
|
#ifdef VGZ_EXTENSIONS |
| 45 |
|
static void |
| 46 |
8200 |
vtc_report_gz_bits(struct vtclog *vl, const z_stream *vz) |
| 47 |
|
{ |
| 48 |
16400 |
vtc_log(vl, 4, "startbit = %ju %ju/%ju", |
| 49 |
8200 |
(uintmax_t)vz->start_bit, |
| 50 |
8200 |
(uintmax_t)vz->start_bit >> 3, (uintmax_t)vz->start_bit & 7); |
| 51 |
16400 |
vtc_log(vl, 4, "lastbit = %ju %ju/%ju", |
| 52 |
8200 |
(uintmax_t)vz->last_bit, |
| 53 |
8200 |
(uintmax_t)vz->last_bit >> 3, (uintmax_t)vz->last_bit & 7); |
| 54 |
16400 |
vtc_log(vl, 4, "stopbit = %ju %ju/%ju", |
| 55 |
8200 |
(uintmax_t)vz->stop_bit, |
| 56 |
8200 |
(uintmax_t)vz->stop_bit >> 3, (uintmax_t)vz->stop_bit & 7); |
| 57 |
8200 |
} |
| 58 |
|
#endif |
| 59 |
|
|
| 60 |
|
static size_t |
| 61 |
33600 |
APOS(ssize_t sz) |
| 62 |
|
{ |
| 63 |
33600 |
assert(sz >= 0); |
| 64 |
33600 |
return (sz); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
/********************************************************************** |
| 68 |
|
* GUNZIPery |
| 69 |
|
*/ |
| 70 |
|
|
| 71 |
|
static struct vsb * |
| 72 |
1560 |
vtc_gunzip_vsb(struct vtclog *vl, int fatal, const struct vsb *vin) |
| 73 |
|
{ |
| 74 |
|
z_stream vz; |
| 75 |
|
struct vsb *vout; |
| 76 |
|
int i; |
| 77 |
|
char buf[BUFSIZ]; |
| 78 |
|
|
| 79 |
1560 |
memset(&vz, 0, sizeof vz); |
| 80 |
1560 |
vout = VSB_new_auto(); |
| 81 |
1560 |
AN(vout); |
| 82 |
|
|
| 83 |
1560 |
vz.next_in = (void*)VSB_data(vin); |
| 84 |
1560 |
vz.avail_in = APOS(VSB_len(vin)); |
| 85 |
|
|
| 86 |
1560 |
assert(Z_OK == inflateInit2(&vz, 31)); |
| 87 |
|
|
| 88 |
1560 |
do { |
| 89 |
1960 |
vz.next_out = (void*)buf; |
| 90 |
1960 |
vz.avail_out = sizeof buf; |
| 91 |
1960 |
i = inflate(&vz, Z_FINISH); |
| 92 |
1960 |
if (vz.avail_out != sizeof buf) |
| 93 |
1960 |
VSB_bcat(vout, buf, sizeof buf - vz.avail_out); |
| 94 |
1960 |
} while (i == Z_OK || i == Z_BUF_ERROR); |
| 95 |
1560 |
if (i != Z_STREAM_END) |
| 96 |
0 |
vtc_log(vl, fatal, |
| 97 |
|
"Gunzip error = %d (%s) in:%jd out:%jd len:%zd", |
| 98 |
0 |
i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out, |
| 99 |
0 |
VSB_len(vin)); |
| 100 |
1560 |
AZ(VSB_finish(vout)); |
| 101 |
|
#ifdef VGZ_EXTENSIONS |
| 102 |
1560 |
vtc_report_gz_bits(vl, &vz); |
| 103 |
|
#endif |
| 104 |
1560 |
assert(Z_OK == inflateEnd(&vz)); |
| 105 |
1560 |
return (vout); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
void |
| 109 |
1560 |
vtc_gunzip(struct http *hp, char *body, long *bodylen) |
| 110 |
|
{ |
| 111 |
|
struct vsb *vin, *vout; |
| 112 |
|
|
| 113 |
1560 |
AN(body); |
| 114 |
1560 |
if (body[0] != (char)0x1f || body[1] != (char)0x8b) |
| 115 |
0 |
vtc_log(hp->vl, hp->fatal, |
| 116 |
|
"Gunzip error: body lacks gzip magic"); |
| 117 |
|
|
| 118 |
1560 |
vin = VSB_new_auto(); |
| 119 |
1560 |
AN(vin); |
| 120 |
1560 |
VSB_bcat(vin, body, *bodylen); |
| 121 |
1560 |
AZ(VSB_finish(vin)); |
| 122 |
1560 |
vout = vtc_gunzip_vsb(hp->vl, hp->fatal, vin); |
| 123 |
1560 |
VSB_destroy(&vin); |
| 124 |
|
|
| 125 |
1560 |
memcpy(body, VSB_data(vout), APOS(VSB_len(vout) + 1)); |
| 126 |
1560 |
*bodylen = APOS(VSB_len(vout)); |
| 127 |
1560 |
VSB_destroy(&vout); |
| 128 |
1560 |
vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen); |
| 129 |
1560 |
vtc_dump(hp->vl, 4, "body", body, *bodylen); |
| 130 |
1560 |
bprintf(hp->bodylen, "%ld", *bodylen); |
| 131 |
1560 |
} |
| 132 |
|
|
| 133 |
|
/********************************************************************** |
| 134 |
|
* GZIPery |
| 135 |
|
*/ |
| 136 |
|
|
| 137 |
|
static int |
| 138 |
9000 |
vtc_gzip_chunk(z_stream *vz, struct vsb *vout, const char *in, size_t inlen, int flush) |
| 139 |
|
{ |
| 140 |
|
int i; |
| 141 |
|
char buf[BUFSIZ]; |
| 142 |
|
|
| 143 |
9000 |
vz->next_in = TRUST_ME(in); |
| 144 |
9000 |
vz->avail_in = APOS(inlen); |
| 145 |
9000 |
do { |
| 146 |
11720 |
vz->next_out = (void*)buf; |
| 147 |
11720 |
vz->avail_out = sizeof buf; |
| 148 |
11720 |
i = deflate(vz, flush); |
| 149 |
11720 |
if (vz->avail_out != sizeof buf) |
| 150 |
9360 |
VSB_bcat(vout, buf, sizeof buf - vz->avail_out); |
| 151 |
11720 |
} while (i == Z_OK || vz->avail_in > 0); |
| 152 |
9000 |
vz->next_out = NULL; |
| 153 |
9000 |
vz->avail_out = 0; |
| 154 |
9000 |
vz->next_in = NULL; |
| 155 |
9000 |
AZ(vz->avail_in); |
| 156 |
9000 |
vz->avail_in = 0; |
| 157 |
9000 |
return (i); |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
static void |
| 161 |
6640 |
vtc_gzip(struct http *hp, const char *input, char **body, long *bodylen, int fragment) |
| 162 |
|
{ |
| 163 |
|
struct vsb *vout; |
| 164 |
|
int i, res; |
| 165 |
6640 |
size_t inlen = strlen(input); |
| 166 |
|
z_stream vz; |
| 167 |
|
|
| 168 |
6640 |
memset(&vz, 0, sizeof vz); |
| 169 |
6640 |
vout = VSB_new_auto(); |
| 170 |
6640 |
AN(vout); |
| 171 |
|
|
| 172 |
6640 |
assert(Z_OK == deflateInit2(&vz, |
| 173 |
|
hp->gziplevel, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY)); |
| 174 |
|
|
| 175 |
9000 |
while (fragment && inlen > 3) { |
| 176 |
2360 |
res = inlen / 3; |
| 177 |
2360 |
i = vtc_gzip_chunk(&vz, vout, input, res, Z_BLOCK); |
| 178 |
2360 |
if (i != Z_OK && i != Z_BUF_ERROR) { |
| 179 |
0 |
vtc_log(hp->vl, hp->fatal, |
| 180 |
|
"Gzip error = %d (%s) in:%jd out:%jd len:%zd", |
| 181 |
0 |
i, vz.msg, (intmax_t)vz.total_in, |
| 182 |
0 |
(intmax_t)vz.total_out, strlen(input)); |
| 183 |
0 |
} |
| 184 |
2360 |
input += res; |
| 185 |
2360 |
inlen -= res; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
6640 |
i = vtc_gzip_chunk(&vz, vout, input, inlen, Z_FINISH); |
| 189 |
6640 |
if (i != Z_STREAM_END) { |
| 190 |
0 |
vtc_log(hp->vl, hp->fatal, |
| 191 |
|
"Gzip error = %d (%s) in:%jd out:%jd len:%zd", |
| 192 |
0 |
i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out, |
| 193 |
0 |
strlen(input)); |
| 194 |
0 |
} |
| 195 |
6640 |
AZ(VSB_finish(vout)); |
| 196 |
|
#ifdef VGZ_EXTENSIONS |
| 197 |
6640 |
res = vz.stop_bit & 7; |
| 198 |
6640 |
vtc_report_gz_bits(hp->vl, &vz); |
| 199 |
|
#else |
| 200 |
|
res = 0; |
| 201 |
|
#endif |
| 202 |
6640 |
assert(Z_OK == deflateEnd(&vz)); |
| 203 |
|
|
| 204 |
|
#ifdef VGZ_EXTENSIONS |
| 205 |
6640 |
if (hp->gzipresidual >= 0 && hp->gzipresidual != res) |
| 206 |
0 |
vtc_log(hp->vl, hp->fatal, |
| 207 |
|
"Wrong gzip residual got %d wanted %d", |
| 208 |
0 |
res, hp->gzipresidual); |
| 209 |
|
#endif |
| 210 |
6640 |
*body = malloc(APOS(VSB_len(vout) + 1)); |
| 211 |
6640 |
AN(*body); |
| 212 |
6640 |
memcpy(*body, VSB_data(vout), APOS(VSB_len(vout) + 1)); |
| 213 |
6640 |
*bodylen = APOS(VSB_len(vout)); |
| 214 |
6640 |
VSB_destroy(&vout); |
| 215 |
6640 |
vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen); |
| 216 |
6640 |
vtc_dump(hp->vl, 4, "body", *body, *bodylen); |
| 217 |
6640 |
bprintf(hp->bodylen, "%ld", *bodylen); |
| 218 |
6640 |
} |
| 219 |
|
|
| 220 |
|
int |
| 221 |
7320 |
vtc_gzip_cmd(struct http *hp, char * const *av, char **body, long *bodylen) |
| 222 |
|
{ |
| 223 |
|
char *b; |
| 224 |
|
|
| 225 |
7320 |
AN(hp); |
| 226 |
7320 |
AN(av); |
| 227 |
7320 |
AN(body); |
| 228 |
7320 |
AN(bodylen); |
| 229 |
|
|
| 230 |
7320 |
if (!strcmp(*av, "-gzipresidual")) { |
| 231 |
320 |
hp->gzipresidual = strtoul(av[1], NULL, 0); |
| 232 |
320 |
return (1); |
| 233 |
|
} |
| 234 |
7000 |
if (!strcmp(*av, "-gziplevel")) { |
| 235 |
360 |
hp->gziplevel = strtoul(av[1], NULL, 0); |
| 236 |
360 |
return (1); |
| 237 |
|
} |
| 238 |
6640 |
if (!strcmp(*av, "-gzipbody")) { |
| 239 |
6440 |
if (*body != NULL) |
| 240 |
6360 |
free(*body); |
| 241 |
6440 |
*body = NULL; |
| 242 |
6440 |
vtc_gzip(hp, av[1], body, bodylen, 0); |
| 243 |
6440 |
AN(*body); |
| 244 |
6440 |
return (2); |
| 245 |
|
} |
| 246 |
200 |
if (!strcmp(*av, "-gziplen")) { |
| 247 |
200 |
if (*body != NULL) |
| 248 |
160 |
free(*body); |
| 249 |
200 |
*body = NULL; |
| 250 |
200 |
b = synth_body(av[1], 1); |
| 251 |
200 |
vtc_gzip(hp, b, body, bodylen, 1); |
| 252 |
200 |
AN(*body); |
| 253 |
200 |
free(b); |
| 254 |
200 |
return (2); |
| 255 |
|
} |
| 256 |
0 |
return (0); |
| 257 |
7320 |
} |