summaryrefslogtreecommitdiff
path: root/src/ne_compress.c
blob: b84b46de51e3f014188514942993cbe0cc61b25a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* 
   Handling of compressed HTTP responses
   Copyright (C) 2001-2002, Joe Orton <joe@manyfish.co.uk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA

*/

#include "config.h"

#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include "ne_request.h"
#include "ne_compress.h"
#include "ne_utils.h"

#ifdef NEON_ZLIB

#include <zlib.h>

/* Adds support for the 'gzip' Content-Encoding in HTTP.  gzip is a
 * file format which wraps the DEFLATE compression algorithm.  zlib
 * implements DEFLATE: we have to unwrap the gzip format (specified in
 * RFC1952) as it comes off the wire, and hand off chunks of data to
 * be inflated. */

struct ne_decompress_s {
    ne_session *session; /* associated session. */
    /* temporary buffer for holding inflated data. */
    char outbuf[BUFSIZ];
    z_stream zstr;
    int zstrinit; /* non-zero if zstr has been initialized */
    char *enchdr; /* value of Content-Enconding response header. */

    /* pass blocks back to this. */
    ne_block_reader reader;
    void *userdata;

    /* buffer for gzip header bytes. */
    union {
	unsigned char buf[10];
	struct header {
	    unsigned char id1;
	    unsigned char id2;
	    unsigned char cmeth; /* compression method. */
	    unsigned char flags;
	    unsigned int mtime; /* breaks when sizeof int != 4 */
	    unsigned char xflags;
	    unsigned char os;
	} hdr;
    } in;
    size_t incount;    /* bytes in in.buf */
    
    unsigned char footer[8];
    size_t footcount; /* bytes in footer. */

    /* CRC32 checksum: odd that zlib uses uLong for this since it is a
     * 64-bit integer on LP64 platforms. */
    uLong checksum;

    /* current state. */
    enum state {
	NE_Z_BEFORE_DATA, /* not received any response blocks yet. */
	NE_Z_PASSTHROUGH, /* response not compressed: passing through. */
	NE_Z_IN_HEADER, /* received a few bytes of response data, but not
			 * got past the gzip header yet. */
	NE_Z_POST_HEADER, /* waiting for the end of the NUL-terminated bits. */
	NE_Z_INFLATING, /* inflating response bytes. */
	NE_Z_AFTER_DATA, /* after data; reading CRC32 & ISIZE */
	NE_Z_FINISHED, /* stream is finished. */
	NE_Z_ERROR /* inflate bombed. */
    } state;
};

#define ID1 0x1f
#define ID2 0x8b

#define HDR_DONE 0
#define HDR_EXTENDED 1
#define HDR_ERROR 2

/* parse_header parses the gzip header, sets the next state and returns
 *   HDR_DONE: all done, bytes following are raw DEFLATE data.
 *   HDR_EXTENDED: all done, expect a NUL-termianted string
 *                 before the DEFLATE data
 *   HDR_ERROR: invalid header, give up.
 */
static int parse_header(ne_decompress *ctx)
{
    struct header *h = &ctx->in.hdr;

    NE_DEBUG(NE_DBG_HTTP, "ID1: %d  ID2: %d, cmeth %d, flags %d\n", 
	    h->id1, h->id2, h->cmeth, h->flags);
    
    if (h->id1 != ID1 || h->id2 != ID2 || h->cmeth != 8) {
	ctx->state = NE_Z_ERROR;
	ne_set_error(ctx->session, "Compressed stream invalid");
	return HDR_ERROR;
    }

    NE_DEBUG(NE_DBG_HTTP, "mtime: %d, xflags: %d, os: %d\n",
	     h->mtime, h->xflags, h->os);
    
    /* TODO: we can only handle one NUL-terminated extensions field
     * currently.  Really, we should count the number of bits set, and
     * skip as many fields as bits set (bailing if any reserved bits
     * are set. */
    if (h->flags == 8) {
	ctx->state = NE_Z_POST_HEADER;
	return HDR_EXTENDED;
    } else if (h->flags != 0) {
	ctx->state = NE_Z_ERROR;
	ne_set_error(ctx->session, "Compressed stream not supported");
	return HDR_ERROR;
    }

    NE_DEBUG(NE_DBG_HTTP, "compress: Good stream.\n");
    
    ctx->state = NE_Z_INFLATING;
    return HDR_DONE;
}

/* Convert 'buf' to unsigned int; 'buf' must be 'unsigned char *' */
#define BUF2UINT(buf) ((buf[3]<<24) + (buf[2]<<16) + (buf[1]<<8) + buf[0])

