branch off v1 as R_1_7
[m6w6/ext-http] / http_filter_api.c
index 1836b27d6b259cb362d3189985420932c15b5207..23b71190acebb730a3d5002b734d4f108299e9ed 100644 (file)
@@ -6,16 +6,12 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2005, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
 /* $Id$ */
 
-#ifdef HAVE_CONFIG_H
-#      include "config.h"
-#endif
-
 #define HTTP_WANT_ZLIB
 #include "php_http.h"
 
@@ -78,7 +74,7 @@ PHP_MINIT_FUNCTION(http_filter)
                php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
        }
 
-typedef struct {
+typedef struct _http_chunked_decode_filter_buffer_t {
        phpstr  buffer;
        ulong   hexlen;
 } HTTP_FILTER_BUFFER(chunked_decode);
@@ -109,7 +105,7 @@ static HTTP_FILTER_FUNCTION(chunked_decode)
                                *bytes_consumed += ptr->buflen;
                        }
                
-                       if ((size_t) -1 == phpstr_append(PHPSTR(buffer), ptr->buf, ptr->buflen)) {
+                       if (PHPSTR_NOMEM == phpstr_append(PHPSTR(buffer), ptr->buf, ptr->buflen)) {
                                return PSFS_ERR_FATAL;
                        }
                        
@@ -131,7 +127,7 @@ static HTTP_FILTER_FUNCTION(chunked_decode)
                        if (PHPSTR_LEN(buffer) < buffer->hexlen) {
                        
                                /* flush anyway? */
-                               if (flags == PSFS_FLAG_FLUSH_INC) {
+                               if (flags & PSFS_FLAG_FLUSH_INC) {
                                
                                        /* flush all data (should only be chunk data) */
                                        out_avail = 1;
@@ -169,8 +165,8 @@ static HTTP_FILTER_FUNCTION(chunked_decode)
                        
                        /* ignore preceeding CRLFs (too loose?) */
                        while (off < PHPSTR_LEN(buffer) && (
-                                       PHPSTR_VAL(buffer)[off] == 0xa || 
-                                       PHPSTR_VAL(buffer)[off] == 0xd)) {
+                                       PHPSTR_VAL(buffer)[off] == '\n' || 
+                                       PHPSTR_VAL(buffer)[off] == '\r')) {
                                ++off;
                        }
                        if (off) {
@@ -210,7 +206,7 @@ static HTTP_FILTER_FUNCTION(chunked_decode)
        }
        
        /* flush before close, but only if we are already waiting for more data */
-       if (flags == PSFS_FLAG_FLUSH_CLOSE && buffer->hexlen && PHPSTR_LEN(buffer)) {
+       if ((flags & PSFS_FLAG_FLUSH_CLOSE) && buffer->hexlen && PHPSTR_LEN(buffer)) {
                out_avail = 1;
                NEW_BUCKET(PHPSTR_VAL(buffer), PHPSTR_LEN(buffer));
                phpstr_reset(PHPSTR(buffer));
@@ -269,7 +265,7 @@ static HTTP_FILTER_FUNCTION(chunked_encode)
        }
        
        /* terminate with "0" */
-       if (flags == PSFS_FLAG_FLUSH_CLOSE) {
+       if (flags & PSFS_FLAG_FLUSH_CLOSE) {
                out_avail = 1;
                NEW_BUCKET("0" HTTP_CRLF, lenof("0" HTTP_CRLF));
        }
@@ -317,8 +313,10 @@ static HTTP_FILTER_FUNCTION(deflate)
                        if (ptr->buflen) {
                                http_encoding_deflate_stream_update(buffer, ptr->buf, ptr->buflen, &encoded, &encoded_len);
                                if (encoded) {
-                                       out_avail = 1;
-                                       NEW_BUCKET(encoded, encoded_len);
+                                       if (encoded_len) {
+                                               out_avail = 1;
+                                               NEW_BUCKET(encoded, encoded_len);
+                                       }
                                        efree(encoded);
                                }
                        }
@@ -329,14 +327,30 @@ static HTTP_FILTER_FUNCTION(deflate)
        }
        
        /* flush & close */
-       if (flags == PSFS_FLAG_FLUSH_CLOSE) {
+       if (flags & PSFS_FLAG_FLUSH_INC) {
+               char *encoded = NULL;
+               size_t encoded_len = 0;
+               
+               http_encoding_deflate_stream_flush(buffer, &encoded, &encoded_len);
+               if (encoded) {
+                       if (encoded_len) {
+                               out_avail = 1;
+                               NEW_BUCKET(encoded, encoded_len);
+                       }
+                       efree(encoded);
+               }
+       }
+       
+       if (flags & PSFS_FLAG_FLUSH_CLOSE) {
                char *encoded = NULL;
                size_t encoded_len = 0;
                
                http_encoding_deflate_stream_finish(buffer, &encoded, &encoded_len);
                if (encoded) {
-                       out_avail = 1;
-                       NEW_BUCKET(encoded, encoded_len);
+                       if (encoded_len) {
+                               out_avail = 1;
+                               NEW_BUCKET(encoded, encoded_len);
+                       }
                        efree(encoded);
                }
        }
@@ -370,8 +384,10 @@ static HTTP_FILTER_FUNCTION(inflate)
                        if (ptr->buflen) {
                                http_encoding_inflate_stream_update(buffer, ptr->buf, ptr->buflen, &decoded, &decoded_len);
                                if (decoded) {
-                                       out_avail = 1;
-                                       NEW_BUCKET(decoded, decoded_len);
+                                       if (decoded_len) {
+                                               out_avail = 1;
+                                               NEW_BUCKET(decoded, decoded_len);
+                                       }
                                        efree(decoded);
                                }
                        }
@@ -382,14 +398,30 @@ static HTTP_FILTER_FUNCTION(inflate)
        }
        
        /* flush & close */
-       if (flags == PSFS_FLAG_FLUSH_CLOSE) {
+       if (flags & PSFS_FLAG_FLUSH_INC) {
+               char *decoded = NULL;
+               size_t decoded_len = 0;
+               
+               http_encoding_inflate_stream_flush(buffer, &decoded, &decoded_len);
+               if (decoded) {
+                       if (decoded_len) {
+                               out_avail = 1;
+                               NEW_BUCKET(decoded, decoded_len);
+                       }
+                       efree(decoded);
+               }
+       }
+       
+       if (flags & PSFS_FLAG_FLUSH_CLOSE) {
                char *decoded = NULL;
                size_t decoded_len = 0;
                
                http_encoding_inflate_stream_finish(buffer, &decoded, &decoded_len);
                if (decoded) {
-                       out_avail = 1;
-                       NEW_BUCKET(decoded, decoded_len);
+                       if (decoded_len) {
+                               out_avail = 1;
+                               NEW_BUCKET(decoded, decoded_len);
+                       }
                        efree(decoded);
                }
        }
@@ -460,20 +492,18 @@ static php_stream_filter *http_filter_create(const char *name, zval *params, int
                HTTP_FILTER_BUFFER(deflate) *b = NULL;
                
                if (params) {
-                       switch (Z_TYPE_P(params))
-                       {
+                       switch (Z_TYPE_P(params)) {
                                case IS_ARRAY:
                                case IS_OBJECT:
-                                       if (SUCCESS != zend_hash_find(HASH_OF(params), "flags", sizeof("flags"), (void **) &tmp)) {
+                                       if (SUCCESS != zend_hash_find(HASH_OF(params), "flags", sizeof("flags"), (void *) &tmp)) {
                                                break;
                                        }
                                default:
                                {
-                                       zval *orig = *tmp;
+                                       zval *num = http_zsep(IS_LONG, *tmp);
                                        
-                                       convert_to_long_ex(tmp);
-                                       flags |= (Z_LVAL_PP(tmp) & 0x0fffffff);
-                                       if (orig != *tmp) zval_ptr_dtor(tmp);
+                                       flags |= (Z_LVAL_P(num) & 0x0fffffff);
+                                       zval_ptr_dtor(&num);
                                }
                        }
                }