- fix inclusion of zlib.h
[m6w6/ext-http] / http_filter_api.c
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;