- allow to avoid deps on shared extensions on build time
[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
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 TSRMLS_DC)
162 {
163 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
164 php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
165 HTTP_G->send.deflate.encoding = 0;
166 } else {
167 if (!HTTP_G->send.deflate.encoding) {
168 /* emit a content-length header */
169 if (content_length) {
170 char cl_header_str[128];
171 size_t cl_header_len;
172 cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %zu", content_length);
173 http_send_header_string_ex(cl_header_str, cl_header_len, 1);
174 }
175 } else {
176 #ifndef HTTP_HAVE_ZLIB
177 HTTP_G->send.deflate.encoding = 0;
178 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
179 #else
180 HashTable *selected;
181 zval zsupported;
182
183 INIT_PZVAL(&zsupported);
184 array_init(&zsupported);
185 add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
186 add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
187 add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
188
189 HTTP_G->send.deflate.encoding = 0;
190
191 if ((selected = http_negotiate_encoding(&zsupported))) {
192 STATUS hs = FAILURE;
193 char *encoding = NULL;
194 ulong idx;
195
196 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
197 if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
198 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
199 HTTP_G->send.deflate.encoding = HTTP_ENCODING_GZIP;
200 }
201 } else if (!strcmp(encoding, "deflate")) {
202 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
203 HTTP_G->send.deflate.encoding = HTTP_ENCODING_DEFLATE;
204 }
205 }
206 if (SUCCESS == hs) {
207 http_send_header_string("Vary: Accept-Encoding");
208 }
209 }
210
211 zend_hash_destroy(selected);
212 FREE_HASHTABLE(selected);
213 }
214
215 zval_dtor(&zsupported);
216 return HTTP_G->send.deflate.encoding;
217 #endif
218 }
219 }
220 return 0;
221 }
222 /* }}} */
223
224 #ifdef HTTP_HAVE_ZLIB
225
226 /* {{{ inline int http_inflate_rounds */
227 static inline int http_inflate_rounds(z_stream *Z, int flush, char **buf, size_t *len)
228 {
229 int status = 0, round = 0;
230 phpstr buffer;
231
232 *buf = NULL;
233 *len = 0;
234
235 phpstr_init_ex(&buffer, Z->avail_in, PHPSTR_INIT_PREALLOC);
236
237 do {
238 if (PHPSTR_NOMEM == phpstr_resize_ex(&buffer, buffer.size, 0, 1)) {
239 status = Z_MEM_ERROR;
240 } else {
241 Z->avail_out = buffer.free;
242 Z->next_out = (Bytef *) buffer.data + buffer.used;
243 #if 0
244 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);
245 #endif
246 status = inflate(Z, flush);
247
248 buffer.used += buffer.free - Z->avail_out;
249 buffer.free = Z->avail_out;
250 #if 0
251 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);
252 #endif
253 HTTP_INFLATE_BUFFER_SIZE_ALIGN(buffer.size);
254 }
255 } while ((Z_BUF_ERROR == status || (Z_OK == status && Z->avail_in)) && ++round < HTTP_INFLATE_ROUNDS);
256
257 if (status == Z_OK || status == Z_STREAM_END) {
258 phpstr_shrink(&buffer);
259 phpstr_fix(&buffer);
260 *buf = buffer.data;
261 *len = buffer.used;
262 } else {
263 phpstr_dtor(&buffer);
264 }
265
266 return status;
267 }
268 /* }}} */
269
270 /* {{{ STATUS http_encoding_deflate(int, char *, size_t, char **, size_t *) */
271 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)
272 {
273 int status, level, wbits, strategy;
274 z_stream Z;
275
276 HTTP_DEFLATE_LEVEL_SET(flags, level);
277 HTTP_DEFLATE_WBITS_SET(flags, wbits);
278 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
279
280 memset(&Z, 0, sizeof(z_stream));
281 *encoded = NULL;
282 *encoded_len = 0;
283
284 status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
285 if (Z_OK == status) {
286 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
287 *encoded = emalloc_rel(*encoded_len);
288
289 Z.next_in = (Bytef *) data;
290 Z.next_out = (Bytef *) *encoded;
291 Z.avail_in = data_len;
292 Z.avail_out = *encoded_len;
293
294 status = deflate(&Z, Z_FINISH);
295 deflateEnd(&Z);
296
297 if (Z_STREAM_END == status) {
298 /* size buffer down to actual length */
299 *encoded = erealloc_rel(*encoded, Z.total_out + 1);
300 (*encoded)[*encoded_len = Z.total_out] = '\0';
301 return SUCCESS;
302 } else {
303 STR_SET(*encoded, NULL);
304 *encoded_len = 0;
305 }
306 }
307
308 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
309 return FAILURE;
310 }
311 /* }}} */
312
313 /* {{{ STATUS http_encoding_inflate(char *, size_t, char **, size_t) */
314 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)
315 {
316 z_stream Z;
317 int status, wbits = HTTP_WINDOW_BITS_ANY;
318
319 memset(&Z, 0, sizeof(z_stream));
320
321 retry_raw_inflate:
322 status = inflateInit2(&Z, wbits);
323 if (Z_OK == status) {
324 Z.next_in = (Bytef *) data;
325 Z.avail_in = data_len;
326
327 switch (status = http_inflate_rounds(&Z, Z_NO_FLUSH, decoded, decoded_len)) {
328 case Z_OK:
329 case Z_STREAM_END:
330 return SUCCESS;
331
332 case Z_DATA_ERROR:
333 /* raw deflated data? */
334 if (HTTP_WINDOW_BITS_ANY == wbits) {
335 inflateEnd(&Z);
336 wbits = HTTP_WINDOW_BITS_RAW;
337 goto retry_raw_inflate;
338 }
339 }
340 inflateEnd(&Z);
341 }
342
343 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
344 return FAILURE;
345 }
346 /* }}} */
347
348 /* {{{ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *, int) */
349 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)
350 {
351 int status, level, wbits, strategy, free_stream;
352
353 if ((free_stream = !s)) {
354 s = pemalloc_rel(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
355 }
356 memset(s, 0, sizeof(http_encoding_stream));
357 s->flags = flags;
358
359 HTTP_DEFLATE_LEVEL_SET(flags, level);
360 HTTP_DEFLATE_WBITS_SET(flags, wbits);
361 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
362
363 if (Z_OK == (status = deflateInit2(&s->stream, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
364 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
365
366 if ((s->stream.opaque = phpstr_init_ex(NULL, HTTP_DEFLATE_BUFFER_SIZE, p))) {
367 return s;
368 }
369 deflateEnd(&s->stream);
370 status = Z_MEM_ERROR;
371 }
372
373 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize deflate encoding stream: %s", zError(status));
374 if (free_stream) {
375 efree(s);
376 }
377 return NULL;
378 }
379 /* }}} */
380
381 /* {{{ http_encoding_stream *http_encoding_inflate_stream_init(http_encoding_stream *, int) */
382 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)
383 {
384 int status, wbits, free_stream;
385
386 if ((free_stream = !s)) {
387 s = pemalloc_rel(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
388 }
389 memset(s, 0, sizeof(http_encoding_stream));
390 s->flags = flags;
391
392 HTTP_INFLATE_WBITS_SET(flags, wbits);
393
394 if (Z_OK == (status = inflateInit2(&s->stream, wbits))) {
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 inflateEnd(&s->stream);
401 status = Z_MEM_ERROR;
402 }
403
404 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize inflate stream: %s", zError(status));
405 if (free_stream) {
406 efree(s);
407 }
408 return NULL;
409 }
410 /* }}} */
411
412 /* {{{ STATUS http_encoding_deflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
413 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)
414 {
415 int status;
416
417 /* append input to our buffer */
418 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
419
420 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
421 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
422
423 /* deflate */
424 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
425 *encoded = emalloc_rel(*encoded_len);
426 s->stream.avail_out = *encoded_len;
427 s->stream.next_out = (Bytef *) *encoded;
428
429 switch (status = deflate(&s->stream, HTTP_ENCODING_STREAM_FLUSH_FLAG(s->flags))) {
430 case Z_OK:
431 case Z_STREAM_END:
432 /* cut processed chunk off the buffer */
433 if (s->stream.avail_in) {
434 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
435 } else {
436 phpstr_reset(PHPSTR(s->stream.opaque));
437 }
438
439 /* size buffer down to actual size */
440 *encoded_len -= s->stream.avail_out;
441 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
442 (*encoded)[*encoded_len] = '\0';
443 return SUCCESS;
444 }
445
446 STR_SET(*encoded, NULL);
447 *encoded_len = 0;
448 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
449 return FAILURE;
450 }
451 /* }}} */
452
453 /* {{{ STATUS http_encoding_inflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
454 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)
455 {
456 int status;
457
458 /* append input to buffer */
459 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
460
461 retry_raw_inflate:
462 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
463 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
464
465 switch (status = http_inflate_rounds(&s->stream, HTTP_ENCODING_STREAM_FLUSH_FLAG(s->flags), decoded, decoded_len)) {
466 case Z_OK:
467 case Z_STREAM_END:
468 /* cut off */
469 if (s->stream.avail_in) {
470 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
471 } else {
472 phpstr_reset(PHPSTR(s->stream.opaque));
473 }
474 return SUCCESS;
475
476 case Z_DATA_ERROR:
477 /* raw deflated data ? */
478 if (!(s->flags & HTTP_INFLATE_TYPE_RAW) && !s->stream.total_out) {
479 inflateEnd(&s->stream);
480 s->flags |= HTTP_INFLATE_TYPE_RAW;
481 inflateInit2(&s->stream, HTTP_WINDOW_BITS_RAW);
482 goto retry_raw_inflate;
483 }
484 }
485
486 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update inflate stream: %s", zError(status));
487 return FAILURE;
488 }
489 /* }}} */
490
491 /* {{{ STATUS http_encoding_deflate_stream_flush(http_encoding_stream *, char **, size_t *) */
492 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)
493 {
494 int status;
495
496 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE;
497 *encoded = emalloc_rel(*encoded_len);
498
499 s->stream.avail_in = 0;
500 s->stream.next_in = NULL;
501 s->stream.avail_out = *encoded_len;
502 s->stream.next_out = (Bytef *) *encoded;
503
504 switch (status = deflate(&s->stream, Z_FULL_FLUSH)) {
505 case Z_OK:
506 case Z_STREAM_END:
507 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE - s->stream.avail_out;
508 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
509 (*encoded)[*encoded_len] = '\0';
510 return SUCCESS;
511 }
512
513 STR_SET(*encoded, NULL);
514 *encoded_len = 0;
515 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
516 return FAILURE;
517 }
518 /* }}} */
519
520 /* {{{ STATUS http_encoding_inflate_straem_flush(http_encoding_stream *, char **, size_t *) */
521 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)
522 {
523 /* noop */
524 *decoded = estrndup("", *decoded_len = 0);
525 return SUCCESS;
526 }
527 /* }}} */
528
529 /* {{{ STATUS http_encoding_deflate_stream_finish(http_encoding_stream *, char **, size_t *) */
530 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)
531 {
532 int status;
533
534 *encoded_len = HTTP_DEFLATE_BUFFER_SIZE;
535 *encoded = emalloc_rel(*encoded_len);
536
537 /* deflate remaining input */
538 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
539 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
540
541 s->stream.avail_out = *encoded_len;
542 s->stream.next_out = (Bytef *) *encoded;
543
544 do {
545 status = deflate(&s->stream, Z_FINISH);
546 } while (Z_OK == status);
547
548 if (Z_STREAM_END == status) {
549 /* cut processed intp off */
550 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
551
552 /* size down */
553 *encoded_len -= s->stream.avail_out;
554 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
555 (*encoded)[*encoded_len] = '\0';
556 return SUCCESS;
557 }
558
559 STR_SET(*encoded, NULL);
560 *encoded_len = 0;
561 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
562 return FAILURE;
563 }
564 /* }}} */
565
566 /* {{{ STATUS http_encoding_inflate_stream_finish(http_encoding_stream *, char **, size_t *) */
567 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)
568 {
569 int status;
570
571 if (!PHPSTR_LEN(s->stream.opaque)) {
572 *decoded = NULL;
573 *decoded_len = 0;
574 return SUCCESS;
575 }
576
577 *decoded_len = (PHPSTR_LEN(s->stream.opaque) + 1) * HTTP_INFLATE_ROUNDS;
578 *decoded = emalloc_rel(*decoded_len);
579
580 /* inflate remaining input */
581 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
582 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
583
584 s->stream.avail_out = *decoded_len;
585 s->stream.next_out = (Bytef *) *decoded;
586
587 if (Z_STREAM_END == (status = inflate(&s->stream, Z_FINISH))) {
588 /* cut processed input off */
589 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
590
591 /* size down */
592 *decoded_len -= s->stream.avail_out;
593 *decoded = erealloc_rel(*decoded, *decoded_len + 1);
594 (*decoded)[*decoded_len] = '\0';
595 return SUCCESS;
596 }
597
598 STR_SET(*decoded, NULL);
599 *decoded_len = 0;
600 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
601 return FAILURE;
602 }
603 /* }}} */
604
605 /* {{{ void http_encoding_deflate_stream_dtor(http_encoding_stream *) */
606 PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
607 {
608 if (s) {
609 if (s->stream.opaque) {
610 phpstr_free((phpstr **) &s->stream.opaque);
611 }
612 deflateEnd(&s->stream);
613 }
614 }
615 /* }}} */
616
617 /* {{{ void http_encoding_inflate_stream_dtor(http_encoding_stream *) */
618 PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
619 {
620 if (s) {
621 if (s->stream.opaque) {
622 phpstr_free((phpstr **) &s->stream.opaque);
623 }
624 inflateEnd(&s->stream);
625 }
626 }
627 /* }}} */
628
629 /* {{{ void http_encoding_deflate_stream_free(http_encoding_stream **) */
630 PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
631 {
632 if (s) {
633 http_encoding_deflate_stream_dtor(*s);
634 if (*s) {
635 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
636 }
637 *s = NULL;
638 }
639 }
640 /* }}} */
641
642 /* {{{ void http_encoding_inflate_stream_free(http_encoding_stream **) */
643 PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
644 {
645 if (s) {
646 http_encoding_inflate_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_ob_deflatehandler(char *, uint, char **, uint *, int) */
656 void _http_ob_deflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
657 {
658 *handled_output = NULL;
659 *handled_output_len = 0;
660
661 if (mode & PHP_OUTPUT_HANDLER_START) {
662 int flags;
663
664 if (HTTP_G->send.deflate.stream) {
665 zend_error(E_ERROR, "ob_deflatehandler() can only be used once");
666 return;
667 }
668
669 HTTP_G->send.deflate.encoding = !0;
670
671 switch (http_encoding_response_start(0)) {
672 case HTTP_ENCODING_GZIP:
673 flags = HTTP_DEFLATE_TYPE_GZIP;
674 break;
675
676 case HTTP_ENCODING_DEFLATE:
677 flags = HTTP_DEFLATE_TYPE_ZLIB;
678 break;
679
680 default:
681 goto deflate_passthru_plain;
682 }
683
684 flags |= (HTTP_G->send.deflate.start_flags &~ 0xf0);
685 HTTP_G->send.deflate.stream = http_encoding_deflate_stream_init(NULL, flags);
686 }
687
688 if (HTTP_G->send.deflate.stream) {
689 http_encoding_deflate_stream_update((http_encoding_stream *) HTTP_G->send.deflate.stream, output, output_len, handled_output, handled_output_len);
690
691 if (mode & PHP_OUTPUT_HANDLER_END) {
692 char *remaining = NULL;
693 size_t remaining_len = 0;
694
695 http_encoding_deflate_stream_finish((http_encoding_stream *) HTTP_G->send.deflate.stream, &remaining, &remaining_len);
696 http_encoding_deflate_stream_free((http_encoding_stream **) &HTTP_G->send.deflate.stream);
697 if (remaining) {
698 *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
699 memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
700 (*handled_output)[*handled_output_len += remaining_len] = '\0';
701 efree(remaining);
702 }
703 }
704 } else {
705 deflate_passthru_plain:
706 *handled_output = estrndup(output, *handled_output_len = output_len);
707 }
708 }
709 /* }}} */
710
711 /* {{{ void http_ob_inflatehandler(char *, uint, char **, uint *, int) */
712 void _http_ob_inflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
713 {
714 *handled_output = NULL;
715 *handled_output_len = 0;
716
717 if (mode & PHP_OUTPUT_HANDLER_START) {
718 if (HTTP_G->send.inflate.stream) {
719 zend_error(E_ERROR, "ob_inflatehandler() can only be used once");
720 return;
721 }
722 HTTP_G->send.inflate.stream = http_encoding_inflate_stream_init(NULL, (HTTP_G->send.inflate.start_flags &~ 0xf0));
723 }
724
725 if (HTTP_G->send.inflate.stream) {
726 http_encoding_inflate_stream_update((http_encoding_stream *) HTTP_G->send.inflate.stream, output, output_len, handled_output, handled_output_len);
727
728 if (mode & PHP_OUTPUT_HANDLER_END) {
729 char *remaining = NULL;
730 size_t remaining_len = 0;
731
732 http_encoding_inflate_stream_finish((http_encoding_stream *) HTTP_G->send.inflate.stream, &remaining, &remaining_len);
733 http_encoding_inflate_stream_free((http_encoding_stream **) &HTTP_G->send.inflate.stream);
734 if (remaining) {
735 *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
736 memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
737 (*handled_output)[*handled_output_len += remaining_len] = '\0';
738 efree(remaining);
739 }
740 }
741 } else {
742 *handled_output = estrndup(output, *handled_output_len = output_len);
743 }
744 }
745 /* }}} */
746
747 #endif /* HTTP_HAVE_ZLIB */
748
749 /*
750 * Local variables:
751 * tab-width: 4
752 * c-basic-offset: 4
753 * End:
754 * vim600: noet sw=4 ts=4 fdm=marker
755 * vim<600: noet sw=4 ts=4
756 */
757