- add flush() to encoding stream (filters)
authorMichael Wallner <mike@php.net>
Thu, 22 Dec 2005 21:30:53 +0000 (21:30 +0000)
committerMichael Wallner <mike@php.net>
Thu, 22 Dec 2005 21:30:53 +0000 (21:30 +0000)
http_encoding_api.c
http_filter_api.c
http_url_api.c
php_http_encoding_api.h

index 0b68397491bf5d5cbf59fe5ad6c1d17b36747d73..3cabd063f7101d247e8776772b3281318034fd68 100644 (file)
@@ -193,7 +193,7 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco
 #define HTTP_WINDOW_BITS_ANY   0x0000002f
 #define HTTP_WINDOW_BITS_RAW   -0x000000f
 
-STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
 {
        int status, level, wbits, strategy;
        z_stream Z;
@@ -234,7 +234,7 @@ STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char
        return FAILURE;
 }
 
-STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
 {
        int status, max = 0, wbits = HTTP_WINDOW_BITS_ANY;
        z_stream Z;
@@ -287,7 +287,7 @@ retry_inflate:
 }
 
 
-http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
+PHP_HTTP_API http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
 {
        int status, level, wbits, strategy, free_stream;
        
@@ -318,7 +318,7 @@ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s
        return NULL;
 }
 
-http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
+PHP_HTTP_API http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
 {
        int status, wbits, free_stream;
        
@@ -347,7 +347,7 @@ http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s
        return NULL;
 }
 
-STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
+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 TSRMLS_DC)
 {
        int status;
        
@@ -382,7 +382,7 @@ STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s, const char
        return FAILURE;
 }
 
-STATUS _http_encoding_inflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
+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 TSRMLS_DC)
 {
        int status, max = 0;
        
@@ -437,7 +437,59 @@ retry_raw_inflate:
        return FAILURE;
 }
 
-STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC)
+{
+       int status;
+       
+       s->stream.avail_in = 0;
+       s->stream.next_in = NULL;
+       s->stream.avail_out = *encoded_len = 0x800;
+       s->stream.next_out = *encoded = emalloc(*encoded_len);
+       
+       switch (status = deflate(&s->stream, Z_SYNC_FLUSH))
+       {
+               case Z_OK:
+               case Z_STREAM_END:
+                       *encoded_len = 0x800 - s->stream.avail_out;
+                       *encoded = erealloc(*encoded, *encoded_len + 1);
+                       (*encoded)[*encoded_len] = '\0';
+                       return SUCCESS;
+               break;
+       }
+       
+       STR_SET(*encoded, NULL);
+       *encoded_len = 0;
+       http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
+       return FAILURE;
+}
+
+PHP_HTTP_API STATUS _http_encoding_inflate_stream_flush(http_encoding_stream *s, char **decoded, size_t *decoded_len TSRMLS_DC)
+{
+       int status;
+       
+       s->stream.avail_in = 0;
+       s->stream.next_in = NULL;
+       s->stream.avail_out = *decoded_len = 0x800;
+       s->stream.next_out = *decoded = emalloc(*decoded_len);
+       
+       switch (status = inflate(&s->stream, Z_SYNC_FLUSH))
+       {
+               case Z_OK:
+               case Z_STREAM_END:
+                       *decoded_len = 0x800 - s->stream.avail_out;
+                       *decoded = erealloc(*decoded, *decoded_len + 1);
+                       (*decoded)[*decoded_len] = '\0';
+                       return SUCCESS;
+               break;
+       }
+       
+       STR_SET(*decoded, NULL);
+       *decoded_len = 0;
+       http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush inflate stream: %s", zError(status));
+       return FAILURE;
+}
+
+PHP_HTTP_API STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC)
 {
        int status;
        
@@ -469,7 +521,7 @@ STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **enco
        return FAILURE;
 }
 
-STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **decoded, size_t *decoded_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **decoded, size_t *decoded_len TSRMLS_DC)
 {
        int status;
        
@@ -497,7 +549,7 @@ STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **deco
        return FAILURE;
 }
 
