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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_ZLIB
24 #include "php_streams.h"
25 #include "php_http_api.h"
26 #include "php_http_encoding_api.h"
27 #include "php_http_filter_api.h"
29 PHP_MINIT_FUNCTION(http_filter
)
31 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC
);
39 #define HTTP_FILTER_PARAMS \
41 php_stream_filter *this, \
42 php_stream_bucket_brigade *buckets_in, \
43 php_stream_bucket_brigade *buckets_out, \
44 size_t *bytes_consumed, int flags \
46 #define HTTP_FILTER_OP(filter) \
47 http_filter_op_ ##filter
48 #define HTTP_FILTER_OPS(filter) \
49 php_stream_filter_ops HTTP_FILTER_OP(filter)
50 #define HTTP_FILTER_DTOR(filter) \
51 http_filter_ ##filter## _dtor
52 #define HTTP_FILTER_DESTRUCTOR(filter) \
53 void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
54 #define HTTP_FILTER_FUNC(filter) \
56 #define HTTP_FILTER_FUNCTION(filter) \
57 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
58 #define HTTP_FILTER_BUFFER(filter) \
59 http_filter_ ##filter## _buffer
61 #define NEW_BUCKET(data, length) \
64 php_stream_bucket *__buck; \
66 __data = pemalloc(length, this->is_persistent); \
68 return PSFS_ERR_FATAL; \
70 memcpy(__data, data, length); \
72 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
74 pefree(__data, this->is_persistent); \
75 return PSFS_ERR_FATAL; \
78 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
84 } HTTP_FILTER_BUFFER(chunked_decode
);
87 typedef http_encoding_stream
HTTP_FILTER_BUFFER(deflate
);
88 typedef http_encoding_stream
HTTP_FILTER_BUFFER(inflate
);
89 #endif /* HTTP_HAVE_ZLIB */
92 static HTTP_FILTER_FUNCTION(chunked_decode
)
95 php_stream_bucket
*ptr
, *nxt
;
96 HTTP_FILTER_BUFFER(chunked_decode
) *buffer
= (HTTP_FILTER_BUFFER(chunked_decode
) *) (this->abstract
);
102 /* new data available? */
103 if (buckets_in
->head
) {
105 /* fetch available bucket data */
106 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
108 if (bytes_consumed
) {
109 *bytes_consumed
+= ptr
->buflen
;
112 if ((size_t) -1 == phpstr_append(PHPSTR(buffer
), ptr
->buf
, ptr
->buflen
)) {
113 return PSFS_ERR_FATAL
;
116 php_stream_bucket_unlink(ptr TSRMLS_CC
);
117 php_stream_bucket_delref(ptr TSRMLS_CC
);
120 if (!phpstr_fix(PHPSTR(buffer
))) {
121 return PSFS_ERR_FATAL
;
124 /* we have data in our buffer */
125 while (PHPSTR_LEN(buffer
)) {
127 /* we already know the size of the chunk and are waiting for data */
128 if (buffer
->hexlen
) {
130 /* not enough data buffered */
131 if (PHPSTR_LEN(buffer
) < buffer
->hexlen
) {
134 if (flags
== PSFS_FLAG_FLUSH_INC
) {
136 /* flush all data (should only be chunk data) */
138 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
140 /* waiting for less data now */
141 buffer
->hexlen
-= PHPSTR_LEN(buffer
);
142 /* no more buffered data */
143 phpstr_reset(PHPSTR(buffer
));
147 /* we have too less data and don't need to flush */
153 /* we seem to have all data of the chunk */
156 NEW_BUCKET(PHPSTR_VAL(buffer
), buffer
->hexlen
);
158 /* remove outgoing data from the buffer */
159 phpstr_cut(PHPSTR(buffer
), 0, buffer
->hexlen
);
166 /* we don't know the length of the chunk yet */
170 /* ignore preceeding CRLFs (too loose?) */
171 while (off
< PHPSTR_LEN(buffer
) && (
172 PHPSTR_VAL(buffer
)[off
] == 0xa ||
173 PHPSTR_VAL(buffer
)[off
] == 0xd)) {
177 phpstr_cut(PHPSTR(buffer
), 0, off
);
180 /* still data there? */
181 if (PHPSTR_LEN(buffer
)) {
185 /* we need eol, so we can be sure we have all hex digits */
186 phpstr_fix(PHPSTR(buffer
));
187 if ((eolstr
= http_locate_eol(PHPSTR_VAL(buffer
), &eollen
))) {
190 /* read in chunk size */
191 buffer
->hexlen
= strtoul(PHPSTR_VAL(buffer
), &stop
, 16);
193 /* if strtoul() stops at the beginning of the buffered data
194 there's domething oddly wrong, i.e. bad input */
195 if (stop
== PHPSTR_VAL(buffer
)) {
196 return PSFS_ERR_FATAL
;
199 /* cut out <chunk size hex><chunk extension><eol> */
200 phpstr_cut(PHPSTR(buffer
), 0, eolstr
+ eollen
- PHPSTR_VAL(buffer
));
201 /* buffer->hexlen is 0 now or contains the size of the next chunk */
204 /* we have not enough data buffered to read in chunk size */
212 /* flush before close, but only if we are already waiting for more data */
213 if (flags
== PSFS_FLAG_FLUSH_CLOSE
&& buffer
->hexlen
&& PHPSTR_LEN(buffer
)) {
215 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
216 phpstr_reset(PHPSTR(buffer
));
220 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
223 static HTTP_FILTER_DESTRUCTOR(chunked_decode
)
225 HTTP_FILTER_BUFFER(chunked_decode
) *b
= (HTTP_FILTER_BUFFER(chunked_decode
) *) (this->abstract
);
227 phpstr_dtor(PHPSTR(b
));
228 pefree(b
, this->is_persistent
);
231 static HTTP_FILTER_FUNCTION(chunked_encode
)
234 php_stream_bucket
*ptr
, *nxt
;
236 if (bytes_consumed
) {
240 /* new data available? */
241 if (buckets_in
->head
) {
247 /* fetch available bucket data */
248 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
250 if (bytes_consumed
) {
251 *bytes_consumed
+= ptr
->buflen
;
254 phpstr_appendf(&buf
, "%x" HTTP_CRLF
, ptr
->buflen
);
255 phpstr_append(&buf
, ptr
->buf
, ptr
->buflen
);
256 phpstr_appends(&buf
, HTTP_CRLF
);
259 NEW_BUCKET(PHPSTR_VAL(&buf
), PHPSTR_LEN(&buf
));
263 php_stream_bucket_unlink(ptr TSRMLS_CC
);
264 php_stream_bucket_delref(ptr TSRMLS_CC
);
271 /* terminate with "0" */
272 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
274 NEW_BUCKET("0" HTTP_CRLF
, lenof("0" HTTP_CRLF
));
277 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
280 static HTTP_FILTER_OPS(chunked_decode
) = {
281 HTTP_FILTER_FUNC(chunked_decode
),
282 HTTP_FILTER_DTOR(chunked_decode
),
283 "http.chunked_decode"
286 static HTTP_FILTER_OPS(chunked_encode
) = {
287 HTTP_FILTER_FUNC(chunked_encode
),
289 "http.chunked_encode"
292 #ifdef HTTP_HAVE_ZLIB
294 static HTTP_FILTER_FUNCTION(deflate
)
297 php_stream_bucket
*ptr
, *nxt
;
298 HTTP_FILTER_BUFFER(inflate
) *buffer
= (HTTP_FILTER_BUFFER(deflate
) *) this->abstract
;
300 if (bytes_consumed
) {
304 /* new data available? */
305 if (buckets_in
->head
) {
307 /* fetch available bucket data */
308 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
309 char *encoded
= NULL
;
310 size_t encoded_len
= 0;
313 if (bytes_consumed
) {
314 *bytes_consumed
+= ptr
->buflen
;
318 http_encoding_deflate_stream_update(buffer
, ptr
->buf
, ptr
->buflen
, &encoded
, &encoded_len
);
321 NEW_BUCKET(encoded
, encoded_len
);
326 php_stream_bucket_unlink(ptr TSRMLS_CC
);
327 php_stream_bucket_delref(ptr TSRMLS_CC
);
332 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
333 char *encoded
= NULL
;
334 size_t encoded_len
= 0;
336 http_encoding_deflate_stream_finish(buffer
, &encoded
, &encoded_len
);
339 NEW_BUCKET(encoded
, encoded_len
);
344 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
347 static HTTP_FILTER_FUNCTION(inflate
)
350 php_stream_bucket
*ptr
, *nxt
;
351 HTTP_FILTER_BUFFER(inflate
) *buffer
= (HTTP_FILTER_BUFFER(inflate
) *) this->abstract
;
353 if (bytes_consumed
) {
357 /* new data available? */
358 if (buckets_in
->head
) {
360 /* fetch available bucket data */
361 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
362 char *decoded
= NULL
;
363 size_t decoded_len
= 0;
366 if (bytes_consumed
) {
367 *bytes_consumed
+= ptr
->buflen
;
371 http_encoding_inflate_stream_update(buffer
, ptr
->buf
, ptr
->buflen
, &decoded
, &decoded_len
);
374 NEW_BUCKET(decoded
, decoded_len
);
379 php_stream_bucket_unlink(ptr TSRMLS_CC
);
380 php_stream_bucket_delref(ptr TSRMLS_CC
);
385 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
386 char *decoded
= NULL
;
387 size_t decoded_len
= 0;
389 http_encoding_inflate_stream_finish(buffer
, &decoded
, &decoded_len
);
392 NEW_BUCKET(decoded
, decoded_len
);
397 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
400 static HTTP_FILTER_DESTRUCTOR(deflate
)
402 HTTP_FILTER_BUFFER(deflate
) *buffer
= (HTTP_FILTER_BUFFER(deflate
) *) this->abstract
;
403 http_encoding_deflate_stream_free(&buffer
);
406 static HTTP_FILTER_DESTRUCTOR(inflate
)
408 HTTP_FILTER_BUFFER(inflate
) *buffer
= (HTTP_FILTER_BUFFER(inflate
) *) this->abstract
;
409 http_encoding_inflate_stream_free(&buffer
);
412 static HTTP_FILTER_OPS(deflate
) = {
413 HTTP_FILTER_FUNC(deflate
),
414 HTTP_FILTER_DTOR(deflate
),
418 static HTTP_FILTER_OPS(inflate
) = {
419 HTTP_FILTER_FUNC(inflate
),
420 HTTP_FILTER_DTOR(inflate
),
424 #endif /* HTTP_HAVE_ZLIB */
426 static php_stream_filter
*http_filter_create(const char *name
, zval
*params
, int p TSRMLS_DC
)
428 zval
**tmp
= ¶ms
;
429 php_stream_filter
*f
= NULL
;
431 if (!strcasecmp(name
, "http.chunked_decode")) {
432 HTTP_FILTER_BUFFER(chunked_decode
) *b
= NULL
;
434 if ((b
= pecalloc(1, sizeof(HTTP_FILTER_BUFFER(chunked_decode
)), p
))) {
435 phpstr_init_ex(PHPSTR(b
), 4096, p
? PHPSTR_INIT_PERSISTENT
: 0);
436 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_decode
), b
, p
))) {
442 if (!strcasecmp(name
, "http.chunked_encode")) {
443 f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode
), NULL
, p
);
444 #ifdef HTTP_HAVE_ZLIB
447 if (!strcasecmp(name
, "http.inflate")) {
448 int flags
= p
? HTTP_ENCODING_STREAM_PERSISTENT
: 0;
449 HTTP_FILTER_BUFFER(inflate
) *b
= NULL
;
451 if ((b
= http_encoding_inflate_stream_init(NULL
, flags
))) {
452 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(inflate
), b
, p
))) {
453 http_encoding_inflate_stream_free(&b
);
458 if (!strcasecmp(name
, "http.deflate")) {
459 int flags
= p
? HTTP_ENCODING_STREAM_PERSISTENT
: 0;
460 HTTP_FILTER_BUFFER(deflate
) *b
= NULL
;
463 switch (Z_TYPE_P(params
))
467 if (SUCCESS
!= zend_hash_find(HASH_OF(params
), "flags", sizeof("flags"), (void **) &tmp
)) {
474 convert_to_long_ex(tmp
);
475 flags
|= (Z_LVAL_PP(tmp
) & 0x0fffffff);
476 if (orig
!= *tmp
) zval_ptr_dtor(tmp
);
480 if ((b
= http_encoding_deflate_stream_init(NULL
, flags
))) {
481 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(deflate
), b
, p
))) {
482 http_encoding_deflate_stream_free(&b
);
485 #endif /* HTTP_HAVE_ZLIB */
491 php_stream_filter_factory http_filter_factory
= {
495 #endif /* ZEND_ENGINE_2 */
502 * vim600: noet sw=4 ts=4 fdm=marker
503 * vim<600: noet sw=4 ts=4