| | 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 |
204 |
vtc_report_gz_bits(struct vtclog *vl, const z_stream *vz) |
47 |
|
{ |
48 |
408 |
vtc_log(vl, 4, "startbit = %ju %ju/%ju", |
49 |
204 |
(uintmax_t)vz->start_bit, |
50 |
204 |
(uintmax_t)vz->start_bit >> 3, (uintmax_t)vz->start_bit & 7); |
51 |
408 |
vtc_log(vl, 4, "lastbit = %ju %ju/%ju", |
52 |
204 |
(uintmax_t)vz->last_bit, |
53 |
204 |
(uintmax_t)vz->last_bit >> 3, (uintmax_t)vz->last_bit & 7); |
54 |
408 |
vtc_log(vl, 4, "stopbit = %ju %ju/%ju", |
55 |
204 |
(uintmax_t)vz->stop_bit, |
56 |
204 |
(uintmax_t)vz->stop_bit >> 3, (uintmax_t)vz->stop_bit & 7); |
57 |
204 |
} |
58 |
|
#endif |
59 |
|
|
60 |
|
static size_t |
61 |
814 |
APOS(ssize_t sz) |
62 |
|
{ |
63 |
814 |
assert(sz >= 0); |
64 |
814 |
return (sz); |
65 |
|
} |
66 |
|
|
67 |
|
/********************************************************************** |
68 |
|
* GUNZIPery |
69 |
|
*/ |
70 |
|
|
71 |
|
static struct vsb * |
72 |
39 |
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 |
39 |
memset(&vz, 0, sizeof vz); |
80 |
39 |
vout = VSB_new_auto(); |
81 |
39 |
AN(vout); |
82 |
|
|
83 |
39 |
vz.next_in = (void*)VSB_data(vin); |
84 |
39 |
vz.avail_in = APOS(VSB_len(vin)); |
85 |
|
|
86 |
39 |
assert(Z_OK == inflateInit2(&vz, 31)); |
87 |
|
|
88 |
39 |
do { |
89 |
49 |
vz.next_out = (void*)buf; |
90 |
49 |
vz.avail_out = sizeof buf; |
91 |
49 |
i = inflate(&vz, Z_FINISH); |
92 |
49 |
if (vz.avail_out != sizeof buf) |
93 |
49 |
VSB_bcat(vout, buf, sizeof buf - vz.avail_out); |
94 |
49 |
} while (i == Z_OK || i == Z_BUF_ERROR); |
95 |
39 |
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 |
39 |
AZ(VSB_finish(vout)); |
101 |
|
#ifdef VGZ_EXTENSIONS |
102 |
39 |
vtc_report_gz_bits(vl, &vz); |
103 |
|
#endif |
104 |
39 |
assert(Z_OK == inflateEnd(&vz)); |
105 |
39 |
return (vout); |
106 |
|
} |
107 |
|
|
108 |
|
void |
109 |
39 |
vtc_gunzip(struct http *hp, char *body, long *bodylen) |
110 |
|
{ |
111 |
|
struct vsb *vin, *vout; |
112 |
|
|
113 |
39 |
AN(body); |
114 |
39 |
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 |
39 |
vin = VSB_new_auto(); |
119 |
39 |
AN(vin); |
120 |
39 |
VSB_bcat(vin, body, *bodylen); |
121 |
39 |
AZ(VSB_finish(vin)); |
122 |
39 |
vout = vtc_gunzip_vsb(hp->vl, hp->fatal, vin); |
123 |
39 |
VSB_destroy(&vin); |
124 |
|
|
125 |
39 |
memcpy(body, VSB_data(vout), APOS(VSB_len(vout) + 1)); |
126 |
39 |
*bodylen = APOS(VSB_len(vout)); |
127 |
39 |
VSB_destroy(&vout); |
128 |
39 |
vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen); |
129 |
39 |
vtc_dump(hp->vl, 4, "body", body, *bodylen); |
130 |
39 |
bprintf(hp->bodylen, "%ld", *bodylen); |
131 |
39 |
} |
132 |
|
|
133 |
|
/********************************************************************** |
134 |
|
* GZIPery |
135 |
|
*/ |
136 |
|
|
137 |
|
static int |
138 |
202 |
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 |
202 |
vz->next_in = TRUST_ME(in); |
144 |
202 |
vz->avail_in = APOS(inlen); |
145 |
202 |
do { |
146 |
240 |
vz->next_out = (void*)buf; |
147 |
240 |
vz->avail_out = sizeof buf; |
148 |
240 |
i = deflate(vz, flush); |
149 |
240 |
if (vz->avail_out != sizeof buf) |
150 |
203 |
VSB_bcat(vout, buf, sizeof buf - vz->avail_out); |
151 |
240 |
} while (i == Z_OK || vz->avail_in > 0); |
152 |
202 |
vz->next_out = NULL; |
153 |
202 |
vz->avail_out = 0; |
154 |
202 |
vz->next_in = NULL; |
155 |
202 |
AZ(vz->avail_in); |
156 |
202 |
vz->avail_in = 0; |
157 |
202 |
return (i); |
158 |
|
} |
159 |
|
|
160 |
|
static void |
161 |
165 |
vtc_gzip(struct http *hp, const char *input, char **body, long *bodylen, int fragment) |
162 |
|
{ |
163 |
|
struct vsb *vout; |
164 |
|
int i, res; |
165 |
165 |
size_t inlen = strlen(input); |
166 |
|
z_stream vz; |
167 |
|
|
168 |
165 |
memset(&vz, 0, sizeof vz); |
169 |
165 |
vout = VSB_new_auto(); |
170 |
165 |
AN(vout); |
171 |
|
|
172 |
165 |
assert(Z_OK == deflateInit2(&vz, |
173 |
|
hp->gziplevel, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY)); |
174 |
|
|
175 |
202 |
while (fragment && inlen > 3) { |
176 |
37 |
res = inlen / 3; |
177 |
37 |
i = vtc_gzip_chunk(&vz, vout, input, res, Z_BLOCK); |
178 |
37 |
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 |
37 |
input += res; |
185 |
37 |
inlen -= res; |
186 |
|
} |
187 |
|
|
188 |
165 |
i = vtc_gzip_chunk(&vz, vout, input, inlen, Z_FINISH); |
189 |
165 |
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 |
165 |
AZ(VSB_finish(vout)); |
196 |
|
#ifdef VGZ_EXTENSIONS |
197 |
165 |
res = vz.stop_bit & 7; |
198 |
165 |
vtc_report_gz_bits(hp->vl, &vz); |
199 |
|
#else |
200 |
|
res = 0; |
201 |
|
#endif |
202 |
165 |
assert(Z_OK == deflateEnd(&vz)); |
203 |
|
|
204 |
|
#ifdef VGZ_EXTENSIONS |
205 |
165 |
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 |
165 |
*body = malloc(APOS(VSB_len(vout) + 1)); |
211 |
165 |
AN(*body); |
212 |
165 |
memcpy(*body, VSB_data(vout), APOS(VSB_len(vout) + 1)); |
213 |
165 |
*bodylen = APOS(VSB_len(vout)); |
214 |
165 |
VSB_destroy(&vout); |
215 |
165 |
vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen); |
216 |
165 |
vtc_dump(hp->vl, 4, "body", *body, *bodylen); |
217 |
165 |
bprintf(hp->bodylen, "%ld", *bodylen); |
218 |
165 |
} |
219 |
|
|
220 |
|
int |
221 |
182 |
vtc_gzip_cmd(struct http *hp, char * const *av, char **body, long *bodylen) |
222 |
|
{ |
223 |
|
char *b; |
224 |
|
|
225 |
182 |
AN(hp); |
226 |
182 |
AN(av); |
227 |
182 |
AN(body); |
228 |
182 |
AN(bodylen); |
229 |
|
|
230 |
182 |
if (!strcmp(*av, "-gzipresidual")) { |
231 |
8 |
hp->gzipresidual = strtoul(av[1], NULL, 0); |
232 |
8 |
return (1); |
233 |
|
} |
234 |
174 |
if (!strcmp(*av, "-gziplevel")) { |
235 |
9 |
hp->gziplevel = strtoul(av[1], NULL, 0); |
236 |
9 |
return (1); |
237 |
|
} |
238 |
165 |
if (!strcmp(*av, "-gzipbody")) { |
239 |
161 |
if (*body != NULL) |
240 |
159 |
free(*body); |
241 |
161 |
*body = NULL; |
242 |
161 |
vtc_gzip(hp, av[1], body, bodylen, 0); |
243 |
161 |
AN(*body); |
244 |
161 |
return (2); |
245 |
|
} |
246 |
4 |
if (!strcmp(*av, "-gziplen")) { |
247 |
4 |
if (*body != NULL) |
248 |
3 |
free(*body); |
249 |
4 |
*body = NULL; |
250 |
4 |
b = synth_body(av[1], 1); |
251 |
4 |
vtc_gzip(hp, b, body, bodylen, 1); |
252 |
4 |
AN(*body); |
253 |
4 |
free(b); |
254 |
4 |
return (2); |
255 |
|
} |
256 |
0 |
return (0); |
257 |
182 |
} |