varnish-cache/bin/varnishtest/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
#include "vgz.h"
39
40
#ifdef VGZ_EXTENSIONS
41
static void
42 8160
vtc_report_gz_bits(struct vtclog *vl, const z_stream *vz)
43
{
44 16320
        vtc_log(vl, 4, "startbit = %ju %ju/%ju",
45 8160
            (uintmax_t)vz->start_bit,
46 8160
            (uintmax_t)vz->start_bit >> 3, (uintmax_t)vz->start_bit & 7);
47 16320
        vtc_log(vl, 4, "lastbit = %ju %ju/%ju",
48 8160
            (uintmax_t)vz->last_bit,
49 8160
            (uintmax_t)vz->last_bit >> 3, (uintmax_t)vz->last_bit & 7);
50 16320
        vtc_log(vl, 4, "stopbit = %ju %ju/%ju",
51 8160
            (uintmax_t)vz->stop_bit,
52 8160
            (uintmax_t)vz->stop_bit >> 3, (uintmax_t)vz->stop_bit & 7);
53 8160
}
54
#endif
55
56
static size_t
57 32560
APOS(ssize_t sz)
58
{
59 32560
        assert(sz >= 0);
60 32560
        return (sz);
61
}
62
63
/**********************************************************************
64
 * GUNZIPery
65
 */
66
67
static struct vsb *
68 1560
vtc_gunzip_vsb(struct vtclog *vl, int fatal, const struct vsb *vin)
69
{
70
        z_stream vz;
71
        struct vsb *vout;
72
        int i;
73
        char buf[BUFSIZ];
74
75 1560
        memset(&vz, 0, sizeof vz);
76 1560
        vout = VSB_new_auto();
77 1560
        AN(vout);
78
79 1560
        vz.next_in = (void*)VSB_data(vin);
80 1560
        vz.avail_in = APOS(VSB_len(vin));
81
82 1560
        assert(Z_OK == inflateInit2(&vz, 31));
83
84 1560
        do {
85 1960
                vz.next_out = (void*)buf;
86 1960
                vz.avail_out = sizeof buf;
87 1960
                i = inflate(&vz, Z_FINISH);
88 1960
                if (vz.avail_out != sizeof buf)
89 1960
                        VSB_bcat(vout, buf, sizeof buf - vz.avail_out);
90 1960
        } while (i == Z_OK || i == Z_BUF_ERROR);
91 1560
        if (i != Z_STREAM_END)
92 0
                vtc_log(vl, fatal,
93
                    "Gunzip error = %d (%s) in:%jd out:%jd len:%zd",
94 0
                    i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out,
95 0
                    VSB_len(vin));
96 1560
        AZ(VSB_finish(vout));
97
#ifdef VGZ_EXTENSIONS
98 1560
        vtc_report_gz_bits(vl, &vz);
99
#endif
100 1560
        assert(Z_OK == inflateEnd(&vz));
101 1560
        return (vout);
102
}
103
104
void
105 1560
vtc_gunzip(struct http *hp, char *body, long *bodylen)
106
{
107
        struct vsb *vin, *vout;
108
109 1560
        AN(body);
110 1560
        if (body[0] != (char)0x1f || body[1] != (char)0x8b)
111 0
                vtc_log(hp->vl, hp->fatal,
112
                    "Gunzip error: body lacks gzip magic");
113
114 1560
        vin = VSB_new_auto();
115 1560
        AN(vin);
116 1560
        VSB_bcat(vin, body, *bodylen);
117 1560
        AZ(VSB_finish(vin));
118 1560
        vout = vtc_gunzip_vsb(hp->vl, hp->fatal, vin);
119 1560
        VSB_destroy(&vin);
120
121 1560
        memcpy(body, VSB_data(vout), APOS(VSB_len(vout) + 1));
122 1560
        *bodylen = APOS(VSB_len(vout));
123 1560
        VSB_destroy(&vout);
124 1560
        vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen);
125 1560
        vtc_dump(hp->vl, 4, "body", body, *bodylen);
126 1560
        bprintf(hp->bodylen, "%ld", *bodylen);
127 1560
}
128
129
/**********************************************************************
130
 * GZIPery
131
 */