/* Process extra 'len' bytes of 'buf' which were received after the
 * DEFLATE data. */
static void process_footer(ne_decompress *ctx, 
			   const unsigned char *buf, size_t len)
{
    if (len + ctx->footcount > 8) {
        ne_set_error(ctx->session, 
                     "Too many bytes (%" NE_FMT_SIZE_T ") in gzip footer",
                     len);
	ctx->state = NE_Z_ERROR;
    } else {
	memcpy(ctx->footer + ctx->footcount, buf, len);
	ctx->footcount += len;
	if (ctx->footcount == 8) {
	    uLong crc = BUF2UINT(ctx->footer) & 0xFFFFFFFF;
	    if (crc == ctx->checksum) {
		ctx->state = NE_Z_FINISHED;
		NE_DEBUG(NE_DBG_HTTP, "compress: Checksum match.\n");
	    } else {
		NE_DEBUG(NE_DBG_HTTP, "compress: Checksum mismatch: "
			 "given %lu vs computed %lu\n", crc, ctx->checksum);
		ne_set_error(ctx->session, 
			     "Checksum invalid for compressed stream");
		ctx->state = NE_Z_ERROR;
	    }
	}
    }
}

/* Inflate response buffer 'buf' of length 'len'. */
static void do_inflate(ne_decompress *ctx, const char *buf, size_t len)
{
    int ret;

    ctx->zstr.avail_in = len;
    ctx->zstr.next_in = (unsigned char *)buf;
    ctx->zstr.total_in = 0;
    
    do {
	ctx->zstr.avail_out = sizeof ctx->outbuf;
	ctx->zstr.next_out = (unsigned char *)ctx->outbuf;
	ctx->zstr.total_out = 0;
	
	ret = inflate(&ctx->zstr, Z_NO_FLUSH);
	
	NE_DEBUG(NE_DBG_HTTP, 
		 "compress: inflate %d, %ld bytes out, %d remaining\n",
		 ret, ctx->zstr.total_out, ctx->zstr.avail_in);
#if 0
	NE_DEBUG(NE_DBG_HTTPBODY,
		 "Inflated body block (%ld):\n[%.*s]\n", 
		 ctx->zstr.total_out, (int)ctx->zstr.total_out, 
		 ctx->outbuf);
#endif
	/* update checksum. */
	ctx->checksum = crc32(ctx->checksum, (unsigned char *)ctx->outbuf, 
			      ctx->zstr.total_out);

	/* pass on the inflated data */
	ctx->reader(ctx->userdata, ctx->outbuf, ctx->zstr.total_out);
	
    } while (ret == Z_OK && ctx->zstr.avail_in > 0);
    
    if (ret == Z_STREAM_END) {
	NE_DEBUG(NE_DBG_HTTP, "compress: end of data stream, remaining %d.\n",
		 ctx->zstr.avail_in);
	/* process the footer. */
	ctx->state = NE_Z_AFTER_DATA;
	process_footer(ctx, ctx->zstr.next_in, ctx->zstr.avail_in);
    } else if (ret != Z_OK) {
	ctx->state = NE_Z_ERROR;
	ne_set_error(ctx->session, "Error reading compressed data.");
	NE_DEBUG(NE_DBG_HTTP, "compress: inflate failed (%d): %s\n", 
		 ret, ctx->zstr.msg?ctx->zstr.msg:"(no message)");
    }
}

