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"
28 PHP_MINIT_FUNCTION(http_encoding
)
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
);
44 PHP_RINIT_FUNCTION(http_encoding
)
48 if (G
->send
.inflate
.start_auto
) {
49 php_ob_set_internal_handler(_http_ob_inflatehandler
, 0x1000, "http inflate", 0 TSRMLS_CC
);
51 if (G
->send
.deflate
.start_auto
) {
52 php_ob_set_internal_handler(_http_ob_deflatehandler
, 0x8000, "http deflate", 0 TSRMLS_CC
);
57 PHP_RSHUTDOWN_FUNCTION(http_encoding
)
61 if (G
->send
.deflate
.stream
) {
62 http_encoding_deflate_stream_free((http_encoding_stream
**) &G
->send
.deflate
.stream
);
64 if (G
->send
.inflate
.stream
) {
65 http_encoding_inflate_stream_free((http_encoding_stream
**) &G
->send
.inflate
.stream
);
71 static inline int eol_match(char **line
, int *eol_len
)
75 while (0x20 == *ptr
) ++ptr
;
77 if (ptr
== http_locate_eol(*line
, eol_len
)) {
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
)
90 const char *e_ptr
= encoded
;
93 *decoded
= ecalloc(1, encoded_len
);
95 while ((encoded
+ encoded_len
- e_ptr
) > 0) {
96 ulong chunk_len
= 0, rest
;
98 chunk_len
= strtoul(e_ptr
, &n_ptr
, 16);
100 /* we could not read in chunk size */
101 if (n_ptr
== e_ptr
) {
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
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
;
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
);
119 /* reached the end */
121 /* move over '0' chunked encoding terminator */
122 while (*e_ptr
== '0') ++e_ptr
;
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
)) {
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));
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
);
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
);
143 memcpy(*decoded
+ *decoded_len
, n_ptr
, chunk_len
);
144 *decoded_len
+= chunk_len
;
146 if (chunk_len
== rest
) {
147 e_ptr
= n_ptr
+ chunk_len
;
150 /* advance to next chunk */
151 e_ptr
= n_ptr
+ chunk_len
+ eol_len
;
159 #ifdef HTTP_HAVE_ZLIB
161 #define HTTP_DEFLATE_LEVEL_SET(flags, level) \
162 switch (flags & 0xf) \
165 if ((flags & 0xf) < 10) { \
166 level = flags & 0xf; \
169 case HTTP_DEFLATE_LEVEL_DEF: \
170 level = Z_DEFAULT_COMPRESSION; \
174 #define HTTP_DEFLATE_WBITS_SET(flags, wbits) \
175 switch (flags & 0xf0) \
177 case HTTP_DEFLATE_TYPE_GZIP: \
178 wbits = HTTP_WINDOW_BITS_GZIP; \
180 case HTTP_DEFLATE_TYPE_RAW: \
181 wbits = HTTP_WINDOW_BITS_RAW; \
184 wbits = HTTP_WINDOW_BITS_ZLIB; \
188 #define HTTP_INFLATE_WBITS_SET(flags, wbits) \
189 if (flags & HTTP_INFLATE_TYPE_RAW) { \
190 wbits = HTTP_WINDOW_BITS_RAW; \
192 wbits = HTTP_WINDOW_BITS_ANY; \
195 #define HTTP_DEFLATE_STRATEGY_SET(flags, strategy) \
196 switch (flags & 0xf00) \
198 case HTTP_DEFLATE_STRATEGY_FILT: \
199 strategy = Z_FILTERED; \
201 case HTTP_DEFLATE_STRATEGY_HUFF: \
202 strategy = Z_HUFFMAN_ONLY; \
204 case HTTP_DEFLATE_STRATEGY_RLE: \
207 case HTTP_DEFLATE_STRATEGY_FIXED: \
208 strategy = Z_FIXED; \
211 strategy = Z_DEFAULT_STRATEGY; \
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
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
)
222 int status
, level
, wbits
, strategy
;
225 HTTP_DEFLATE_LEVEL_SET(flags
, level
);
226 HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
227 HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
229 memset(&Z
, 0, sizeof(z_stream
));
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
);
238 Z
.next_in
= (Bytef
*) data
;
239 Z
.next_out
= (Bytef
*) *encoded
;
240 Z
.avail_in
= data_len
;
241 Z
.avail_out
= *encoded_len
;
243 status
= deflate(&Z
, Z_FINISH
);
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';
252 STR_SET(*encoded
, NULL
);
257 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not deflate data: %s (%s)", zError(status
));
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
)
263 int status
, max
= 0, wbits
= HTTP_WINDOW_BITS_ANY
;
267 memset(&Z
, 0, sizeof(z_stream
));
271 phpstr_init_ex(&buffer
, data_len
<< 2, PHPSTR_INIT_PREALLOC
);
272 buffer
.size
= data_len
;
275 status
= inflateInit2(&Z
, wbits
);
276 if (Z_OK
== status
) {
277 Z
.next_in
= (Bytef
*) data
;
278 Z
.avail_in
= data_len
;
281 phpstr_resize(&buffer
, data_len
<< 2);
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
);
290 if (Z_DATA_ERROR
== status
&& HTTP_WINDOW_BITS_ANY
== wbits
) {
291 /* raw deflated data? */
293 wbits
= HTTP_WINDOW_BITS_RAW
;
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';
305 phpstr_dtor(&buffer
);
309 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not inflate data: %s", zError(status
));
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
)
316 int status
, level
, wbits
, strategy
, free_stream
;
318 if ((free_stream
= !s
)) {
319 s
= pemalloc_rel(sizeof(http_encoding_stream
), (flags
& HTTP_ENCODING_STREAM_PERSISTENT
));
321 memset(s
, 0, sizeof(http_encoding_stream
));
324 HTTP_DEFLATE_LEVEL_SET(flags
, level
);
325 HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
326 HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
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;
331 if ((s
->stream
.opaque
= phpstr_init_ex(NULL
, 0x8000, p
))) {
334 deflateEnd(&s
->stream
);
335 status
= Z_MEM_ERROR
;
338 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to initialize deflate encoding stream: %s", zError(status
));
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
)
347 int status
, wbits
, free_stream
;
349 if ((free_stream
= !s
)) {
350 s
= pemalloc_rel(sizeof(http_encoding_stream
), (flags
& HTTP_ENCODING_STREAM_PERSISTENT
));
352 memset(s
, 0, sizeof(http_encoding_stream
));
355 HTTP_INFLATE_WBITS_SET(flags
, wbits
);
357 if (Z_OK
== (status
= inflateInit2(&s
->stream
, wbits
))) {
358 int p
= (flags
& HTTP_ENCODING_STREAM_PERSISTENT
) ? PHPSTR_INIT_PERSISTENT
:0;
360 if ((s
->stream
.opaque
= phpstr_init_ex(NULL
, 0x8000, p
))) {
363 inflateEnd(&s
->stream
);
364 status
= Z_MEM_ERROR
;
367 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to initialize inflate stream: %s", zError(status
));
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
)
378 /* append input to our buffer */
379 phpstr_append(PHPSTR(s
->stream
.opaque
), data
, data_len
);
381 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
382 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
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
;
390 switch (status
= deflate(&s
->stream
, Z_NO_FLUSH
))
394 /* cut processed chunk off the buffer */
395 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
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';
405 STR_SET(*encoded
, NULL
);
407 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to update deflate stream: %s", zError(status
));
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
)
415 /* append input to buffer */
416 phpstr_append(PHPSTR(s
->stream
.opaque
), data
, data_len
);
420 *decoded_len
= data_len
<< 1;
425 *decoded
= erealloc_rel(*decoded
, *decoded_len
);
428 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
429 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
431 s
->stream
.next_out
= (Bytef
*) *decoded
;
432 s
->stream
.avail_out
= *decoded_len
;
434 switch (status
= inflate(&s
->stream
, Z_NO_FLUSH
))
439 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
442 *decoded_len
-= s
->stream
.avail_out
;
443 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
444 (*decoded
)[*decoded_len
] = '\0';
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
;
458 } while (Z_BUF_ERROR
== status
&& ++max
< HTTP_ENCODING_MAXTRY
);
460 STR_SET(*decoded
, NULL
);
462 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to update inflate stream: %s", zError(status
));
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
)
470 *encoded_len
= 0x8000;
471 *encoded
= emalloc_rel(*encoded_len
);
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
;
478 switch (status
= deflate(&s
->stream
, Z_SYNC_FLUSH
))
482 *encoded_len
= 0x800 - s
->stream
.avail_out
;
483 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
484 (*encoded
)[*encoded_len
] = '\0';
489 STR_SET(*encoded
, NULL
);
491 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to flush deflate stream: %s", zError(status
));
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
)
498 *decoded
= estrndup("", *decoded_len
= 0);
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
)
506 *encoded_len
= 0x8000;
507 *encoded
= emalloc_rel(*encoded_len
);
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
);
513 s
->stream
.avail_out
= *encoded_len
;
514 s
->stream
.next_out
= (Bytef
*) *encoded
;
517 status
= deflate(&s
->stream
, Z_FINISH
);
518 } while (Z_OK
== status
);
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
);
525 *encoded_len
-= s
->stream
.avail_out
;
526 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
527 (*encoded
)[*encoded_len
] = '\0';
531 STR_SET(*encoded
, NULL
);
533 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish deflate stream: %s", zError(status
));
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
)
541 *decoded_len
= s
->stream
.avail_in
<< 2;
542 *decoded
= emalloc_rel(*decoded_len
);
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
);
548 s
->stream
.avail_out
= *decoded_len
;
549 s
->stream
.next_out
= (Bytef
*) *decoded
;
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
);
556 *decoded_len
-= s
->stream
.avail_out
;
557 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
558 (*decoded
)[*decoded_len
] = '\0';
562 STR_SET(*decoded
, NULL
);
564 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish inflate stream: %s", zError(status
));
568 PHP_HTTP_API
void _http_encoding_deflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
571 if (s
->stream
.opaque
) {
572 phpstr_free((phpstr
**) &s
->stream
.opaque
);
574 deflateEnd(&s
->stream
);
578 PHP_HTTP_API
void _http_encoding_inflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
581 if (s
->stream
.opaque
) {
582 phpstr_free((phpstr
**) &s
->stream
.opaque
);
584 inflateEnd(&s
->stream
);
588 PHP_HTTP_API
void _http_encoding_deflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
591 http_encoding_deflate_stream_dtor(*s
);
593 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
599 PHP_HTTP_API
void _http_encoding_inflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
602 http_encoding_inflate_stream_dtor(*s
);
604 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
610 void _http_ob_deflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
614 *handled_output
= NULL
;
615 *handled_output_len
= 0;
617 if (mode
& PHP_OUTPUT_HANDLER_START
) {
620 if (G
->send
.deflate
.stream
) {
621 zend_error(E_ERROR
, "ob_deflatehandler() can only be used once");
625 G
->send
.deflate
.encoding
= !0;
627 switch (http_encoding_response_start(0))
629 case HTTP_ENCODING_GZIP
:
630 flags
= HTTP_DEFLATE_TYPE_GZIP
;
633 case HTTP_ENCODING_DEFLATE
:
634 flags
= HTTP_DEFLATE_TYPE_ZLIB
;
638 goto deflate_passthru_plain
;
642 flags
|= (G
->send
.deflate
.start_flags
&~ 0xf0);
643 G
->send
.deflate
.stream
= http_encoding_deflate_stream_init(NULL
, flags
);
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
);
649 if (mode
& PHP_OUTPUT_HANDLER_END
) {
650 char *remaining
= NULL
;
651 size_t remaining_len
= 0;
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
);
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';
663 deflate_passthru_plain
:
664 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
668 void _http_ob_inflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
672 *handled_output
= NULL
;
673 *handled_output_len
= 0;
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");
680 G
->send
.inflate
.stream
= http_encoding_inflate_stream_init(NULL
, (HTTP_G(send
).inflate
.start_flags
&~ 0xf0));
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
);
686 if (mode
& PHP_OUTPUT_HANDLER_END
) {
687 char *remaining
= NULL
;
688 size_t remaining_len
= 0;
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
);
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';
700 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
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
715 (const char) 0x0b // OS_CODE
717 (const char) 0x03 // OS_CODE
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
)
723 size_t offset
= sizeof(http_encoding_gzip_header
);
725 if (data_len
< offset
) {
726 goto really_bad_gzip_header
;
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));
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 */
738 if ((data
[3] & 0x4) == 0x4) {
739 if (data_len
< offset
+ 2) {
740 goto really_bad_gzip_header
;
742 /* there are extra fields, the length follows the common header as 2 bytes LSB */
743 offset
+= (unsigned) ((data
[offset
] & 0xFF));
745 offset
+= (unsigned) ((data
[offset
] & 0xFF) << 8);
748 if ((data
[3] & 0x8) == 0x8) {
749 if (data_len
<= offset
) {
750 goto really_bad_gzip_header
;
752 /* there's a file name */
753 offset
+= strlen(&data
[offset
]) + 1 /*NUL*/;
755 if ((data
[3] & 0x10) == 0x10) {
756 if (data_len
<= offset
) {
757 goto really_bad_gzip_header
;
759 /* there's a comment */
760 offset
+= strlen(&data
[offset
]) + 1 /* NUL */;
762 if ((data
[3] & 0x2) == 0x2) {
763 /* there's a CRC16 of the header */
765 if (data_len
<= offset
) {
766 goto really_bad_gzip_header
;
770 cmp
= (unsigned) ((data
[offset
-2] & 0xFF));
771 cmp
+= (unsigned) ((data
[offset
-1] & 0xFF) << 8);
773 crc
= crc32(0L, Z_NULL
, 0);
774 crc
= crc32(crc
, (const Bytef
*) data
, sizeof(http_encoding_gzip_header
));
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);
783 if (data_len
< offset
+ 8) {
784 http_error(error_level TSRMLS_CC
, HTTP_E_ENCODING
, "Missing or truncated GZIP footer");
789 *encoded
= data
+ offset
;
792 *encoded_len
= data_len
- offset
- 8 /* size of the assumed GZIP footer */;
797 really_bad_gzip_header
:
798 http_error(error_level TSRMLS_CC
, HTTP_E_ENCODING
, "Missing or truncated GZIP header");
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
)
804 STATUS status
= SUCCESS
;
807 crc
= crc32(0L, Z_NULL
, 0);
808 crc
= crc32(crc
, (const Bytef
*) decoded
, decoded_len
);
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);
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
);
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
);
830 #endif /* HTTP_HAVE_ZLIB */
832 PHP_HTTP_API
int _http_encoding_response_start(size_t content_length TSRMLS_DC
)
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;
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);
847 #ifndef HTTP_HAVE_ZLIB
848 HTTP_G(send
).deflate
.encoding
= 0;
849 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC
);
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);
860 HTTP_G(send
).deflate
.encoding
= 0;
862 if ((selected
= http_negotiate_encoding(&zsupported
))) {
864 char *encoding
= NULL
;
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
;
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
;
878 http_send_header_string("Vary: Accept-Encoding");
882 zend_hash_destroy(selected
);
883 FREE_HASHTABLE(selected
);
886 zval_dtor(&zsupported
);
887 return HTTP_G(send
).deflate
.encoding
;
899 * vim600: noet sw=4 ts=4 fdm=marker
900 * vim<600: noet sw=4 ts=4