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