2 +--------------------------------------------------------------------+
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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_ZLIB
18 #include "php_http_api.h"
19 #include "php_http_encoding_api.h"
20 #include "php_http_send_api.h"
21 #include "php_http_headers_api.h"
25 PHP_MINIT_FUNCTION(http_encoding
)
27 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF
);
28 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN
);
29 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX
);
30 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB
);
31 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP
);
32 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_RAW", HTTP_DEFLATE_TYPE_RAW
);
33 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_DEF", HTTP_DEFLATE_STRATEGY_DEF
);
34 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FILT", HTTP_DEFLATE_STRATEGY_FILT
);
35 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_HUFF", HTTP_DEFLATE_STRATEGY_HUFF
);
36 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_RLE", HTTP_DEFLATE_STRATEGY_RLE
);
37 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FIXED", HTTP_DEFLATE_STRATEGY_FIXED
);
41 PHP_RINIT_FUNCTION(http_encoding
)
43 if (HTTP_G
->send
.inflate
.start_auto
) {
44 php_ob_set_internal_handler(_http_ob_inflatehandler
, HTTP_INFLATE_BUFFER_SIZE
, "http inflate", 0 TSRMLS_CC
);
46 if (HTTP_G
->send
.deflate
.start_auto
) {
47 php_ob_set_internal_handler(_http_ob_deflatehandler
, HTTP_DEFLATE_BUFFER_SIZE
, "http deflate", 0 TSRMLS_CC
);
52 PHP_RSHUTDOWN_FUNCTION(http_encoding
)
54 if (HTTP_G
->send
.deflate
.stream
) {
55 http_encoding_deflate_stream_free((http_encoding_stream
**) &HTTP_G
->send
.deflate
.stream
);
57 if (HTTP_G
->send
.inflate
.stream
) {
58 http_encoding_inflate_stream_free((http_encoding_stream
**) &HTTP_G
->send
.inflate
.stream
);
65 /* {{{ eol_match(char **, int *) */
66 static inline int eol_match(char **line
, int *eol_len
)
70 while (' ' == *ptr
) ++ptr
;
72 if (ptr
== http_locate_eol(*line
, eol_len
)) {
81 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
82 PHP_HTTP_API
const char *_http_encoding_dechunk(const char *encoded
, size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
86 const char *e_ptr
= encoded
;
89 *decoded
= ecalloc(1, encoded_len
);
91 while ((encoded
+ encoded_len
- e_ptr
) > 0) {
92 ulong chunk_len
= 0, rest
;
94 chunk_len
= strtoul(e_ptr
, &n_ptr
, 16);
96 /* we could not read in chunk size */
99 * if this is the first turn and there doesn't seem to be a chunk
100 * size at the begining of the body, do not fail on apparently
101 * not encoded data and return a copy
103 if (e_ptr
== encoded
) {
104 http_error(HE_NOTICE
, HTTP_E_ENCODING
, "Data does not seem to be chunked encoded");
105 memcpy(*decoded
, encoded
, encoded_len
);
106 *decoded_len
= encoded_len
;
107 return encoded
+ encoded_len
;
110 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 /* reached the end */
117 /* move over '0' chunked encoding terminator */
118 while (*e_ptr
== '0') ++e_ptr
;
122 /* there should be CRLF after the chunk size, but we'll ignore SP+ too */
123 if (*n_ptr
&& !eol_match(&n_ptr
, &eol_len
)) {
125 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));
127 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 /* chunk size pretends more data than we actually got, so it's probably a truncated message */
133 if (chunk_len
> (rest
= encoded
+ encoded_len
- n_ptr
)) {
134 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 memcpy(*decoded
+ *decoded_len
, n_ptr
, chunk_len
);
140 *decoded_len
+= chunk_len
;
142 if (chunk_len
== rest
) {
143 e_ptr
= n_ptr
+ chunk_len
;
146 /* advance to next chunk */
147 e_ptr
= n_ptr
+ chunk_len
+ eol_len
;
155 /* {{{ int http_encoding_response_start(size_t) */
156 PHP_HTTP_API
int _http_encoding_response_start(size_t content_length TSRMLS_DC
)
158 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC
) ||
159 php_ob_handler_used("zlib output compression" TSRMLS_CC
)) {
160 HTTP_G
->send
.deflate
.encoding
= 0;
162 if (!HTTP_G
->send
.deflate
.encoding
) {
163 /* emit a content-length header */
164 if (content_length
) {
165 char cl_header_str
[128];
166 size_t cl_header_len
;
167 cl_header_len
= snprintf(cl_header_str
, lenof(cl_header_str
), "Content-Length: %zu", content_length
);
168 http_send_header_string_ex(cl_header_str
, cl_header_len
, 1);
171 #ifndef HTTP_HAVE_ZLIB
172 HTTP_G
->send
.deflate
.encoding
= 0;
173 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC
);
178 INIT_PZVAL(&zsupported
);
179 array_init(&zsupported
);
180 add_next_index_stringl(&zsupported
, "gzip", lenof("gzip"), 1);
181 add_next_index_stringl(&zsupported
, "x-gzip", lenof("x-gzip"), 1);
182 add_next_index_stringl(&zsupported
, "deflate", lenof("deflate"), 1);
184 HTTP_G
->send
.deflate
.encoding
= 0;
186 if ((selected
= http_negotiate_encoding(&zsupported
))) {
188 char *encoding
= NULL
;
191 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(selected
, &encoding
, &idx
, 0) && encoding
) {
192 if (!strcmp(encoding
, "gzip") || !strcmp(encoding
, "x-gzip")) {
193 if (SUCCESS
== (hs
= http_send_header_string("Content-Encoding: gzip"))) {
194 HTTP_G
->send
.deflate
.encoding
= HTTP_ENCODING_GZIP
;
196 } else if (!strcmp(encoding
, "deflate")) {
197 if (SUCCESS
== (hs
= http_send_header_string("Content-Encoding: deflate"))) {
198 HTTP_G
->send
.deflate
.encoding
= HTTP_ENCODING_DEFLATE
;
202 http_send_header_string("Vary: Accept-Encoding");
206 zend_hash_destroy(selected
);
207 FREE_HASHTABLE(selected
);
210 zval_dtor(&zsupported
);
211 return HTTP_G
->send
.deflate
.encoding
;
219 #ifdef HTTP_HAVE_ZLIB
222 #define HTTP_DEFLATE_LEVEL_SET(flags, level) \
223 switch (flags & 0xf) \
226 if ((flags & 0xf) < 10) { \
227 level = flags & 0xf; \
230 case HTTP_DEFLATE_LEVEL_DEF: \
231 level = Z_DEFAULT_COMPRESSION; \
235 #define HTTP_DEFLATE_WBITS_SET(flags, wbits) \
236 switch (flags & 0xf0) \
238 case HTTP_DEFLATE_TYPE_GZIP: \
239 wbits = HTTP_WINDOW_BITS_GZIP; \
241 case HTTP_DEFLATE_TYPE_RAW: \
242 wbits = HTTP_WINDOW_BITS_RAW; \
245 wbits = HTTP_WINDOW_BITS_ZLIB; \
249 #define HTTP_INFLATE_WBITS_SET(flags, wbits) \
250 if (flags & HTTP_INFLATE_TYPE_RAW) { \
251 wbits = HTTP_WINDOW_BITS_RAW; \
253 wbits = HTTP_WINDOW_BITS_ANY; \
256 #define HTTP_DEFLATE_STRATEGY_SET(flags, strategy) \
257 switch (flags & 0xf00) \
259 case HTTP_DEFLATE_STRATEGY_FILT: \
260 strategy = Z_FILTERED; \
262 case HTTP_DEFLATE_STRATEGY_HUFF: \
263 strategy = Z_HUFFMAN_ONLY; \
265 case HTTP_DEFLATE_STRATEGY_RLE: \
268 case HTTP_DEFLATE_STRATEGY_FIXED: \
269 strategy = Z_FIXED; \
272 strategy = Z_DEFAULT_STRATEGY; \
276 #define HTTP_WINDOW_BITS_ZLIB 0x0000000f
277 #define HTTP_WINDOW_BITS_GZIP 0x0000001f
278 #define HTTP_WINDOW_BITS_ANY 0x0000002f
279 #define HTTP_WINDOW_BITS_RAW -0x000000f
282 /* {{{ STATUS http_encoding_deflate(int, char *, size_t, char **, size_t *) */
283 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
)
285 int status
, level
, wbits
, strategy
;
288 HTTP_DEFLATE_LEVEL_SET(flags
, level
);
289 HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
290 HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
292 memset(&Z
, 0, sizeof(z_stream
));
296 status
= deflateInit2(&Z
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
);
297 if (Z_OK
== status
) {
298 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
299 *encoded
= emalloc_rel(*encoded_len
);
301 Z
.next_in
= (Bytef
*) data
;
302 Z
.next_out
= (Bytef
*) *encoded
;
303 Z
.avail_in
= data_len
;
304 Z
.avail_out
= *encoded_len
;
306 status
= deflate(&Z
, Z_FINISH
);
309 if (Z_STREAM_END
== status
) {
310 /* size buffer down to actual length */
311 *encoded
= erealloc_rel(*encoded
, Z
.total_out
+ 1);
312 (*encoded
)[*encoded_len
= Z
.total_out
] = '\0';
315 STR_SET(*encoded
, NULL
);
320 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not deflate data: %s", zError(status
));
325 /* {{{ STATUS http_encoding_inflate(char *, size_t, char **, size_t) */
326 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
)
328 int status
, round
= 0, wbits
= HTTP_WINDOW_BITS_ANY
;
332 memset(&Z
, 0, sizeof(z_stream
));
336 phpstr_init_ex(&buffer
, data_len
<< 2, PHPSTR_INIT_PREALLOC
);
337 buffer
.size
= data_len
;
340 status
= inflateInit2(&Z
, wbits
);
341 if (Z_OK
== status
) {
342 Z
.next_in
= (Bytef
*) data
;
343 Z
.avail_in
= data_len
;
346 phpstr_resize(&buffer
, data_len
<< 2);
349 Z
.avail_out
= (buffer
.free
-= Z
.total_out
- buffer
.used
);
350 Z
.next_out
= (Bytef
*) buffer
.data
+ (buffer
.used
= Z
.total_out
);
351 status
= inflate(&Z
, Z_NO_FLUSH
);
352 } while (Z_OK
== status
);
353 } while (Z_BUF_ERROR
== status
&& ++round
< HTTP_INFLATE_ROUNDS
);
355 if (Z_DATA_ERROR
== status
&& HTTP_WINDOW_BITS_ANY
== wbits
) {
356 /* raw deflated data? */
358 wbits
= HTTP_WINDOW_BITS_RAW
;
364 if (Z_STREAM_END
== status
) {
365 *decoded_len
= Z
.total_out
;
366 *decoded
= erealloc_rel(buffer
.data
, *decoded_len
+ 1);
367 (*decoded
)[*decoded_len
] = '\0';
370 phpstr_dtor(&buffer
);
374 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not inflate data: %s", zError(status
));
379 /* {{{ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *, int) */
380 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
)
382 int status
, level
, wbits
, strategy
, free_stream
;
384 if ((free_stream
= !s
)) {
385 s
= pemalloc_rel(sizeof(http_encoding_stream
), (flags
& HTTP_ENCODING_STREAM_PERSISTENT
));
387 memset(s
, 0, sizeof(http_encoding_stream
));
390 HTTP_DEFLATE_LEVEL_SET(flags
, level
);
391 HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
392 HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
394 if (Z_OK
== (status
= deflateInit2(&s
->stream
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
))) {
395 int p
= (flags
& HTTP_ENCODING_STREAM_PERSISTENT
) ? PHPSTR_INIT_PERSISTENT
:0;
397 if ((s
->stream
.opaque
= phpstr_init_ex(NULL
, HTTP_DEFLATE_BUFFER_SIZE
, p
))) {
400 deflateEnd(&s
->stream
);
401 status
= Z_MEM_ERROR
;
404 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to initialize deflate encoding stream: %s", zError(status
));
412 /* {{{ http_encoding_stream *http_encoding_inflate_stream_init(http_encoding_stream *, int) */
413 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
)
415 int status
, wbits
, free_stream
;
417 if ((free_stream
= !s
)) {
418 s
= pemalloc_rel(sizeof(http_encoding_stream
), (flags
& HTTP_ENCODING_STREAM_PERSISTENT
));
420 memset(s
, 0, sizeof(http_encoding_stream
));
423 HTTP_INFLATE_WBITS_SET(flags
, wbits
);
425 if (Z_OK
== (status
= inflateInit2(&s
->stream
, wbits
))) {
426 int p
= (flags
& HTTP_ENCODING_STREAM_PERSISTENT
) ? PHPSTR_INIT_PERSISTENT
:0;
428 if ((s
->stream
.opaque
= phpstr_init_ex(NULL
, HTTP_DEFLATE_BUFFER_SIZE
, p
))) {
431 inflateEnd(&s
->stream
);
432 status
= Z_MEM_ERROR
;
435 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to initialize inflate stream: %s", zError(status
));
443 /* {{{ STATUS http_encoding_deflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
444 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
)
448 /* append input to our buffer */
449 phpstr_append(PHPSTR(s
->stream
.opaque
), data
, data_len
);
451 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
452 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
455 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
456 *encoded
= emalloc_rel(*encoded_len
);
457 s
->stream
.avail_out
= *encoded_len
;
458 s
->stream
.next_out
= (Bytef
*) *encoded
;
460 switch (status
= deflate(&s
->stream
, Z_NO_FLUSH
))
464 /* cut processed chunk off the buffer */
465 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
467 /* size buffer down to actual size */
468 *encoded_len
-= s
->stream
.avail_out
;
469 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
470 (*encoded
)[*encoded_len
] = '\0';
475 STR_SET(*encoded
, NULL
);
477 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to update deflate stream: %s", zError(status
));
482 /* {{{ STATUS http_encoding_inflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
483 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
)
485 int status
, round
= 0;
487 /* append input to buffer */
488 phpstr_append(PHPSTR(s
->stream
.opaque
), data
, data_len
);
492 *decoded_len
= data_len
<< 1;
497 *decoded
= erealloc_rel(*decoded
, *decoded_len
);
500 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
501 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
503 s
->stream
.next_out
= (Bytef
*) *decoded
;
504 s
->stream
.avail_out
= *decoded_len
;
506 switch (status
= inflate(&s
->stream
, Z_NO_FLUSH
))
511 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
514 *decoded_len
-= s
->stream
.avail_out
;
515 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
516 (*decoded
)[*decoded_len
] = '\0';
521 /* raw deflated data ? */
522 if (!(s
->flags
& HTTP_INFLATE_TYPE_RAW
) && !s
->stream
.total_out
) {
523 inflateEnd(&s
->stream
);
524 s
->flags
|= HTTP_INFLATE_TYPE_RAW
;
525 inflateInit2(&s
->stream
, HTTP_WINDOW_BITS_RAW
);
526 goto retry_raw_inflate
;
530 } while (Z_BUF_ERROR
== status
&& ++round
< HTTP_INFLATE_ROUNDS
);
532 STR_SET(*decoded
, NULL
);
534 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to update inflate stream: %s", zError(status
));
539 /* {{{ STATUS http_encoding_deflate_stream_flush(http_encoding_stream *, char **, size_t *) */
540 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
)
544 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
;
545 *encoded
= emalloc_rel(*encoded_len
);
547 s
->stream
.avail_in
= 0;
548 s
->stream
.next_in
= NULL
;
549 s
->stream
.avail_out
= *encoded_len
;
550 s
->stream
.next_out
= (Bytef
*) *encoded
;
552 switch (status
= deflate(&s
->stream
, Z_SYNC_FLUSH
))
556 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
- s
->stream
.avail_out
;
557 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
558 (*encoded
)[*encoded_len
] = '\0';
563 STR_SET(*encoded
, NULL
);
565 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to flush deflate stream: %s", zError(status
));
570 /* {{{ STATUS http_encoding_inflate_straem_flush(http_encoding_stream *, char **, size_t *) */
571 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
)
574 *decoded
= estrndup("", *decoded_len
= 0);
579 /* {{{ STATUS http_encoding_deflate_stream_finish(http_encoding_stream *, char **, size_t *) */
580 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
)
584 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
;
585 *encoded
= emalloc_rel(*encoded_len
);
587 /* deflate remaining input */
588 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
589 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
591 s
->stream
.avail_out
= *encoded_len
;
592 s
->stream
.next_out
= (Bytef
*) *encoded
;
595 status
= deflate(&s
->stream
, Z_FINISH
);
596 } while (Z_OK
== status
);
598 if (Z_STREAM_END
== status
) {
599 /* cut processed intp off */
600 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
603 *encoded_len
-= s
->stream
.avail_out
;
604 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
605 (*encoded
)[*encoded_len
] = '\0';
609 STR_SET(*encoded
, NULL
);
611 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish deflate stream: %s", zError(status
));
616 /* {{{ STATUS http_encoding_inflate_stream_finish(http_encoding_stream *, char **, size_t *) */
617 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
)
621 *decoded_len
= (PHPSTR_LEN(s
->stream
.opaque
) + 1) * HTTP_INFLATE_ROUNDS
;
622 *decoded
= emalloc_rel(*decoded_len
);
624 /* inflate remaining input */
625 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
626 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
628 s
->stream
.avail_out
= *decoded_len
;
629 s
->stream
.next_out
= (Bytef
*) *decoded
;
631 if (Z_STREAM_END
== (status
= inflate(&s
->stream
, Z_FINISH
))) {
632 /* cut processed input off */
633 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
636 *decoded_len
-= s
->stream
.avail_out
;
637 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
638 (*decoded
)[*decoded_len
] = '\0';
642 STR_SET(*decoded
, NULL
);
644 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish inflate stream: %s", zError(status
));
649 /* {{{ void http_encoding_deflate_stream_dtor(http_encoding_stream *) */
650 PHP_HTTP_API
void _http_encoding_deflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
653 if (s
->stream
.opaque
) {
654 phpstr_free((phpstr
**) &s
->stream
.opaque
);
656 deflateEnd(&s
->stream
);
661 /* {{{ void http_encoding_inflate_stream_dtor(http_encoding_stream *) */
662 PHP_HTTP_API
void _http_encoding_inflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
665 if (s
->stream
.opaque
) {
666 phpstr_free((phpstr
**) &s
->stream
.opaque
);
668 inflateEnd(&s
->stream
);
673 /* {{{ void http_encoding_deflate_stream_free(http_encoding_stream **) */
674 PHP_HTTP_API
void _http_encoding_deflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
677 http_encoding_deflate_stream_dtor(*s
);
679 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
686 /* {{{ void http_encoding_inflate_stream_free(http_encoding_stream **) */
687 PHP_HTTP_API
void _http_encoding_inflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
690 http_encoding_inflate_stream_dtor(*s
);
692 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
699 /* {{{ void http_ob_deflatehandler(char *, uint, char **, uint *, int) */
700 void _http_ob_deflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
702 *handled_output
= NULL
;
703 *handled_output_len
= 0;
705 if (mode
& PHP_OUTPUT_HANDLER_START
) {
708 if (HTTP_G
->send
.deflate
.stream
) {
709 zend_error(E_ERROR
, "ob_deflatehandler() can only be used once");
713 HTTP_G
->send
.deflate
.encoding
= !0;
715 switch (http_encoding_response_start(0))
717 case HTTP_ENCODING_GZIP
:
718 flags
= HTTP_DEFLATE_TYPE_GZIP
;
721 case HTTP_ENCODING_DEFLATE
:
722 flags
= HTTP_DEFLATE_TYPE_ZLIB
;
726 goto deflate_passthru_plain
;
730 flags
|= (HTTP_G
->send
.deflate
.start_flags
&~ 0xf0);
731 HTTP_G
->send
.deflate
.stream
= http_encoding_deflate_stream_init(NULL
, flags
);
734 if (HTTP_G
->send
.deflate
.stream
) {
735 http_encoding_deflate_stream_update((http_encoding_stream
*) HTTP_G
->send
.deflate
.stream
, output
, output_len
, handled_output
, handled_output_len
);
737 if (mode
& PHP_OUTPUT_HANDLER_END
) {
738 char *remaining
= NULL
;
739 size_t remaining_len
= 0;
741 http_encoding_deflate_stream_finish((http_encoding_stream
*) HTTP_G
->send
.deflate
.stream
, &remaining
, &remaining_len
);
742 http_encoding_deflate_stream_free((http_encoding_stream
**) &HTTP_G
->send
.deflate
.stream
);
744 *handled_output
= erealloc(*handled_output
, *handled_output_len
+ remaining_len
+ 1);
745 memcpy(*handled_output
+ *handled_output_len
, remaining
, remaining_len
);
746 (*handled_output
)[*handled_output_len
+= remaining_len
] = '\0';
751 deflate_passthru_plain
:
752 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
757 /* {{{ void http_ob_inflatehandler(char *, uint, char **, uint *, int) */
758 void _http_ob_inflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
760 *handled_output
= NULL
;
761 *handled_output_len
= 0;
763 if (mode
& PHP_OUTPUT_HANDLER_START
) {
764 if (HTTP_G
->send
.inflate
.stream
) {
765 zend_error(E_ERROR
, "ob_inflatehandler() can only be used once");
768 HTTP_G
->send
.inflate
.stream
= http_encoding_inflate_stream_init(NULL
, (HTTP_G
->send
.inflate
.start_flags
&~ 0xf0));
771 if (HTTP_G
->send
.inflate
.stream
) {
772 http_encoding_inflate_stream_update((http_encoding_stream
*) HTTP_G
->send
.inflate
.stream
, output
, output_len
, handled_output
, handled_output_len
);
774 if (mode
& PHP_OUTPUT_HANDLER_END
) {
775 char *remaining
= NULL
;
776 size_t remaining_len
= 0;
778 http_encoding_inflate_stream_finish((http_encoding_stream
*) HTTP_G
->send
.inflate
.stream
, &remaining
, &remaining_len
);
779 http_encoding_inflate_stream_free((http_encoding_stream
**) &HTTP_G
->send
.inflate
.stream
);
781 *handled_output
= erealloc(*handled_output
, *handled_output_len
+ remaining_len
+ 1);
782 memcpy(*handled_output
+ *handled_output_len
, remaining
, remaining_len
);
783 (*handled_output
)[*handled_output_len
+= remaining_len
] = '\0';
788 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
793 #endif /* HTTP_HAVE_ZLIB */
800 * vim600: noet sw=4 ts=4 fdm=marker
801 * vim<600: noet sw=4 ts=4