#include "php_http_send_api.h"
#include "php_http_headers_api.h"
+/* {{{ */
#ifdef HTTP_HAVE_ZLIB
PHP_MINIT_FUNCTION(http_encoding)
{
getGlobals(G);
if (G->send.inflate.start_auto) {
- php_ob_set_internal_handler(_http_ob_inflatehandler, 0x1000, "http inflate", 0 TSRMLS_CC);
+ php_ob_set_internal_handler(_http_ob_inflatehandler, HTTP_INFLATE_BUFFER_SIZE, "http inflate", 0 TSRMLS_CC);
}
if (G->send.deflate.start_auto) {
- php_ob_set_internal_handler(_http_ob_deflatehandler, 0x8000, "http deflate", 0 TSRMLS_CC);
+ php_ob_set_internal_handler(_http_ob_deflatehandler, HTTP_DEFLATE_BUFFER_SIZE, "http deflate", 0 TSRMLS_CC);
}
return SUCCESS;
}
return SUCCESS;
}
#endif
+/* }}} */
+/* {{{ eol_match(char **, int *) */
static inline int eol_match(char **line, int *eol_len)
{
char *ptr = *line;
- while (0x20 == *ptr) ++ptr;
+ while (' ' == *ptr) ++ptr;
if (ptr == http_locate_eol(*line, eol_len)) {
*line = ptr;
return 0;
}
}
+/* }}} */
/* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
}
/* }}} */
+/* {{{ int http_encoding_response_start(size_t) */
+PHP_HTTP_API int _http_encoding_response_start(size_t content_length TSRMLS_DC)
+{
+ if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
+ php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
+ HTTP_G(send).deflate.encoding = 0;
+ } else {
+ if (!HTTP_G(send).deflate.encoding) {
+ /* emit a content-length header */
+ if (content_length) {
+ char cl_header_str[128];
+ size_t cl_header_len;
+ cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %zu", content_length);
+ http_send_header_string_ex(cl_header_str, cl_header_len, 1);
+ }
+ } else {
+#ifndef HTTP_HAVE_ZLIB
+ HTTP_G(send).deflate.encoding = 0;
+ php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
+#else
+ HashTable *selected;
+ zval zsupported;
+
+ INIT_PZVAL(&zsupported);
+ array_init(&zsupported);
+ add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
+ add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
+ add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
+
+ HTTP_G(send).deflate.encoding = 0;
+
+ if ((selected = http_negotiate_encoding(&zsupported))) {
+ STATUS hs = FAILURE;
+ char *encoding = NULL;
+ ulong idx;
+
+ if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
+ if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
+ if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
+ HTTP_G(send).deflate.encoding = HTTP_ENCODING_GZIP;
+ }
+ } else if (!strcmp(encoding, "deflate")) {
+ if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
+ HTTP_G(send).deflate.encoding = HTTP_ENCODING_DEFLATE;
+ }
+ }
+ if (SUCCESS == hs) {
+ http_send_header_string("Vary: Accept-Encoding");
+ }
+ }
+
+ zend_hash_destroy(selected);
+ FREE_HASHTABLE(selected);
+ }
+
+ zval_dtor(&zsupported);
+ return HTTP_G(send).deflate.encoding;
+#endif
+ }
+ }
+ return 0;
+}
+/* }}} */
+
#ifdef HTTP_HAVE_ZLIB
+/* {{{ */
#define HTTP_DEFLATE_LEVEL_SET(flags, level) \
switch (flags & 0xf) \
{ \
#define HTTP_WINDOW_BITS_GZIP 0x0000001f
#define HTTP_WINDOW_BITS_ANY 0x0000002f
#define HTTP_WINDOW_BITS_RAW -0x000000f
+/* }}} */
+/* {{{ STATUS http_encoding_deflate(int, char *, size_t, char **, size_t *) */
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)
{
int status, level, wbits, strategy;
status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
if (Z_OK == status) {
- *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
+ *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
*encoded = emalloc_rel(*encoded_len);
Z.next_in = (Bytef *) data;
}
}
- http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s (%s)", zError(status));
+ http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
return FAILURE;
}
+/* }}} */
+/* {{{ STATUS http_encoding_inflate(char *, size_t, char **, size_t) */
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)
{
- int status, max = 0, wbits = HTTP_WINDOW_BITS_ANY;
+ int status, round = 0, wbits = HTTP_WINDOW_BITS_ANY;
z_stream Z;
phpstr buffer;
Z.next_out = (Bytef *) buffer.data + (buffer.used = Z.total_out);
status = inflate(&Z, Z_NO_FLUSH);
} while (Z_OK == status);
- } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
+ } while (Z_BUF_ERROR == status && ++round < HTTP_INFLATE_ROUNDS);
if (Z_DATA_ERROR == status && HTTP_WINDOW_BITS_ANY == wbits) {
/* raw deflated data? */
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
return FAILURE;
}
+/* }}} */
-
+/* {{{ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *, int) */
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)
{
int status, level, wbits, strategy, free_stream;
if (Z_OK == (status = deflateInit2(&s->stream, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
- if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
+ if ((s->stream.opaque = phpstr_init_ex(NULL, HTTP_DEFLATE_BUFFER_SIZE, p))) {
return s;
}
deflateEnd(&s->stream);
}
return NULL;
}
+/* }}} */
+/* {{{ http_encoding_stream *http_encoding_inflate_stream_init(http_encoding_stream *, int) */
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)
{
int status, wbits, free_stream;
if (Z_OK == (status = inflateInit2(&s->stream, wbits))) {
int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
- if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
+ if ((s->stream.opaque = phpstr_init_ex(NULL, HTTP_DEFLATE_BUFFER_SIZE, p))) {
return s;
}
inflateEnd(&s->stream);
}
return NULL;
}
+/* }}} */
+/* {{{ STATUS http_encoding_deflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
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)
{
int status;
s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
/* deflate */
- *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
+ *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
*encoded = emalloc_rel(*encoded_len);
s->stream.avail_out = *encoded_len;
s->stream.next_out = (Bytef *) *encoded;
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
return FAILURE;
}
+/* }}} */
+/* {{{ STATUS http_encoding_inflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
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)
{
- int status, max = 0;
+ int status, round = 0;
/* append input to buffer */
phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
}
break;
}
- } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
+ } while (Z_BUF_ERROR == status && ++round < HTTP_INFLATE_ROUNDS);
STR_SET(*decoded, NULL);
*decoded_len = 0;
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update inflate stream: %s", zError(status));
return FAILURE;
}
+/* }}} */
+/* {{{ STATUS http_encoding_deflate_stream_flush(http_encoding_stream *, char **, size_t *) */
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)
{
int status;
- *encoded_len = 0x8000;
+ *encoded_len = HTTP_DEFLATE_BUFFER_SIZE;
*encoded = emalloc_rel(*encoded_len);
s->stream.avail_in = 0;
{
case Z_OK:
case Z_STREAM_END:
- *encoded_len = 0x8000 - s->stream.avail_out;
+ *encoded_len = HTTP_DEFLATE_BUFFER_SIZE - s->stream.avail_out;
*encoded = erealloc_rel(*encoded, *encoded_len + 1);
(*encoded)[*encoded_len] = '\0';
return SUCCESS;
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
return FAILURE;
}
+/* }}} */
+/* {{{ STATUS http_encoding_inflate_straem_flush(http_encoding_stream *, char **, size_t *) */
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)
{
/* noop */
*decoded = estrndup("", *decoded_len = 0);
return SUCCESS;
}
+/* }}} */
+/* {{{ STATUS http_encoding_deflate_stream_finish(http_encoding_stream *, char **, size_t *) */
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)
{
int status;
- *encoded_len = 0x8000;
+ *encoded_len = HTTP_DEFLATE_BUFFER_SIZE;
*encoded = emalloc_rel(*encoded_len);
/* deflate remaining input */
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
return FAILURE;
}
+/* }}} */
+/* {{{ STATUS http_encoding_inflate_stream_finish(http_encoding_stream *, char **, size_t *) */
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)
{
int status;
- *decoded_len = PHPSTR_LEN(s->stream.opaque) << 2;
+ *decoded_len = (PHPSTR_LEN(s->stream.opaque) + 1) * HTTP_INFLATE_ROUNDS;
*decoded = emalloc_rel(*decoded_len);
/* inflate remaining input */
http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
return FAILURE;
}
+/* }}} */
+/* {{{ void http_encoding_deflate_stream_dtor(http_encoding_stream *) */
PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
{
if (s) {
deflateEnd(&s->stream);
}
}
+/* }}} */
+/* {{{ void http_encoding_inflate_stream_dtor(http_encoding_stream *) */
PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
{
if (s) {
inflateEnd(&s->stream);
}
}
+/* }}} */
+/* {{{ void http_encoding_deflate_stream_free(http_encoding_stream **) */
PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
{
if (s) {
*s = NULL;
}
}
+/* }}} */
+/* {{{ void http_encoding_inflate_stream_free(http_encoding_stream **) */
PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
{
if (s) {
*s = NULL;
}
}
+/* }}} */
+/* {{{ void http_ob_deflatehandler(char *, uint, char **, uint *, int) */
void _http_ob_deflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
{
getGlobals(G);
*handled_output = estrndup(output, *handled_output_len = output_len);
}
}
+/* }}} */
+/* {{{ void http_ob_inflatehandler(char *, uint, char **, uint *, int) */
void _http_ob_inflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
{
getGlobals(G);
*handled_output = estrndup(output, *handled_output_len = output_len);
}
}
-
-static const char http_encoding_gzip_header[] = {
- (const char) 0x1f, // fixed value
- (const char) 0x8b, // fixed value
- (const char) Z_DEFLATED, // compression algorithm
- (const char) 0, // none of the possible flags defined by the GZIP "RFC"
- (const char) 0, // MTIME
- (const char) 0, // =*=
- (const char) 0, // =*=
- (const char) 0, // =*=
- (const char) 0, // two possible flag values for 9 compression levels? o_O
-#ifdef PHP_WIN32
- (const char) 0x0b // OS_CODE
-#else
- (const char) 0x03 // OS_CODE
-#endif
-};
-
-PHP_HTTP_API STATUS _http_encoding_gzencode_verify(const char *data, size_t data_len, const char **encoded, size_t *encoded_len, int error_level TSRMLS_DC)
-{
- size_t offset = sizeof(http_encoding_gzip_header);
-
- if (data_len < offset) {
- goto really_bad_gzip_header;
- }
-
- if (data[0] != (const char) 0x1F || data[1] != (const char) 0x8B) {
- 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));
- return FAILURE;
- }
-
- if (data[2] != (const char) Z_DEFLATED) {
- http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized compression format (%d)", (int) (data[2] & 0xFF));
- /* still try to decode */
- }
- if ((data[3] & 0x4) == 0x4) {
- if (data_len < offset + 2) {
- goto really_bad_gzip_header;
- }
- /* there are extra fields, the length follows the common header as 2 bytes LSB */
- offset += (unsigned) ((data[offset] & 0xFF));
- offset += 1;
- offset += (unsigned) ((data[offset] & 0xFF) << 8);
- offset += 1;
- }
- if ((data[3] & 0x8) == 0x8) {
- if (data_len <= offset) {
- goto really_bad_gzip_header;
- }
- /* there's a file name */
- offset += strlen(&data[offset]) + 1 /*NUL*/;
- }
- if ((data[3] & 0x10) == 0x10) {
- if (data_len <= offset) {
- goto really_bad_gzip_header;
- }
- /* there's a comment */
- offset += strlen(&data[offset]) + 1 /* NUL */;
- }
- if ((data[3] & 0x2) == 0x2) {
- /* there's a CRC16 of the header */
- offset += 2;
- if (data_len <= offset) {
- goto really_bad_gzip_header;
- } else {
- ulong crc, cmp;
-
- cmp = (unsigned) ((data[offset-2] & 0xFF));
- cmp += (unsigned) ((data[offset-1] & 0xFF) << 8);
-
- crc = crc32(0L, Z_NULL, 0);
- crc = crc32(crc, (const Bytef *) data, sizeof(http_encoding_gzip_header));
-
- if (cmp != (crc & 0xFFFF)) {
- http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "GZIP headers CRC checksums so not match (%lu, %lu)", cmp, crc & 0xFFFF);
- return FAILURE;
- }
- }
- }
-
- if (data_len < offset + 8) {
- http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP footer");
- return FAILURE;
- }
-
- if (encoded) {
- *encoded = data + offset;
- }
- if (encoded_len) {
- *encoded_len = data_len - offset - 8 /* size of the assumed GZIP footer */;
- }
-
- return SUCCESS;
-
-really_bad_gzip_header:
- http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP header");
- return FAILURE;
-}
-
-PHP_HTTP_API STATUS _http_encoding_gzdecode_verify(const char *data, size_t data_len, const char *decoded, size_t decoded_len, int error_level TSRMLS_DC)
-{
- STATUS status = SUCCESS;
- ulong len, cmp, crc;
-
- crc = crc32(0L, Z_NULL, 0);
- crc = crc32(crc, (const Bytef *) decoded, decoded_len);
-
- cmp = (unsigned) ((data[data_len-8] & 0xFF));
- cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
- cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
- cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
- len = (unsigned) ((data[data_len-4] & 0xFF));
- len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
- len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
- len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
-
- if (cmp != crc) {
- http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
- status = FAILURE;
- }
- if (len != decoded_len) {
- 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);
- status = FAILURE;
- }
- return status;
-}
+/* }}} */
#endif /* HTTP_HAVE_ZLIB */
-PHP_HTTP_API int _http_encoding_response_start(size_t content_length TSRMLS_DC)
-{
- if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
- php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
- HTTP_G(send).deflate.encoding = 0;
- } else {
- if (!HTTP_G(send).deflate.encoding) {
- /* emit a content-length header */
- if (content_length) {
- char cl_header_str[128];
- size_t cl_header_len;
- cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %zu", content_length);
- http_send_header_string_ex(cl_header_str, cl_header_len, 1);
- }
- } else {
-#ifndef HTTP_HAVE_ZLIB
- HTTP_G(send).deflate.encoding = 0;
- php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
-#else
- HashTable *selected;
- zval zsupported;
-
- INIT_PZVAL(&zsupported);
- array_init(&zsupported);
- add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
- add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
- add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
-
- HTTP_G(send).deflate.encoding = 0;
-
- if ((selected = http_negotiate_encoding(&zsupported))) {
- STATUS hs = FAILURE;
- char *encoding = NULL;
- ulong idx;
-
- if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
- if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
- if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
- HTTP_G(send).deflate.encoding = HTTP_ENCODING_GZIP;
- }
- } else if (!strcmp(encoding, "deflate")) {
- if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
- HTTP_G(send).deflate.encoding = HTTP_ENCODING_DEFLATE;
- }
- }
- if (SUCCESS == hs) {
- http_send_header_string("Vary: Accept-Encoding");
- }
- }
-
- zend_hash_destroy(selected);
- FREE_HASHTABLE(selected);
- }
-
- zval_dtor(&zsupported);
- return HTTP_G(send).deflate.encoding;
-#endif
- }
- }
- return 0;
-}
-
/*
* Local variables:
* tab-width: 4