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
);
39 HTTP_LONG_CONSTANT("HTTP_ENCODING_STREAM_FLUSH_NONE", HTTP_ENCODING_STREAM_FLUSH_NONE
);
40 HTTP_LONG_CONSTANT("HTTP_ENCODING_STREAM_FLUSH_SYNC", HTTP_ENCODING_STREAM_FLUSH_SYNC
);
41 HTTP_LONG_CONSTANT("HTTP_ENCODING_STREAM_FLUSH_FULL", HTTP_ENCODING_STREAM_FLUSH_FULL
);
46 PHP_RINIT_FUNCTION(http_encoding
)
48 if (HTTP_G
->send
.inflate
.start_auto
) {
49 php_ob_set_internal_handler(_http_ob_inflatehandler
, HTTP_INFLATE_BUFFER_SIZE
, "http inflate", 0 TSRMLS_CC
);
51 if (HTTP_G
->send
.deflate
.start_auto
) {
52 php_ob_set_internal_handler(_http_ob_deflatehandler
, HTTP_DEFLATE_BUFFER_SIZE
, "http deflate", 0 TSRMLS_CC
);
57 PHP_RSHUTDOWN_FUNCTION(http_encoding
)
59 if (HTTP_G
->send
.deflate
.stream
) {
60 http_encoding_deflate_stream_free((http_encoding_stream
**) &HTTP_G
->send
.deflate
.stream
);
62 if (HTTP_G
->send
.inflate
.stream
) {
63 http_encoding_inflate_stream_free((http_encoding_stream
**) &HTTP_G
->send
.inflate
.stream
);
70 /* {{{ eol_match(char **, int *) */
71 static inline int eol_match(char **line
, int *eol_len
)
75 while (' ' == *ptr
) ++ptr
;
77 if (ptr
== http_locate_eol(*line
, eol_len
)) {
86 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
87 PHP_HTTP_API
const char *_http_encoding_dechunk(const char *encoded
, size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
91 const char *e_ptr
= encoded
;
94 *decoded
= ecalloc(1, encoded_len
);
96 while ((encoded
+ encoded_len
- e_ptr
) > 0) {
97 ulong chunk_len
= 0, rest
;
99 chunk_len
= strtoul(e_ptr
, &n_ptr
, 16);
101 /* we could not read in chunk size */
102 if (n_ptr
== e_ptr
) {
104 * if this is the first turn and there doesn't seem to be a chunk
105 * size at the begining of the body, do not fail on apparently
106 * not encoded data and return a copy
108 if (e_ptr
== encoded
) {
109 http_error(HE_NOTICE
, HTTP_E_ENCODING
, "Data does not seem to be chunked encoded");
110 memcpy(*decoded
, encoded
, encoded_len
);
111 *decoded_len
= encoded_len
;
112 return encoded
+ encoded_len
;
115 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Expected chunk size at pos %tu of %zu but got trash", n_ptr
- encoded
, encoded_len
);
120 /* reached the end */
122 /* move over '0' chunked encoding terminator */
123 while (*e_ptr
== '0') ++e_ptr
;
127 /* there should be CRLF after the chunk size, but we'll ignore SP+ too */
128 if (*n_ptr
&& !eol_match(&n_ptr
, &eol_len
)) {
130 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));
132 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
);
137 /* chunk size pretends more data than we actually got, so it's probably a truncated message */
138 if (chunk_len
> (rest
= encoded
+ encoded_len
- n_ptr
)) {
139 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
);
144 memcpy(*decoded
+ *decoded_len
, n_ptr
, chunk_len
);
145 *decoded_len
+= chunk_len
;
147 if (chunk_len
== rest
) {
148 e_ptr
= n_ptr
+ chunk_len
;
151 /* advance to next chunk */
152 e_ptr
= n_ptr
+ chunk_len
+ eol_len
;
160 /* {{{ int http_encoding_response_start(size_t) */
161 PHP_HTTP_API
int _http_encoding_response_start(size_t content_length TSRMLS_DC
)
163 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC
) ||
164 php_ob_handler_used("zlib output compression" TSRMLS_CC
)) {
165 HTTP_G
->send
.deflate
.encoding
= 0;
167 if (!HTTP_G
->send
.deflate
.encoding
) {
168 /* emit a content-length header */
169 if (content_length
) {
170 char cl_header_str
[128];
171 size_t cl_header_len
;
172 cl_header_len
= snprintf(cl_header_str
, lenof(cl_header_str
), "Content-Length: %zu", content_length
);
173 http_send_header_string_ex(cl_header_str
, cl_header_len
, 1);
176 #ifndef HTTP_HAVE_ZLIB
177 HTTP_G
->send
.deflate
.encoding
= 0;
178 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC
);
183 INIT_PZVAL(&zsupported
);
184 array_init(&zsupported
);
185 add_next_index_stringl(&zsupported
, "gzip", lenof("gzip"), 1);
186 add_next_index_stringl(&zsupported
, "x-gzip", lenof("x-gzip"), 1);
187 add_next_index_stringl(&zsupported
, "deflate", lenof("deflate"), 1);
189 HTTP_G
->send
.deflate
.encoding
= 0;
191 if ((selected
= http_negotiate_encoding(&zsupported
))) {
193 char *encoding
= NULL
;
196 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(selected
, &encoding
, &idx
, 0) && encoding
) {
197 if (!strcmp(encoding
, "gzip") || !strcmp(encoding
, "x-gzip")) {
198 if (SUCCESS
== (hs
= http_send_header_string("Content-Encoding: gzip"))) {
199 HTTP_G
->send
.deflate
.encoding
= HTTP_ENCODING_GZIP
;
201 } else if (!strcmp(encoding
, "deflate")) {
202 if (SUCCESS
== (hs
= http_send_header_string("Content-Encoding: deflate"))) {
203 HTTP_G
->send
.deflate
.encoding
= HTTP_ENCODING_DEFLATE
;
207 http_send_header_string("Vary: Accept-Encoding");
211 zend_hash_destroy(selected
);
212 FREE_HASHTABLE(selected
);
215 zval_dtor(&zsupported
);
216 return HTTP_G
->send
.deflate
.encoding
;
224 #ifdef HTTP_HAVE_ZLIB
226 /* {{{ inline int http_inflate_rounds */
227 static inline int http_inflate_rounds(z_stream
*Z
, int flush
, char **buf
, size_t *len
)
229 int status
= 0, round
= 0;
235 phpstr_init_ex(&buffer
, Z
->avail_in
, PHPSTR_INIT_PREALLOC
);
238 if (PHPSTR_NOMEM
== phpstr_resize_ex(&buffer
, buffer
.size
, 0, 1)) {
239 status
= Z_MEM_ERROR
;
241 Z
->avail_out
= buffer
.free
;
242 Z
->next_out
= (Bytef
*) buffer
.data
+ buffer
.used
;
244 fprintf(stderr
, "\n%3d: %3d PRIOR: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round
, status
, buffer
.size
, buffer
.free
, buffer
.used
, Z
->avail_in
, Z
->avail_out
);
246 status
= inflate(Z
, flush
);
248 buffer
.used
+= buffer
.free
- Z
->avail_out
;
249 buffer
.free
= Z
->avail_out
;
251 fprintf(stderr
, "%3d: %3d AFTER: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round
, status
, buffer
.size
, buffer
.free
, buffer
.used
, Z
->avail_in
, Z
->avail_out
);
253 HTTP_INFLATE_BUFFER_SIZE_ALIGN(buffer
.size
);
255 } while ((Z_BUF_ERROR
== status
|| (Z_OK
== status
&& Z
->avail_in
)) && ++round
< HTTP_INFLATE_ROUNDS
);
257 if (status
== Z_OK
|| status
== Z_STREAM_END
) {
258 phpstr_shrink(&buffer
);
263 phpstr_dtor(&buffer
);
270 /* {{{ STATUS http_encoding_deflate(int, char *, size_t, char **, size_t *) */
271 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
)
273 int status
, level
, wbits
, strategy
;
276 HTTP_DEFLATE_LEVEL_SET(flags
, level
);
277 HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
278 HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
280 memset(&Z
, 0, sizeof(z_stream
));
284 status
= deflateInit2(&Z
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
);
285 if (Z_OK
== status
) {
286 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
287 *encoded
= emalloc_rel(*encoded_len
);
289 Z
.next_in
= (Bytef
*) data
;
290 Z
.next_out
= (Bytef
*) *encoded
;
291 Z
.avail_in
= data_len
;
292 Z
.avail_out
= *encoded_len
;
294 status
= deflate(&Z
, Z_FINISH
);
297 if (Z_STREAM_END
== status
) {
298 /* size buffer down to actual length */
299 *encoded
= erealloc_rel(*encoded
, Z
.total_out
+ 1);
300 (*encoded
)[*encoded_len
= Z
.total_out
] = '\0';
303 STR_SET(*encoded
, NULL
);
308 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not deflate data: %s", zError(status
));
313 /* {{{ STATUS http_encoding_inflate(char *, size_t, char **, size_t) */
314 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
)
317 int status
, wbits
= HTTP_WINDOW_BITS_ANY
;
319 memset(&Z
, 0, sizeof(z_stream
));
322 status
= inflateInit2(&Z
, wbits
);
323 if (Z_OK
== status
) {
324 Z
.next_in
= (Bytef
*) data
;
325 Z
.avail_in
= data_len
;
327 switch (status
= http_inflate_rounds(&Z
, Z_NO_FLUSH
, decoded
, decoded_len
)) {
334 /* raw deflated data? */
335 if (HTTP_WINDOW_BITS_ANY
== wbits
) {
337 wbits
= HTTP_WINDOW_BITS_RAW
;
338 goto retry_raw_inflate
;
344 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not inflate data: %s", zError(status
));
349 /* {{{ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *, int) */
350 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
)
352 int status
, level
, wbits
, strategy
, free_stream
;
354 if ((free_stream
= !s
)) {
355 s
= pemalloc_rel(sizeof(http_encoding_stream
), (flags
& HTTP_ENCODING_STREAM_PERSISTENT
));
357 memset(s
, 0, sizeof(http_encoding_stream
));
360 HTTP_DEFLATE_LEVEL_SET(flags
, level
);
361 HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
362 HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
364 if (Z_OK
== (status
= deflateInit2(&s
->stream
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
))) {
365 int p
= (flags
& HTTP_ENCODING_STREAM_PERSISTENT
) ? PHPSTR_INIT_PERSISTENT
:0;
367 if ((s
->stream
.opaque
= phpstr_init_ex(NULL
, HTTP_DEFLATE_BUFFER_SIZE
, p
))) {
370 deflateEnd(&s
->stream
);
371 status
= Z_MEM_ERROR
;
374 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to initialize deflate encoding stream: %s", zError(status
));
382 /* {{{ http_encoding_stream *http_encoding_inflate_stream_init(http_encoding_stream *, int) */
383 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
)
385 int status
, wbits
, free_stream
;
387 if ((free_stream
= !s
)) {
388 s
= pemalloc_rel(sizeof(http_encoding_stream
), (flags
& HTTP_ENCODING_STREAM_PERSISTENT
));
390 memset(s
, 0, sizeof(http_encoding_stream
));
393 HTTP_INFLATE_WBITS_SET(flags
, wbits
);
395 if (Z_OK
== (status
= inflateInit2(&s
->stream
, wbits
))) {
396 int p
= (flags
& HTTP_ENCODING_STREAM_PERSISTENT
) ? PHPSTR_INIT_PERSISTENT
:0;
398 if ((s
->stream
.opaque
= phpstr_init_ex(NULL
, HTTP_DEFLATE_BUFFER_SIZE
, p
))) {
401 inflateEnd(&s
->stream
);
402 status
= Z_MEM_ERROR
;
405 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to initialize inflate stream: %s", zError(status
));
413 /* {{{ STATUS http_encoding_deflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
414 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
)
418 /* append input to our buffer */
419 phpstr_append(PHPSTR(s
->stream
.opaque
), data
, data_len
);
421 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
422 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
425 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
426 *encoded
= emalloc_rel(*encoded_len
);
427 s
->stream
.avail_out
= *encoded_len
;
428 s
->stream
.next_out
= (Bytef
*) *encoded
;
430 switch (status
= deflate(&s
->stream
, HTTP_ENCODING_STREAM_FLUSH_FLAG(s
->flags
))) {
433 /* cut processed chunk off the buffer */
434 if (s
->stream
.avail_in
) {
435 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
437 phpstr_reset(PHPSTR(s
->stream
.opaque
));
440 /* size buffer down to actual size */
441 *encoded_len
-= s
->stream
.avail_out
;
442 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
443 (*encoded
)[*encoded_len
] = '\0';
447 STR_SET(*encoded
, NULL
);
449 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to update deflate stream: %s", zError(status
));
454 /* {{{ STATUS http_encoding_inflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
455 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
)
459 /* append input to buffer */
460 phpstr_append(PHPSTR(s
->stream
.opaque
), data
, data_len
);
463 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
464 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
466 switch (status
= http_inflate_rounds(&s
->stream
, HTTP_ENCODING_STREAM_FLUSH_FLAG(s
->flags
), decoded
, decoded_len
)) {
470 if (s
->stream
.avail_in
) {
471 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
473 phpstr_reset(PHPSTR(s
->stream
.opaque
));
478 /* raw deflated data ? */
479 if (!(s
->flags
& HTTP_INFLATE_TYPE_RAW
) && !s
->stream
.total_out
) {
480 inflateEnd(&s
->stream
);
481 s
->flags
|= HTTP_INFLATE_TYPE_RAW
;
482 inflateInit2(&s
->stream
, HTTP_WINDOW_BITS_RAW
);
483 goto retry_raw_inflate
;
487 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to update inflate stream: %s", zError(status
));
492 /* {{{ STATUS http_encoding_deflate_stream_flush(http_encoding_stream *, char **, size_t *) */
493 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
)
497 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
;
498 *encoded
= emalloc_rel(*encoded_len
);
500 s
->stream
.avail_in
= 0;
501 s
->stream
.next_in
= NULL
;
502 s
->stream
.avail_out
= *encoded_len
;
503 s
->stream
.next_out
= (Bytef
*) *encoded
;
505 switch (status
= deflate(&s
->stream
, Z_FULL_FLUSH
)) {
508 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
- s
->stream
.avail_out
;
509 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
510 (*encoded
)[*encoded_len
] = '\0';
514 STR_SET(*encoded
, NULL
);
516 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to flush deflate stream: %s", zError(status
));
521 /* {{{ STATUS http_encoding_inflate_straem_flush(http_encoding_stream *, char **, size_t *) */
522 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
)
525 *decoded
= estrndup("", *decoded_len
= 0);
530 /* {{{ STATUS http_encoding_deflate_stream_finish(http_encoding_stream *, char **, size_t *) */
531 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
)
535 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
;
536 *encoded
= emalloc_rel(*encoded_len
);
538 /* deflate remaining input */
539 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
540 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
542 s
->stream
.avail_out
= *encoded_len
;
543 s
->stream
.next_out
= (Bytef
*) *encoded
;
546 status
= deflate(&s
->stream
, Z_FINISH
);
547 } while (Z_OK
== status
);
549 if (Z_STREAM_END
== status
) {
550 /* cut processed intp off */
551 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
554 *encoded_len
-= s
->stream
.avail_out
;
555 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
556 (*encoded
)[*encoded_len
] = '\0';
560 STR_SET(*encoded
, NULL
);
562 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish deflate stream: %s", zError(status
));
567 /* {{{ STATUS http_encoding_inflate_stream_finish(http_encoding_stream *, char **, size_t *) */
568 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
)
572 if (!PHPSTR_LEN(s
->stream
.opaque
)) {
578 *decoded_len
= (PHPSTR_LEN(s
->stream
.opaque
) + 1) * HTTP_INFLATE_ROUNDS
;
579 *decoded
= emalloc_rel(*decoded_len
);
581 /* inflate remaining input */
582 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
583 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
585 s
->stream
.avail_out
= *decoded_len
;
586 s
->stream
.next_out
= (Bytef
*) *decoded
;
588 if (Z_STREAM_END
== (status
= inflate(&s
->stream
, Z_FINISH
))) {
589 /* cut processed input off */
590 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
593 *decoded_len
-= s
->stream
.avail_out
;
594 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
595 (*decoded
)[*decoded_len
] = '\0';
599 STR_SET(*decoded
, NULL
);
601 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish inflate stream: %s", zError(status
));
606 /* {{{ void http_encoding_deflate_stream_dtor(http_encoding_stream *) */
607 PHP_HTTP_API
void _http_encoding_deflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
610 if (s
->stream
.opaque
) {
611 phpstr_free((phpstr
**) &s
->stream
.opaque
);
613 deflateEnd(&s
->stream
);
618 /* {{{ void http_encoding_inflate_stream_dtor(http_encoding_stream *) */
619 PHP_HTTP_API
void _http_encoding_inflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
622 if (s
->stream
.opaque
) {
623 phpstr_free((phpstr
**) &s
->stream
.opaque
);
625 inflateEnd(&s
->stream
);
630 /* {{{ void http_encoding_deflate_stream_free(http_encoding_stream **) */
631 PHP_HTTP_API
void _http_encoding_deflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
634 http_encoding_deflate_stream_dtor(*s
);
636 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
643 /* {{{ void http_encoding_inflate_stream_free(http_encoding_stream **) */
644 PHP_HTTP_API
void _http_encoding_inflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
647 http_encoding_inflate_stream_dtor(*s
);
649 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
656 /* {{{ void http_ob_deflatehandler(char *, uint, char **, uint *, int) */
657 void _http_ob_deflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
659 *handled_output
= NULL
;
660 *handled_output_len
= 0;
662 if (mode
& PHP_OUTPUT_HANDLER_START
) {
665 if (HTTP_G
->send
.deflate
.stream
) {
666 zend_error(E_ERROR
, "ob_deflatehandler() can only be used once");
670 HTTP_G
->send
.deflate
.encoding
= !0;
672 switch (http_encoding_response_start(0)) {
673 case HTTP_ENCODING_GZIP
:
674 flags
= HTTP_DEFLATE_TYPE_GZIP
;
677 case HTTP_ENCODING_DEFLATE
:
678 flags
= HTTP_DEFLATE_TYPE_ZLIB
;
682 goto deflate_passthru_plain
;
685 flags
|= (HTTP_G
->send
.deflate
.start_flags
&~ 0xf0);
686 HTTP_G
->send
.deflate
.stream
= http_encoding_deflate_stream_init(NULL
, flags
);
689 if (HTTP_G
->send
.deflate
.stream
) {
690 http_encoding_deflate_stream_update((http_encoding_stream
*) HTTP_G
->send
.deflate
.stream
, output
, output_len
, handled_output
, handled_output_len
);
692 if (mode
& PHP_OUTPUT_HANDLER_END
) {
693 char *remaining
= NULL
;
694 size_t remaining_len
= 0;
696 http_encoding_deflate_stream_finish((http_encoding_stream
*) HTTP_G
->send
.deflate
.stream
, &remaining
, &remaining_len
);
697 http_encoding_deflate_stream_free((http_encoding_stream
**) &HTTP_G
->send
.deflate
.stream
);
699 *handled_output
= erealloc(*handled_output
, *handled_output_len
+ remaining_len
+ 1);
700 memcpy(*handled_output
+ *handled_output_len
, remaining
, remaining_len
);
701 (*handled_output
)[*handled_output_len
+= remaining_len
] = '\0';
706 deflate_passthru_plain
:
707 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
712 /* {{{ void http_ob_inflatehandler(char *, uint, char **, uint *, int) */
713 void _http_ob_inflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
715 *handled_output
= NULL
;
716 *handled_output_len
= 0;
718 if (mode
& PHP_OUTPUT_HANDLER_START
) {
719 if (HTTP_G
->send
.inflate
.stream
) {
720 zend_error(E_ERROR
, "ob_inflatehandler() can only be used once");
723 HTTP_G
->send
.inflate
.stream
= http_encoding_inflate_stream_init(NULL
, (HTTP_G
->send
.inflate
.start_flags
&~ 0xf0));
726 if (HTTP_G
->send
.inflate
.stream
) {
727 http_encoding_inflate_stream_update((http_encoding_stream
*) HTTP_G
->send
.inflate
.stream
, output
, output_len
, handled_output
, handled_output_len
);
729 if (mode
& PHP_OUTPUT_HANDLER_END
) {
730 char *remaining
= NULL
;
731 size_t remaining_len
= 0;
733 http_encoding_inflate_stream_finish((http_encoding_stream
*) HTTP_G
->send
.inflate
.stream
, &remaining
, &remaining_len
);
734 http_encoding_inflate_stream_free((http_encoding_stream
**) &HTTP_G
->send
.inflate
.stream
);
736 *handled_output
= erealloc(*handled_output
, *handled_output_len
+ remaining_len
+ 1);
737 memcpy(*handled_output
+ *handled_output_len
, remaining
, remaining_len
);
738 (*handled_output
)[*handled_output_len
+= remaining_len
] = '\0';
743 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
748 #endif /* HTTP_HAVE_ZLIB */
755 * vim600: noet sw=4 ts=4 fdm=marker
756 * vim<600: noet sw=4 ts=4