132
133
static int
134 8080
vtc_gzip_chunk(z_stream *vz, struct vsb *vout, const char *in, size_t inlen, int flush)
135
{
136
        int i;
137
        char buf[BUFSIZ];
138
139 8080
        vz->next_in = TRUST_ME(in);
140 8080
        vz->avail_in = APOS(inlen);
141 8080
        do {
142 9600
                vz->next_out = (void*)buf;
143 9600
                vz->avail_out = sizeof buf;
144 9600
                i = deflate(vz, flush);
145 9600
                if (vz->avail_out != sizeof buf)
146 8120
                        VSB_bcat(vout, buf, sizeof buf - vz->avail_out);
147 9600
        } while (i == Z_OK || vz->avail_in > 0);
148 8080
        vz->next_out = NULL;
149 8080
        vz->avail_out = 0;
150 8080
        vz->next_in = NULL;
151 8080
        AZ(vz->avail_in);
152 8080
        vz->avail_in = 0;
153 8080
        return (i);
154
}
155
156
static void
157 6600
vtc_gzip(struct http *hp, const char *input, char **body, long *bodylen, int fragment)
158
{
159
        struct vsb *vout;
160
        int i, res;
161 6600
        size_t inlen = strlen(input);
162
        z_stream vz;
163
164 6600
        memset(&vz, 0, sizeof vz);
165 6600
        vout = VSB_new_auto();
166 6600
        AN(vout);
167
168 6600
        assert(Z_OK == deflateInit2(&vz,
169
            hp->gziplevel, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY));
170
171 8080
        while (fragment && inlen > 3) {
172 1480
                res = inlen / 3;
173 1480
                i = vtc_gzip_chunk(&vz, vout, input, res, Z_BLOCK);
174 1480
                if (i != Z_OK && i != Z_BUF_ERROR) {
175 0
                        vtc_log(hp->vl, hp->fatal,
176
                            "Gzip error = %d (%s) in:%jd out:%jd len:%zd",
177 0
                            i, vz.msg, (intmax_t)vz.total_in,
178 0
                            (intmax_t)vz.total_out, strlen(input));
179 0
                }
180 1480
                input += res;
181 1480
                inlen -= res;
182
        }
183
184 6600
        i = vtc_gzip_chunk(&vz, vout, input, inlen, Z_FINISH);
185 6600
        if (i != Z_STREAM_END) {
186 0
                vtc_log(hp->vl, hp->fatal,
187
                    "Gzip error = %d (%s) in:%jd out:%jd len:%zd",
188 0
                    i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out,
189 0
                    strlen(input));
190 0
        }
191 6600
        AZ(VSB_finish(vout));
192
#ifdef VGZ_EXTENSIONS
193 6600
        res = vz.stop_bit & 7;
194 6600
        vtc_report_gz_bits(hp->vl, &vz);
195
#else
196
        res = 0;
197
#endif
198 6600
        assert(Z_OK == deflateEnd(&vz));
199
200
#ifdef VGZ_EXTENSIONS
201 6600
        if (hp->gzipresidual >= 0 && hp->gzipresidual != res)
202 0
                vtc_log(hp->vl, hp->fatal,
203
                    "Wrong gzip residual got %d wanted %d",
204 0
                    res, hp->gzipresidual);
205
#endif
206 6600
        *body = malloc(APOS(VSB_len(vout) + 1));
207 6600
        AN(*body);
208 6600
        memcpy(*body, VSB_data(vout), APOS(VSB_len(vout) + 1));
209 6600
        *bodylen = APOS(VSB_len(vout));
210 6600
        VSB_destroy(&vout);
211 6600
        vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen);
212 6600
        vtc_dump(hp->vl, 4, "body", *body, *bodylen);
213 6600
        bprintf(hp->bodylen, "%ld", *bodylen);
214 6600
}
215
216
int
217 7240
vtc_gzip_cmd(struct http *hp, char * const *av, char **body, long *bodylen)
218
{
219
        char *b;
220
221 7240
        AN(hp);
222 7240
        AN(av);
223 7240
        AN(body);
224 7240
        AN(bodylen);
225
226 7240
        if (!strcmp(*av, "-gzipresidual")) {
227 320
                hp->gzipresidual = strtoul(av[1], NULL, 0);
228 320
                return (1);
229
        }
230 6920
        if (!strcmp(*av, "-gziplevel")) {
231 320
                hp->gziplevel = strtoul(av[1], NULL, 0);
232 320
                return (1);
233
        }
234 6600
        if (!strcmp(*av, "-gzipbody")) {
235 6440
                if (*body != NULL)
236 6360
                        free(*body);
237 6440
                *body = NULL;
238 6440
                vtc_gzip(hp, av[1], body, bodylen, 0);
239 6440
                AN(*body);
240 6440
                return (2);
241
        }
242 160
        if (!strcmp(*av, "-gziplen")) {
243 160
                if (*body != NULL)
244 120
                        free(*body);
245 160
                *body = NULL;
246 160
                b = synth_body(av[1], 1);
247 160
                vtc_gzip(hp, b, body, bodylen, 1);
248 160
                AN(*body);
249 160
                free(b);
250 160
                return (2);
251
        }
252 0
        return (0);
253 7240
}