- fix inclusion of zlib.h
authorMichael Wallner <mike@php.net>
Sun, 18 Dec 2005 12:03:42 +0000 (12:03 +0000)
committerMichael Wallner <mike@php.net>
Sun, 18 Dec 2005 12:03:42 +0000 (12:03 +0000)
- made encoding stream persistance aware
- added gzencode/deflate stream filter

config.w32
http_api.c
http_encoding_api.c
http_filter_api.c
http_functions.c
http_message_api.c
http_send_api.c
php_http_encoding_api.h

index b4d2fd35cdcbb8dfcd8d8cdbbe6ef13532539b52..7db93da54e6daa0067fdcc6d311b53be64c2e163 100644 (file)
@@ -54,7 +54,7 @@ if (PHP_HTTP != "no") {
                "http_request_api.c http_date_api.c http_headers_api.c "+
                "http_message_api.c http_send_api.c http_url_api.c "+
                "http_info_api.c http_request_method_api.c http_encoding_api.c "+
-               "http_filter_api.c http_request_body_api.c http_wrapper_api.c",
+               "http_filter_api.c http_request_body_api.c",
                null,
                "/I\"" + configure_module_dirname + "/phpstr\"");
        ADD_SOURCES(configure_module_dirname + "/phpstr", "phpstr.c", "http");
index 27986373b2f328895cd22ecb7c5c933e0506b2b3..22d622cdf6cb42e8ed12a0003a652a91354f87fe 100644 (file)
@@ -21,7 +21,6 @@
 #include "SAPI.h"
 #include "php_output.h"
 #include "ext/standard/url.h"
-#include "ext/standard/head.h"
 
 #include "php_http_api.h"
 #include "php_http_send_api.h"
@@ -250,7 +249,7 @@ STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header
        }
        
        php_end_ob_buffers(0 TSRMLS_CC);
-       if (php_header(TSRMLS_C) && body) {
+       if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
                PHPWRITE(body, strlen(body));
        }
        
index 5d1a908b5bfae52bab857d5b3c67e33695cfae5d..efbaa7f8669065d006803847406f01e0a8e85bd1 100644 (file)
@@ -15,6 +15,8 @@
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
+
+#define HTTP_WANT_ZLIB
 #include "php_http.h"
 
 #include "php_http_api.h"
@@ -411,19 +413,21 @@ PHP_HTTP_API STATUS _http_encoding_gzdecode_verify(const char *data, size_t data
                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)