-void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
+PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
 {
        if (s) {
                if (s->stream.opaque) {
@@ -507,7 +559,7 @@ void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
        }
 }
 
-void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
+PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
 {
        if (s) {
                if (s->stream.opaque) {
@@ -517,7 +569,7 @@ void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
        }
 }
 
-void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
+PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
 {
        if (s) {
                http_encoding_deflate_stream_dtor(*s);
@@ -528,7 +580,7 @@ void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
        }
 }
 
-void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
+PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
 {
        if (s) {
                http_encoding_inflate_stream_dtor(*s);
index 1836b27d6b259cb362d3189985420932c15b5207..a942c1fd33df48ea3c7aea9dfbd01ab217cd481f 100644 (file)
@@ -131,7 +131,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;
@@ -210,7 +210,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 +269,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));
        }
@@ -329,7 +329,19 @@ 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) {
+                       out_avail = 1;
+                       NEW_BUCKET(encoded, encoded_len);
+                       efree(encoded);
+               }
+       }
+       
+       if (flags & PSFS_FLAG_FLUSH_CLOSE) {
                char *encoded = NULL;
                size_t encoded_len = 0;
                
@@ -382,7 +394,19 @@ 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) {
+                       out_avail = 1;
+                       NEW_BUCKET(decoded, decoded_len);
+                       efree(decoded);
+               }
+       }
+       
+       if (flags & PSFS_FLAG_FLUSH_CLOSE) {
                char *decoded = NULL;
                size_t decoded_len = 0;
                
index dcc50627602f05bd8423e27f5b77ce7ac7a045a5..07204608b1af731e721e820746e01e9c4abd5cb5 100644 (file)
@@ -301,7 +301,7 @@ PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, c
                } else {
                        zval *val;
                        
-                       MAKE_STD_ZVAL(val);
+                       ALLOC_ZVAL(val);
                        *val = **data;
                        INIT_PZVAL(val);
                        zval_copy_ctor(val);
index 70cc42bd9834074e3199a1b4a320ae1cc709f064..a8a55bf9339c1c251df46767ff29719c258ca22f 100644 (file)
@@ -25,6 +25,7 @@ PHP_HTTP_API zend_bool _http_encoding_response_start(size_t content_length TSRML
 
 extern PHP_MINIT_FUNCTION(http_encoding);
 
+/* 100% compression should be fairly good */
 #define HTTP_ENCODING_MAXTRY 100
 /* safe padding */
 #define HTTP_ENCODING_SAFPAD 28
@@ -70,6 +71,8 @@ PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, ch
 PHP_HTTP_API http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC);
 #define http_encoding_deflate_stream_update(s, d, dl, e, el) _http_encoding_deflate_stream_update((s), (d), (dl), (e), (el) TSRMLS_CC)
 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 TSRMLS_DC);
+#define http_encoding_deflate_stream_flush(s, e, el) _http_encoding_deflate_stream_flush((s), (e), (el) TSRMLS_CC)
+PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC);
 #define http_encoding_deflate_stream_finish(s, e, el) _http_encoding_deflate_stream_finish((s), (e), (el) TSRMLS_CC)
 PHP_HTTP_API STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC);
 #define http_encoding_deflate_stream_dtor(s) _http_encoding_deflate_stream_dtor((s) TSRMLS_CC)
@@ -81,6 +84,8 @@ PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TS
 PHP_HTTP_API http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC);
 #define http_encoding_inflate_stream_update(s, d, dl, e, el) _http_encoding_inflate_stream_update((s), (d), (dl), (e), (el) TSRMLS_CC)
 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 TSRMLS_DC);
+#define http_encoding_inflate_stream_flush(s, d, dl) _http_encoding_inflate_stream_flush((s), (d), (dl) TSRMLS_CC)
+PHP_HTTP_API STATUS _http_encoding_inflate_stream_flush(http_encoding_stream *s, char **decoded, size_t *decoded_len TSRMLS_DC);
 #define http_encoding_inflate_stream_finish(s, e, el) _http_encoding_inflate_stream_finish((s), (e), (el) TSRMLS_CC)
 PHP_HTTP_API STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **decoded, size_t *decoded_len TSRMLS_DC);
 #define http_encoding_inflate_stream_dtor(s) _http_encoding_inflate_stream_dtor((s) TSRMLS_CC)