X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_filter_api.c;h=648c509adff7d66177a9f07bed22b04704796156;hp=3315f75841f216b7085042402648a90ff82d58a0;hb=05c863a6faa9a3ddd83ac1bdf62edbfc7a6ccf4f;hpb=8ad43ce5b543cdd9766300ceb080c1d9ff228110 diff --git a/http_filter_api.c b/http_filter_api.c index 3315f75..648c509 100644 --- a/http_filter_api.c +++ b/http_filter_api.c @@ -1,16 +1,13 @@ /* - +----------------------------------------------------------------------+ - | PECL :: http | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.0 of the PHP license, that | - | is bundled with this package in the file LICENSE, and is available | - | through the world-wide-web at http://www.php.net/license/3_0.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Copyright (c) 2004-2005 Michael Wallner | - +----------------------------------------------------------------------+ + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2005, Michael Wallner | + +--------------------------------------------------------------------+ */ /* $Id$ */ @@ -18,34 +15,48 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif -#include "php.h" -#include "php_http_std_defs.h" -#include "php_http_api.h" -#include "php_http_filter_api.h" +#define HTTP_WANT_ZLIB +#include "php_http.h" -#include "phpstr/phpstr.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" -/* - * TODO: allow use with persistent streams - */ +PHP_MINIT_FUNCTION(http_filter) +{ + php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC); + return SUCCESS; +} -typedef struct { - phpstr buffer; - ulong hexlen; -} http_filter_buffer; +/* + - +*/ -#define PHP_STREAM_FILTER_OP_FILTER_PARAMS \ +#define HTTP_FILTER_PARAMS \ php_stream *stream, \ php_stream_filter *this, \ php_stream_bucket_brigade *buckets_in, \ php_stream_bucket_brigade *buckets_out, \ size_t *bytes_consumed, int flags \ TSRMLS_DC -#define PHP_STREAM_FILTER_OP_FILTER(function) \ - static php_stream_filter_status_t function(PHP_STREAM_FILTER_OP_FILTER_PARAMS) +#define HTTP_FILTER_OP(filter) \ + http_filter_op_ ##filter +#define HTTP_FILTER_OPS(filter) \ + php_stream_filter_ops HTTP_FILTER_OP(filter) +#define HTTP_FILTER_DTOR(filter) \ + http_filter_ ##filter## _dtor +#define HTTP_FILTER_DESTRUCTOR(filter) \ + void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC) +#define HTTP_FILTER_FUNC(filter) \ + 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) \ { \ @@ -67,11 +78,25 @@ typedef struct { php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \ } -PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_decode) +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; @@ -87,13 +112,17 @@ PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_decode) *bytes_consumed += ptr->buflen; } - phpstr_append(PHPSTR(buffer), ptr->buf, ptr->buflen); + if ((size_t) -1 == phpstr_append(PHPSTR(buffer), ptr->buf, ptr->buflen)) { + return PSFS_ERR_FATAL; + } + php_stream_bucket_unlink(ptr TSRMLS_CC); php_stream_bucket_delref(ptr TSRMLS_CC); - } } - phpstr_fix(PHPSTR(buffer)); + if (!phpstr_fix(PHPSTR(buffer))) { + return PSFS_ERR_FATAL; + } /* we have data in our buffer */ while (PHPSTR_LEN(buffer)) { @@ -113,8 +142,9 @@ PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_decode) /* waiting for less data now */ buffer->hexlen -= PHPSTR_LEN(buffer); - /* no more buffered data (breaks loop) */ + /* no more buffered data */ phpstr_reset(PHPSTR(buffer)); + /* break */ } /* we have too less data and don't need to flush */ @@ -132,6 +162,7 @@ PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_decode) phpstr_cut(PHPSTR(buffer), 0, buffer->hexlen); /* reset hexlen */ buffer->hexlen = 0; + /* continue */ } } @@ -156,7 +187,7 @@ PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_decode) /* we need eol, so we can be sure we have all hex digits */ phpstr_fix(PHPSTR(buffer)); - if (eolstr = http_locate_eol(PHPSTR_VAL(buffer), &eollen)) { + if ((eolstr = http_locate_eol(PHPSTR_VAL(buffer), &eollen))) { char *stop = NULL; /* read in chunk size */ @@ -172,6 +203,9 @@ PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_decode) phpstr_cut(PHPSTR(buffer), 0, eolstr + eollen - PHPSTR_VAL(buffer)); /* buffer->hexlen is 0 now or contains the size of the next chunk */ /* continue */ + } else { + /* we have not enough data buffered to read in chunk size */ + break; } } /* break */ @@ -189,15 +223,15 @@ PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_decode) return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME; } -static void http_filter_chunked_decode_dtor(php_stream_filter *this TSRMLS_DC) +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); } -PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_encode) +static HTTP_FILTER_FUNCTION(chunked_encode) { int out_avail = 0; php_stream_bucket *ptr, *nxt; @@ -227,7 +261,7 @@ PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_encode) /* pass through */ NEW_BUCKET(PHPSTR_VAL(&buf), PHPSTR_LEN(&buf)); /* reset */ - PHPSTR_LEN(&buf) = 0; + phpstr_reset(&buf); php_stream_bucket_unlink(ptr TSRMLS_CC); php_stream_bucket_delref(ptr TSRMLS_CC); @@ -246,40 +280,151 @@ PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_encode) return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME; } -static php_stream_filter_ops http_filter_ops_chunked_decode = { - http_filter_chunked_decode, - http_filter_chunked_decode_dtor, +static HTTP_FILTER_OPS(chunked_decode) = { + HTTP_FILTER_FUNC(chunked_decode), + HTTP_FILTER_DTOR(chunked_decode), "http.chunked_decode" }; -static php_stream_filter_ops http_filter_ops_chunked_encode = { - http_filter_chunked_encode, +static HTTP_FILTER_OPS(chunked_encode) = { + HTTP_FILTER_FUNC(chunked_encode), NULL, "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; - /* FIXXME: allow usage with persistent streams */ - if (p) { - return NULL; + 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); + } } + } else + + 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), p)) { - phpstr_init(PHPSTR(b)); - if (!(f = php_stream_filter_alloc(&http_filter_ops_chunked_decode, b, p))) { + 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.chunked_encode")) { - f = php_stream_filter_alloc(&http_filter_ops_chunked_encode, NULL, p); + 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; @@ -289,11 +434,7 @@ php_stream_filter_factory http_filter_factory = { http_filter_create }; -PHP_MINIT_FUNCTION(http_filter) -{ - php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC); - return SUCCESS; -} +#endif /* ZEND_ENGINE_2 */ /* * Local variables: @@ -303,4 +444,3 @@ PHP_MINIT_FUNCTION(http_filter) * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ -