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