/* Callback which is passed blocks of the response body. */
static void gz_reader(void *ud, const char *buf, size_t len)
{
    ne_decompress *ctx = ud;
    const char *zbuf;
    size_t count;

    switch (ctx->state) {
    case NE_Z_PASSTHROUGH:
	/* move along there. */
	ctx->reader(ctx->userdata, buf, len);
	return;

    case NE_Z_ERROR:
	/* beyond hope. */
	break;

    case NE_Z_FINISHED:
	/* Could argue for tolerance, and ignoring trailing content;
	 * but it could mean something more serious. */
	if (len > 0) {
	    ctx->state = NE_Z_ERROR;
	    ne_set_error(ctx->session,
			 "Unexpected content received after compressed stream");
	}
	break;

    case NE_Z_BEFORE_DATA:
	/* work out whether this is a compressed response or not. */
	if (ctx->enchdr && strcasecmp(ctx->enchdr, "gzip") == 0) {
	    NE_DEBUG(NE_DBG_HTTP, "compress: got gzipped stream.\n");

	    /* This is the magic bit: using plain inflateInit()
	     * doesn't work, and this does, but I have no idea why..
	     * Google showed me the way. */
	    if (inflateInit2(&ctx->zstr, -MAX_WBITS) != Z_OK) {
		ne_set_error(ctx->session, ctx->zstr.msg);
		ctx->state = NE_Z_ERROR;
		return;
	    }
	    ctx->zstrinit = 1;

	} else {
	    /* No Content-Encoding header: pass it on.  TODO: we could
	     * hack it and register the real callback now. But that
	     * would require add_resp_body_rdr to have defined
	     * ordering semantics etc etc */
	    ctx->state = NE_Z_PASSTHROUGH;
	    ctx->reader(ctx->userdata, buf, len);
	    return;
	}

	ctx->state = NE_Z_IN_HEADER;
	/* FALLTHROUGH */

    case NE_Z_IN_HEADER:
	/* copy as many bytes as possible into the buffer. */
	if (len + ctx->incount > 10) {
	    count = 10 - ctx->incount;
	} else {
	    count = len;
	}
	memcpy(ctx->in.buf + ctx->incount, buf, count);
	ctx->incount += count;
	/* have we got the full header yet? */
	if (ctx->incount != 10) {
	    return;
	}

	buf += count;
	len -= count;

	switch (parse_header(ctx)) {
	case HDR_ERROR:
	    return;
	case HDR_EXTENDED:
	    if (len == 0)
		return;
	    break;
	case HDR_DONE:
	    if (len > 0) {
		do_inflate(ctx, buf, len);
	    }
	    return;
	}

	/* FALLTHROUGH */

    case NE_Z_POST_HEADER:
	/* eating the filename string. */
	zbuf = memchr(buf, '\0', len);
	if (zbuf == NULL) {
	    /* not found it yet. */
	    return;
	}

	NE_DEBUG(NE_DBG_HTTP,
		 "compresss: skipped %" NE_FMT_SIZE_T " header bytes.\n", 
		 zbuf - buf);
	/* found end of string. */
	len -= (1 + zbuf - buf);
	buf = zbuf + 1;
	ctx->state = NE_Z_INFLATING;
	if (len == 0) {
	    /* end of string was at end of buffer. */
	    return;
	}

	/* FALLTHROUGH */

    case NE_Z_INFLATING:
	do_inflate(ctx, buf, len);
	break;

    case NE_Z_AFTER_DATA:
	process_footer(ctx, (unsigned char *)buf, len);
	break;
    }

}

int ne_decompress_destroy(ne_decompress *ctx)
{
    int ret;

    if (ctx->zstrinit)
	/* inflateEnd only fails if it's passed NULL etc; ignore
	 * return value. */
	inflateEnd(&ctx->zstr);

    if (ctx->enchdr)
	ne_free(ctx->enchdr);

    switch (ctx->state) {
    case NE_Z_BEFORE_DATA:
    case NE_Z_PASSTHROUGH:
    case NE_Z_FINISHED:
	ret = NE_OK;
	break;
    case NE_Z_ERROR:
	/* session error already set. */
	ret = NE_ERROR;
	break;
    default:
	/* truncated response. */
	ne_set_error(ctx->session, "Compressed response was truncated");
	ret = NE_ERROR;
	break;
    }

    ne_free(ctx);
    return ret;
}

ne_decompress *ne_decompress_reader(ne_request *req, ne_accept_response acpt,
				    ne_block_reader rdr, void *userdata)
{
    ne_decompress *ctx = ne_calloc(sizeof *ctx);

    ne_add_request_header(req, "Accept-Encoding", "gzip");

    ne_add_response_header_handler(req, "Content-Encoding", 
				   ne_duplicate_header, &ctx->enchdr);

    ne_add_response_body_reader(req, acpt, gz_reader, ctx);

    ctx->state = NE_Z_BEFORE_DATA;
    ctx->reader = rdr;
    ctx->userdata = userdata;
    ctx->session = ne_get_session(req);
    /* initialize the checksum. */
    ctx->checksum = crc32(0L, Z_NULL, 0);

    return ctx;    
}

#else /* !NEON_ZLIB */

/* Pass-through interface present to provide ABI compatibility. */

ne_decompress *ne_decompress_reader(ne_request *req, ne_accept_response acpt,
				    ne_block_reader rdr, void *userdata)
{
    ne_add_response_body_reader(req, acpt, rdr, userdata);
    /* an arbitrary return value: don't confuse them by returning NULL. */
    return (ne_decompress *)req;
}

int ne_decompress_destroy(ne_decompress *dc)
{
    return 0;
}

#endif /* NEON_ZLIB */