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, data_len
- 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, data_len
- 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
= 0x800;
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
)
499 *decoded_len
= 0x800;
500 *decoded
= emalloc_rel(*decoded_len
);
502 s
->stream
.avail_in
= 0;
503 s
->stream
.next_in
= NULL
;
504 s
->stream
.avail_out
= *decoded_len
;
505 s
->stream
.next_out
= (Bytef
*) *decoded
;
507 switch (status
= inflate(&s
->stream
, Z_SYNC_FLUSH
))
511 *decoded_len
= 0x800 - s
->stream
.avail_out
;
512 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
513 (*decoded
)[*decoded_len
] = '\0';
518 STR_SET(*decoded
, NULL
);
520 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to flush inflate stream: %s", zError(status
));
524 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
)
528 *encoded_len
= 0x800;
529 *encoded
= emalloc_rel(*encoded_len
);
531 /* deflate remaining input */
532 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
533 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
535 s
->stream
.avail_out
= *encoded_len
;
536 s
->stream
.next_out
= (Bytef
*) *encoded
;
539 status
= deflate(&s
->stream
, Z_FINISH
);
540 } while (Z_OK
== status
);
542 if (Z_STREAM_END
== status
) {
543 /* cut processed intp off */
544 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
547 *encoded_len
-= s
->stream
.avail_out
;
548 *encoded
= erealloc_rel(*encoded
, *encoded_len
+ 1);
549 (*encoded
)[*encoded_len
] = '\0';
553 STR_SET(*encoded
, NULL
);
555 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish deflate stream: %s", zError(status
));
559 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
)
563 *decoded_len
= s
->stream
.avail_in
<< 2;
564 *decoded
= emalloc_rel(*decoded_len
);
566 /* inflate remaining input */
567 s
->stream
.next_in
= (Bytef
*) PHPSTR_VAL(s
->stream
.opaque
);
568 s
->stream
.avail_in
= PHPSTR_LEN(s
->stream
.opaque
);
570 s
->stream
.avail_out
= *decoded_len
;
571 s
->stream
.next_out
= (Bytef
*) *decoded
;
573 if (Z_STREAM_END
== (status
= inflate(&s
->stream
, Z_FINISH
))) {
574 /* cut processed input off */
575 phpstr_cut(PHPSTR(s
->stream
.opaque
), 0, PHPSTR_LEN(s
->stream
.opaque
) - s
->stream
.avail_in
);
578 *decoded_len
-= s
->stream
.avail_out
;
579 *decoded
= erealloc_rel(*decoded
, *decoded_len
+ 1);
580 (*decoded
)[*decoded_len
] = '\0';
584 STR_SET(*decoded
, NULL
);
586 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Failed to finish inflate stream: %s", zError(status
));
590 PHP_HTTP_API
void _http_encoding_deflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
593 if (s
->stream
.opaque
) {
594 phpstr_free((phpstr
**) &s
->stream
.opaque
);
596 deflateEnd(&s
->stream
);
600 PHP_HTTP_API
void _http_encoding_inflate_stream_dtor(http_encoding_stream
*s TSRMLS_DC
)
603 if (s
->stream
.opaque
) {
604 phpstr_free((phpstr
**) &s
->stream
.opaque
);
606 inflateEnd(&s
->stream
);
610 PHP_HTTP_API
void _http_encoding_deflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
613 http_encoding_deflate_stream_dtor(*s
);
615 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
621 PHP_HTTP_API
void _http_encoding_inflate_stream_free(http_encoding_stream
**s TSRMLS_DC
)
624 http_encoding_inflate_stream_dtor(*s
);
626 pefree(*s
, (*s
)->flags
& HTTP_ENCODING_STREAM_PERSISTENT
);
632 void _http_ob_deflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
636 *handled_output
= NULL
;
637 *handled_output_len
= 0;
639 if (mode
& PHP_OUTPUT_HANDLER_START
) {
642 if (G
->send
.deflate
.stream
) {
643 zend_error(E_ERROR
, "ob_deflatehandler() can only be used once");
647 G
->send
.deflate
.encoding
= !0;
649 switch (http_encoding_response_start(0))
651 case HTTP_ENCODING_GZIP
:
652 flags
= HTTP_DEFLATE_TYPE_GZIP
;
655 case HTTP_ENCODING_DEFLATE
:
656 flags
= HTTP_DEFLATE_TYPE_ZLIB
;
660 goto deflate_passthru_plain
;
664 flags
|= (G
->send
.deflate
.start_flags
&~ 0xf0);
665 G
->send
.deflate
.stream
= http_encoding_deflate_stream_init(NULL
, flags
);
668 if (G
->send
.deflate
.stream
) {
669 http_encoding_deflate_stream_update((http_encoding_stream
*) G
->send
.deflate
.stream
, output
, output_len
, handled_output
, handled_output_len
);
671 if (mode
& PHP_OUTPUT_HANDLER_END
) {
672 char *remaining
= NULL
;
673 size_t remaining_len
= 0;
675 http_encoding_deflate_stream_finish((http_encoding_stream
*) G
->send
.deflate
.stream
, &remaining
, &remaining_len
);
676 http_encoding_deflate_stream_free((http_encoding_stream
**) &G
->send
.deflate
.stream
);
678 *handled_output
= erealloc(*handled_output
, *handled_output_len
+ remaining_len
+ 1);
679 memcpy(*handled_output
+ *handled_output_len
, remaining
, remaining_len
);
680 (*handled_output
)[*handled_output_len
+= remaining_len
] = '\0';
685 deflate_passthru_plain
:
686 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
690 void _http_ob_inflatehandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
694 *handled_output
= NULL
;
695 *handled_output_len
= 0;
697 if (mode
& PHP_OUTPUT_HANDLER_START
) {
698 if (G
->send
.inflate
.stream
) {
699 zend_error(E_ERROR
, "ob_inflatehandler() can only be used once");
702 G
->send
.inflate
.stream
= http_encoding_inflate_stream_init(NULL
, (HTTP_G(send
).inflate
.start_flags
&~ 0xf0));
705 if (G
->send
.inflate
.stream
) {
706 http_encoding_inflate_stream_update((http_encoding_stream
*) G
->send
.inflate
.stream
, output
, output_len
, handled_output
, handled_output_len
);
708 if (mode
& PHP_OUTPUT_HANDLER_END
) {
709 char *remaining
= NULL
;
710 size_t remaining_len
= 0;
712 http_encoding_inflate_stream_finish((http_encoding_stream
*) G
->send
.inflate
.stream
, &remaining
, &remaining_len
);
713 http_encoding_inflate_stream_free((http_encoding_stream
**) &G
->send
.inflate
.stream
);
715 *handled_output
= erealloc(*handled_output
, *handled_output_len
+ remaining_len
+ 1);
716 memcpy(*handled_output
+ *handled_output_len
, remaining
, remaining_len
);
717 (*handled_output
)[*handled_output_len
+= remaining_len
] = '\0';
722 *handled_output
= estrndup(output
, *handled_output_len
= output_len
);
726 static const char http_encoding_gzip_header
[] = {
727 (const char) 0x1f, // fixed value
728 (const char) 0x8b, // fixed value
729 (const char) Z_DEFLATED
, // compression algorithm
730 (const char) 0, // none of the possible flags defined by the GZIP "RFC"
731 (const char) 0, // MTIME
732 (const char) 0, // =*=
733 (const char) 0, // =*=
734 (const char) 0, // =*=
735 (const char) 0, // two possible flag values for 9 compression levels? o_O
737 (const char) 0x0b // OS_CODE
739 (const char) 0x03 // OS_CODE
743 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
)
745 size_t offset
= sizeof(http_encoding_gzip_header
);
747 if (data_len
< offset
) {
748 goto really_bad_gzip_header
;
751 if (data
[0] != (const char) 0x1F || data
[1] != (const char) 0x8B) {
752 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));
756 if (data
[2] != (const char) Z_DEFLATED
) {
757 http_error_ex(error_level TSRMLS_CC
, HTTP_E_ENCODING
, "Unrecognized compression format (%d)", (int) (data
[2] & 0xFF));
758 /* still try to decode */
760 if ((data
[3] & 0x4) == 0x4) {
761 if (data_len
< offset
+ 2) {
762 goto really_bad_gzip_header
;
764 /* there are extra fields, the length follows the common header as 2 bytes LSB */
765 offset
+= (unsigned) ((data
[offset
] & 0xFF));
767 offset
+= (unsigned) ((data
[offset
] & 0xFF) << 8);
770 if ((data
[3] & 0x8) == 0x8) {
771 if (data_len
<= offset
) {
772 goto really_bad_gzip_header
;
774 /* there's a file name */
775 offset
+= strlen(&data
[offset
]) + 1 /*NUL*/;
777 if ((data
[3] & 0x10) == 0x10) {
778 if (data_len
<= offset
) {
779 goto really_bad_gzip_header
;
781 /* there's a comment */
782 offset
+= strlen(&data
[offset
]) + 1 /* NUL */;
784 if ((data
[3] & 0x2) == 0x2) {
785 /* there's a CRC16 of the header */
787 if (data_len
<= offset
) {
788 goto really_bad_gzip_header
;
792 cmp
= (unsigned) ((data
[offset
-2] & 0xFF));
793 cmp
+= (unsigned) ((data
[offset
-1] & 0xFF) << 8);
795 crc
= crc32(0L, Z_NULL
, 0);
796 crc
= crc32(crc
, (const Bytef
*) data
, sizeof(http_encoding_gzip_header
));
798 if (cmp
!= (crc
& 0xFFFF)) {
799 http_error_ex(error_level TSRMLS_CC
, HTTP_E_ENCODING
, "GZIP headers CRC checksums so not match (%lu, %lu)", cmp
, crc
& 0xFFFF);
805 if (data_len
< offset
+ 8) {
806 http_error(error_level TSRMLS_CC
, HTTP_E_ENCODING
, "Missing or truncated GZIP footer");
811 *encoded
= data
+ offset
;
814 *encoded_len
= data_len
- offset
- 8 /* size of the assumed GZIP footer */;
819 really_bad_gzip_header
:
820 http_error(error_level TSRMLS_CC
, HTTP_E_ENCODING
, "Missing or truncated GZIP header");
824 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
)
826 STATUS status
= SUCCESS
;
829 crc
= crc32(0L, Z_NULL
, 0);
830 crc
= crc32(crc
, (const Bytef
*) decoded
, decoded_len
);
832 cmp
= (unsigned) ((data
[data_len
-8] & 0xFF));
833 cmp
+= (unsigned) ((data
[data_len
-7] & 0xFF) << 8);
834 cmp
+= (unsigned) ((data
[data_len
-6] & 0xFF) << 16);
835 cmp
+= (unsigned) ((data
[data_len
-5] & 0xFF) << 24);
836 len
= (unsigned) ((data
[data_len
-4] & 0xFF));
837 len
+= (unsigned) ((data
[data_len
-3] & 0xFF) << 8);
838 len
+= (unsigned) ((data
[data_len
-2] & 0xFF) << 16);
839 len
+= (unsigned) ((data
[data_len
-1] & 0xFF) << 24);
842 http_error_ex(error_level TSRMLS_CC
, HTTP_E_ENCODING
, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp
, crc
);
845 if (len
!= decoded_len
) {
846 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
);
852 #endif /* HTTP_HAVE_ZLIB */
854 PHP_HTTP_API
int _http_encoding_response_start(size_t content_length TSRMLS_DC
)
856 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC
) ||
857 php_ob_handler_used("zlib output compression" TSRMLS_CC
)) {
858 HTTP_G(send
).deflate
.encoding
= 0;
860 if (!HTTP_G(send
).deflate
.encoding
) {
861 /* emit a content-length header */
862 if (content_length
) {
863 char cl_header_str
[128];
864 size_t cl_header_len
;
865 cl_header_len
= snprintf(cl_header_str
, lenof(cl_header_str
), "Content-Length: %zu", content_length
);
866 http_send_header_string_ex(cl_header_str
, cl_header_len
, 1);
869 #ifndef HTTP_HAVE_ZLIB
870 HTTP_G(send
).deflate
.encoding
= 0;
871 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC
);
876 INIT_PZVAL(&zsupported
);
877 array_init(&zsupported
);
878 add_next_index_stringl(&zsupported
, "gzip", lenof("gzip"), 1);
879 add_next_index_stringl(&zsupported
, "x-gzip", lenof("x-gzip"), 1);
880 add_next_index_stringl(&zsupported
, "deflate", lenof("deflate"), 1);
882 HTTP_G(send
).deflate
.encoding
= 0;
884 if ((selected
= http_negotiate_encoding(&zsupported
))) {
886 char *encoding
= NULL
;
889 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(selected
, &encoding
, &idx
, 0) && encoding
) {
890 if (!strcmp(encoding
, "gzip") || !strcmp(encoding
, "x-gzip")) {
891 if (SUCCESS
== (hs
= http_send_header_string("Content-Encoding: gzip"))) {
892 HTTP_G(send
).deflate
.encoding
= HTTP_ENCODING_GZIP
;
894 } else if (!strcmp(encoding
, "deflate")) {
895 if (SUCCESS
== (hs
= http_send_header_string("Content-Encoding: deflate"))) {
896 HTTP_G(send
).deflate
.encoding
= HTTP_ENCODING_DEFLATE
;
900 http_send_header_string("Vary: Accept-Encoding");
904 zend_hash_destroy(selected
);
905 FREE_HASHTABLE(selected
);
908 zval_dtor(&zsupported
);
909 return HTTP_G(send
).deflate
.encoding
;
921 * vim600: noet sw=4 ts=4 fdm=marker
922 * vim<600: noet sw=4 ts=4