- fix previous commit and adjust tests
[m6w6/ext-http] / http_encoding_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "php_http_encoding_api.h"
24 #include "php_http.h"
25 #include "php_http_api.h"
26
27 #ifdef HTTP_HAVE_ZLIB
28 # include "php_http_send_api.h"
29 # include "php_http_headers_api.h"
30 # include <zlib.h>
31 #endif
32
33 ZEND_EXTERN_MODULE_GLOBALS(http);
34
35 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
36 PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
37 {
38 const char *e_ptr;
39 char *d_ptr;
40 long rest;
41
42 *decoded_len = 0;
43 *decoded = ecalloc(1, encoded_len);
44 d_ptr = *decoded;
45 e_ptr = encoded;
46
47 while ((rest = encoded + encoded_len - e_ptr) > 0) {
48 long chunk_len = 0;
49 int EOL_len = 0, eol_mismatch = 0;
50 char *n_ptr;
51
52 chunk_len = strtol(e_ptr, &n_ptr, 16);
53
54 /* check if:
55 * - we could not read in chunk size
56 * - we got a negative chunk size
57 * - chunk size is greater then remaining size
58 * - chunk size is not followed by (CR)LF|NUL
59 */
60 if ( (n_ptr == e_ptr) || (chunk_len < 0) || (chunk_len > rest) ||
61 (*n_ptr && (eol_mismatch = (n_ptr != http_locate_eol(e_ptr, &EOL_len))))) {
62 /* don't fail on apperently not encoded data */
63 if (e_ptr == encoded) {
64 memcpy(*decoded, encoded, encoded_len);
65 *decoded_len = encoded_len;
66 return encoded + encoded_len;
67 } else {
68 efree(*decoded);
69 if (eol_mismatch) {
70 if (EOL_len == 2) {
71 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0D 0x0A; got: 0x%X 0x%X)", *n_ptr, *(n_ptr + 1));
72 } else {
73 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0A; got: 0x%X)", *n_ptr);
74 }
75 } else {
76 char *error = estrndup(n_ptr, strcspn(n_ptr, "\r\n "));
77 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid chunk size: '%s' at pos %d", error, n_ptr - encoded);
78 efree(error);
79 }
80 return NULL;
81 }
82 } else {
83 e_ptr = n_ptr;
84 }
85
86 /* reached the end */
87 if (!chunk_len) {
88 break;
89 }
90
91 memcpy(d_ptr, e_ptr += EOL_len, chunk_len);
92 d_ptr += chunk_len;
93 e_ptr += chunk_len + EOL_len;
94 *decoded_len += chunk_len;
95 }
96
97 return e_ptr;
98 }
99 /* }}} */
100
101 #ifdef HTTP_HAVE_ZLIB
102
103 static const char http_encoding_gzip_header[] = {
104 (const char) 0x1f, // fixed value
105 (const char) 0x8b, // fixed value
106 (const char) Z_DEFLATED, // compression algorithm
107 (const char) 0, // none of the possible flags defined by the GZIP "RFC"
108 (const char) 0, // no MTIME available (4 bytes)
109 (const char) 0, // =*=
110 (const char) 0, // =*=
111 (const char) 0, // =*=
112 (const char) 0, // two possible flag values for 9 compression levels? o_O
113 (const char) 0x03 // assume *nix OS
114 };
115
116 inline void http_init_gzencode_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
117 {
118 Z->zalloc = Z_NULL;
119 Z->zfree = Z_NULL;
120 Z->opaque = Z_NULL;
121
122 Z->next_in = (Bytef *) data;
123 Z->avail_in = data_len;
124 Z->avail_out = HTTP_ENCODING_BUFLEN(data_len) + HTTP_ENCODING_SAFPAD - 1;
125
126 *buf_ptr = emalloc(HTTP_ENCODING_BUFLEN(data_len) + sizeof(http_encoding_gzip_header) + HTTP_ENCODING_SAFPAD);
127 memcpy(*buf_ptr, http_encoding_gzip_header, sizeof(http_encoding_gzip_header));
128
129 Z->next_out = *buf_ptr + sizeof(http_encoding_gzip_header);
130 }
131
132 inline void http_init_deflate_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
133 {
134 Z->zalloc = Z_NULL;
135 Z->zfree = Z_NULL;
136 Z->opaque = Z_NULL;
137
138 Z->data_type = Z_UNKNOWN;
139 Z->next_in = (Bytef *) data;
140 Z->avail_in = data_len;
141 Z->avail_out = HTTP_ENCODING_BUFLEN(data_len) - 1;
142 Z->next_out = emalloc(HTTP_ENCODING_BUFLEN(data_len));
143
144 *buf_ptr = Z->next_out;
145 }
146
147 inline void http_init_uncompress_buffer(size_t data_len, char **buf_ptr, size_t *buf_len, int *iteration)
148 {
149 if (!*iteration) {
150 *buf_len = data_len * 2;
151 *buf_ptr = emalloc(*buf_len + 1);
152 } else {
153 size_t new_len = *buf_len << 2;
154 char *new_ptr = erealloc(*buf_ptr, new_len + 1);
155
156 if (new_ptr) {
157 *buf_ptr = new_ptr;
158 *buf_len = new_len;
159 } else {
160 *iteration = INT_MAX;
161 }
162 }
163 }
164
165 inline void http_init_inflate_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr, size_t *buf_len, int *iteration)
166 {
167 Z->zalloc = Z_NULL;
168 Z->zfree = Z_NULL;
169
170 http_init_uncompress_buffer(data_len, buf_ptr, buf_len, iteration);
171
172 Z->next_in = (Bytef *) data;
173 Z->avail_in = data_len;
174 Z->avail_out = *buf_len;
175 Z->next_out = *buf_ptr;
176 }
177
178 inline size_t http_finish_buffer(size_t buf_len, char **buf_ptr)
179 {
180 (*buf_ptr)[buf_len] = '\0';
181 return buf_len;
182 }
183
184 inline size_t http_finish_gzencode_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
185 {
186 unsigned long crc;
187 char *trailer;
188
189 crc = crc32(0L, Z_NULL, 0);
190 crc = crc32(crc, (const Bytef *) data, data_len);
191
192 trailer = *buf_ptr + sizeof(http_encoding_gzip_header) + Z->total_out;
193
194 /* LSB */
195 trailer[0] = (char) (crc & 0xFF);
196 trailer[1] = (char) ((crc >> 8) & 0xFF);
197 trailer[2] = (char) ((crc >> 16) & 0xFF);
198 trailer[3] = (char) ((crc >> 24) & 0xFF);
199 trailer[4] = (char) ((Z->total_in) & 0xFF);
200 trailer[5] = (char) ((Z->total_in >> 8) & 0xFF);
201 trailer[6] = (char) ((Z->total_in >> 16) & 0xFF);
202 trailer[7] = (char) ((Z->total_in >> 24) & 0xFF);
203
204 return http_finish_buffer(Z->total_out + sizeof(http_encoding_gzip_header) + 8, buf_ptr);
205 }
206
207 inline STATUS http_verify_gzencode_buffer(const char *data, size_t data_len, const char **encoded, size_t *encoded_len, int error_level TSRMLS_DC)
208 {
209 size_t offset = sizeof(http_encoding_gzip_header);
210
211 if (data_len < offset) {
212 goto really_bad_gzip_header;
213 }
214
215 if (data[0] != (const char) 0x1F || data[1] != (const char) 0x8B) {
216 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized GZIP header start: 0x%02X 0x%02X", (int) data[0], (int) (data[1] & 0xFF));
217 return FAILURE;
218 }
219
220 if (data[2] != (const char) Z_DEFLATED) {
221 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized compression format (%d)", (int) (data[2] & 0xFF));
222 /* still try to decode */
223 }
224 if ((data[3] & 0x4) == 0x4) {
225 if (data_len < offset + 2) {
226 goto really_bad_gzip_header;
227 }
228 /* there are extra fields, the length follows the common header as 2 bytes LSB */
229 offset += (unsigned) ((data[offset] & 0xFF));
230 offset += 1;
231 offset += (unsigned) ((data[offset] & 0xFF) << 8);
232 offset += 1;
233 }
234 if ((data[3] & 0x8) == 0x8) {
235 if (data_len <= offset) {
236 goto really_bad_gzip_header;
237 }
238 /* there's a file name */
239 offset += strlen(&data[offset]) + 1 /*NUL*/;
240 }
241 if ((data[3] & 0x10) == 0x10) {
242 if (data_len <= offset) {
243 goto really_bad_gzip_header;
244 }
245 /* there's a comment */
246 offset += strlen(&data[offset]) + 1 /* NUL */;
247 }
248 if ((data[3] & 0x2) == 0x2) {
249 /* there's a CRC16 of the header */
250 offset += 2;
251 if (data_len <= offset) {
252 goto really_bad_gzip_header;
253 } else {
254 unsigned long crc, cmp;
255
256 cmp = (unsigned) ((data[offset-2] & 0xFF));
257 cmp += (unsigned) ((data[offset-1] & 0xFF) << 8);
258
259 crc = crc32(0L, Z_NULL, 0);
260 crc = crc32(crc, data, sizeof(http_encoding_gzip_header));
261
262 if (cmp != (crc & 0xFFFF)) {
263 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "GZIP headers CRC checksums so not match (%lu, %lu)", cmp, crc & 0xFFFF);
264 return FAILURE;
265 }
266 }
267 }
268
269 if (data_len < offset + 8) {
270 http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP footer");
271 return FAILURE;
272 }
273
274 if (encoded) {
275 *encoded = data + offset;
276 }
277 if (encoded_len) {
278 *encoded_len = data_len - offset - 8 /* size of the assumed GZIP footer */;
279 }
280
281 return SUCCESS;
282
283 really_bad_gzip_header:
284 http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP header");
285 return FAILURE;
286 }
287
288 inline STATUS http_verify_gzdecode_buffer(const char *data, size_t data_len, const char *decoded, size_t decoded_len, int error_level TSRMLS_DC)
289 {
290 STATUS status = SUCCESS;
291 unsigned long len, cmp, crc;
292
293 crc = crc32(0L, Z_NULL, 0);
294 crc = crc32(crc, (const Bytef *) decoded, decoded_len);
295
296 cmp = (unsigned) ((data[data_len-8] & 0xFF));
297 cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
298 cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
299 cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
300 len = (unsigned) ((data[data_len-4] & 0xFF));
301 len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
302 len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
303 len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
304
305 if (cmp != crc) {
306 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
307 status = FAILURE;
308 }
309 if (len != decoded_len) {
310 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: data sizes do not match (%lu, %lu)", len, decoded_len);
311 status = FAILURE;
312 }
313 return status;
314 }
315
316 PHP_HTTP_API STATUS _http_encode(http_encoding_type type, int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
317 {
318 STATUS status = SUCCESS;
319
320 switch (type)
321 {
322 case HTTP_ENCODING_ANY:
323 case HTTP_ENCODING_GZIP:
324 status = http_encoding_gzencode(level, data, data_len, encoded, encoded_len);
325 break;
326
327 case HTTP_ENCODING_DEFLATE:
328 status = http_encoding_deflate(level, data, data_len, encoded, encoded_len);
329 break;
330
331 case HTTP_ENCODING_COMPRESS:
332 status = http_encoding_compress(level, data, data_len, encoded, encoded_len);
333 break;
334
335 case HTTP_ENCODING_NONE:
336 default:
337 *encoded = estrndup(data, data_len);
338 *encoded_len = data_len;
339 break;
340 }
341
342 return status;
343 }
344
345 PHP_HTTP_API STATUS _http_decode(http_encoding_type type, const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
346 {
347 STATUS status = SUCCESS;
348
349 switch (type)
350 {
351 case HTTP_ENCODING_ANY:
352 if ( SUCCESS != http_encoding_gzdecode(data, data_len, decoded, decoded_len) &&
353 SUCCESS != http_encoding_inflate(data, data_len, decoded, decoded_len) &&
354 SUCCESS != http_encoding_uncompress(data, data_len, decoded, decoded_len)) {
355 status = FAILURE;
356 }
357 break;
358
359 case HTTP_ENCODING_GZIP:
360 status = http_encoding_gzdecode(data, data_len, decoded, decoded_len);
361 break;
362
363 case HTTP_ENCODING_DEFLATE:
364 status = http_encoding_inflate(data, data_len, decoded, decoded_len);
365 break;
366
367 case HTTP_ENCODING_COMPRESS:
368 status = http_encoding_uncompress(data, data_len, decoded, decoded_len);
369 break;
370
371 case HTTP_ENCODING_NONE:
372 default:
373 *decoded = estrndup(data, data_len);
374 *decoded_len = data_len;
375 break;
376 }
377
378 return status;
379 }
380
381 PHP_HTTP_API STATUS _http_encoding_gzencode(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
382 {
383 z_stream Z;
384 STATUS status = Z_OK;
385
386 http_init_gzencode_buffer(&Z, data, data_len, encoded);
387
388 if ( (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) &&
389 (Z_STREAM_END == (status = deflate(&Z, Z_FINISH))) &&
390 (Z_OK == (status = deflateEnd(&Z)))) {
391 *encoded_len = http_finish_gzencode_buffer(&Z, data, data_len, encoded);
392 return SUCCESS;
393 }
394
395 efree(*encoded);
396 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not gzencode data: %s", zError(status));
397 return FAILURE;
398 }
399
400 PHP_HTTP_API STATUS _http_encoding_deflate(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
401 {
402 z_stream Z;
403 STATUS status = Z_OK;
404
405 http_init_deflate_buffer(&Z, data, data_len, encoded);
406
407 if ( (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) &&
408 (Z_STREAM_END == (status = deflate(&Z, Z_FINISH))) &&
409 (Z_OK == (status = deflateEnd(&Z)))) {
410 *encoded_len = http_finish_buffer(Z.total_out, encoded);
411 return SUCCESS;
412 }
413
414 efree(encoded);
415 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
416 return FAILURE;
417 }
418
419 PHP_HTTP_API STATUS _http_encoding_compress(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
420 {
421 STATUS status;
422
423 *encoded = emalloc(*encoded_len = HTTP_ENCODING_BUFLEN(data_len));
424
425 if (Z_OK == (status = compress2(*encoded, encoded_len, data, data_len, level))) {
426 http_finish_buffer(*encoded_len, encoded);
427 return SUCCESS;
428 }
429
430 efree(encoded);
431 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not compress data: %s", zError(status));
432 return FAILURE;
433 }
434
435 PHP_HTTP_API STATUS _http_encoding_gzdecode(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
436 {
437 const char *encoded;
438 size_t encoded_len;
439
440 if ( (SUCCESS == http_verify_gzencode_buffer(data, data_len, &encoded, &encoded_len, HE_NOTICE)) &&
441 (SUCCESS == http_encoding_inflate(encoded, encoded_len, decoded, decoded_len))) {
442 http_verify_gzdecode_buffer(data, data_len, *decoded, *decoded_len, HE_NOTICE);
443 return SUCCESS;
444 }
445
446 return FAILURE;
447 }
448
449 PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
450 {
451 int max = 0;
452 STATUS status;
453 z_stream Z;
454
455 do {
456 http_init_inflate_buffer(&Z, data, data_len, decoded, decoded_len, &max);
457 if (Z_OK == (status = inflateInit2(&Z, -MAX_WBITS))) {
458 if (Z_STREAM_END == (status = inflate(&Z, Z_FINISH))) {
459 if (Z_OK == (status = inflateEnd(&Z))) {
460 *decoded_len = http_finish_buffer(Z.total_out, decoded);
461 return SUCCESS;
462 }
463 }
464 }
465 } while (++max < HTTP_ENCODING_MAXTRY && status == Z_BUF_ERROR);
466
467 efree(*decoded);
468 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
469 return FAILURE;
470 }
471
472 PHP_HTTP_API STATUS _http_encoding_uncompress(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
473 {
474 int max = 0;
475 STATUS status;
476
477 do {
478 http_init_uncompress_buffer(data_len, decoded, decoded_len, &max);
479 if (Z_OK == (status = uncompress(*decoded, decoded_len, data, data_len))) {
480 http_finish_buffer(*decoded_len, decoded);
481 return SUCCESS;
482 }
483 } while (++max < HTTP_ENCODING_MAXTRY && status == Z_BUF_ERROR);
484
485 efree(*decoded);
486 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not uncompress data: %s", zError(status));
487 return FAILURE;
488 }
489
490 #define HTTP_ENCODING_STREAM_ERROR(status, tofree) \
491 { \
492 if (tofree) efree(tofree); \
493 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "GZIP stream error: %s", zError(status)); \
494 return FAILURE; \
495 }
496
497 PHP_HTTP_API STATUS _http_encoding_stream_init(http_encoding_stream *s, int gzip, int level, char **encoded, size_t *encoded_len TSRMLS_DC)
498 {
499 STATUS status;
500
501 memset(s, 0, sizeof(http_encoding_stream));
502 if (Z_OK != (status = deflateInit2(&s->Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) {
503 HTTP_ENCODING_STREAM_ERROR(status, NULL);
504 }
505
506 if (s->gzip = gzip) {
507 s->crc = crc32(0L, Z_NULL, 0);
508 *encoded_len = sizeof(http_encoding_gzip_header);
509 *encoded = emalloc(*encoded_len);
510 memcpy(*encoded, http_encoding_gzip_header, *encoded_len);
511 } else {
512 *encoded_len = 0;
513 *encoded = NULL;
514 }
515
516 return SUCCESS;
517 }
518
519 PHP_HTTP_API STATUS _http_encoding_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
520 {
521 STATUS status;
522
523 *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
524 *encoded = emalloc(*encoded_len);
525
526 s->Z.next_in = (Bytef *) data;
527 s->Z.avail_in = data_len;
528 s->Z.next_out = *encoded;
529 s->Z.avail_out = *encoded_len;
530
531 status = deflate(&s->Z, Z_SYNC_FLUSH);
532
533 if (Z_OK != status && Z_STREAM_END != status) {
534 HTTP_ENCODING_STREAM_ERROR(status, *encoded);
535 }
536 *encoded_len -= s->Z.avail_out;
537
538 if (s->gzip) {
539 s->crc = crc32(s->crc, (const Bytef *) data, data_len);
540 }
541
542 return SUCCESS;
543 }
544
545 PHP_HTTP_API STATUS _http_encoding_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC)
546 {
547 STATUS status;
548
549 *encoded_len = 1024;
550 *encoded = emalloc(*encoded_len);
551
552 s->Z.next_out = *encoded;
553 s->Z.avail_out = *encoded_len;
554
555 if (Z_STREAM_END != (status = deflate(&s->Z, Z_FINISH)) || Z_OK != (status = deflateEnd(&s->Z))) {
556 HTTP_ENCODING_STREAM_ERROR(status, *encoded);
557 }
558
559 *encoded_len -= s->Z.avail_out;
560 if (s->gzip) {
561 if (s->Z.avail_out < 8) {
562 *encoded = erealloc(*encoded, *encoded_len + 8);
563 }
564 (*encoded)[(*encoded_len)++] = (char) (s->crc & 0xFF);
565 (*encoded)[(*encoded_len)++] = (char) ((s->crc >> 8) & 0xFF);
566 (*encoded)[(*encoded_len)++] = (char) ((s->crc >> 16) & 0xFF);
567 (*encoded)[(*encoded_len)++] = (char) ((s->crc >> 24) & 0xFF);
568 (*encoded)[(*encoded_len)++] = (char) ((s->Z.total_in) & 0xFF);
569 (*encoded)[(*encoded_len)++] = (char) ((s->Z.total_in >> 8) & 0xFF);
570 (*encoded)[(*encoded_len)++] = (char) ((s->Z.total_in >> 16) & 0xFF);
571 (*encoded)[(*encoded_len)++] = (char) ((s->Z.total_in >> 24) & 0xFF);
572 }
573
574 return SUCCESS;
575 }
576
577 #endif /* HTTP_HAVE_ZLIB */
578
579 PHP_HTTP_API zend_bool _http_encoding_response_start(size_t content_length TSRMLS_DC)
580 {
581 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
582 php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
583 HTTP_G(send).gzip_encoding = 0;
584 } else {
585 if (!HTTP_G(send).gzip_encoding) {
586 /* emit a content-length header */
587 if (content_length) {
588 char cl_header_str[128];
589 size_t cl_header_len;
590 cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %lu", (unsigned long) content_length);
591 http_send_header_string_ex(cl_header_str, cl_header_len, 1);
592 }
593 } else {
594 #ifndef HTTP_HAVE_ZLIB
595 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
596 #else
597 HashTable *selected;
598 zval zsupported;
599
600 INIT_PZVAL(&zsupported);
601 array_init(&zsupported);
602 add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
603 add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
604
605 if (selected = http_negotiate_encoding(&zsupported)) {
606 STATUS hs = FAILURE;
607 char *encoding = NULL;
608 ulong idx;
609
610 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
611 if (!strcmp(encoding, "gzip")) {
612 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
613 HTTP_G(send).gzip_encoding = HTTP_ENCODING_GZIP;
614 }
615 } else if (!strcmp(encoding, "deflate")) {
616 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
617 HTTP_G(send).gzip_encoding = HTTP_ENCODING_DEFLATE;
618 }
619 }
620 if (SUCCESS == hs) {
621 http_send_header_string("Vary: Accept-Encoding");
622 } else {
623 HTTP_G(send).gzip_encoding = 0;
624 }
625 }
626
627 zend_hash_destroy(selected);
628 FREE_HASHTABLE(selected);
629 }
630
631 zval_dtor(&zsupported);
632 return 1;
633 #endif
634 }
635 }
636 return 0;
637 }
638
639 /*
640 * Local variables:
641 * tab-width: 4
642 * c-basic-offset: 4
643 * End:
644 * vim600: noet sw=4 ts=4 fdm=marker
645 * vim<600: noet sw=4 ts=4
646 */
647