2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
23 #include "php_http_std_defs.h"
24 #include "php_http_api.h"
25 #include "php_http_filter_api.h"
27 #include "phpstr/phpstr.h"
29 #include "php_streams.h"
32 * TODO: allow use with persistent streams
40 #define PHP_STREAM_FILTER_OP_FILTER_PARAMS \
42 php_stream_filter *this, \
43 php_stream_bucket_brigade *buckets_in, \
44 php_stream_bucket_brigade *buckets_out, \
45 size_t *bytes_consumed, int flags \
47 #define PHP_STREAM_FILTER_OP_FILTER(function) \
48 static php_stream_filter_status_t function(PHP_STREAM_FILTER_OP_FILTER_PARAMS)
50 #define NEW_BUCKET(data, length) \
53 php_stream_bucket *__buck; \
55 __data = pemalloc(length, this->is_persistent); \
57 return PSFS_ERR_FATAL; \
59 memcpy(__data, data, length); \
61 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
63 pefree(__data, this->is_persistent); \
64 return PSFS_ERR_FATAL; \
67 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
70 PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_decode
)
73 php_stream_bucket
*ptr
, *nxt
;
74 http_filter_buffer
*buffer
= (http_filter_buffer
*) (this->abstract
);
80 /* new data available? */
81 if (buckets_in
->head
) {
83 /* fetch available bucket data */
84 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
87 *bytes_consumed
+= ptr
->buflen
;
90 phpstr_append(PHPSTR(buffer
), ptr
->buf
, ptr
->buflen
);
91 php_stream_bucket_unlink(ptr TSRMLS_CC
);
92 php_stream_bucket_delref(ptr TSRMLS_CC
);
96 phpstr_fix(PHPSTR(buffer
));
98 /* we have data in our buffer */
99 while (PHPSTR_LEN(buffer
)) {
101 /* we already know the size of the chunk and are waiting for data */
102 if (buffer
->hexlen
) {
104 /* not enough data buffered */
105 if (PHPSTR_LEN(buffer
) < buffer
->hexlen
) {
108 if (flags
== PSFS_FLAG_FLUSH_INC
) {
110 /* flush all data (should only be chunk data) */
112 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
114 /* waiting for less data now */
115 buffer
->hexlen
-= PHPSTR_LEN(buffer
);
116 /* no more buffered data (breaks loop) */
117 phpstr_reset(PHPSTR(buffer
));
120 /* we have too less data and don't need to flush */
126 /* we seem to have all data of the chunk */
129 NEW_BUCKET(PHPSTR_VAL(buffer
), buffer
->hexlen
);
131 /* remove outgoing data from the buffer */
132 phpstr_cut(PHPSTR(buffer
), 0, buffer
->hexlen
);
138 /* we don't know the length of the chunk yet */
142 /* ignore preceeding CRLFs (too loose?) */
143 while (off
< PHPSTR_LEN(buffer
) && (
144 PHPSTR_VAL(buffer
)[off
] == 0xa ||
145 PHPSTR_VAL(buffer
)[off
] == 0xd)) {
149 phpstr_cut(PHPSTR(buffer
), 0, off
);
152 /* still data there? */
153 if (PHPSTR_LEN(buffer
)) {
157 /* we need eol, so we can be sure we have all hex digits */
158 phpstr_fix(PHPSTR(buffer
));
159 if (eolstr
= http_locate_eol(PHPSTR_VAL(buffer
), &eollen
)) {
162 /* read in chunk size */
163 buffer
->hexlen
= strtoul(PHPSTR_VAL(buffer
), &stop
, 16);
165 /* if strtoul() stops at the beginning of the buffered data
166 there's domething oddly wrong, i.e. bad input */
167 if (stop
== PHPSTR_VAL(buffer
)) {
168 return PSFS_ERR_FATAL
;
171 /* cut out <chunk size hex><chunk extension><eol> */
172 phpstr_cut(PHPSTR(buffer
), 0, eolstr
+ eollen
- PHPSTR_VAL(buffer
));
173 /* buffer->hexlen is 0 now or contains the size of the next chunk */
181 /* flush before close, but only if we are already waiting for more data */
182 if (flags
== PSFS_FLAG_FLUSH_CLOSE
&& buffer
->hexlen
&& PHPSTR_LEN(buffer
)) {
184 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
185 phpstr_reset(PHPSTR(buffer
));
189 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
192 static void http_filter_chunked_decode_dtor(php_stream_filter
*this TSRMLS_DC
)
194 http_filter_buffer
*b
= (http_filter_buffer
*) (this->abstract
);
196 phpstr_dtor(PHPSTR(b
));
197 pefree(b
, this->is_persistent
);
200 PHP_STREAM_FILTER_OP_FILTER(http_filter_chunked_encode
)
203 php_stream_bucket
*ptr
, *nxt
;
205 if (bytes_consumed
) {
209 /* new data available? */
210 if (buckets_in
->head
) {
216 /* fetch available bucket data */
217 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
219 if (bytes_consumed
) {
220 *bytes_consumed
+= ptr
->buflen
;
223 phpstr_appendf(&buf
, "%x" HTTP_CRLF
, ptr
->buflen
);
224 phpstr_append(&buf
, ptr
->buf
, ptr
->buflen
);
225 phpstr_appends(&buf
, HTTP_CRLF
);
228 NEW_BUCKET(PHPSTR_VAL(&buf
), PHPSTR_LEN(&buf
));
230 PHPSTR_LEN(&buf
) = 0;
232 php_stream_bucket_unlink(ptr TSRMLS_CC
);
233 php_stream_bucket_delref(ptr TSRMLS_CC
);
240 /* terminate with "0" */
241 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
243 NEW_BUCKET("0" HTTP_CRLF
, lenof("0" HTTP_CRLF
));
246 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
249 static php_stream_filter_ops http_filter_ops_chunked_decode
= {
250 http_filter_chunked_decode
,
251 http_filter_chunked_decode_dtor
,
252 "http.chunked_decode"
255 static php_stream_filter_ops http_filter_ops_chunked_encode
= {
256 http_filter_chunked_encode
,
258 "http.chunked_encode"
261 static php_stream_filter
*http_filter_create(const char *name
, zval
*params
, int p TSRMLS_DC
)
263 php_stream_filter
*f
= NULL
;
265 if (!strcasecmp(name
, "http.chunked_decode")) {
266 http_filter_buffer
*b
= NULL
;
268 /* FIXXME: allow usage with persistent streams */
273 if (b
= pecalloc(1, sizeof(http_filter_buffer
), p
)) {
274 phpstr_init(PHPSTR(b
));
275 if (!(f
= php_stream_filter_alloc(&http_filter_ops_chunked_decode
, b
, p
))) {
281 if (!strcasecmp(name
, "http.chunked_encode")) {
282 f
= php_stream_filter_alloc(&http_filter_ops_chunked_encode
, NULL
, p
);
288 php_stream_filter_factory http_filter_factory
= {
292 PHP_MINIT_FUNCTION(http_filter
)
294 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC
);
303 * vim600: noet sw=4 ts=4 fdm=marker
304 * vim<600: noet sw=4 ts=4