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