+PHP_HTTP_API STATUS _http_encoding_stream_init(http_encoding_stream *s, int flags, int level, char **encoded, size_t *encoded_len TSRMLS_DC)
 {
        STATUS status;
+       int wbits = (flags & HTTP_ENCODING_STREAM_ZLIB_HEADER) ? MAX_WBITS : -MAX_WBITS;
        
        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))) {
+       if (Z_OK != (status = deflateInit2(&s->Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) {
                HTTP_ENCODING_STREAM_ERROR(status, NULL);
        }
        
-       if ((s->gzip = gzip)) {
+       s->persistent = (flags & HTTP_ENCODING_STREAM_PERSISTENT);
+       if ((s->gzip = (flags & HTTP_ENCODING_STREAM_GZIP_HEADER))) {
                s->crc = crc32(0L, Z_NULL, 0);
                *encoded_len = sizeof(http_encoding_gzip_header);
-               *encoded = emalloc(*encoded_len);
+               *encoded = pemalloc(*encoded_len, s->persistent);
                memcpy(*encoded, http_encoding_gzip_header, *encoded_len);
        } else {
                *encoded_len = 0;
@@ -438,7 +442,7 @@ PHP_HTTP_API STATUS _http_encoding_stream_update(http_encoding_stream *s, const
        STATUS status;
        
        *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
-       *encoded = emalloc(*encoded_len);
+       *encoded = pemalloc(*encoded_len, s->persistent);
        
        s->Z.next_in = (Bytef *) data;
        s->Z.avail_in = data_len;
@@ -464,7 +468,7 @@ PHP_HTTP_API STATUS _http_encoding_stream_finish(http_encoding_stream *s, char *
        STATUS status;
        
        *encoded_len = 1024;
-       *encoded = emalloc(*encoded_len);
+       *encoded = pemalloc(*encoded_len, s->persistent);
        
        s->Z.next_out = (Bytef *) *encoded;
        s->Z.avail_out = *encoded_len;
@@ -476,7 +480,7 @@ PHP_HTTP_API STATUS _http_encoding_stream_finish(http_encoding_stream *s, char *
        *encoded_len -= s->Z.avail_out;
        if (s->gzip) {
                if (s->Z.avail_out < 8) {
-                       *encoded = erealloc(*encoded, *encoded_len + 8);
+                       *encoded = perealloc(*encoded, *encoded_len + 8, s->persistent);
                }
                (*encoded)[(*encoded_len)++] = (char) (s->crc & 0xFF);
                (*encoded)[(*encoded_len)++] = (char) ((s->crc >> 8) & 0xFF);
index 444e3d067ffb530c829b820845f9ec139eab5ce9..648c509adff7d66177a9f07bed22b04704796156 100644 (file)
 #      include "config.h"
 #endif
 
+#define HTTP_WANT_ZLIB
 #include "php_http.h"
 
 #ifdef ZEND_ENGINE_2
 
 #include "php_streams.h"
 #include "php_http_api.h"
+#include "php_http_encoding_api.h"
 #include "php_http_filter_api.h"
 
 PHP_MINIT_FUNCTION(http_filter)
@@ -34,11 +36,6 @@ PHP_MINIT_FUNCTION(http_filter)
        -
 */
 
-typedef struct {
-       phpstr  buffer;
-       ulong   hexlen;
-} http_filter_buffer;
-
 #define HTTP_FILTER_PARAMS \
        php_stream *stream, \
        php_stream_filter *this, \
@@ -58,6 +55,8 @@ typedef struct {
        http_filter_ ##filter
 #define HTTP_FILTER_FUNCTION(filter) \
        php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
+#define HTTP_FILTER_BUFFER(filter) \
+       http_filter_ ##filter## _buffer
 
 #define NEW_BUCKET(data, length) \
        { \
@@ -79,11 +78,25 @@ typedef struct {
                php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
        }
 
+typedef struct {
+       phpstr  buffer;
+       ulong   hexlen;
+} HTTP_FILTER_BUFFER(chunked_decode);
+
+#ifdef HTTP_HAVE_ZLIB
+typedef struct {
+       int init;
+       int flags;
+       http_encoding_stream stream;
+} HTTP_FILTER_BUFFER(gzip);
+#endif /* HTTP_HAVE_ZLIB */
+
+
 static HTTP_FILTER_FUNCTION(chunked_decode)
 {
        int out_avail = 0;
        php_stream_bucket *ptr, *nxt;
-       http_filter_buffer *buffer = (http_filter_buffer *) (this->abstract);
+       HTTP_FILTER_BUFFER(chunked_decode) *buffer = (HTTP_FILTER_BUFFER(chunked_decode) *) (this->abstract);
        
        if (bytes_consumed) {
                *bytes_consumed = 0;
@@ -212,7 +225,7 @@ static HTTP_FILTER_FUNCTION(chunked_decode)
 
 static HTTP_FILTER_DESTRUCTOR(chunked_decode)
 {
-       http_filter_buffer *b = (http_filter_buffer *) (this->abstract);
+       HTTP_FILTER_BUFFER(chunked_decode) *b = (HTTP_FILTER_BUFFER(chunked_decode) *) (this->abstract);
        
        phpstr_dtor(PHPSTR(b));
        pefree(b, this->is_persistent);
@@ -279,14 +292,101 @@ static HTTP_FILTER_OPS(chunked_encode) = {
        "http.chunked_encode"
 };
 
+#ifdef HTTP_HAVE_ZLIB
+static HTTP_FILTER_FUNCTION(gzip)
+{
+       int out_avail = 0;
+       php_stream_bucket *ptr, *nxt;
+       HTTP_FILTER_BUFFER(gzip) *buffer = (HTTP_FILTER_BUFFER(gzip) *) this->abstract;
+       
+       if (bytes_consumed) {
+               *bytes_consumed = 0;
+       }
+       
+       /* first round */
+       if (!buffer->init) {
+               char *encoded = NULL;
+               size_t encoded_len = 0;
+               
+               buffer->init = 1;
+               http_encoding_stream_init(&buffer->stream, buffer->flags, -1, &encoded, &encoded_len);
+               
+               if (encoded) {
+                       out_avail = 1;
+                       NEW_BUCKET(encoded, encoded_len);
+                       pefree(encoded, this->is_persistent);
+               }
+       }
+       
+       /* new data available? */
+       if (buckets_in->head) {
+               
+               /* fetch available bucket data */
+               for (ptr = buckets_in->head; ptr; ptr = nxt) {
+                       char *encoded = NULL;
+                       size_t encoded_len = 0;
+                       
+                       nxt = ptr->next;
+                       if (bytes_consumed) {
+                               *bytes_consumed += ptr->buflen;
+                       }
+                       
+                       /* this is actually flushing implicitly */
+                       http_encoding_stream_update(&buffer->stream, ptr->buf, ptr->buflen, &encoded, &encoded_len);
+                       if (encoded) {
+                               out_avail = 1;
+                               NEW_BUCKET(encoded, encoded_len);
+                               pefree(encoded, this->is_persistent);
+                       }
+                       
+                       php_stream_bucket_unlink(ptr TSRMLS_CC);
+                       php_stream_bucket_delref(ptr TSRMLS_CC);
+               }
+       }
+       
+       /* flush & close */
+       if (flags == PSFS_FLAG_FLUSH_CLOSE) {
+               char *encoded = NULL;
+               size_t encoded_len = 0;
+               
+               http_encoding_stream_finish(&buffer->stream, &encoded, &encoded_len);
+               if (encoded) {
+                       out_avail = 1;
+                       NEW_BUCKET(encoded, encoded_len);
+               }
+       }
+       
+       return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
+}
+
+static HTTP_FILTER_DESTRUCTOR(gzip)
+{
+       HTTP_FILTER_BUFFER(gzip) *buffer = (HTTP_FILTER_BUFFER(gzip) *) this->abstract;
+       
+       pefree(buffer, this->is_persistent);
+}
+
+static HTTP_FILTER_OPS(gzencode) = {
+       HTTP_FILTER_FUNC(gzip),
+       HTTP_FILTER_DTOR(gzip),
+       "http.gzencode"
+};
+
+static HTTP_FILTER_OPS(deflate) = {
+       HTTP_FILTER_FUNC(gzip),
+       HTTP_FILTER_DTOR(gzip),
+       "http.deflate"
+};
+#endif /* HTTP_HAVE_ZLIB */
+
 static php_stream_filter *http_filter_create(const char *name, zval *params, int p TSRMLS_DC)
 {
        php_stream_filter *f = NULL;
        
        if (!strcasecmp(name, "http.chunked_decode")) {
-               http_filter_buffer *b = NULL;
+               HTTP_FILTER_BUFFER(chunked_decode) *b = NULL;
                
-               if ((b = pecalloc(1, sizeof(http_filter_buffer), p))) {
+               if ((b = pecalloc(1, sizeof(HTTP_FILTER_BUFFER(chunked_decode)), p))) {
                        phpstr_init_ex(PHPSTR(b), 4096, p ? PHPSTR_INIT_PERSISTENT : 0);
                        if (!(f = php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_decode), b, p))) {
                                pefree(b, p);
@@ -296,6 +396,35 @@ static php_stream_filter *http_filter_create(const char *name, zval *params, int
        
        if (!strcasecmp(name, "http.chunked_encode")) {
                f = php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode), NULL, p);
+#ifdef HTTP_HAVE_ZLIB
+       } else
+       
+       if (!strcasecmp(name, "http.gzencode")) {
+               HTTP_FILTER_BUFFER(gzip) *b = NULL;
+               
+               if ((b = pecalloc(1, sizeof(HTTP_FILTER_BUFFER(gzip)), p))) {
+                       b->flags = HTTP_ENCODING_STREAM_GZIP_HEADER;
+                       if (p) {
+                               b->flags |= HTTP_ENCODING_STREAM_PERSISTENT;
+                       }
+                       if (!(f = php_stream_filter_alloc(&HTTP_FILTER_OP(gzencode), b, p))) {
+                               pefree(b, p);
+                       }
+               }
+       } else
+       
+       if (!strcasecmp(name, "http.deflate")) {
+               HTTP_FILTER_BUFFER(gzip) *b = NULL;
+               
+               if ((b = pecalloc(1, sizeof(HTTP_FILTER_BUFFER(gzip)), p))) {
+                       if (p) {
+                               b->flags |= HTTP_ENCODING_STREAM_PERSISTENT;
+                       }
+                       if (!(f = php_stream_filter_alloc(&HTTP_FILTER_OP(deflate), b, p))) {
+                               pefree(b, p);
+                       }
+               }
+#endif /* HTTP_HAVE_ZLIB */
        }
        
        return f;
index 80030ce0469279b2ba76545b7c2471a5b5598aea..ed59fc20799d520db457402cae35bd2988f7244f 100644 (file)
@@ -17,6 +17,7 @@
 #endif
 
 #define HTTP_WANT_CURL
+#define HTTP_WANT_ZLIB
 #include "php_http.h"
 
 #include "SAPI.h"
index 6641f5df897d1fd5ec654879a24ca958705835d3..dd38e7c8a72517fe465b0d2c0648e2c7cc28df1d 100644 (file)
@@ -17,6 +17,7 @@
 #endif
 
 #define HTTP_WANT_CURL
+#define HTTP_WANT_ZLIB
 #include "php_http.h"
 
 #include "SAPI.h"
index cfb088ae4929d40bbdcdd93c72aff7cf324ae95d..6403cf26fe97708e33b7d50d5d2dbb8b51036464 100644 (file)
@@ -16,6 +16,7 @@
 #      include "config.h"
 #endif
 
+#define HTTP_WANT_ZLIB
 #define HTTP_WANT_MAGIC
 #include "php_http.h"
 
@@ -74,10 +75,11 @@ static inline void _http_send_response_start(void **buffer, size_t content_lengt
 #ifdef HTTP_HAVE_ZLIB
                char *encoded;
                size_t encoded_len;
+               int gzip = (HTTP_G(send).gzip_encoding == HTTP_ENCODING_GZIP);
                http_encoding_stream *s = emalloc(sizeof(http_encoding_stream));
                
-               http_encoding_stream_init(s, HTTP_G(send).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);
+               http_encoding_stream_init(s, gzip?HTTP_ENCODING_STREAM_GZIP_HEADER:0, -1, &encoded, &encoded_len);
+               phpstr_chunked_output(&PHPSTR(s->storage), encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
                STR_FREE(encoded);
                *buffer = s;
 #endif
@@ -96,7 +98,7 @@ static inline void _http_send_response_data_plain(void **buffer, const char *dat
                http_encoding_stream *s = *((http_encoding_stream **) buffer);
                
                http_encoding_stream_update(s, data, data_len, &encoded, &encoded_len);
-               phpstr_chunked_output(&s->storage, encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
+               phpstr_chunked_output(&PHPSTR(s->storage), encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
                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");
@@ -167,7 +169,7 @@ static inline void _http_send_response_finish(void **buffer TSRMLS_DC)
                http_encoding_stream *s = *((http_encoding_stream **) buffer);
                
                http_encoding_stream_finish(s, &encoded, &encoded_len);
-               phpstr_chunked_output(&s->storage, encoded, encoded_len, 0, _http_flush TSRMLS_CC);
+               phpstr_chunked_output(&PHPSTR(s->storage), encoded, encoded_len, 0, _http_flush TSRMLS_CC);
                STR_FREE(encoded);
                efree(s);
 #else
index f4a2e52c381e17ccdc203e86541b9485aab087d9..8c18f3a39188453886b330f712941c5d46c9567b 100644 (file)
 #ifndef PHP_HTTP_ENCODING_API_H
 #define PHP_HTTP_ENCODING_API_H
 
-#ifdef HTTP_HAVE_ZLIB
-#      include <zlib.h>
-#endif
-
 #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);
 
@@ -40,15 +36,20 @@ typedef enum {
        HTTP_ENCODING_DEFLATE,
 } http_encoding_type;
 
+#define HTTP_ENCODING_STREAM_GZIP_HEADER       0x1
+#define HTTP_ENCODING_STREAM_ZLIB_HEADER       0x2
+#define HTTP_ENCODING_STREAM_PERSISTENT                0x4
+
 typedef struct {
        z_stream Z;
        int gzip;
+       int persistent;
        ulong crc;
-       phpstr *storage;
+       void *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);
+PHP_HTTP_API STATUS _http_encoding_stream_init(http_encoding_stream *s, int flags, 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)