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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_ZLIB
20 #include "php_streams.h"
21 #include "php_http_api.h"
22 #include "php_http_encoding_api.h"
23 #include "php_http_filter_api.h"
25 PHP_MINIT_FUNCTION(http_filter
)
27 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC
);
35 #define HTTP_FILTER_PARAMS \
37 php_stream_filter *this, \
38 php_stream_bucket_brigade *buckets_in, \
39 php_stream_bucket_brigade *buckets_out, \
40 size_t *bytes_consumed, int flags \
42 #define HTTP_FILTER_OP(filter) \
43 http_filter_op_ ##filter
44 #define HTTP_FILTER_OPS(filter) \
45 php_stream_filter_ops HTTP_FILTER_OP(filter)
46 #define HTTP_FILTER_DTOR(filter) \
47 http_filter_ ##filter## _dtor
48 #define HTTP_FILTER_DESTRUCTOR(filter) \
49 void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
50 #define HTTP_FILTER_FUNC(filter) \
52 #define HTTP_FILTER_FUNCTION(filter) \
53 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
54 #define HTTP_FILTER_BUFFER(filter) \
55 http_filter_ ##filter## _buffer
57 #define NEW_BUCKET(data, length) \
60 php_stream_bucket *__buck; \
62 __data = pemalloc(length, this->is_persistent); \
64 return PSFS_ERR_FATAL; \
66 memcpy(__data, data, length); \
68 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
70 pefree(__data, this->is_persistent); \
71 return PSFS_ERR_FATAL; \
74 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
77 typedef struct _http_chunked_decode_filter_buffer_t
{
80 } HTTP_FILTER_BUFFER(chunked_decode
);
83 typedef http_encoding_stream
HTTP_FILTER_BUFFER(deflate
);
84 typedef http_encoding_stream
HTTP_FILTER_BUFFER(inflate
);
85 #endif /* HTTP_HAVE_ZLIB */
88 static HTTP_FILTER_FUNCTION(chunked_decode
)
91 php_stream_bucket
*ptr
, *nxt
;
92 HTTP_FILTER_BUFFER(chunked_decode
) *buffer
= (HTTP_FILTER_BUFFER(chunked_decode
) *) (this->abstract
);
98 /* new data available? */
99 if (buckets_in
->head
) {
101 /* fetch available bucket data */
102 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
104 if (bytes_consumed
) {
105 *bytes_consumed
+= ptr
->buflen
;
108 if (PHPSTR_NOMEM
== phpstr_append(PHPSTR(buffer
), ptr
->buf
, ptr
->buflen
)) {
109 return PSFS_ERR_FATAL
;
112 php_stream_bucket_unlink(ptr TSRMLS_CC
);
113 php_stream_bucket_delref(ptr TSRMLS_CC
);
116 if (!phpstr_fix(PHPSTR(buffer
))) {
117 return PSFS_ERR_FATAL
;
120 /* we have data in our buffer */
121 while (PHPSTR_LEN(buffer
)) {
123 /* we already know the size of the chunk and are waiting for data */
124 if (buffer
->hexlen
) {
126 /* not enough data buffered */
127 if (PHPSTR_LEN(buffer
) < buffer
->hexlen
) {
130 if (flags
& PSFS_FLAG_FLUSH_INC
) {
132 /* flush all data (should only be chunk data) */
134 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
136 /* waiting for less data now */
137 buffer
->hexlen
-= PHPSTR_LEN(buffer
);
138 /* no more buffered data */
139 phpstr_reset(PHPSTR(buffer
));
143 /* we have too less data and don't need to flush */
149 /* we seem to have all data of the chunk */
152 NEW_BUCKET(PHPSTR_VAL(buffer
), buffer
->hexlen
);
154 /* remove outgoing data from the buffer */
155 phpstr_cut(PHPSTR(buffer
), 0, buffer
->hexlen
);
162 /* we don't know the length of the chunk yet */
166 /* ignore preceeding CRLFs (too loose?) */
167 while (off
< PHPSTR_LEN(buffer
) && (
168 PHPSTR_VAL(buffer
)[off
] == '\n' ||
169 PHPSTR_VAL(buffer
)[off
] == '\r')) {
173 phpstr_cut(PHPSTR(buffer
), 0, off
);
176 /* still data there? */
177 if (PHPSTR_LEN(buffer
)) {
181 /* we need eol, so we can be sure we have all hex digits */
182 phpstr_fix(PHPSTR(buffer
));
183 if ((eolstr
= http_locate_eol(PHPSTR_VAL(buffer
), &eollen
))) {
186 /* read in chunk size */
187 buffer
->hexlen
= strtoul(PHPSTR_VAL(buffer
), &stop
, 16);
189 /* if strtoul() stops at the beginning of the buffered data
190 there's domething oddly wrong, i.e. bad input */
191 if (stop
== PHPSTR_VAL(buffer
)) {
192 return PSFS_ERR_FATAL
;
195 /* cut out <chunk size hex><chunk extension><eol> */
196 phpstr_cut(PHPSTR(buffer
), 0, eolstr
+ eollen
- PHPSTR_VAL(buffer
));
197 /* buffer->hexlen is 0 now or contains the size of the next chunk */
200 /* we have not enough data buffered to read in chunk size */
208 /* flush before close, but only if we are already waiting for more data */
209 if ((flags
& PSFS_FLAG_FLUSH_CLOSE
) && buffer
->hexlen
&& PHPSTR_LEN(buffer
)) {
211 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
212 phpstr_reset(PHPSTR(buffer
));
216 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
219 static HTTP_FILTER_DESTRUCTOR(chunked_decode
)
221 HTTP_FILTER_BUFFER(chunked_decode
) *b
= (HTTP_FILTER_BUFFER(chunked_decode
) *) (this->abstract
);
223 phpstr_dtor(PHPSTR(b
));
224 pefree(b
, this->is_persistent
);
227 static HTTP_FILTER_FUNCTION(chunked_encode
)
230 php_stream_bucket
*ptr
, *nxt
;
232 if (bytes_consumed
) {
236 /* new data available? */
237 if (buckets_in
->head
) {
243 /* fetch available bucket data */
244 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
246 if (bytes_consumed
) {
247 *bytes_consumed
+= ptr
->buflen
;
250 phpstr_appendf(&buf
, "%x" HTTP_CRLF
, ptr
->buflen
);
251 phpstr_append(&buf
, ptr
->buf
, ptr
->buflen
);
252 phpstr_appends(&buf
, HTTP_CRLF
);
255 NEW_BUCKET(PHPSTR_VAL(&buf
), PHPSTR_LEN(&buf
));
259 php_stream_bucket_unlink(ptr TSRMLS_CC
);
260 php_stream_bucket_delref(ptr TSRMLS_CC
);
267 /* terminate with "0" */
268 if (flags
& PSFS_FLAG_FLUSH_CLOSE
) {
270 NEW_BUCKET("0" HTTP_CRLF
, lenof("0" HTTP_CRLF
));
273 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
276 static HTTP_FILTER_OPS(chunked_decode
) = {
277 HTTP_FILTER_FUNC(chunked_decode
),
278 HTTP_FILTER_DTOR(chunked_decode
),
279 "http.chunked_decode"
282 static HTTP_FILTER_OPS(chunked_encode
) = {
283 HTTP_FILTER_FUNC(chunked_encode
),
285 "http.chunked_encode"
288 #ifdef HTTP_HAVE_ZLIB
290 static HTTP_FILTER_FUNCTION(deflate
)
293 php_stream_bucket
*ptr
, *nxt
;
294 HTTP_FILTER_BUFFER(inflate
) *buffer
= (HTTP_FILTER_BUFFER(deflate
) *) this->abstract
;
296 if (bytes_consumed
) {
300 /* new data available? */
301 if (buckets_in
->head
) {
303 /* fetch available bucket data */
304 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
305 char *encoded
= NULL
;
306 size_t encoded_len
= 0;
309 if (bytes_consumed
) {
310 *bytes_consumed
+= ptr
->buflen
;
314 http_encoding_deflate_stream_update(buffer
, ptr
->buf
, ptr
->buflen
, &encoded
, &encoded_len
);
318 NEW_BUCKET(encoded
, encoded_len
);
324 php_stream_bucket_unlink(ptr TSRMLS_CC
);
325 php_stream_bucket_delref(ptr TSRMLS_CC
);
330 if (flags
& PSFS_FLAG_FLUSH_INC
) {
331 char *encoded
= NULL
;
332 size_t encoded_len
= 0;
334 http_encoding_deflate_stream_flush(buffer
, &encoded
, &encoded_len
);
338 NEW_BUCKET(encoded
, encoded_len
);
344 if (flags
& PSFS_FLAG_FLUSH_CLOSE
) {
345 char *encoded
= NULL
;
346 size_t encoded_len
= 0;
348 http_encoding_deflate_stream_finish(buffer
, &encoded
, &encoded_len
);
352 NEW_BUCKET(encoded
, encoded_len
);
358 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
361 static HTTP_FILTER_FUNCTION(inflate
)
364 php_stream_bucket
*ptr
, *nxt
;
365 HTTP_FILTER_BUFFER(inflate
) *buffer
= (HTTP_FILTER_BUFFER(inflate
) *) this->abstract
;
367 if (bytes_consumed
) {
371 /* new data available? */
372 if (buckets_in
->head
) {
374 /* fetch available bucket data */
375 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
376 char *decoded
= NULL
;
377 size_t decoded_len
= 0;
380 if (bytes_consumed
) {
381 *bytes_consumed
+= ptr
->buflen
;
385 http_encoding_inflate_stream_update(buffer
, ptr
->buf
, ptr
->buflen
, &decoded
, &decoded_len
);
389 NEW_BUCKET(decoded
, decoded_len
);
395 php_stream_bucket_unlink(ptr TSRMLS_CC
);
396 php_stream_bucket_delref(ptr TSRMLS_CC
);
401 if (flags
& PSFS_FLAG_FLUSH_INC
) {
402 char *decoded
= NULL
;
403 size_t decoded_len
= 0;
405 http_encoding_inflate_stream_flush(buffer
, &decoded
, &decoded_len
);
409 NEW_BUCKET(decoded
, decoded_len
);
415 if (flags
& PSFS_FLAG_FLUSH_CLOSE
) {
416 char *decoded
= NULL
;
417 size_t decoded_len
= 0;
419 http_encoding_inflate_stream_finish(buffer
, &decoded
, &decoded_len
);
423 NEW_BUCKET(decoded
, decoded_len
);
429 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
432 static HTTP_FILTER_DESTRUCTOR(deflate
)
434 HTTP_FILTER_BUFFER(deflate
) *buffer
= (HTTP_FILTER_BUFFER(deflate
) *) this->abstract
;
435 http_encoding_deflate_stream_free(&buffer
);
438 static HTTP_FILTER_DESTRUCTOR(inflate
)
440 HTTP_FILTER_BUFFER(inflate
) *buffer
= (HTTP_FILTER_BUFFER(inflate
) *) this->abstract
;
441 http_encoding_inflate_stream_free(&buffer
);
444 static HTTP_FILTER_OPS(deflate
) = {
445 HTTP_FILTER_FUNC(deflate
),
446 HTTP_FILTER_DTOR(deflate
),
450 static HTTP_FILTER_OPS(inflate
) = {
451 HTTP_FILTER_FUNC(inflate
),
452 HTTP_FILTER_DTOR(inflate
),
456 #endif /* HTTP_HAVE_ZLIB */
458 static php_stream_filter
*http_filter_create(const char *name
, zval
*params
, int p TSRMLS_DC
)
460 zval
**tmp
= ¶ms
;
461 php_stream_filter
*f
= NULL
;
463 if (!strcasecmp(name
, "http.chunked_decode")) {
464 HTTP_FILTER_BUFFER(chunked_decode
) *b
= NULL
;
466 if ((b
= pecalloc(1, sizeof(HTTP_FILTER_BUFFER(chunked_decode
)), p
))) {
467 phpstr_init_ex(PHPSTR(b
), 4096, p
? PHPSTR_INIT_PERSISTENT
: 0);
468 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_decode
), b
, p
))) {
474 if (!strcasecmp(name
, "http.chunked_encode")) {
475 f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode
), NULL
, p
);
476 #ifdef HTTP_HAVE_ZLIB
479 if (!strcasecmp(name
, "http.inflate")) {
480 int flags
= p
? HTTP_ENCODING_STREAM_PERSISTENT
: 0;
481 HTTP_FILTER_BUFFER(inflate
) *b
= NULL
;
483 if ((b
= http_encoding_inflate_stream_init(NULL
, flags
))) {
484 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(inflate
), b
, p
))) {
485 http_encoding_inflate_stream_free(&b
);
490 if (!strcasecmp(name
, "http.deflate")) {
491 int flags
= p
? HTTP_ENCODING_STREAM_PERSISTENT
: 0;
492 HTTP_FILTER_BUFFER(deflate
) *b
= NULL
;
495 switch (Z_TYPE_P(params
)) {
498 if (SUCCESS
!= zend_hash_find(HASH_OF(params
), "flags", sizeof("flags"), (void *) &tmp
)) {
505 convert_to_long_ex(tmp
);
506 flags
|= (Z_LVAL_PP(tmp
) & 0x0fffffff);
507 if (orig
!= *tmp
) zval_ptr_dtor(tmp
);
511 if ((b
= http_encoding_deflate_stream_init(NULL
, flags
))) {
512 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(deflate
), b
, p
))) {
513 http_encoding_deflate_stream_free(&b
);
516 #endif /* HTTP_HAVE_ZLIB */
522 php_stream_filter_factory http_filter_factory
= {
526 #endif /* ZEND_ENGINE_2 */
533 * vim600: noet sw=4 ts=4 fdm=marker
534 * vim<600: noet sw=4 ts=4