2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 PHP_MINIT_FUNCTION(http_filter
)
17 php_stream_filter_register_factory("http.*", &php_http_filter_factory TSRMLS_CC
);
21 #define PHP_HTTP_FILTER_PARAMS \
23 php_stream_filter *this, \
24 php_stream_bucket_brigade *buckets_in, \
25 php_stream_bucket_brigade *buckets_out, \
26 size_t *bytes_consumed, int flags \
28 #define PHP_HTTP_FILTER_OP(filter) \
29 http_filter_op_ ##filter
30 #define PHP_HTTP_FILTER_OPS(filter) \
31 php_stream_filter_ops PHP_HTTP_FILTER_OP(filter)
32 #define PHP_HTTP_FILTER_DTOR(filter) \
33 http_filter_ ##filter## _dtor
34 #define PHP_HTTP_FILTER_DESTRUCTOR(filter) \
35 void PHP_HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
36 #define PHP_HTTP_FILTER_FUNC(filter) \
38 #define PHP_HTTP_FILTER_FUNCTION(filter) \
39 php_stream_filter_status_t PHP_HTTP_FILTER_FUNC(filter)(PHP_HTTP_FILTER_PARAMS)
40 #define PHP_HTTP_FILTER_BUFFER(filter) \
41 http_filter_ ##filter## _buffer
43 #define NEW_BUCKET(data, length) \
46 php_stream_bucket *__buck; \
48 __data = pemalloc(length, this->is_persistent); \
50 return PSFS_ERR_FATAL; \
52 memcpy(__data, data, length); \
54 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
56 pefree(__data, this->is_persistent); \
57 return PSFS_ERR_FATAL; \
60 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
63 typedef struct _http_chunked_decode_filter_buffer_t
{
64 php_http_buffer_t buffer
;
66 } PHP_HTTP_FILTER_BUFFER(chunked_decode
);
68 typedef php_http_encoding_stream_t
PHP_HTTP_FILTER_BUFFER(zlib
);
70 static PHP_HTTP_FILTER_FUNCTION(chunked_decode
)
73 php_stream_bucket
*ptr
, *nxt
;
74 PHP_HTTP_FILTER_BUFFER(chunked_decode
) *buffer
= (PHP_HTTP_FILTER_BUFFER(chunked_decode
) *) (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 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(PHP_HTTP_BUFFER(buffer
), ptr
->buf
, ptr
->buflen
)) {
91 return PSFS_ERR_FATAL
;
94 php_stream_bucket_unlink(ptr TSRMLS_CC
);
95 php_stream_bucket_delref(ptr TSRMLS_CC
);
98 if (!php_http_buffer_fix(PHP_HTTP_BUFFER(buffer
))) {
99 return PSFS_ERR_FATAL
;
102 /* we have data in our buffer */
103 while (PHP_HTTP_BUFFER_LEN(buffer
)) {
105 /* we already know the size of the chunk and are waiting for data */
106 if (buffer
->hexlen
) {
108 /* not enough data buffered */
109 if (PHP_HTTP_BUFFER_LEN(buffer
) < buffer
->hexlen
) {
112 if (flags
& PSFS_FLAG_FLUSH_INC
) {
114 /* flush all data (should only be chunk data) */
116 NEW_BUCKET(PHP_HTTP_BUFFER_VAL(buffer
), PHP_HTTP_BUFFER_LEN(buffer
));
118 /* waiting for less data now */
119 buffer
->hexlen
-= PHP_HTTP_BUFFER_LEN(buffer
);
120 /* no more buffered data */
121 php_http_buffer_reset(PHP_HTTP_BUFFER(buffer
));
125 /* we have too less data and don't need to flush */
131 /* we seem to have all data of the chunk */
134 NEW_BUCKET(PHP_HTTP_BUFFER_VAL(buffer
), buffer
->hexlen
);
136 /* remove outgoing data from the buffer */
137 php_http_buffer_cut(PHP_HTTP_BUFFER(buffer
), 0, buffer
->hexlen
);
144 /* we don't know the length of the chunk yet */
148 /* ignore preceeding CRLFs (too loose?) */
149 while (off
< PHP_HTTP_BUFFER_LEN(buffer
) && (
150 PHP_HTTP_BUFFER_VAL(buffer
)[off
] == '\n' ||
151 PHP_HTTP_BUFFER_VAL(buffer
)[off
] == '\r')) {
155 php_http_buffer_cut(PHP_HTTP_BUFFER(buffer
), 0, off
);
158 /* still data there? */
159 if (PHP_HTTP_BUFFER_LEN(buffer
)) {
163 /* we need eol, so we can be sure we have all hex digits */
164 php_http_buffer_fix(PHP_HTTP_BUFFER(buffer
));
165 if ((eolstr
= php_http_locate_bin_eol(PHP_HTTP_BUFFER_VAL(buffer
), PHP_HTTP_BUFFER_LEN(buffer
), &eollen
))) {
168 /* read in chunk size */
169 buffer
->hexlen
= strtoul(PHP_HTTP_BUFFER_VAL(buffer
), &stop
, 16);
171 /* if strtoul() stops at the beginning of the buffered data
172 there's domething oddly wrong, i.e. bad input */
173 if (stop
== PHP_HTTP_BUFFER_VAL(buffer
)) {
174 return PSFS_ERR_FATAL
;
177 /* cut out <chunk size hex><chunk extension><eol> */
178 php_http_buffer_cut(PHP_HTTP_BUFFER(buffer
), 0, eolstr
+ eollen
- PHP_HTTP_BUFFER_VAL(buffer
));
179 /* buffer->hexlen is 0 now or contains the size of the next chunk */
180 if (!buffer
->hexlen
) {
181 php_stream_notify_info(stream
->context
, PHP_STREAM_NOTIFY_COMPLETED
, NULL
, 0);
186 /* we have not enough data buffered to read in chunk size */
194 /* flush before close, but only if we are already waiting for more data */
195 if ((flags
& PSFS_FLAG_FLUSH_CLOSE
) && buffer
->hexlen
&& PHP_HTTP_BUFFER_LEN(buffer
)) {
197 NEW_BUCKET(PHP_HTTP_BUFFER_VAL(buffer
), PHP_HTTP_BUFFER_LEN(buffer
));
198 php_http_buffer_reset(PHP_HTTP_BUFFER(buffer
));
202 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
205 static PHP_HTTP_FILTER_DESTRUCTOR(chunked_decode
)
207 PHP_HTTP_FILTER_BUFFER(chunked_decode
) *b
= (PHP_HTTP_FILTER_BUFFER(chunked_decode
) *) (this->abstract
);
209 php_http_buffer_dtor(PHP_HTTP_BUFFER(b
));
210 pefree(b
, this->is_persistent
);
213 static PHP_HTTP_FILTER_FUNCTION(chunked_encode
)
216 php_stream_bucket
*ptr
, *nxt
;
218 if (bytes_consumed
) {
222 /* new data available? */
223 if (buckets_in
->head
) {
224 php_http_buffer_t buf
;
227 php_http_buffer_init(&buf
);
229 /* fetch available bucket data */
230 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
232 if (bytes_consumed
) {
233 *bytes_consumed
+= ptr
->buflen
;
236 php_http_buffer_appendf(&buf
, "%lx" PHP_HTTP_CRLF
, (long unsigned int) ptr
->buflen
);
237 php_http_buffer_append(&buf
, ptr
->buf
, ptr
->buflen
);
238 php_http_buffer_appends(&buf
, PHP_HTTP_CRLF
);
241 NEW_BUCKET(PHP_HTTP_BUFFER_VAL(&buf
), PHP_HTTP_BUFFER_LEN(&buf
));
243 php_http_buffer_reset(&buf
);
245 php_stream_bucket_unlink(ptr TSRMLS_CC
);
246 php_stream_bucket_delref(ptr TSRMLS_CC
);
250 php_http_buffer_dtor(&buf
);
253 /* terminate with "0" */
254 if (flags
& PSFS_FLAG_FLUSH_CLOSE
) {
256 NEW_BUCKET("0" PHP_HTTP_CRLF
, lenof("0" PHP_HTTP_CRLF
));
259 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
262 static PHP_HTTP_FILTER_OPS(chunked_decode
) = {
263 PHP_HTTP_FILTER_FUNC(chunked_decode
),
264 PHP_HTTP_FILTER_DTOR(chunked_decode
),
265 "http.chunked_decode"
268 static PHP_HTTP_FILTER_OPS(chunked_encode
) = {
269 PHP_HTTP_FILTER_FUNC(chunked_encode
),
271 "http.chunked_encode"
274 static PHP_HTTP_FILTER_FUNCTION(zlib
)
277 php_stream_bucket
*ptr
, *nxt
;
278 PHP_HTTP_FILTER_BUFFER(zlib
) *buffer
= (PHP_HTTP_FILTER_BUFFER(zlib
) *) this->abstract
;
280 if (bytes_consumed
) {
284 /* new data available? */
285 if (buckets_in
->head
) {
287 /* fetch available bucket data */
288 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
289 char *encoded
= NULL
;
290 size_t encoded_len
= 0;
293 if (bytes_consumed
) {
294 *bytes_consumed
+= ptr
->buflen
;
298 php_http_encoding_stream_update(buffer
, ptr
->buf
, ptr
->buflen
, &encoded
, &encoded_len
);
302 NEW_BUCKET(encoded
, encoded_len
);
308 php_stream_bucket_unlink(ptr TSRMLS_CC
);
309 php_stream_bucket_delref(ptr TSRMLS_CC
);
314 if (flags
& PSFS_FLAG_FLUSH_INC
) {
315 char *encoded
= NULL
;
316 size_t encoded_len
= 0;
318 php_http_encoding_stream_flush(buffer
, &encoded
, &encoded_len
);
322 NEW_BUCKET(encoded
, encoded_len
);
328 if (flags
& PSFS_FLAG_FLUSH_CLOSE
) {
329 char *encoded
= NULL
;
330 size_t encoded_len
= 0;
332 php_http_encoding_stream_finish(buffer
, &encoded
, &encoded_len
);
336 NEW_BUCKET(encoded
, encoded_len
);
342 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
344 static PHP_HTTP_FILTER_DESTRUCTOR(zlib
)
346 PHP_HTTP_FILTER_BUFFER(zlib
) *buffer
= (PHP_HTTP_FILTER_BUFFER(zlib
) *) this->abstract
;
347 php_http_encoding_stream_free(&buffer
);
350 static PHP_HTTP_FILTER_OPS(deflate
) = {
351 PHP_HTTP_FILTER_FUNC(zlib
),
352 PHP_HTTP_FILTER_DTOR(zlib
),
356 static PHP_HTTP_FILTER_OPS(inflate
) = {
357 PHP_HTTP_FILTER_FUNC(zlib
),
358 PHP_HTTP_FILTER_DTOR(zlib
),
362 static php_stream_filter
*http_filter_create(const char *name
, zval
*params
, int p TSRMLS_DC
)
364 zval
**tmp
= ¶ms
;
365 php_stream_filter
*f
= NULL
;
366 int flags
= p
? PHP_HTTP_ENCODING_STREAM_PERSISTENT
: 0;
369 switch (Z_TYPE_P(params
)) {
372 if (SUCCESS
!= zend_hash_find(HASH_OF(params
), "flags", sizeof("flags"), (void *) &tmp
)) {
378 zval
*num
= php_http_ztyp(IS_LONG
, *tmp
);
380 flags
|= (Z_LVAL_P(num
) & 0x0fffffff);
388 if (!strcasecmp(name
, "http.chunked_decode")) {
389 PHP_HTTP_FILTER_BUFFER(chunked_decode
) *b
= NULL
;
391 if ((b
= pecalloc(1, sizeof(PHP_HTTP_FILTER_BUFFER(chunked_decode
)), p
))) {
392 php_http_buffer_init_ex(PHP_HTTP_BUFFER(b
), 4096, p
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0);
393 if (!(f
= php_stream_filter_alloc(&PHP_HTTP_FILTER_OP(chunked_decode
), b
, p
))) {
399 if (!strcasecmp(name
, "http.chunked_encode")) {
400 f
= php_stream_filter_alloc(&PHP_HTTP_FILTER_OP(chunked_encode
), NULL
, p
);
403 if (!strcasecmp(name
, "http.inflate")) {
404 PHP_HTTP_FILTER_BUFFER(zlib
) *b
= NULL
;
406 if ((b
= php_http_encoding_stream_init(NULL
, php_http_encoding_stream_get_inflate_ops(), flags TSRMLS_CC
))) {
407 if (!(f
= php_stream_filter_alloc(&PHP_HTTP_FILTER_OP(inflate
), b
, p
))) {
408 php_http_encoding_stream_free(&b
);
413 if (!strcasecmp(name
, "http.deflate")) {
414 PHP_HTTP_FILTER_BUFFER(zlib
) *b
= NULL
;
416 if ((b
= php_http_encoding_stream_init(NULL
, php_http_encoding_stream_get_deflate_ops(), flags TSRMLS_CC
))) {
417 if (!(f
= php_stream_filter_alloc(&PHP_HTTP_FILTER_OP(deflate
), b
, p
))) {
418 php_http_encoding_stream_free(&b
);
426 php_stream_filter_factory php_http_filter_factory
= {
436 * vim600: noet sw=4 ts=4 fdm=marker
437 * vim<600: noet sw=4 ts=4