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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_ZLIB
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"
29 PHP_MINIT_FUNCTION(http_encoding
)
31 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF
);
32 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN
);
33 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX
);
34 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB
);
35 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP
);
36 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_RAW", HTTP_DEFLATE_TYPE_RAW
);
37 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_DEF", HTTP_DEFLATE_STRATEGY_DEF
);
38 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FILT", HTTP_DEFLATE_STRATEGY_FILT
);
39 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_HUFF", HTTP_DEFLATE_STRATEGY_HUFF
);
40 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_RLE", HTTP_DEFLATE_STRATEGY_RLE
);
41 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FIXED", HTTP_DEFLATE_STRATEGY_FIXED
);
45 PHP_RINIT_FUNCTION(http_encoding
)
49 if (G
->send
.inflate
.start_auto
) {
50 php_ob_set_internal_handler(_http_ob_inflatehandler
, HTTP_INFLATE_BUFFER_SIZE
, "http inflate", 0 TSRMLS_CC
);
52 if (G
->send
.deflate
.start_auto
) {
53 php_ob_set_internal_handler(_http_ob_deflatehandler
, HTTP_DEFLATE_BUFFER_SIZE
, "http deflate", 0 TSRMLS_CC
);
58 PHP_RSHUTDOWN_FUNCTION(http_encoding
)
62 if (G
->send
.deflate
.stream
) {
63 http_encoding_deflate_stream_free((http_encoding_stream
**) &G
->send
.deflate
.stream
);
65 if (G
->send
.inflate
.stream
) {
66 http_encoding_inflate_stream_free((http_encoding_stream
**) &G
->send
.inflate
.stream
);
73 /* {{{ eol_match(char **, int *) */
74 static inline int eol_match(char **line
, int *eol_len
)
78 while (' ' == *ptr
) ++ptr
;
80 if (ptr
== http_locate_eol(*line
, eol_len
)) {
89 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
90 PHP_HTTP_API
const char *_http_encoding_dechunk(const char *encoded
, size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
94 const char *e_ptr
= encoded
;
97 *decoded
= ecalloc(1, encoded_len
);
99 while ((encoded
+ encoded_len
- e_ptr
) > 0) {
100 ulong chunk_len
= 0, rest
;
102 chunk_len
= strtoul(e_ptr
, &n_ptr
, 16);
104 /* we could not read in chunk size */
105 if (n_ptr
== e_ptr
) {
107 * if this is the first turn and there doesn't seem to be a chunk
108 * size at the begining of the body, do not fail on apparently
109 * not encoded data and return a copy
111 if (e_ptr
== encoded
) {
112 http_error(HE_NOTICE
, HTTP_E_ENCODING
, "Data does not seem to be chunked encoded");
113 memcpy(*decoded
, encoded
, encoded_len
);
114 *decoded_len
= encoded_len
;
115 return encoded
+ encoded_len
;
118 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Expected chunk size at pos %tu of %zu but got trash", n_ptr
- encoded
, encoded_len
);
123 /* reached the end */
125 /* move over '0' chunked encoding terminator */
126 while (*e_ptr
== '0') ++e_ptr
;
130 /* there should be CRLF after the chunk size, but we'll ignore SP+ too */
131 if (*n_ptr
&& !eol_match(&n_ptr
, &eol_len
)) {
133 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));
135 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
);
140 /* chunk size pretends more data than we actually got, so it's probably a truncated message */
141 if (chunk_len
> (rest
= encoded
+ encoded_len
- n_ptr
)) {
142 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
);
147 memcpy(*decoded
+ *decoded_len
, n_ptr
, chunk_len
);
148 *decoded_len
+= chunk_len
;
150 if (chunk_len
== rest
) {
151 e_ptr
= n_ptr
+ chunk_len
;
154 /* advance to next chunk */
155 e_ptr
= n_ptr
+ chunk_len
+ eol_len
;
163 /* {{{ int http_encoding_response_start(size_t) */
164 PHP_HTTP_API
int _http_encoding_response_start(size_t content_length TSRMLS_DC
)
166 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC
) ||
167 php_ob_handler_used("zlib output compression" TSRMLS_CC
)) {
168 HTTP_G(send
).deflate
.encoding
= 0;
170 if (!HTTP_G(send
).deflate
.encoding
) {
171 /* emit a content-length header */
172 if (content_length
) {
173 char cl_header_str
[128];
174 size_t cl_header_len
;
175 cl_header_len
= snprintf(cl_header_str
, lenof(cl_header_str
), "Content-Length: %zu", content_length
);
176 http_send_header_string_ex(cl_header_str
, cl_header_len
, 1);
179 #ifndef HTTP_HAVE_ZLIB
180 HTTP_G(send
).deflate
.encoding
= 0;
181 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC
);
186 INIT_PZVAL(&zsupported
);
187 array_init(&zsupported
);
188 add_next_index_stringl(&zsupported
, "gzip", lenof("gzip"), 1);
189 add_next_index_stringl(&zsupported
, "x-gzip", lenof("x-gzip"), 1);
190 add_next_index_stringl(&zsupported
, "deflate", lenof("deflate"), 1);
192 HTTP_G(send
).deflate
.encoding
= 0;
194 if ((selected
= http_negotiate_encoding(&zsupported
))) {
196 char *encoding
= NULL
;
199 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(selected
, &encoding
, &idx
, 0) && encoding
) {
200 if (!strcmp(encoding
, "gzip") || !strcmp(encoding
, "x-gzip")) {
201 if (SUCCESS
== (hs
= http_send_header_string("Content-Encoding: gzip"))) {
202 HTTP_G(send
).deflate
.encoding
= HTTP_ENCODING_GZIP
;
204 } else if (!strcmp(encoding
, "deflate")) {
205 if (SUCCESS
== (hs
= http_send_header_string("Content-Encoding: deflate"))) {
206 HTTP_G(send
).deflate
.encoding
= HTTP_ENCODING_DEFLATE
;
210 http_send_header_string("Vary: Accept-Encoding");
214 zend_hash_destroy(selected
);
215 FREE_HASHTABLE(selected
);
218 zval_dtor(&zsupported
);
219 return HTTP_G(send
).deflate
.encoding
;
227 #ifdef HTTP_HAVE_ZLIB
230 #define HTTP_DEFLATE_LEVEL_SET(flags, level) \
231 switch (flags & 0xf) \
234 if ((flags & 0xf) < 10) { \
235 level = flags & 0xf; \
238 case HTTP_DEFLATE_LEVEL_DEF: \
239 level = Z_DEFAULT_COMPRESSION; \
243 #define HTTP_DEFLATE_WBITS_SET(flags, wbits) \
244 switch (flags & 0xf0) \
246 case HTTP_DEFLATE_TYPE_GZIP: \
247 wbits = HTTP_WINDOW_BITS_GZIP; \
249 case HTTP_DEFLATE_TYPE_RAW: \
250 wbits = HTTP_WINDOW_BITS_RAW; \
253 wbits = HTTP_WINDOW_BITS_ZLIB; \
257 #define HTTP_INFLATE_WBITS_SET(flags, wbits) \
258 if (flags & HTTP_INFLATE_TYPE_RAW) { \
259 wbits = HTTP_WINDOW_BITS_RAW; \
261 wbits = HTTP_WINDOW_BITS_ANY; \
264 #define HTTP_DEFLATE_STRATEGY_SET(flags, strategy) \
265 switch (flags & 0xf00) \
267 case HTTP_DEFLATE_STRATEGY_FILT: \
268 strategy = Z_FILTERED; \
270 case HTTP_DEFLATE_STRATEGY_HUFF: \
271 strategy = Z_HUFFMAN_ONLY; \
273 case HTTP_DEFLATE_STRATEGY_RLE: \
276 case HTTP_DEFLATE_STRATEGY_FIXED: \
277 strategy = Z_FIXED; \
280 strategy = Z_DEFAULT_STRATEGY; \
284 #define HTTP_WINDOW_BITS_ZLIB 0x0000000f
285 #define HTTP_WINDOW_BITS_GZIP 0x0000001f
286 #define HTTP_WINDOW_BITS_ANY 0x0000002f
287 #define HTTP_WINDOW_BITS_RAW -0x000000f
290 /* {{{ STATUS http_encoding_deflate(int, char *, size_t, char **, size_t *) */
291 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
)
293 int status
, level
, wbits
, strategy
;
296 HTTP_DEFLATE_LEVEL_SET(flags
, level
);
297 HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
298 HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
300 memset(&Z
, 0, sizeof(z_stream
));
304 status
= deflateInit2(&Z
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
);
305 if (Z_OK
== status
) {
306 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
307 *encoded
= emalloc_rel(*encoded_len
);
309 Z
.next_in
= (Bytef
*) data
;
310 Z
.next_out
= (Bytef
*) *encoded
;
311 Z
.avail_in
= data_len
;
312 Z
.avail_out
= *encoded_len
;
314 status
= deflate(&Z
, Z_FINISH
);
317 if (Z_STREAM_END
== status
) {
318 /* size buffer down to actual length */
319 *encoded
= erealloc_rel(*encoded
, Z
.total_out
+ 1);
320 (*encoded
)[*encoded_len
= Z
.total_out
] = '\0';
323 STR_SET(*encoded
, NULL
);
328 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not deflate data: %s", zError(status
));
333 /* {{{ STATUS http_encoding_inflate(char *, size_t, char **, size_t) */
334 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
)
336 int status
, round
= 0, wbits
= HTTP_WINDOW_BITS_ANY
;
340 memset(&Z
, 0, sizeof(z_stream
));
344 phpstr_init_ex(&buffer
, data_len
<< 2, PHPSTR_INIT_PREALLOC
);
345 buffer
.size
= data_len
;
348 status
= inflateInit2(&Z
, wbits
);
349 if (Z_OK
== status
) {
350 Z
.next_in
= (Bytef
*) data
;
351 Z
.avail_in
= data_len
;
354 phpstr_resize(&buffer
, data_len
<< 2);
357 Z
.avail_out
= (buffer
.free
-= Z
.total_out
- buffer
.used
);
358 Z
.next_out
= (Bytef
*) buffer
.data
+ (buffer
.used
= Z
.total_out
);
359 status
= inflate(&Z
, Z_NO_FLUSH
);
360 } while (Z_OK
== status
);
361 } while (Z_BUF_ERROR
== status
&& ++round
< HTTP_INFLATE_ROUNDS
);
363 if (Z_DATA_ERROR
== status
&& HTTP_WINDOW_BITS_ANY
== wbits
) {
364 /* raw deflated data? */
366 wbits
= HTTP_WINDOW_BITS_RAW
;
372 if (Z_STREAM_END
== status
) {
373 *decoded_len
= Z
.total_out
;
374 *decoded
= erealloc_rel(buffer
.data
, *decoded_len
+ 1);
375 (*decoded
)[*decoded_len
] = '\0';
378 phpstr_dtor(&buffer
);
382 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not inflate data: %s", zError(status
));
387 /* {{{ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *, int) */
388 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
)
390 int status
, level
, wbits
, strategy
, free_stream
;
392 if ((free_stream
= !s
)) {
393 s
= pemalloc_rel(sizeof(http_encoding_stream
), (flags
& HTTP_ENCODING_STREAM_PERSISTENT
));
395 memset(s
, 0, sizeof(http_encoding_stream
));
398 HTTP_DEFLATE_LEVEL_SET(flags
, level
);
399 HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
400 HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
402 if (Z_OK
== (status
= deflateInit2(&s
->stream
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
))) {
403 int p
= (flags
& HTTP_ENCODING_STREAM_PERSISTENT
) ? PHPSTR_INIT_PERSISTENT
:0;
405 if ((s
->stream
.opaque
= phpstr_init_ex(NULL
, HTTP_DEFLATE_BUFFER_SIZE
, p
))) {
408 deflateEnd(&s
->stream
);
409 status
= Z_MEM_ERROR
;
412 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to initialize deflate encoding stream: %s", zError(status
));
420 /* {{{ http_encoding_stream *http_encoding_inflate_stream_init(http_encoding_stream *, int) */
421 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
)
423 int status
, wbits
, free_stream
;
425 if ((free_stream
= !s
)) {
426 s
= pemalloc_rel(sizeof(http_encoding_stream
), (flags
& HTTP_ENCODING_STREAM_PERSISTENT
));
428 memset(s
, 0, sizeof(http_encoding_stream
));
431 HTTP_INFLATE_WBITS_SET(flags
, wbits
);
433 if (Z_OK
== (status
= inflateInit2(&s
->stream
, wbits
))) {
434 int p
= (flags
& HTTP_ENCODING_STREAM_PERSISTENT
) ? PHPSTR_INIT_PERSISTENT
:0;
436 if ((s
->stream
.opaque
= phpstr_init_ex(NULL
, HTTP_DEFLATE_BUFFER_SIZE
, p
))) {
439 inflateEnd(&s
->stream
);
440 status
= Z_MEM_ERROR
;
443 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to initialize inflate stream: %s", zError(status
));
451 /* {{{ STATUS http_encoding_deflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
452 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
)
456 /* append input to our buffer */
457 phpstr_append(PHPSTR(s
->stream
.opaque
), data
, data_len
);
459 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
460 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
463 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
464 *encoded
= emalloc_rel(*encoded_len
);
465 s
->stream
.avail_out
= *encoded_len
;
466 s
->stream
.next_out
= (Bytef
*) *encoded
;
468 switch (status
= deflate(&s
->stream
, Z_NO_FLUSH
))
472 /* cut processed chunk off the buffer */
473 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
475 /* size buffer down to actual size */
476 *encoded_len
-= s
->stream
.avail_out
;
477 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
478 (*encoded
)[*encoded_len
] = '\0';
483 STR_SET(*encoded
, NULL
);
485 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to update deflate stream: %s", zError(status
));
490 /* {{{ STATUS http_encoding_inflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
491 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
)
493 int status
, round
= 0;
495 /* append input to buffer */
496 phpstr_append(PHPSTR(s
->stream
.opaque
), data
, data_len
);
500 *decoded_len
= data_len
<< 1;
505 *decoded
= erealloc_rel(*decoded
, *decoded_len
);
508 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
509 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
511 s
->stream
.next_out
= (Bytef
*) *decoded
;
512 s
->stream
.avail_out
= *decoded_len
;
514 switch (status
= inflate(&s
->stream
, Z_NO_FLUSH
))
519 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
522 *decoded_len
-= s
->stream
.avail_out
;
523 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
524 (*decoded
)[*decoded_len
] = '\0';
529 /* raw deflated data ? */
530 if (!(s
->flags
& HTTP_INFLATE_TYPE_RAW
) && !s
->stream
.total_out
) {
531 inflateEnd(&s
->stream
);
532 s
->flags
|= HTTP_INFLATE_TYPE_RAW
;
533 inflateInit2(&s
->stream
, HTTP_WINDOW_BITS_RAW
);
534 goto retry_raw_inflate
;
538 } while (Z_BUF_ERROR
== status
&& ++round
< HTTP_INFLATE_ROUNDS
);
540 STR_SET(*decoded
, NULL
);
542 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to update inflate stream: %s", zError(status
));
547 /* {{{ STATUS http_encoding_deflate_stream_flush(http_encoding_stream *, char **, size_t *) */
548 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
)
552 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
;
553 *encoded
= emalloc_rel(*encoded_len
);
555 s
->stream
.avail_in
= 0;
556 s
->stream
.next_in
= NULL
;
557 s
->stream
.avail_out
= *encoded_len
;
558 s
->stream
.next_out
= (Bytef
*) *encoded
;
560 switch (status
= deflate(&s
->stream
, Z_SYNC_FLUSH
))
564 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
- s
->stream
.avail_out
;
565 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
566 (*encoded
)[*encoded_len
] = '\0';
571 STR_SET(*encoded
, NULL
);
573 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to flush deflate stream: %s", zError(status
));
578 /* {{{ STATUS http_encoding_inflate_straem_flush(http_encoding_stream *, char **, size_t *) */
579 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
)
582 *decoded
= estrndup("", *decoded_len
= 0);
587 /* {{{ STATUS http_encoding_deflate_stream_finish(http_encoding_stream *, char **, size_t *) */
588 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
)
592 *encoded_len
= HTTP_DEFLATE_BUFFER_SIZE
;
593 *encoded
= emalloc_rel(*encoded_len
);
595 /* deflate remaining input */
596 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
597 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
599 s
->stream
.avail_out
= *encoded_len
;
600 s
->stream
.next_out
= (Bytef
*) *encoded
;
603 status
= deflate(&s
->stream
, Z_FINISH
);
604 } while (Z_OK
== status
);
606 if (Z_STREAM_END
== status
) {
607 /* cut processed intp off */
608 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
611 *encoded_len
-= s
->stream
.avail_out
;
612 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
613 (*encoded
)[*encoded_len
] = '\0';
617 STR_SET(*encoded
, NULL
);
619 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish deflate stream: %s", zError(status
));
624 /* {{{ STATUS http_encoding_inflate_stream_finish(http_encoding_stream *, char **, size_t *) */
625 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
)
629 *decoded_len
= (PHPSTR_LEN(s
->stream
.opaque
) + 1) * HTTP_INFLATE_ROUNDS
;
630 *decoded
= emalloc_rel(*decoded_len
);
632 /* inflate remaining input */
633 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
634 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
636 s
->stream
.avail_out
= *decoded_len
;
637 s
->stream
.next_out
= (Bytef
*) *decoded
;
639 if (Z_STREAM_END
== (status
= inflate(&s
->stream
, Z_FINISH
))) {
640 /* cut processed input off */
641 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
644 *decoded_len
-= s
->stream
.avail_out
;
645 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
646 (*decoded
)[*decoded_len
] = '\0';
650 STR_SET(*decoded
, NULL
);
652 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish inflate stream: %s", zError(status
));
657 /* {{{ void http_encoding_deflate_stream_dtor(http_encoding_stream *) */
658 PHP_HTTP_API
void _http_encoding_deflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
661 if (s
->stream
.opaque
) {
662 phpstr_free((phpstr
**) &s
->stream
.opaque
);
664 deflateEnd(&s
->stream
);
669 /* {{{ void http_encoding_inflate_stream_dtor(http_encoding_stream *) */
670 PHP_HTTP_API
void _http_encoding_inflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
673 if (s
->stream
.opaque
) {
674 phpstr_free((phpstr
**) &s
->stream
.opaque
);
676 inflateEnd(&s
->stream
);
681 /* {{{ void http_encoding_deflate_stream_free(http_encoding_stream **) */
682 PHP_HTTP_API
void _http_encoding_deflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
685 http_encoding_deflate_stream_dtor(*s
);
687 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
694 /* {{{ void http_encoding_inflate_stream_free(http_encoding_stream **) */
695 PHP_HTTP_API
void _http_encoding_inflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
698 http_encoding_inflate_stream_dtor(*s
);
700 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
707 /* {{{ void http_ob_deflatehandler(char *, uint, char **, uint *, int) */
708 void _http_ob_deflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
712 *handled_output
= NULL
;
713 *handled_output_len
= 0;
715 if (mode
& PHP_OUTPUT_HANDLER_START
) {
718 if (G
->send
.deflate
.stream
) {
719 zend_error(E_ERROR
, "ob_deflatehandler() can only be used once");
723 G
->send
.deflate
.encoding
= !0;
725 switch (http_encoding_response_start(0))
727 case HTTP_ENCODING_GZIP
:
728 flags
= HTTP_DEFLATE_TYPE_GZIP
;
731 case HTTP_ENCODING_DEFLATE
:
732 flags
= HTTP_DEFLATE_TYPE_ZLIB
;
736 goto deflate_passthru_plain
;
740 flags
|= (G
->send
.deflate
.start_flags
&~ 0xf0);
741 G
->send
.deflate
.stream
= http_encoding_deflate_stream_init(NULL
, flags
);
744 if (G
->send
.deflate
.stream
) {
745 http_encoding_deflate_stream_update((http_encoding_stream
*) G
->send
.deflate
.stream
, output
, output_len
, handled_output
, handled_output_len
);
747 if (mode
& PHP_OUTPUT_HANDLER_END
) {
748 char *remaining
= NULL
;
749 size_t remaining_len
= 0;
751 http_encoding_deflate_stream_finish((http_encoding_stream
*) G
->send
.deflate
.stream
, &remaining
, &remaining_len
);
752 http_encoding_deflate_stream_free((http_encoding_stream
**) &G
->send
.deflate
.stream
);
754 *handled_output
= erealloc(*handled_output
, *handled_output_len
+ remaining_len
+ 1);
755 memcpy(*handled_output
+ *handled_output_len
, remaining
, remaining_len
);
756 (*handled_output
)[*handled_output_len
+= remaining_len
] = '\0';
761 deflate_passthru_plain
:
762 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
767 /* {{{ void http_ob_inflatehandler(char *, uint, char **, uint *, int) */
768 void _http_ob_inflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
772 *handled_output
= NULL
;
773 *handled_output_len
= 0;
775 if (mode
& PHP_OUTPUT_HANDLER_START
) {
776 if (G
->send
.inflate
.stream
) {
777 zend_error(E_ERROR
, "ob_inflatehandler() can only be used once");
780 G
->send
.inflate
.stream
= http_encoding_inflate_stream_init(NULL
, (HTTP_G(send
).inflate
.start_flags
&~ 0xf0));
783 if (G
->send
.inflate
.stream
) {
784 http_encoding_inflate_stream_update((http_encoding_stream
*) G
->send
.inflate
.stream
, output
, output_len
, handled_output
, handled_output_len
);
786 if (mode
& PHP_OUTPUT_HANDLER_END
) {
787 char *remaining
= NULL
;
788 size_t remaining_len
= 0;
790 http_encoding_inflate_stream_finish((http_encoding_stream
*) G
->send
.inflate
.stream
, &remaining
, &remaining_len
);
791 http_encoding_inflate_stream_free((http_encoding_stream
**) &G
->send
.inflate
.stream
);
793 *handled_output
= erealloc(*handled_output
, *handled_output_len
+ remaining_len
+ 1);
794 memcpy(*handled_output
+ *handled_output_len
, remaining
, remaining_len
);
795 (*handled_output
)[*handled_output_len
+= remaining_len
] = '\0';
800 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
805 #endif /* HTTP_HAVE_ZLIB */
812 * vim600: noet sw=4 ts=4 fdm=marker
813 * vim<600: noet sw=4 ts=4