c0d1140d4d452f5e0d304caea652959836709c1d
[m6w6/ext-http] / http_encoding_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #define HTTP_WANT_ZLIB
20 #include "php_http.h"
21
22 #include "php_http_api.h"
23 #include "php_http_encoding_api.h"
24 #include "php_http_send_api.h"
25 #include "php_http_headers_api.h"
26
27 #ifdef HTTP_HAVE_ZLIB
28 PHP_MINIT_FUNCTION(http_encoding)
29 {
30 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF);
31 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN);
32 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX);
33 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB);
34 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP);
35 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_RAW", HTTP_DEFLATE_TYPE_RAW);
36 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_DEF", HTTP_DEFLATE_STRATEGY_DEF);
37 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FILT", HTTP_DEFLATE_STRATEGY_FILT);
38 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_HUFF", HTTP_DEFLATE_STRATEGY_HUFF);
39 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_RLE", HTTP_DEFLATE_STRATEGY_RLE);
40 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FIXED", HTTP_DEFLATE_STRATEGY_FIXED);
41 return SUCCESS;
42 }
43
44 PHP_RINIT_FUNCTION(http_encoding)
45 {
46 getGlobals(G);
47
48 if (G->send.inflate.start_auto) {
49 php_ob_set_internal_handler(_http_ob_inflatehandler, 0x1000, "http inflate", 0 TSRMLS_CC);
50 }
51 if (G->send.deflate.start_auto) {
52 php_ob_set_internal_handler(_http_ob_deflatehandler, 0x8000, "http deflate", 0 TSRMLS_CC);
53 }
54 return SUCCESS;
55 }
56
57 PHP_RSHUTDOWN_FUNCTION(http_encoding)
58 {
59 getGlobals(G);
60
61 if (G->send.deflate.stream) {
62 http_encoding_deflate_stream_free((http_encoding_stream **) &G->send.deflate.stream);
63 }
64 if (G->send.inflate.stream) {
65 http_encoding_inflate_stream_free((http_encoding_stream **) &G->send.inflate.stream);
66 }
67 return SUCCESS;
68 }
69 #endif
70
71 static inline int eol_match(char **line, int *eol_len)
72 {
73 char *ptr = *line;
74
75 while (0x20 == *ptr) ++ptr;
76
77 if (ptr == http_locate_eol(*line, eol_len)) {
78 *line = ptr;
79 return 1;
80 } else {
81 return 0;
82 }
83 }
84
85 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
86 PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
87 {
88 int eol_len = 0;
89 char *n_ptr = NULL;
90 const char *e_ptr = encoded;
91
92 *decoded_len = 0;
93 *decoded = ecalloc(1, encoded_len);
94
95 while ((encoded + encoded_len - e_ptr) > 0) {
96 ulong chunk_len = 0, rest;
97
98 chunk_len = strtoul(e_ptr, &n_ptr, 16);
99
100 /* we could not read in chunk size */
101 if (n_ptr == e_ptr) {
102 /*
103 * if this is the first turn and there doesn't seem to be a chunk
104 * size at the begining of the body, do not fail on apparently
105 * not encoded data and return a copy
106 */
107 if (e_ptr == encoded) {
108 http_error(HE_NOTICE, HTTP_E_ENCODING, "Data does not seem to be chunked encoded");
109 memcpy(*decoded, encoded, encoded_len);
110 *decoded_len = encoded_len;
111 return encoded + encoded_len;
112 } else {
113 efree(*decoded);
114 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Expected chunk size at pos %tu of %zu but got trash", n_ptr - encoded, encoded_len);
115 return NULL;
116 }
117 }
118
119 /* reached the end */
120 if (!chunk_len) {
121 /* move over '0' chunked encoding terminator */
122 while (*e_ptr == '0') ++e_ptr;
123 break;
124 }
125
126 /* there should be CRLF after the chunk size, but we'll ignore SP+ too */
127 if (*n_ptr && !eol_match(&n_ptr, &eol_len)) {
128 if (eol_len == 2) {
129 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Expected CRLF at pos %tu of %zu but got 0x%02X 0x%02X", n_ptr - encoded, encoded_len, *n_ptr, *(n_ptr + 1));
130 } else {
131 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Expected LF at pos %tu of %zu but got 0x%02X", n_ptr - encoded, encoded_len, *n_ptr);
132 }
133 }
134 n_ptr += eol_len;
135
136 /* chunk size pretends more data than we actually got, so it's probably a truncated message */
137 if (chunk_len > (rest = encoded + encoded_len - n_ptr)) {
138 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Truncated message: chunk size %lu exceeds remaining data size %lu at pos %tu of %zu", chunk_len, rest, n_ptr - encoded, encoded_len);
139 chunk_len = rest;
140 }
141
142 /* copy the chunk */
143 memcpy(*decoded + *decoded_len, n_ptr, chunk_len);
144 *decoded_len += chunk_len;
145
146 if (chunk_len == rest) {
147 e_ptr = n_ptr + chunk_len;
148 break;
149 } else {
150 /* advance to next chunk */
151 e_ptr = n_ptr + chunk_len + eol_len;
152 }
153 }
154
155 return e_ptr;
156 }
157 /* }}} */
158
159 #ifdef HTTP_HAVE_ZLIB
160
161 #define HTTP_DEFLATE_LEVEL_SET(flags, level) \
162 switch (flags & 0xf) \
163 { \
164 default: \
165 if ((flags & 0xf) < 10) { \
166 level = flags & 0xf; \
167 break; \
168 } \
169 case HTTP_DEFLATE_LEVEL_DEF: \
170 level = Z_DEFAULT_COMPRESSION; \
171 break; \
172 }
173
174 #define HTTP_DEFLATE_WBITS_SET(flags, wbits) \
175 switch (flags & 0xf0) \
176 { \
177 case HTTP_DEFLATE_TYPE_GZIP: \
178 wbits = HTTP_WINDOW_BITS_GZIP; \
179 break; \
180 case HTTP_DEFLATE_TYPE_RAW: \
181 wbits = HTTP_WINDOW_BITS_RAW; \
182 break; \
183 default: \
184 wbits = HTTP_WINDOW_BITS_ZLIB; \
185 break; \
186 }
187
188 #define HTTP_INFLATE_WBITS_SET(flags, wbits) \
189 if (flags & HTTP_INFLATE_TYPE_RAW) { \
190 wbits = HTTP_WINDOW_BITS_RAW; \
191 } else { \
192 wbits = HTTP_WINDOW_BITS_ANY; \
193 }
194
195 #define HTTP_DEFLATE_STRATEGY_SET(flags, strategy) \
196 switch (flags & 0xf00) \
197 { \
198 case HTTP_DEFLATE_STRATEGY_FILT: \
199 strategy = Z_FILTERED; \
200 break; \
201 case HTTP_DEFLATE_STRATEGY_HUFF: \
202 strategy = Z_HUFFMAN_ONLY; \
203 break; \
204 case HTTP_DEFLATE_STRATEGY_RLE: \
205 strategy = Z_RLE; \
206 break; \
207 case HTTP_DEFLATE_STRATEGY_FIXED: \
208 strategy = Z_FIXED; \
209 break; \
210 default: \
211 strategy = Z_DEFAULT_STRATEGY; \
212 break; \
213 }
214
215 #define HTTP_WINDOW_BITS_ZLIB 0x0000000f
216 #define HTTP_WINDOW_BITS_GZIP 0x0000001f
217 #define HTTP_WINDOW_BITS_ANY 0x0000002f
218 #define HTTP_WINDOW_BITS_RAW -0x000000f
219
220 PHP_HTTP_API STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
221 {
222 int status, level, wbits, strategy;
223 z_stream Z;
224
225 HTTP_DEFLATE_LEVEL_SET(flags, level);
226 HTTP_DEFLATE_WBITS_SET(flags, wbits);
227 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
228
229 memset(&Z, 0, sizeof(z_stream));
230 *encoded = NULL;
231 *encoded_len = 0;
232
233 status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
234 if (Z_OK == status) {
235 *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
236 *encoded = emalloc_rel(*encoded_len);
237
238 Z.next_in = (Bytef *) data;
239 Z.next_out = (Bytef *) *encoded;
240 Z.avail_in = data_len;
241 Z.avail_out = *encoded_len;
242
243 status = deflate(&Z, Z_FINISH);
244 deflateEnd(&Z);
245
246 if (Z_STREAM_END == status) {
247 /* size buffer down to actual length */
248 *encoded = erealloc_rel(*encoded, Z.total_out + 1);
249 (*encoded)[*encoded_len = Z.total_out] = '\0';
250 return SUCCESS;
251 } else {
252 STR_SET(*encoded, NULL);
253 *encoded_len = 0;
254 }
255 }
256
257 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s (%s)", zError(status));
258 return FAILURE;
259 }
260
261 PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
262 {
263 int status, max = 0, wbits = HTTP_WINDOW_BITS_ANY;
264 z_stream Z;
265 phpstr buffer;
266
267 memset(&Z, 0, sizeof(z_stream));
268 *decoded = NULL;
269 *decoded_len = 0;
270
271 phpstr_init_ex(&buffer, data_len << 2, PHPSTR_INIT_PREALLOC);
272 buffer.size = data_len;
273
274 retry_inflate:
275 status = inflateInit2(&Z, wbits);
276 if (Z_OK == status) {
277 Z.next_in = (Bytef *) data;
278 Z.avail_in = data_len;
279
280 do {
281 phpstr_resize(&buffer, data_len << 2);
282
283 do {
284 Z.avail_out = (buffer.free -= Z.total_out - buffer.used);
285 Z.next_out = (Bytef *) buffer.data + (buffer.used = Z.total_out);
286 status = inflate(&Z, Z_NO_FLUSH);
287 } while (Z_OK == status);
288 } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
289
290 if (Z_DATA_ERROR == status && HTTP_WINDOW_BITS_ANY == wbits) {
291 /* raw deflated data? */
292 inflateEnd(&Z);
293 wbits = HTTP_WINDOW_BITS_RAW;
294 goto retry_inflate;
295 }
296
297 inflateEnd(&Z);
298
299 if (Z_STREAM_END == status) {
300 *decoded_len = Z.total_out;
301 *decoded = erealloc_rel(buffer.data, *decoded_len + 1);
302 (*decoded)[*decoded_len] = '\0';
303 return SUCCESS;
304 } else {
305 phpstr_dtor(&buffer);
306 }
307 }
308
309 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
310 return FAILURE;
311 }
312
313
314 PHP_HTTP_API http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
315 {
316 int status, level, wbits, strategy, free_stream;
317
318 if ((free_stream = !s)) {
319 s = pemalloc_rel(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
320 }
321 memset(s, 0, sizeof(http_encoding_stream));
322 s->flags = flags;
323
324 HTTP_DEFLATE_LEVEL_SET(flags, level);
325 HTTP_DEFLATE_WBITS_SET(flags, wbits);
326 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
327
328 if (Z_OK == (status = deflateInit2(&s->stream, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
329 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
330
331 if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
332 return s;
333 }
334 deflateEnd(&s->stream);
335 status = Z_MEM_ERROR;
336 }
337
338 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize deflate encoding stream: %s", zError(status));
339 if (free_stream) {
340 efree(s);
341 }
342 return NULL;
343 }
344
345 PHP_HTTP_API http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
346 {
347 int status, wbits, free_stream;
348
349 if ((free_stream = !s)) {
350 s = pemalloc_rel(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
351 }
352 memset(s, 0, sizeof(http_encoding_stream));
353 s->flags = flags;
354
355 HTTP_INFLATE_WBITS_SET(flags, wbits);
356
357 if (Z_OK == (status = inflateInit2(&s->stream, wbits))) {
358 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
359
360 if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
361 return s;
362 }
363 inflateEnd(&s->stream);
364 status = Z_MEM_ERROR;
365 }
366
367 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize inflate stream: %s", zError(status));
368 if (free_stream) {
369 efree(s);
370 }
371 return NULL;
372 }
373
374 PHP_HTTP_API STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
375 {
376 int status;
377
378 /* append input to our buffer */
379 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
380
381 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
382 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
383
384 /* deflate */
385 *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
386 *encoded = emalloc_rel(*encoded_len);
387 s->stream.avail_out = *encoded_len;
388 s->stream.next_out = (Bytef *) *encoded;
389
390 switch (status = deflate(&s->stream, Z_NO_FLUSH))
391 {
392 case Z_OK:
393 case Z_STREAM_END:
394 /* cut processed chunk off the buffer */
395 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
396
397 /* size buffer down to actual size */
398 *encoded_len -= s->stream.avail_out;
399 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
400 (*encoded)[*encoded_len] = '\0';
401 return SUCCESS;
402 break;
403 }
404
405 STR_SET(*encoded, NULL);
406 *encoded_len = 0;
407 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
408 return FAILURE;
409 }
410
411 PHP_HTTP_API STATUS _http_encoding_inflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
412 {
413 int status, max = 0;
414
415 /* append input to buffer */
416 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
417
418 /* for realloc() */
419 *decoded = NULL;
420 *decoded_len = data_len << 1;
421
422 /* inflate */
423 do {
424 *decoded_len <<= 1;
425 *decoded = erealloc_rel(*decoded, *decoded_len);
426
427 retry_raw_inflate:
428 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
429 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
430
431 s->stream.next_out = (Bytef *) *decoded;
432 s->stream.avail_out = *decoded_len;
433
434 switch (status = inflate(&s->stream, Z_NO_FLUSH))
435 {
436 case Z_OK:
437 case Z_STREAM_END:
438 /* cut off */
439 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
440
441 /* size down */
442 *decoded_len -= s->stream.avail_out;
443 *decoded = erealloc_rel(*decoded, *decoded_len + 1);
444 (*decoded)[*decoded_len] = '\0';
445 return SUCCESS;
446 break;
447
448 case Z_DATA_ERROR:
449 /* raw deflated data ? */
450 if (!(s->flags & HTTP_INFLATE_TYPE_RAW) && !s->stream.total_out) {
451 inflateEnd(&s->stream);
452 s->flags |= HTTP_INFLATE_TYPE_RAW;
453 inflateInit2(&s->stream, HTTP_WINDOW_BITS_RAW);
454 goto retry_raw_inflate;
455 }
456 break;
457 }
458 } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
459
460 STR_SET(*decoded, NULL);
461 *decoded_len = 0;
462 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update inflate stream: %s", zError(status));
463 return FAILURE;
464 }
465
466 PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(http_encoding_stream *s, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
467 {
468 int status;
469
470 *encoded_len = 0x8000;
471 *encoded = emalloc_rel(*encoded_len);
472
473 s->stream.avail_in = 0;
474 s->stream.next_in = NULL;
475 s->stream.avail_out = *encoded_len;
476 s->stream.next_out = (Bytef *) *encoded;
477
478 switch (status = deflate(&s->stream, Z_SYNC_FLUSH))
479 {
480 case Z_OK:
481 case Z_STREAM_END:
482 *encoded_len = 0x8000 - s->stream.avail_out;
483 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
484 (*encoded)[*encoded_len] = '\0';
485 return SUCCESS;
486 break;
487 }
488
489 STR_SET(*encoded, NULL);
490 *encoded_len = 0;
491 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
492 return FAILURE;
493 }
494
495 PHP_HTTP_API STATUS _http_encoding_inflate_stream_flush(http_encoding_stream *s, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
496 {
497 /* noop */
498 *decoded = estrndup("", *decoded_len = 0);
499 return SUCCESS;
500 }
501
502 PHP_HTTP_API STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
503 {
504 int status;
505
506 *encoded_len = 0x8000;
507 *encoded = emalloc_rel(*encoded_len);
508
509 /* deflate remaining input */
510 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
511 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
512
513 s->stream.avail_out = *encoded_len;
514 s->stream.next_out = (Bytef *) *encoded;
515
516 do {
517 status = deflate(&s->stream, Z_FINISH);
518 } while (Z_OK == status);
519
520 if (Z_STREAM_END == status) {
521 /* cut processed intp off */
522 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
523
524 /* size down */
525 *encoded_len -= s->stream.avail_out;
526 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
527 (*encoded)[*encoded_len] = '\0';
528 return SUCCESS;
529 }
530
531 STR_SET(*encoded, NULL);
532 *encoded_len = 0;
533 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
534 return FAILURE;
535 }
536
537 PHP_HTTP_API STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
538 {
539 int status;
540
541 *decoded_len = PHPSTR_LEN(s->stream.opaque) << 2;
542 *decoded = emalloc_rel(*decoded_len);
543
544 /* inflate remaining input */
545 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
546 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
547
548 s->stream.avail_out = *decoded_len;
549 s->stream.next_out = (Bytef *) *decoded;
550
551 if (Z_STREAM_END == (status = inflate(&s->stream, Z_FINISH))) {
552 /* cut processed input off */
553 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
554
555 /* size down */
556 *decoded_len -= s->stream.avail_out;
557 *decoded = erealloc_rel(*decoded, *decoded_len + 1);
558 (*decoded)[*decoded_len] = '\0';
559 return SUCCESS;
560 }
561
562 STR_SET(*decoded, NULL);
563 *decoded_len = 0;
564 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
565 return FAILURE;
566 }
567
568 PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
569 {
570 if (s) {
571 if (s->stream.opaque) {
572 phpstr_free((phpstr **) &s->stream.opaque);
573 }
574 deflateEnd(&s->stream);
575 }
576 }
577
578 PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
579 {
580 if (s) {
581 if (s->stream.opaque) {
582 phpstr_free((phpstr **) &s->stream.opaque);
583 }
584 inflateEnd(&s->stream);
585 }
586 }
587
588 PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
589 {
590 if (s) {
591 http_encoding_deflate_stream_dtor(*s);
592 if (*s) {
593 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
594 }
595 *s = NULL;
596 }
597 }
598
599 PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
600 {
601 if (s) {
602 http_encoding_inflate_stream_dtor(*s);
603 if (*s) {
604 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
605 }
606 *s = NULL;
607 }
608 }
609
610 void _http_ob_deflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
611 {
612 getGlobals(G);
613
614 *handled_output = NULL;
615 *handled_output_len = 0;
616
617 if (mode & PHP_OUTPUT_HANDLER_START) {
618 int flags;
619
620 if (G->send.deflate.stream) {
621 zend_error(E_ERROR, "ob_deflatehandler() can only be used once");
622 return;
623 }
624
625 G->send.deflate.encoding = !0;
626
627 switch (http_encoding_response_start(0))
628 {
629 case HTTP_ENCODING_GZIP:
630 flags = HTTP_DEFLATE_TYPE_GZIP;
631 break;
632
633 case HTTP_ENCODING_DEFLATE:
634 flags = HTTP_DEFLATE_TYPE_ZLIB;
635 break;
636
637 default:
638 goto deflate_passthru_plain;
639 break;
640 }
641
642 flags |= (G->send.deflate.start_flags &~ 0xf0);
643 G->send.deflate.stream = http_encoding_deflate_stream_init(NULL, flags);
644 }
645
646 if (G->send.deflate.stream) {
647 http_encoding_deflate_stream_update((http_encoding_stream *) G->send.deflate.stream, output, output_len, handled_output, handled_output_len);
648
649 if (mode & PHP_OUTPUT_HANDLER_END) {
650 char *remaining = NULL;
651 size_t remaining_len = 0;
652
653 http_encoding_deflate_stream_finish((http_encoding_stream *) G->send.deflate.stream, &remaining, &remaining_len);
654 http_encoding_deflate_stream_free((http_encoding_stream **) &G->send.deflate.stream);
655 if (remaining) {
656 *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
657 memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
658 (*handled_output)[*handled_output_len += remaining_len] = '\0';
659 efree(remaining);
660 }
661 }
662 } else {
663 deflate_passthru_plain:
664 *handled_output = estrndup(output, *handled_output_len = output_len);
665 }
666 }
667
668 void _http_ob_inflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
669 {
670 getGlobals(G);
671
672 *handled_output = NULL;
673 *handled_output_len = 0;
674
675 if (mode & PHP_OUTPUT_HANDLER_START) {
676 if (G->send.inflate.stream) {
677 zend_error(E_ERROR, "ob_inflatehandler() can only be used once");
678 return;
679 }
680 G->send.inflate.stream = http_encoding_inflate_stream_init(NULL, (HTTP_G(send).inflate.start_flags &~ 0xf0));
681 }
682
683 if (G->send.inflate.stream) {
684 http_encoding_inflate_stream_update((http_encoding_stream *) G->send.inflate.stream, output, output_len, handled_output, handled_output_len);
685
686 if (mode & PHP_OUTPUT_HANDLER_END) {
687 char *remaining = NULL;
688 size_t remaining_len = 0;
689
690 http_encoding_inflate_stream_finish((http_encoding_stream *) G->send.inflate.stream, &remaining, &remaining_len);
691 http_encoding_inflate_stream_free((http_encoding_stream **) &G->send.inflate.stream);
692 if (remaining) {
693 *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
694 memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
695 (*handled_output)[*handled_output_len += remaining_len] = '\0';
696 efree(remaining);
697 }
698 }
699 } else {
700 *handled_output = estrndup(output, *handled_output_len = output_len);
701 }
702 }
703
704 static const char http_encoding_gzip_header[] = {
705 (const char) 0x1f, // fixed value
706 (const char) 0x8b, // fixed value
707 (const char) Z_DEFLATED, // compression algorithm
708 (const char) 0, // none of the possible flags defined by the GZIP "RFC"
709 (const char) 0, // MTIME
710 (const char) 0, // =*=
711 (const char) 0, // =*=
712 (const char) 0, // =*=
713 (const char) 0, // two possible flag values for 9 compression levels? o_O
714 #ifdef PHP_WIN32
715 (const char) 0x0b // OS_CODE
716 #else
717 (const char) 0x03 // OS_CODE
718 #endif
719 };
720
721 PHP_HTTP_API STATUS _http_encoding_gzencode_verify(const char *data, size_t data_len, const char **encoded, size_t *encoded_len, int error_level TSRMLS_DC)
722 {
723 size_t offset = sizeof(http_encoding_gzip_header);
724
725 if (data_len < offset) {
726 goto really_bad_gzip_header;
727 }
728
729 if (data[0] != (const char) 0x1F || data[1] != (const char) 0x8B) {
730 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized GZIP header start: 0x%02X 0x%02X", (int) data[0], (int) (data[1] & 0xFF));
731 return FAILURE;
732 }
733
734 if (data[2] != (const char) Z_DEFLATED) {
735 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized compression format (%d)", (int) (data[2] & 0xFF));
736 /* still try to decode */
737 }
738 if ((data[3] & 0x4) == 0x4) {
739 if (data_len < offset + 2) {
740 goto really_bad_gzip_header;
741 }
742 /* there are extra fields, the length follows the common header as 2 bytes LSB */
743 offset += (unsigned) ((data[offset] & 0xFF));
744 offset += 1;
745 offset += (unsigned) ((data[offset] & 0xFF) << 8);
746 offset += 1;
747 }
748 if ((data[3] & 0x8) == 0x8) {
749 if (data_len <= offset) {
750 goto really_bad_gzip_header;
751 }
752 /* there's a file name */
753 offset += strlen(&data[offset]) + 1 /*NUL*/;
754 }
755 if ((data[3] & 0x10) == 0x10) {
756 if (data_len <= offset) {
757 goto really_bad_gzip_header;
758 }
759 /* there's a comment */
760 offset += strlen(&data[offset]) + 1 /* NUL */;
761 }
762 if ((data[3] & 0x2) == 0x2) {
763 /* there's a CRC16 of the header */
764 offset += 2;
765 if (data_len <= offset) {
766 goto really_bad_gzip_header;
767 } else {
768 ulong crc, cmp;
769
770 cmp = (unsigned) ((data[offset-2] & 0xFF));
771 cmp += (unsigned) ((data[offset-1] & 0xFF) << 8);
772
773 crc = crc32(0L, Z_NULL, 0);
774 crc = crc32(crc, (const Bytef *) data, sizeof(http_encoding_gzip_header));
775
776 if (cmp != (crc & 0xFFFF)) {
777 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "GZIP headers CRC checksums so not match (%lu, %lu)", cmp, crc & 0xFFFF);
778 return FAILURE;
779 }
780 }
781 }
782
783 if (data_len < offset + 8) {
784 http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP footer");
785 return FAILURE;
786 }
787
788 if (encoded) {
789 *encoded = data + offset;
790 }
791 if (encoded_len) {
792 *encoded_len = data_len - offset - 8 /* size of the assumed GZIP footer */;
793 }
794
795 return SUCCESS;
796
797 really_bad_gzip_header:
798 http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP header");
799 return FAILURE;
800 }
801
802 PHP_HTTP_API STATUS _http_encoding_gzdecode_verify(const char *data, size_t data_len, const char *decoded, size_t decoded_len, int error_level TSRMLS_DC)
803 {
804 STATUS status = SUCCESS;
805 ulong len, cmp, crc;
806
807 crc = crc32(0L, Z_NULL, 0);
808 crc = crc32(crc, (const Bytef *) decoded, decoded_len);
809
810 cmp = (unsigned) ((data[data_len-8] & 0xFF));
811 cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
812 cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
813 cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
814 len = (unsigned) ((data[data_len-4] & 0xFF));
815 len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
816 len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
817 len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
818
819 if (cmp != crc) {
820 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
821 status = FAILURE;
822 }
823 if (len != decoded_len) {
824 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: data sizes do not match (%lu, %lu)", len, decoded_len);
825 status = FAILURE;
826 }
827 return status;
828 }
829
830 #endif /* HTTP_HAVE_ZLIB */
831
832 PHP_HTTP_API int _http_encoding_response_start(size_t content_length TSRMLS_DC)
833 {
834 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
835 php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
836 HTTP_G(send).deflate.encoding = 0;
837 } else {
838 if (!HTTP_G(send).deflate.encoding) {
839 /* emit a content-length header */
840 if (content_length) {
841 char cl_header_str[128];
842 size_t cl_header_len;
843 cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %zu", content_length);
844 http_send_header_string_ex(cl_header_str, cl_header_len, 1);
845 }
846 } else {
847 #ifndef HTTP_HAVE_ZLIB
848 HTTP_G(send).deflate.encoding = 0;
849 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
850 #else
851 HashTable *selected;
852 zval zsupported;
853
854 INIT_PZVAL(&zsupported);
855 array_init(&zsupported);
856 add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
857 add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
858 add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
859
860 HTTP_G(send).deflate.encoding = 0;
861
862 if ((selected = http_negotiate_encoding(&zsupported))) {
863 STATUS hs = FAILURE;
864 char *encoding = NULL;
865 ulong idx;
866
867 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
868 if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
869 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
870 HTTP_G(send).deflate.encoding = HTTP_ENCODING_GZIP;
871 }
872 } else if (!strcmp(encoding, "deflate")) {
873 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
874 HTTP_G(send).deflate.encoding = HTTP_ENCODING_DEFLATE;
875 }
876 }
877 if (SUCCESS == hs) {
878 http_send_header_string("Vary: Accept-Encoding");
879 }
880 }
881
882 zend_hash_destroy(selected);
883 FREE_HASHTABLE(selected);
884 }
885
886 zval_dtor(&zsupported);
887 return HTTP_G(send).deflate.encoding;
888 #endif
889 }
890 }
891 return 0;
892 }
893
894 /*
895 * Local variables:
896 * tab-width: 4
897 * c-basic-offset: 4
898 * End:
899 * vim600: noet sw=4 ts=4 fdm=marker
900 * vim<600: noet sw=4 ts=4
901 */
902