thread safety
[m6w6/ext-http] / php_http_encoding.c
index a300eb561fa54048b249ca525191988ca1e31cb6..2580738baba61ee2780727f1306c5fbb0042e08e 100644 (file)
@@ -14,6 +14,8 @@
 
 #include "php_http.h"
 
+#include <zlib.h>
+
 static inline int eol_match(char **line, int *eol_len)
 {
        char *ptr = *line;
@@ -111,7 +113,7 @@ PHP_HTTP_API const char *php_http_encoding_dechunk(const char *encoded, size_t e
 static inline int php_http_inflate_rounds(z_stream *Z, int flush, char **buf, size_t *len)
 {
        int status = 0, round = 0;
-       php_http_buffer buffer;
+       php_http_buffer_t buffer;
        
        *buf = NULL;
        *len = 0;
@@ -339,7 +341,7 @@ PHP_HTTP_API void php_http_encoding_stream_free(php_http_encoding_stream_t **s)
 }
 
 struct dechunk_ctx {
-       php_http_buffer buffer;
+       php_http_buffer_t buffer;
        ulong hexlen;
        unsigned zeroed:1;
 };
@@ -391,7 +393,6 @@ static php_http_encoding_stream_t *inflate_init(php_http_encoding_stream_t *s)
 static php_http_encoding_stream_t *dechunk_init(php_http_encoding_stream_t *s)
 {
        struct dechunk_ctx *ctx = pecalloc(1, sizeof(*ctx), (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
-       TSRMLS_FETCH_FROM_CTX(s->ts);
 
        if (!php_http_buffer_init_ex(&ctx->buffer, PHP_HTTP_BUFFER_DEFAULT_SIZE, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT) ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0)) {
                return NULL;
@@ -484,7 +485,7 @@ static STATUS inflate_update(php_http_encoding_stream_t *s, const char *data, si
        
        /* append input to buffer */
        php_http_buffer_append(PHP_HTTP_BUFFER(ctx->opaque), data, data_len);
-       
+
 retry_raw_inflate:
        ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
        ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
@@ -516,17 +517,16 @@ retry_raw_inflate:
 
 static STATUS dechunk_update(php_http_encoding_stream_t *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len)
 {
-       php_http_buffer tmp;
+       php_http_buffer_t tmp;
        struct dechunk_ctx *ctx = s->ctx;
        TSRMLS_FETCH_FROM_CTX(s->ts);
 
        if (ctx->zeroed) {
+               php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Dechunk encoding stream has already reached the end of chunked input");
                return FAILURE;
        }
-       if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(&ctx->buffer, data, data_len)) {
-               return FAILURE;
-       }
-       if (!php_http_buffer_fix(&ctx->buffer)) {
+       if ((PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(&ctx->buffer, data, data_len)) || !php_http_buffer_fix(&ctx->buffer)) {
+               /* OOM */
                return FAILURE;
        }
 
@@ -602,6 +602,7 @@ static STATUS dechunk_update(php_http_encoding_stream_t *s, const char *data, si
                                        /*      if strtoul() stops at the beginning of the buffered data
                                                there's domething oddly wrong, i.e. bad input */
                                        if (stop == PHP_HTTP_BUFFER_VAL(&ctx->buffer)) {
+                                               php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to parse chunk len from '%.*s'", MIN(16, ctx->buffer.used), ctx->buffer.data);
                                                php_http_buffer_dtor(&tmp);
                                                return FAILURE;
                                        }
@@ -790,7 +791,7 @@ static void deflate_dtor(php_http_encoding_stream_t *s)
                z_streamp ctx = s->ctx;
 
                if (ctx->opaque) {
-                       php_http_buffer_free((php_http_buffer **) &ctx->opaque);
+                       php_http_buffer_free((php_http_buffer_t **) &ctx->opaque);
                }
                deflateEnd(ctx);
                pefree(ctx, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
@@ -804,7 +805,7 @@ static void inflate_dtor(php_http_encoding_stream_t *s)
                z_streamp ctx = s->ctx;
 
                if (ctx->opaque) {
-                       php_http_buffer_free((php_http_buffer **) &ctx->opaque);
+                       php_http_buffer_free((php_http_buffer_t **) &ctx->opaque);
                }
                inflateEnd(ctx);
                pefree(ctx, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
@@ -940,7 +941,7 @@ void php_http_encoding_stream_object_free(void *object TSRMLS_DC)
        php_http_encoding_stream_object_t *o = (php_http_encoding_stream_object_t *) object;
 
        if (o->stream) {
-               php_http_encoding_stream_free(&o->stream TSRMLS_CC);
+               php_http_encoding_stream_free(&o->stream);
        }
        zend_object_std_dtor((zend_object *) o TSRMLS_CC);
        efree(o);
@@ -948,11 +949,11 @@ void php_http_encoding_stream_object_free(void *object TSRMLS_DC)
 
 PHP_METHOD(HttpEncodingStream, __construct)
 {
-       with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) {
+       with_error_handling(EH_THROW, php_http_exception_class_entry) {
                long flags = 0;
 
                if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
-                       with_error_handling(EH_THROW, PHP_HTTP_EX_CE(encoding)) {
+                       with_error_handling(EH_THROW, php_http_exception_class_entry) {
                                php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
                                if (!obj->stream) {
@@ -992,7 +993,7 @@ PHP_METHOD(HttpEncodingStream, update)
                        size_t encoded_len;
                        char *encoded_str;
 
-                       if (SUCCESS == php_http_encoding_stream_update(obj->stream, data_str, data_len, &encoded_str, &encoded_len TSRMLS_CC)) {
+                       if (SUCCESS == php_http_encoding_stream_update(obj->stream, data_str, data_len, &encoded_str, &encoded_len)) {
                                RETURN_STRINGL(encoded_str, encoded_len, 0);
                        }
                }
@@ -1009,7 +1010,7 @@ PHP_METHOD(HttpEncodingStream, flush)
                        char *encoded_str;
                        size_t encoded_len;
 
-                       if (SUCCESS == php_http_encoding_stream_flush(obj->stream, &encoded_str, &encoded_len TSRMLS_CC)) {
+                       if (SUCCESS == php_http_encoding_stream_flush(obj->stream, &encoded_str, &encoded_len)) {
                                RETURN_STRINGL(encoded_str, encoded_len, 0);
                        }
                }
@@ -1038,7 +1039,7 @@ PHP_METHOD(HttpEncodingStream, finish)
                        char *encoded_str;
                        size_t encoded_len;
 
-                       if (SUCCESS == php_http_encoding_stream_finish(obj->stream, &encoded_str, &encoded_len TSRMLS_CC)) {
+                       if (SUCCESS == php_http_encoding_stream_finish(obj->stream, &encoded_str, &encoded_len)) {
                                if (SUCCESS == php_http_encoding_stream_reset(&obj->stream)) {
                                        RETURN_STRINGL(encoded_str, encoded_len, 0);
                                } else {