fixed bug #16893 Content not decoded with zlib support and compress enabled
[m6w6/ext-http] / http_encoding_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_ZLIB
16 #include "php_http.h"
17
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"
22
23 /* {{{ */
24 #ifdef HTTP_HAVE_ZLIB
25 PHP_MINIT_FUNCTION(http_encoding)
26 {
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);
38
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);
42
43 return SUCCESS;
44 }
45
46 PHP_RINIT_FUNCTION(http_encoding)
47 {
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);
50 }
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);
53 }
54 return SUCCESS;
55 }
56
57 PHP_RSHUTDOWN_FUNCTION(http_encoding)
58 {
59 if (HTTP_G->send.deflate.stream) {
60 http_encoding_deflate_stream_free((http_encoding_stream **) &HTTP_G->send.deflate.stream);
61 }
62 if (HTTP_G->send.inflate.stream) {
63 http_encoding_inflate_stream_free((http_encoding_stream **) &HTTP_G->send.inflate.stream);
64 }
65 return SUCCESS;
66 }
67 #endif
68 /* }}} */
69
70 /* {{{ eol_match(char **, int *) */
71 static inline int eol_match(char **line, int *eol_len)
72 {
73 char *ptr = *line;
74
75 while (' ' == *ptr) ++ptr;
76
77 if (ptr == http_locate_eol(*line, eol_len)) {
78 *line = ptr;
79 return 1;
80 } else {
81 return 0;
82 }
83 }
84 /* }}} */
85
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)
88 {
89 int eol_len = 0;
90 char *n_ptr = NULL;
91 const char *e_ptr = encoded;
92
93 *decoded_len = 0;
94 *decoded = ecalloc(1, encoded_len);
95
96 while ((encoded + encoded_len - e_ptr) > 0) {
97 ulong chunk_len = 0, rest;
98
99 chunk_len = strtoul(e_ptr, &n_ptr, 16);
100
101 /* we could not read in chunk size */
102 if (n_ptr == e_ptr) {
103 /*
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
107 */
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;
113 } else {
114 efree(*decoded);
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);
116 return NULL;
117 }
118 }
119
120 /* reached the end */
121 if (!chunk_len) {
122 /* move over '0' chunked encoding terminator */
123 while (*e_ptr == '0') ++e_ptr;
124 break;
125 }
126
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)) {
129 if (eol_len == 2) {
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));
131 } else {
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);
133 }
134 }
135 n_ptr += eol_len;
136
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);
140 chunk_len = rest;
141 }
142
143 /* copy the chunk */
144 memcpy(*decoded + *decoded_len, n_ptr, chunk_len);
145 *decoded_len += chunk_len;
146
147 if (chunk_len == rest) {
148 e_ptr = n_ptr + chunk_len;
149 break;
150 } else {
151 /* advance to next chunk */
152 e_ptr = n_ptr + chunk_len + eol_len;
153 }
154 }
155
156 return e_ptr;
157 }
158 /* }}} */
159
160 /* {{{ int http_encoding_response_start(size_t) */
161 PHP_HTTP_API int _http_encoding_response_start(size_t content_length, zend_bool ignore_http_ohandler TSRMLS_DC)
162 {
163 int response = HTTP_G->send.deflate.response;
164 int ohandler = php_ob_handler_used("ob_gzhandler" TSRMLS_CC) || php_ob_handler_used("zlib output compression" TSRMLS_CC);
165
166 if (!ohandler && !ignore_http_ohandler) {
167 ohandler = php_ob_handler_used("ob_deflatehandler" TSRMLS_CC) || php_ob_handler_used("http deflate" TSRMLS_CC);
168 }
169
170 if (response && !ohandler) {
171 #ifdef HTTP_HAVE_ZLIB
172 HashTable *selected;
173 zval zsupported;
174
175 HTTP_G->send.deflate.encoding = 0;
176
177 INIT_PZVAL(&zsupported);
178 array_init(&zsupported);
179 add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
180 add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
181 add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
182
183 if ((selected = http_negotiate_encoding(&zsupported))) {
184 STATUS hs = FAILURE;
185 char *encoding = NULL;
186 ulong idx;
187
188 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
189 if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
190 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
191 HTTP_G->send.deflate.encoding = HTTP_ENCODING_GZIP;
192 }
193 } else if (!strcmp(encoding, "deflate")) {
194 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
195 HTTP_G->send.deflate.encoding = HTTP_ENCODING_DEFLATE;
196 }
197 }
198 if (SUCCESS == hs) {
199 http_send_header_string("Vary: Accept-Encoding");
200 }
201 }
202
203 zend_hash_destroy(selected);
204 FREE_HASHTABLE(selected);
205 }
206
207 zval_dtor(&zsupported);
208 #else
209 HTTP_G->send.deflate.encoding = 0;
210 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
211 #endif /* HTTP_HAVE_ZLIB */
212 } else if (content_length && !ohandler) {
213 /* emit a content-length header */
214 phpstr header;
215
216 phpstr_init(&header);
217 phpstr_appendf(&header, "Content-Length: %zu", content_length);
218 phpstr_fix(&header);
219 http_send_header_string_ex(PHPSTR_VAL(&header), PHPSTR_LEN(&header), 1);
220 phpstr_dtor(&header);
221 } else {
222 HTTP_G->send.deflate.encoding = 0;
223 }
224
225 return HTTP_G->send.deflate.encoding;
226 }
227 /* }}} */
228
229 #ifdef HTTP_HAVE_ZLIB
230
231 /* {{{ inline int http_inflate_rounds */
232 static inline int http_inflate_rounds(z_stream *Z, int flush, char **buf, size_t *len)
233 {
234 int status = 0, round = 0;
235 phpstr buffer;
236
237 *buf = NULL;
238 *len = 0;
239
240 phpstr_init_ex(&buffer, Z->avail_in, PHPSTR_INIT_PREALLOC);
241
242 do {
243 if (PHPSTR_NOMEM == phpstr_resize_ex(&buffer, buffer.size, 0, 1)) {
244 status = Z_MEM_ERROR;
245 } else {
246 Z->avail_out = buffer.free;
247 Z->next_out = (Bytef *) buffer.data + buffer.used;
248 #if 0
249 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);
250 #endif
251 status = inflate(Z, flush);
252
253 buffer.used += buffer.free - Z->avail_out;
254 buffer.free = Z->avail_out;
255 #if 0
256 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);
257 #endif
258 HTTP_INFLATE_BUFFER_SIZE_ALIGN(buffer.size);
259 }
260 } while ((Z_BUF_ERROR == status || (Z_OK == status && Z->avail_in)) && ++round < HTTP_INFLATE_ROUNDS);
261
262 if (status == Z_OK || status == Z_STREAM_END) {
263 phpstr_shrink(&buffer);
264 phpstr_fix(&buffer);
265 *buf = buffer.data;
266 *len = buffer.used;
267 } else {
268 phpstr_dtor(&buffer);
269 }
270
271 return status;
272 }
273 /* }}} */
274
275 /* {{{ STATUS http_encoding_deflate(int, char *, size_t, char **, size_t *) */
276 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)
277 {
278 int status, level, wbits, strategy;
279 z_stream Z;
280
281 HTTP_DEFLATE_LEVEL_SET(flags, level);
282 HTTP_DEFLATE_WBITS_SET(flags, wbits);
283 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
284
285 memset(&Z, 0, sizeof(z_stream));
286 *encoded = NULL;
287 *encoded_len = 0;
288
289 status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
290 if (Z_OK == status) {
291 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
292 *encoded = emalloc_rel(*encoded_len);
293
294 Z.next_in = (Bytef *) data;
295 Z.next_out = (Bytef *) *encoded;
296 Z.avail_in = data_len;
297 Z.avail_out = *encoded_len;
298
299 status = deflate(&Z, Z_FINISH);
300 deflateEnd(&Z);
301
302 if (Z_STREAM_END == status) {
303 /* size buffer down to actual length */
304 *encoded = erealloc_rel(*encoded, Z.total_out + 1);
305 (*encoded)[*encoded_len = Z.total_out] = '\0';
306 return SUCCESS;
307 } else {
308 STR_SET(*encoded, NULL);
309 *encoded_len = 0;
310 }
311 }
312
313 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
314 return FAILURE;
315 }
316 /* }}} */
317
318 /* {{{ STATUS http_encoding_inflate(char *, size_t, char **, size_t) */
319 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)
320 {
321 z_stream Z;
322 int status, wbits = HTTP_WINDOW_BITS_ANY;
323
324 memset(&Z, 0, sizeof(z_stream));
325
326 retry_raw_inflate:
327 status = inflateInit2(&Z, wbits);
328 if (Z_OK == status) {
329 Z.next_in = (Bytef *) data;
330 Z.avail_in = data_len;
331
332 switch (status = http_inflate_rounds(&Z, Z_NO_FLUSH, decoded, decoded_len)) {
333 case Z_STREAM_END:
334 inflateEnd(&Z);
335 return SUCCESS;
336
337 case Z_OK:
338 status = Z_DATA_ERROR;
339 break;
340
341 case Z_DATA_ERROR:
342 /* raw deflated data? */
343 if (HTTP_WINDOW_BITS_ANY == wbits) {
344 inflateEnd(&Z);
345 wbits = HTTP_WINDOW_BITS_RAW;
346 goto retry_raw_inflate;
347 }
348 }
349 inflateEnd(&Z);
350
351 if (decoded_len && *decoded) {
352 efree(*decoded);
353 }
354 }
355
356 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
357 return FAILURE;
358 }
359 /* }}} */
360
361 /* {{{ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *, int) */
362 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)
363 {
364 int status, level, wbits, strategy, free_stream;
365
366 if ((free_stream = !s)) {
367 s = pemalloc_rel(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
368 }
369 memset(s, 0, sizeof(http_encoding_stream));
370 s->flags = flags;
371
372 HTTP_DEFLATE_LEVEL_SET(flags, level);
373 HTTP_DEFLATE_WBITS_SET(flags, wbits);
374 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
375
376 if (Z_OK == (status = deflateInit2(&s->stream, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
377 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
378
379 if ((s->stream.opaque = phpstr_init_ex(NULL, HTTP_DEFLATE_BUFFER_SIZE, p))) {
380 return s;
381 }
382 deflateEnd(&s->stream);
383 status = Z_MEM_ERROR;
384 }
385
386 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize deflate encoding stream: %s", zError(status));
387 if (free_stream) {
388 efree(s);
389 }
390 return NULL;
391 }
392 /* }}} */
393
394 /* {{{ http_encoding_stream *http_encoding_inflate_stream_init(http_encoding_stream *, int) */
395 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)
396 {
397 int status, wbits, free_stream;
398
399 if ((free_stream = !s)) {
400 s = pemalloc_rel(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
401 }
402 memset(s, 0, sizeof(http_encoding_stream));
403 s->flags = flags;
404
405 HTTP_INFLATE_WBITS_SET(flags, wbits);
406
407 if (Z_OK == (status = inflateInit2(&s->stream, wbits))) {
408 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
409
410 if ((s->stream.opaque = phpstr_init_ex(NULL, HTTP_DEFLATE_BUFFER_SIZE, p))) {
411 return s;
412 }
413 inflateEnd(&s->stream);
414 status = Z_MEM_ERROR;
415 }
416
417 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize inflate stream: %s", zError(status));
418 if (free_stream) {
419 efree(s);
420 }
421 return NULL;
422 }
423 /* }}} */
424
425 /* {{{ STATUS http_encoding_deflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
426 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)
427 {
428 int status;
429
430 /* append input to our buffer */
431 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
432
433 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
434 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
435
436 /* deflate */
437 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
438 *encoded = emalloc_rel(*encoded_len);
439 s->stream.avail_out = *encoded_len;
440 s->stream.next_out = (Bytef *) *encoded;
441
442 switch (status = deflate(&s->stream, HTTP_ENCODING_STREAM_FLUSH_FLAG(s->flags))) {
443 case Z_OK:
444 case Z_STREAM_END:
445 /* cut processed chunk off the buffer */
446 if (s->stream.avail_in) {
447 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
448 } else {
449 phpstr_reset(PHPSTR(s->stream.opaque));
450 }
451
452 /* size buffer down to actual size */
453 *encoded_len -= s->stream.avail_out;
454 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
455 (*encoded)[*encoded_len] = '\0';
456 return SUCCESS;
457 }
458
459 STR_SET(*encoded, NULL);
460 *encoded_len = 0;
461 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
462 return FAILURE;
463 }
464 /* }}} */
465
466 /* {{{ STATUS http_encoding_inflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
467 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)
468 {
469 int status;
470
471 /* append input to buffer */
472 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
473
474 retry_raw_inflate:
475 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
476 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
477
478 switch (status = http_inflate_rounds(&s->stream, HTTP_ENCODING_STREAM_FLUSH_FLAG(s->flags), decoded, decoded_len)) {
479 case Z_OK:
480 case Z_STREAM_END:
481 /* cut off */
482 if (s->stream.avail_in) {
483 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
484 } else {
485 phpstr_reset(PHPSTR(s->stream.opaque));
486 }
487 return SUCCESS;
488
489 case Z_DATA_ERROR:
490 /* raw deflated data ? */
491 if (!(s->flags & HTTP_INFLATE_TYPE_RAW) && !s->stream.total_out) {
492 inflateEnd(&s->stream);
493 s->flags |= HTTP_INFLATE_TYPE_RAW;
494 inflateInit2(&s->stream, HTTP_WINDOW_BITS_RAW);
495 goto retry_raw_inflate;
496 }
497 }
498
499 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update inflate stream: %s", zError(status));
500 return FAILURE;
501 }
502 /* }}} */
503
504 /* {{{ STATUS http_encoding_deflate_stream_flush(http_encoding_stream *, char **, size_t *) */
505 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)
506 {
507 int status;
508
509 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE;
510 *encoded = emalloc_rel(*encoded_len);
511
512 s->stream.avail_in = 0;
513 s->stream.next_in = NULL;
514 s->stream.avail_out = *encoded_len;
515 s->stream.next_out = (Bytef *) *encoded;
516
517 switch (status = deflate(&s->stream, Z_FULL_FLUSH)) {
518 case Z_OK:
519 case Z_STREAM_END:
520 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE - s->stream.avail_out;
521 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
522 (*encoded)[*encoded_len] = '\0';
523 return SUCCESS;
524 }
525
526 STR_SET(*encoded, NULL);
527 *encoded_len = 0;
528 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
529 return FAILURE;
530 }
531 /* }}} */
532
533 /* {{{ STATUS http_encoding_inflate_straem_flush(http_encoding_stream *, char **, size_t *) */
534 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)
535 {
536 /* noop */
537 *decoded = estrndup("", *decoded_len = 0);
538 return SUCCESS;
539 }
540 /* }}} */
541
542 /* {{{ STATUS http_encoding_deflate_stream_finish(http_encoding_stream *, char **, size_t *) */
543 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)
544 {
545 int status;
546
547 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE;
548 *encoded = emalloc_rel(*encoded_len);
549
550 /* deflate remaining input */
551 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
552 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
553
554 s->stream.avail_out = *encoded_len;
555 s->stream.next_out = (Bytef *) *encoded;
556
557 do {
558 status = deflate(&s->stream, Z_FINISH);
559 } while (Z_OK == status);
560
561 if (Z_STREAM_END == status) {
562 /* cut processed intp off */
563 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
564
565 /* size down */
566 *encoded_len -= s->stream.avail_out;
567 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
568 (*encoded)[*encoded_len] = '\0';
569 return SUCCESS;
570 }
571
572 STR_SET(*encoded, NULL);
573 *encoded_len = 0;
574 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
575 return FAILURE;
576 }
577 /* }}} */
578
579 /* {{{ STATUS http_encoding_inflate_stream_finish(http_encoding_stream *, char **, size_t *) */
580 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)
581 {
582 int status;
583
584 if (!PHPSTR_LEN(s->stream.opaque)) {
585 *decoded = NULL;
586 *decoded_len = 0;
587 return SUCCESS;
588 }
589
590 *decoded_len = (PHPSTR_LEN(s->stream.opaque) + 1) * HTTP_INFLATE_ROUNDS;
591 *decoded = emalloc_rel(*decoded_len);
592
593 /* inflate remaining input */
594 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
595 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
596
597 s->stream.avail_out = *decoded_len;
598 s->stream.next_out = (Bytef *) *decoded;
599
600 if (Z_STREAM_END == (status = inflate(&s->stream, Z_FINISH))) {
601 /* cut processed input off */
602 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
603
604 /* size down */
605 *decoded_len -= s->stream.avail_out;
606 *decoded = erealloc_rel(*decoded, *decoded_len + 1);
607 (*decoded)[*decoded_len] = '\0';
608 return SUCCESS;
609 }
610
611 STR_SET(*decoded, NULL);
612 *decoded_len = 0;
613 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
614 return FAILURE;
615 }
616 /* }}} */
617
618 /* {{{ void http_encoding_deflate_stream_dtor(http_encoding_stream *) */
619 PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
620 {
621 if (s) {
622 if (s->stream.opaque) {
623 phpstr_free((phpstr **) &s->stream.opaque);
624 }
625 deflateEnd(&s->stream);
626 }
627 }
628 /* }}} */
629
630 /* {{{ void http_encoding_inflate_stream_dtor(http_encoding_stream *) */
631 PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
632 {
633 if (s) {
634 if (s->stream.opaque) {
635 phpstr_free((phpstr **) &s->stream.opaque);
636 }
637 inflateEnd(&s->stream);
638 }
639 }
640 /* }}} */
641
642 /* {{{ void http_encoding_deflate_stream_free(http_encoding_stream **) */
643 PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
644 {
645 if (s) {
646 http_encoding_deflate_stream_dtor(*s);
647 if (*s) {
648 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
649 }
650 *s = NULL;
651 }
652 }
653 /* }}} */
654
655 /* {{{ void http_encoding_inflate_stream_free(http_encoding_stream **) */
656 PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
657 {
658 if (s) {
659 http_encoding_inflate_stream_dtor(*s);
660 if (*s) {
661 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
662 }
663 *s = NULL;
664 }
665 }
666 /* }}} */
667
668 /* {{{ void http_ob_deflatehandler(char *, uint, char **, uint *, int) */
669 void _http_ob_deflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
670 {
671 int encoding;
672
673 *handled_output = NULL;
674 *handled_output_len = 0;
675
676 if (mode & PHP_OUTPUT_HANDLER_START) {
677 int flags;
678
679 if (HTTP_G->send.deflate.stream) {
680 zend_error(E_ERROR, "ob_deflatehandler() can only be used once");
681 return;
682 }
683
684 HTTP_G->send.deflate.response = 1;
685 encoding = http_encoding_response_start(0, 1);
686 HTTP_G->send.deflate.response = 0;
687
688 switch (encoding) {
689 case HTTP_ENCODING_GZIP:
690 flags = HTTP_DEFLATE_TYPE_GZIP;
691 break;
692
693 case HTTP_ENCODING_DEFLATE:
694 flags = HTTP_DEFLATE_TYPE_ZLIB;
695 break;
696
697 default:
698 goto deflate_passthru_plain;
699 }
700
701 flags |= (HTTP_G->send.deflate.start_flags &~ 0xf0);
702 HTTP_G->send.deflate.stream = http_encoding_deflate_stream_init(NULL, flags);
703 }
704
705 if (HTTP_G->send.deflate.stream) {
706 if (output_len) {
707 size_t tmp_len;
708
709 http_encoding_deflate_stream_update((http_encoding_stream *) HTTP_G->send.deflate.stream, output, output_len, handled_output, &tmp_len);
710 *handled_output_len = tmp_len;
711 }
712
713 if (mode & PHP_OUTPUT_HANDLER_END) {
714 char *remaining = NULL;
715 size_t remaining_len = 0;
716
717 http_encoding_deflate_stream_finish((http_encoding_stream *) HTTP_G->send.deflate.stream, &remaining, &remaining_len);
718 http_encoding_deflate_stream_free((http_encoding_stream **) &HTTP_G->send.deflate.stream);
719 if (remaining) {
720 *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
721 memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
722 (*handled_output)[*handled_output_len += remaining_len] = '\0';
723 efree(remaining);
724 }
725 }
726 } else {
727 deflate_passthru_plain:
728 *handled_output = estrndup(output, *handled_output_len = output_len);
729 }
730 }
731 /* }}} */
732
733 /* {{{ void http_ob_inflatehandler(char *, uint, char **, uint *, int) */
734 void _http_ob_inflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
735 {
736 *handled_output = NULL;
737 *handled_output_len = 0;
738
739 if (mode & PHP_OUTPUT_HANDLER_START) {
740 if (HTTP_G->send.inflate.stream) {
741 zend_error(E_ERROR, "ob_inflatehandler() can only be used once");
742 return;
743 }
744 HTTP_G->send.inflate.stream = http_encoding_inflate_stream_init(NULL, (HTTP_G->send.inflate.start_flags &~ 0xf0));
745 }
746
747 if (HTTP_G->send.inflate.stream) {
748 if (output_len) {
749 size_t tmp_len;
750
751 http_encoding_inflate_stream_update((http_encoding_stream *) HTTP_G->send.inflate.stream, output, output_len, handled_output, &tmp_len);
752 *handled_output_len = tmp_len;
753 }
754
755 if (mode & PHP_OUTPUT_HANDLER_END) {
756 char *remaining = NULL;
757 size_t remaining_len = 0;
758
759 http_encoding_inflate_stream_finish((http_encoding_stream *) HTTP_G->send.inflate.stream, &remaining, &remaining_len);
760 http_encoding_inflate_stream_free((http_encoding_stream **) &HTTP_G->send.inflate.stream);
761 if (remaining) {
762 *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
763 memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
764 (*handled_output)[*handled_output_len += remaining_len] = '\0';
765 efree(remaining);
766 }
767 }
768 } else {
769 *handled_output = estrndup(output, *handled_output_len = output_len);
770 }
771 }
772 /* }}} */
773
774 #endif /* HTTP_HAVE_ZLIB */
775
776 /*
777 * Local variables:
778 * tab-width: 4
779 * c-basic-offset: 4
780 * End:
781 * vim600: noet sw=4 ts=4 fdm=marker
782 * vim<600: noet sw=4 ts=4
783 */
784