From 5c5ddf9042732a05100245844fe2fb70bfe6d495 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 17 Oct 2005 10:31:14 +0000 Subject: [PATCH] - gzip responses # this is a braindump; do not try to compile --- http_encoding_api.c | 211 +++++++++++++++++--- http_send_api.c | 412 +++++++++++++++++----------------------- php_http_encoding_api.h | 26 +++ php_http_send_api.h | 3 - phpstr/phpstr.c | 45 +++++ phpstr/phpstr.h | 6 + 6 files changed, 437 insertions(+), 266 deletions(-) diff --git a/http_encoding_api.c b/http_encoding_api.c index 28ea423..cba2d25 100644 --- a/http_encoding_api.c +++ b/http_encoding_api.c @@ -24,6 +24,12 @@ #include "php_http.h" #include "php_http_api.h" +#ifdef HTTP_HAVE_ZLIB +# include "php_http_send_api.h" +# include "php_http_headers_api.h" +# include +#endif + ZEND_EXTERN_MODULE_GLOBALS(http); /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */ @@ -93,16 +99,8 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco /* }}} */ #ifdef HTTP_HAVE_ZLIB -#include - -/* max count of uncompress trials, alloc_size <<= 2 for each try */ -#define HTTP_GZMAXTRY 10 -/* safe padding */ -#define HTTP_GZSAFPAD 10 -/* add 1% extra space in case we need to encode widely differing (binary) data */ -#define HTTP_GZBUFLEN(l) (l + (l / 100) + HTTP_GZSAFPAD) -static const char http_gzencode_header[] = { +static const char http_encoding_gzip_header[] = { (const char) 0x1f, // fixed value (const char) 0x8b, // fixed value (const char) Z_DEFLATED, // compression algorithm @@ -123,12 +121,12 @@ inline void http_init_gzencode_buffer(z_stream *Z, const char *data, size_t data Z->next_in = (Bytef *) data; Z->avail_in = data_len; - Z->avail_out = HTTP_GZBUFLEN(data_len) + HTTP_GZSAFPAD - 1; + Z->avail_out = HTTP_ENCODING_BUFLEN(data_len) + HTTP_ENCODING_SAFPAD - 1; - *buf_ptr = emalloc(HTTP_GZBUFLEN(data_len) + sizeof(http_gzencode_header) + HTTP_GZSAFPAD); - memcpy(*buf_ptr, http_gzencode_header, sizeof(http_gzencode_header)); + *buf_ptr = emalloc(HTTP_ENCODING_BUFLEN(data_len) + sizeof(http_encoding_gzip_header) + HTTP_ENCODING_SAFPAD); + memcpy(*buf_ptr, http_encoding_gzip_header, sizeof(http_encoding_gzip_header)); - Z->next_out = *buf_ptr + sizeof(http_gzencode_header); + Z->next_out = *buf_ptr + sizeof(http_encoding_gzip_header); } inline void http_init_deflate_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr) @@ -140,24 +138,31 @@ inline void http_init_deflate_buffer(z_stream *Z, const char *data, size_t data_ Z->data_type = Z_UNKNOWN; Z->next_in = (Bytef *) data; Z->avail_in = data_len; - Z->avail_out = HTTP_GZBUFLEN(data_len) - 1; - Z->next_out = emalloc(HTTP_GZBUFLEN(data_len)); + Z->avail_out = HTTP_ENCODING_BUFLEN(data_len) - 1; + Z->next_out = emalloc(HTTP_ENCODING_BUFLEN(data_len)); *buf_ptr = Z->next_out; } -inline void http_init_uncompress_buffer(size_t data_len, char **buf_ptr, size_t *buf_len, int iteration) +inline void http_init_uncompress_buffer(size_t data_len, char **buf_ptr, size_t *buf_len, int *iteration) { - if (!iteration) { + if (!*iteration) { *buf_len = data_len * 2; *buf_ptr = emalloc(*buf_len + 1); } else { - *buf_len <<= 2; - *buf_ptr = erealloc(*buf_ptr, *buf_len + 1); + size_t new_len = *buf_len << 2; + char *new_ptr = erealloc(*buf_ptr, new_len + 1); + + if (new_ptr) { + *buf_ptr = new_ptr; + *buf_len = new_len; + } else { + *iteration = INT_MAX; + } } } -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) +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) { Z->zalloc = Z_NULL; Z->zfree = Z_NULL; @@ -184,7 +189,7 @@ inline size_t http_finish_gzencode_buffer(z_stream *Z, const char *data, size_t crc = crc32(0L, Z_NULL, 0); crc = crc32(crc, (const Bytef *) data, data_len); - trailer = *buf_ptr + sizeof(http_gzencode_header) + Z->total_out; + trailer = *buf_ptr + sizeof(http_encoding_gzip_header) + Z->total_out; /* LSB */ trailer[0] = (char) (crc & 0xFF); @@ -196,12 +201,12 @@ inline size_t http_finish_gzencode_buffer(z_stream *Z, const char *data, size_t trailer[6] = (char) ((Z->total_in >> 16) & 0xFF); trailer[7] = (char) ((Z->total_in >> 24) & 0xFF); - return http_finish_buffer(Z->total_out + sizeof(http_gzencode_header) + 8, buf_ptr); + return http_finish_buffer(Z->total_out + sizeof(http_encoding_gzip_header) + 8, buf_ptr); } 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) { - size_t offset = sizeof(http_gzencode_header); + size_t offset = sizeof(http_encoding_gzip_header); if (data_len < offset) { goto really_bad_gzip_header; @@ -252,7 +257,7 @@ inline STATUS http_verify_gzencode_buffer(const char *data, size_t data_len, con cmp += (unsigned) ((data[offset-1] & 0xFF) << 8); crc = crc32(0L, Z_NULL, 0); - crc = crc32(crc, data, sizeof(http_gzencode_header)); + crc = crc32(crc, 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); @@ -415,7 +420,7 @@ PHP_HTTP_API STATUS _http_encoding_compress(int level, const char *data, size_t { STATUS status; - *encoded = emalloc(*encoded_len = HTTP_GZBUFLEN(data_len)); + *encoded = emalloc(*encoded_len = HTTP_ENCODING_BUFLEN(data_len)); if (Z_OK == (status = compress2(*encoded, encoded_len, data, data_len, level))) { http_finish_buffer(*encoded_len, encoded); @@ -448,7 +453,7 @@ PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, ch z_stream Z; do { - http_init_inflate_buffer(&Z, data, data_len, decoded, decoded_len, max++); + http_init_inflate_buffer(&Z, data, data_len, decoded, decoded_len, &max); if (Z_OK == (status = inflateInit2(&Z, -MAX_WBITS))) { if (Z_STREAM_END == (status = inflate(&Z, Z_FINISH))) { if (Z_OK == (status = inflateEnd(&Z))) { @@ -457,7 +462,7 @@ PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, ch } } } - } while (max < HTTP_GZMAXTRY && status == Z_BUF_ERROR); + } while (++max < HTTP_ENCODING_MAXTRY && status == Z_BUF_ERROR); efree(*decoded); http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status)); @@ -470,20 +475,168 @@ PHP_HTTP_API STATUS _http_encoding_uncompress(const char *data, size_t data_len, STATUS status; do { - http_init_uncompress_buffer(data_len, decoded, decoded_len, max++); + http_init_uncompress_buffer(data_len, decoded, decoded_len, &max); if (Z_OK == (status = uncompress(*decoded, decoded_len, data, data_len))) { http_finish_buffer(*decoded_len, decoded); return SUCCESS; } - } while (max < HTTP_GZMAXTRY && status == Z_BUF_ERROR); + } while (++max < HTTP_ENCODING_MAXTRY && status == Z_BUF_ERROR); efree(*decoded); http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not uncompress data: %s", zError(status)); return FAILURE; } +#define HTTP_ENCODING_STREAM_ERROR(status, tofree) \ + { \ + if (tofree) efree(tofree); \ + http_error_ex(HE_WARNING, HTTP_E_ENCODING, "GZIP stream error: %s", zError(status)); \ + return FAILURE; \ + } + +PHP_HTTP_API STATUS _http_encoding_stream_init(http_encoding_stream *s, int gzip, int level, char **encoded, size_t *encoded_len TSRMLS_DC) +{ + STATUS status; + + memset(s, 0, sizeof(http_encoding_stream)); + if (Z_OK != (status = deflateInit2(&s->Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) { + HTTP_ENCODING_STREAM_ERROR(status, NULL); + } + + if (s->gzip = gzip) { + s->crc = crc32(0L, Z_NULL, 0); + *encoded_len = sizeof(http_encoding_gzip_header); + *encoded = emalloc(*encoded_len); + memcpy(*encoded, http_encoding_gzip_header, *encoded_len); + } else { + *encoded_len = 0; + *encoded = NULL; + } + + return SUCCESS; +} + +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) +{ + STATUS status; + + *encoded_len = HTTP_ENCODING_BUFLEN(data_len); + *encoded = emalloc(*encoded_len); + + s->Z.next_in = (Bytef *) data; + s->Z.avail_in = data_len; + s->Z.next_out = *encoded; + s->Z.avail_out = *encoded_len; + + status = deflate(&s->Z, Z_SYNC_FLUSH); + + if (Z_OK != status && Z_STREAM_END != status) { + HTTP_GZSTREAM_ERROR(status, *encoded); + } + *encoded_len -= s->Z.avail_out; + + if (s->gzip) { + s->crc = crc32(s->crc, (const Bytef *) data, data_len); + } + + return SUCCESS; +} + +PHP_HTTP_API STATUS _http_encoding_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC) +{ + STATUS status; + + *encoded_len = 1024; + *encoded = emalloc(*encoded_len); + + s->Z.next_out = *encoded; + s->Z.avail_out = *encoded_len; + + if (Z_STREAM_END != (status = deflate(&s->Z, Z_FINISH)) || Z_OK != (status = deflateEnd(&s->Z))) { + HTTP_ENCODING_STREAM_ERROR(status, *encoded); + } + + fprintf(stderr, "Needed %d bytes\n", *encoded_len - s->Z.avail_out); + + *encoded_len -= s->Z.avail_out; + if (s->gzip) { + if (s->Z.avail_out < 8) { + *encoded = erealloc(*encoded, *encoded_len + 8); + } + (*encoded)[(*encoded_len)++] = (char) (s->crc & 0xFF); + (*encoded)[(*encoded_len)++] = (char) ((s->crc >> 8) & 0xFF); + (*encoded)[(*encoded_len)++] = (char) ((s->crc >> 16) & 0xFF); + (*encoded)[(*encoded_len)++] = (char) ((s->crc >> 24) & 0xFF); + (*encoded)[(*encoded_len)++] = (char) ((s->Z.total_in) & 0xFF); + (*encoded)[(*encoded_len)++] = (char) ((s->Z.total_in >> 8) & 0xFF); + (*encoded)[(*encoded_len)++] = (char) ((s->Z.total_in >> 16) & 0xFF); + (*encoded)[(*encoded_len)++] = (char) ((s->Z.total_in >> 24) & 0xFF); + } + + return SUCCESS; +} + #endif /* HTTP_HAVE_ZLIB */ +PHP_HTTP_API zend_bool _http_encoding_response_start(size_t content_length TSRMLS_DC) +{ + if (php_ob_handler_used("ob_gzhandler" TSRMLS_DC)||php_ob_handler_used("zlib output compression" TSRMLS_DC)) { + HTTP_G(send).gzip_encoding = 0; + } else { + if (!HTTP_G(send).gzip_encoding) { + /* emit a content-length header */ + if (content_length) { + char *cl; + spprintf(&cl, 0, "Content-Length: %lu", (unsigned long) content_length); + http_send_header_string(cl); + efree(cl); + } + } else { +#ifndef HTTP_HAVE_ZLIB + 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, "deflate", lenof("deflate"), 1); + + 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")) { + if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) { + HTTP_G(send).gzip_encoding = HTTP_ENCODING_GZIP; + } + } else if (!strcmp(encoding, "deflate")) { + if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) { + HTTP_G(send).gzip_encoding = HTTP_ENCODING_DEFLATE; + } + } + if (SUCCESS == hs) { + http_send_header_string("Vary: Accept-Encoding"); + } else { + HTTP_G(send).gzip_encoding = 0; + } + } + + zend_hash_destroy(selected); + FREE_HASHTABLE(selected); + } + + zval_dtor(&zsupported); + return 1; +#endif + } + } + return 0; +} + /* * Local variables: * tab-width: 4 diff --git a/http_send_api.c b/http_send_api.c index f6b3d73..82c3f7f 100644 --- a/http_send_api.c +++ b/http_send_api.c @@ -36,19 +36,14 @@ ZEND_EXTERN_MODULE_GLOBALS(http); -#define http_flush() _http_flush(TSRMLS_C) +#define http_flush(d, l) _http_flush((d), (l) SRMLS_CC) /* {{{ static inline void http_flush() */ -static inline void _http_flush(TSRMLS_D) +static inline void _http_flush(const char *data, size_t data_len, TSRMLS_DC) { + PHPWRITE(data, data_len); php_end_ob_buffer(1, 1 TSRMLS_CC); sapi_flush(TSRMLS_C); -} -/* }}} */ - -#define http_sleep() _http_sleep(TSRMLS_C) -/* {{{ static inline void http_sleep() */ -static inline void _http_sleep(TSRMLS_D) -{ + #define HTTP_MSEC(s) (s * 1000) #define HTTP_USEC(s) (HTTP_MSEC(s) * 1000) #define HTTP_NSEC(s) (HTTP_USEC(s) * 1000) @@ -74,52 +69,50 @@ static inline void _http_sleep(TSRMLS_D) } } /* }}} */ + +/* {{{ http_send_start_response */ +#define http_send_start_response(b, cl) _http_send_start_response((b), (cl) TSRMLS_CC) +static void _http_send_response_start(void *buffer, size_t content_length TSRMLS_DC) +{ + if (http_encoding_response_start(content_length)) { #ifdef HTTP_HAVE_ZLIB -# define HTTP_CHUNK_ENCODE(data, size, dogzip) \ - if (dogzip) { \ - char *encoded = NULL; \ - size_t encoded_len = 0; \ - \ - if (SUCCESS != http_encode(dogzip, 1, data, size, &encoded, &encoded_len)) { \ - return FAILURE; \ - } \ - \ - data = encoded; \ - size = encoded_len; \ - } -#else -# define HTTP_CHUNK_ENCODE(data, size, dogzip) + char *encoded; + size_t *encoded_len; + http_encoding_stream *s = (http_encoding_stream *) buffer; + + http_encoding_stream_init(s, HTTP_G(gzip).encoding == HTTP_ENCODING_GZIP, -1, &encoded, &encoded_len); + phpstr_chunked_output(s.storage, encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC); + STR_FREE(encoded); #endif - -#define HTTP_CHUNK_AVAIL(len, cs) ((len -= cs) >= 0) -#define HTTP_CHUNK_WRITE(d, l, dofree, dosleep, dogzip) \ - { \ - long size = (long) l; \ - char *data = (char *) d; \ - \ - HTTP_CHUNK_ENCODE(data, size, dogzip); \ - \ - if ((1 > size) || (size - PHPWRITE(data, size))) { \ - if (dofree) { \ - efree(data); \ - } \ - return FAILURE; \ - } \ - \ - http_flush(); \ - if (dosleep) { \ - http_sleep(); \ - } \ } +} -#define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC) -/* {{{ static STATUS http_send_chunk(const void *, size_t, size_t, http_send_mode) */ -static STATUS _http_send_chunk(const void *data, size_t begin, size_t end, http_send_mode mode TSRMLS_DC) +#define http_send_response_data_plain(b, d, dl) _http_send_response_data_plain((b), (d), (dl) TSRMLS_CC) +static void _http_send_response_data_plain(void *buffer, const char *data, size_t data_len TSRMLS_DC) { - long len = end - begin; - size_t chunk_size = HTTP_G(send).buffer_size; - http_encoding_type gzip = HTTP_G(send).gzip_encoding; + if (HTTP_G(send).gzip_encoding) { +#ifdef HTTP_HAVE_ZLIB + char *encoded; + size_t encoded_len; + + http_encoding_stream_update(buffer, data, data_len, &encoded, &encoded_len); + phpstr_chunked_output(&(((http_encoding_stream *) buffer)->storage), data, data_len, HTTP_G(send).buffer_size, _http_flush); + efree(encoded); +#else + http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug"); +#endif + } else { + phpstr_chunked_output(&buffer, data, data_len, HTTP_G(send).buffer_size, _http_flush); + } +} +#define HTTP_CHUNK_AVAIL(len, cs) ((len -= cs) >= 0) +/* {{{ http_send_response_data_fetch */ +#define http_send_response_data_fetch(b, t, d, l, b, e) _http_send_response_data_fetch((b), (t), (d), (l), (b), (e) TSRMLS_CC) +static void _http_send_response_data_fetch(void *buffer, const void *data, size_t data_len, http_send_mode mode, size_t begin, size_t end TSRMLS_DC) +{ + long len = end - begin, chunk_size = 40960; + switch (mode) { case SEND_RSRC: @@ -131,45 +124,62 @@ static STATUS _http_send_chunk(const void *data, size_t begin, size_t end, http_ return FAILURE; } - buf = emalloc(HTTP_G(send).buffer_size); + buf = emalloc(chunk_size); while (HTTP_CHUNK_AVAIL(len, chunk_size)) { - HTTP_CHUNK_WRITE(buf, php_stream_read(s, buf, chunk_size), 1, 1, gzip); + http_send_response_data_plain(buffer, buf, php_stream_read(s, buf, chunk_size)); } - /* read & write left over */ if (len) { - HTTP_CHUNK_WRITE(buf, php_stream_read(s, buf, chunk_size + len), 1, 0, gzip); + http_send_response_data_plain(buffer, buf, php_stream_read(s, buf, chunk_size + len)); } efree(buf); - return SUCCESS; } + break; case SEND_DATA: { char *s = (char *) data + begin; while (HTTP_CHUNK_AVAIL(len, chunk_size)) { - HTTP_CHUNK_WRITE(s, chunk_size, 0, 1, gzip); + http_send_response_data_plain(buffer, s, chunk_size); s += chunk_size; } - /* write left over */ if (len) { - HTTP_CHUNK_WRITE(s, chunk_size + len, 0, 0, gzip); + http_send_response_data_plain(buffer, s, chunk_size + len); } - - return SUCCESS; } - - default: - return FAILURE; break; + + EMPTY_DEFAULT_SWITCH_CASE(); + } +} +/* }}} */ + +/* {{{ http_send_response_finish */ +#define http_send_response_finish(b) _http_send_response_finish((b) TSRMLS_CC) +static void _http_send_response_finish(void *buffer TSRMLS_DC) +{ + if (HTTP_G(send).gzip_encoding) { +#ifdef HTTP_HAVE_ZLIB + char *encoded = NULL; + size_t encoded_len; + + http_encoding_stream_finish(buffer, &encoded, &encoded_len); + buffer = ((http_encoding_stream *) buffer)->storage; + phpstr_chunked_output(buffer, encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush); + STR_FREE(encoded); +#else + http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug"); +#endif } + phpstr_chunked_output(buffer, NULL, 0, 0, _http_flush); } /* }}} */ + /* {{{ STATUS http_send_header(char *, char *, zend_bool) */ PHP_HTTP_API STATUS _http_send_header_ex(const char *name, size_t name_len, const char *value, size_t value_len, zend_bool replace, char **sent_header TSRMLS_DC) { @@ -281,103 +291,25 @@ PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename, size_t } /* }}} */ -/* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */ -PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_t size, http_send_mode mode TSRMLS_DC) -{ - zval **zbegin, **zend, **zrange; - - /* single range */ - if (zend_hash_num_elements(ranges) == 1) { - char range_header[256] = {0}; - - if (SUCCESS != zend_hash_index_find(ranges, 0, (void **) &zrange) || - SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &zbegin) || - SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &zend)) { - http_send_status(500); - return FAILURE; - } - - /* Send HTTP 206 Partial Content */ - http_send_status(206); - - /* send content range header */ - snprintf(range_header, 255, "Content-Range: bytes %ld-%ld/%lu", Z_LVAL_PP(zbegin), Z_LVAL_PP(zend), (ulong) size); - http_send_header_string(range_header); - - /* send requested chunk */ - return http_send_chunk(data, Z_LVAL_PP(zbegin), Z_LVAL_PP(zend) + 1, mode); - } - - /* multi range */ - else { - size_t preface_len; - char bound[23] = {0}, preface[1024] = {0}, - multi_header[68] = "Content-Type: multipart/byteranges; boundary="; - - /* Send HTTP 206 Partial Content */ - http_send_status(206); - - /* send multipart/byteranges header */ - snprintf(bound, 22, "--%lu%0.9f", (ulong) time(NULL), php_combined_lcg(TSRMLS_C)); - strncat(multi_header, bound + 2, 21); - http_send_header_string(multi_header); - - /* send each requested chunk */ - FOREACH_HASH_VAL(ranges, zrange) { - if (SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &zbegin) || - SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &zend)) { - break; - } - - preface_len = snprintf(preface, 1023, - HTTP_CRLF "%s" - HTTP_CRLF "Content-Type: %s" - HTTP_CRLF "Content-Range: bytes %ld-%ld/%lu" - HTTP_CRLF - HTTP_CRLF, - - bound, - HTTP_G(send).content_type ? HTTP_G(send).content_type : "application/x-octetstream", - Z_LVAL_PP(zbegin), - Z_LVAL_PP(zend), - (ulong) size - ); - - PHPWRITE(preface, preface_len); - http_send_chunk(data, Z_LVAL_PP(zbegin), Z_LVAL_PP(zend) + 1, mode); - } - - /* write boundary once more */ - PHPWRITE(HTTP_CRLF, lenof(HTTP_CRLF)); - PHPWRITE(bound, strlen(bound)); - PHPWRITE("--", lenof("--")); - - return SUCCESS; - } -} -/* }}} */ - /* {{{ STATUS http_send(void *, size_t, http_send_mode) */ PHP_HTTP_API STATUS _http_send_ex(const void *data_ptr, size_t data_size, http_send_mode data_mode, zend_bool no_cache TSRMLS_DC) { HashTable ranges; http_range_status range_status; - int cache_etag = 0, external_gzip_handlers = 0; - + int cache_etag = http_interrupt_etag_handler(); +#ifdef HTTP_HAVE_ZLIB + http_encoding_stream s; +#else + phpstr *s = NULL; +#endif + if (!data_ptr) { return FAILURE; } if (!data_size) { return SUCCESS; } - - /* stop on-the-fly etag generation */ - cache_etag = http_interrupt_ob_etaghandler(); - - if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC) || - php_ob_handler_used("zlib output compression" TSRMLS_CC)) { - external_gzip_handlers = 1; - } + /* enable partial dl and resume */ http_send_header_string("Accept-Ranges: bytes"); @@ -389,101 +321,113 @@ PHP_HTTP_API STATUS _http_send_ex(const void *data_ptr, size_t data_size, http_s http_send_status(416); return FAILURE; } - - /* Range Request - only send ranges if entity hasn't changed */ - if ( range_status == RANGE_OK && - http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(send).unquoted_etag, 0) && - http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(send).last_modified, 0) && - http_match_last_modified_ex("HTTP_UNLESS_MODIFIED_SINCE", HTTP_G(send).last_modified, 0)) { - STATUS result = http_send_ranges(&ranges, data_ptr, data_size, data_mode); - zend_hash_destroy(&ranges); - return result; - } - - zend_hash_destroy(&ranges); - - /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */ - if (!no_cache && cache_etag) { - char *etag = NULL; - - if (etag = http_etag(data_ptr, data_size, data_mode)) { - char *sent_header = NULL; - - http_send_etag_ex(etag, strlen(etag), &sent_header); - if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) { - return http_exit_ex(304, sent_header, NULL, 0); - } else { - STR_FREE(sent_header); - } - efree(etag); - } - } - - /* send 304 Not Modified if last modified matches */ - if (!no_cache && http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(send).last_modified)) { - char *sent_header = NULL; - http_send_last_modified_ex(HTTP_G(send).last_modified, &sent_header); - return http_exit_ex(304, sent_header, NULL, 0); - } - - if (external_gzip_handlers) { -#ifdef HTTP_HAVE_ZLIB - if (HTTP_G(send).gzip_encoding) { - HTTP_G(send).gzip_encoding = 0; - } - } else if (HTTP_G(send).gzip_encoding) { - HashTable *selected; - zval zsupported; - - INIT_PZVAL(&zsupported); - array_init(&zsupported); - add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1); - add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1); - add_next_index_stringl(&zsupported, "compress", lenof("compress"), 1); - - if (selected = http_negotiate_encoding(&zsupported)) { - char *encoding = NULL; - ulong idx; - - if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) { - STATUS hs = FAILURE; + + switch (range_status) + { + case RANGE_OK: + /* Range Request - only send ranges if entity hasn't changed */ + if ( http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(send).unquoted_etag, 0) && + http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(send).last_modified, 0) && + http_match_last_modified_ex("HTTP_UNLESS_MODIFIED_SINCE", HTTP_G(send).last_modified, 0)) { - if (!strcmp(encoding, "gzip")) { - if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) { - HTTP_G(send).gzip_encoding = HTTP_ENCODING_GZIP; + if (zend_hash_num_elements(&ranges) == 1) { + /* single range */ + zval **range, **begin, **end; + + if ( SUCCESS == zend_hash_index_find(ranges, 0, (void **) &range) && + SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void **) &begin) && + SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void **) &end)) { + char range_header_str[256]; + size_t range_header_len; + + range_header_len = snprintf(range_header_str, "Content-Range: bytes %ld-%ld/%lu", Z_LVAL_PP(begin), Z_LVAL_PP(end), (unsigned long) data_size); + http_send_status_header_ex(206, range_header_str, range_header_len, 1); + http_send_response_start(s, Z_LVAL_PP(end)-Z_LVAL_PP(begin)); + http_send_response_data_fetch(s, data_ptr, data_size, data_mode, Z_LVAL_PP(begin), Z_LVAL_PP(end) + 1); + http_send_response_finish(s); + zend_hash_destroy(&ranges); + return SUCCESS; } - } else if (!strcmp(encoding, "deflate")) { - if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) { - HTTP_G(send).gzip_encoding = HTTP_ENCODING_DEFLATE; + } else { + /* multi range */ + zval **range, **begin, **end; + const char *content_type = HTTP_G(send).content_type; + char *boundary_str, *range_header_str; + size_t boundary_len, range_header_len; + + boundary_len = spprintf(&boundary_str, 0, "%lu%0.9f", (unsigned long) time(NULL), (float) php_combined_lcg(TSRMLS_C)) + range_header_len = spprintf(&range_header_str, 0, "Content-Type: multipart/byteranges; boundary=%s", boundary_str); + + http_send_status_header_ex(206, range_header, range_header_len, 1); + efree(range_header_str); + http_send_response_start(s, 0); + + if (!content_type) { + content_type = "application/x-octetstream"; } - } else if (!strcmp(encoding, "compress")) { - if (SUCCESS == (hs = http_send_header_string("Content-Encoding: compress"))) { - HTTP_G(send).gzip_encoding = HTTP_ENCODING_COMPRESS; + + FOREACH_HASH_VAL(&ranges, range) { + if ( SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void **) &begin) && + SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void **) &begin)) { + char preface_str[512]; + size_t preface_len; + +#define HTTP_RANGE_PREFACE \ + HTTP_CRLF "%s" \ + HTTP_CRLF "Content-Type: %s" \ + HTTP_CRLF "Content-Range: %ld-%ld/%lu" \ + HTTP_CRLF HTTP_CRLF + + preface_len = snprintf(preface_str, lenof(preface_str), HTTP_RANGE_PREFACE, boundary_str, content_type, Z_LVAL_PP(begin), Z_LVAL_PP(end), data_size); + http_send_response_data_plain(s, preface, preface_len); + http_send_response_data_fetch(s, data_ptr, data_len, data_mode, Z_LVAL_PP(begin), Z_LVAL_PP(end) + 1); + } } - } - if (SUCCESS == hs) { - http_send_header_string("Vary: Accept-Encoding"); - } else { - HTTP_G(send).gzip_encoding = 0; + + http_send_response_data_plain(s, HTTP_CRLF, lenof(HTTP_CRLF)); + http_send_response_data_plain(s, boundary_str, boundary_len); + http_send_response_data_plain(s, "--", lenof("--")); + + efree(boundary_str); + + http_send_response_finish(s); + zend_hash_destroy(&ranges); + return SUCCESS; } } + case RANGE_NO: + zend_hash_destroy(&ranges); - zend_hash_destroy(selected); - FREE_HASHTABLE(selected); - } + /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */ + if (!no_cache && cache_etag) { + char *etag = NULL; + + if (etag = http_etag(data_ptr, data_size, data_mode)) { + char *sent_header = NULL; + + http_send_etag_ex(etag, strlen(etag), &sent_header); + if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) { + return http_exit_ex(304, sent_header, NULL, 0); + } else { + STR_FREE(sent_header); + } + efree(etag); + } + } - zval_dtor(&zsupported); -#endif - } else { - /* emit a content-length header */ - char *cl; - spprintf(&cl, 0, "Content-Length: %lu", (unsigned long) data_size); - http_send_header_string(cl); - efree(cl); + /* send 304 Not Modified if last modified matches */ + if (!no_cache && http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(send).last_modified)) { + char *sent_header = NULL; + http_send_last_modified_ex(HTTP_G(send).last_modified, &sent_header); + return http_exit_ex(304, sent_header, NULL, 0); + } + + /* send full response */ + http_send_response_start(s, data_size); + http_send_response_data_fetch(s, data_ptr, data_size, data_mode, 0, data_size); + http_send_response_finish(s); + return SUCCESS; } - - /* send full entity */ - return http_send_chunk(data_ptr, 0, data_size, data_mode); } /* }}} */ diff --git a/php_http_encoding_api.h b/php_http_encoding_api.h index 60dbc11..8c651a5 100644 --- a/php_http_encoding_api.h +++ b/php_http_encoding_api.h @@ -19,12 +19,21 @@ #define PHP_HTTP_ENCODING_API_H #include "php_http_std_defs.h" +#include "phpstr/phpstr.h" #define http_encoding_dechunk(e, el, d, dl) _http_encoding_dechunk((e), (el), (d), (dl) TSRMLS_CC) PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC); + #ifdef HTTP_HAVE_ZLIB +/* max count of uncompress trials, alloc_size <<= 2 for each try */ +#define HTTP_ENCODING_MAXTRY 10 +/* safe padding */ +#define HTTP_ENCODING_SAFPAD 10 +/* add 1% extra space in case we need to encode widely differing (binary) data */ +#define HTTP_ENCODING_BUFLEN(l) (l + (l / 100) + HTTP_ENCODING_SAFPAD) + typedef enum { HTTP_ENCODING_NONE = 0, HTTP_ENCODING_ANY = 1, @@ -38,6 +47,23 @@ PHP_HTTP_API STATUS _http_encode(http_encoding_type type, int level, const char #define http_decode(t, d, dl, r, rl) _http_decode((t), (l), (d), (dl), (r), (rl) TSRMLS_CC) 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); +typedef struct { + z_stream Z; + int gzip; + unsigned long crc; + phpstr *storage; +} http_encoding_stream; + +#define http_encoding_stream_init(s, g, l, e, el) _http_encoding_stream_init((s), g, (l), (e), (el) TSRMLS_CC) +PHP_HTTP_API STATUS _http_encoding_stream_init(http_encoding_stream *s, int gzip, int level, char **encoded, size_t *encoded_len TSRMLS_DC); +#define http_encoding_stream_update(s, d, dl, e, el) _http_encoding_stream_update((s), (d), (dl), (e), (el) TSRMLS_CC) +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); +#define http_encoding_stream_finish(s, e, el) _http_encoding_stream_finish((s), (e), (el) TSRMLS_CC) +PHP_HTTP_API STATUS _http_encoding_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC); + +#define http_encoding_response_start(cl) _http_encoding_response_start((cl) TSRMS_CC) +PHP_HTTP_API zend_bool _http_encoding_response_start(size_t content_length TSRMLS_DC); + #define http_encoding_gzencode(l, d, dl, r, rl) _http_encoding_gzencode((l), (d), (dl), (r), (rl) TSRMLS_CC) PHP_HTTP_API STATUS _http_encoding_gzencode(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC); #define http_encoding_gzdecode(d, dl, r, rl) _http_encoding_gzdecode((d), (dl), (r), (rl) TSRMLS_CC) diff --git a/php_http_send_api.h b/php_http_send_api.h index 0c5b1f5..8dcbab7 100644 --- a/php_http_send_api.h +++ b/php_http_send_api.h @@ -53,9 +53,6 @@ PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_ #define http_send_content_disposition(f, l, i) _http_send_content_disposition((f), (l), (i) TSRMLS_CC) PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename, size_t f_len, zend_bool send_inline TSRMLS_DC); -#define http_send_ranges(r, d, s, m) _http_send_ranges((r), (d), (s), (m) TSRMLS_CC) -PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_t size, http_send_mode mode TSRMLS_DC); - #define http_send_data(d, l) http_send((d), (l), SEND_DATA) #define http_send_data_ex(d, l, nc) http_send_ex((d), (l), SEND_DATA, (nc)) #define http_send(d, s, m) _http_send_ex((d), (s), (m), 0 TSRMLS_CC) diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index 70673a9..3e8e311 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -245,6 +245,51 @@ PHPSTR_API void phpstr_free(phpstr **buf) } } +PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_len, char **chunk, size_t chunk_size) +{ + phpstr *storage; + + *chunk = NULL; + + if (!*s) { + *s = phpstr_init_ex(NULL, chunk_size * 2, chunk_size ? 1 : 0); + } + storage = *s; + + if (data_len) { + phpstr_append(storage, data, data_len); + } + + if (!chunk_size) { + phpstr_data(storage, chunk, &chunk_size); + phpstr_free(&storage); + return chunk_size; + } + + if (storage->used >= storage->size/2) { + phpstr *avail = phpstr_left(storage, storage->size/2); + *chunk = estrndup(PHPSTR_VAL(avail), PHPSTR_LEN(avail)); + phpstr_free(&avail); + phpstr_cut(storage, 0, storage->size/2); + return storage->size/2; + } + + return 0; +} + +PHPSTR_API void phpstr_chunked_output(phpstr **s, const char *data, size_t data_len, size_t chunk_len, void (*passthru)(const char *, size_t TSRMLS_DC) TSRMLS_DC) +{ + char *chunk = NULL; + size_t got = 0; + + while (got = phpstr_chunk_buffer(s, data, data_len, &chunk, chunk_len)) { + passthru(chunk, got TSRMLS_CC); + efree(chunk); + data = NULL; + data_len = 0; + } +} + /* * Local variables: * tab-width: 4 diff --git a/phpstr/phpstr.h b/phpstr/phpstr.h index 5b2ee56..872bf06 100644 --- a/phpstr/phpstr.h +++ b/phpstr/phpstr.h @@ -155,6 +155,12 @@ PHPSTR_API void phpstr_dtor(phpstr *buf); /* free a phpstr object completely */ PHPSTR_API void phpstr_free(phpstr **buf); +/* stores data in a phpstr until it reaches chunk_size */ +PHPSTR_API size_t phpstr_chunk_buffer(phpstr **s, const char *data, size_t data_len, char **chunk, size_t chunk_size); + +/* wrapper around phpstr_chunk_buffer, which passes available chunks to passthru() */ +PHPSTR_API void phpstr_chunked_output(phpstr **s, const char *data, size_t data_len, size_t chunk_size, void (*passthru)(const char *, size_t TSRMLS_DC) TSRMLS_DC); + #endif -- 2.30.2