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