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