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