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