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