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