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 +--------------------------------------------------------------------+
22 #include "php_http_std_defs.h"
23 #include "php_http_api.h"
24 #include "php_http_filter_api.h"
26 #include "phpstr/phpstr.h"
28 #include "php_streams.h"
30 PHP_MINIT_FUNCTION(http_filter
)
32 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC
);
45 #define HTTP_FILTER_PARAMS \
47 php_stream_filter *this, \
48 php_stream_bucket_brigade *buckets_in, \
49 php_stream_bucket_brigade *buckets_out, \
50 size_t *bytes_consumed, int flags \
52 #define HTTP_FILTER_OP(filter) \
53 http_filter_op_ ##filter
54 #define HTTP_FILTER_OPS(filter) \
55 php_stream_filter_ops HTTP_FILTER_OP(filter)
56 #define HTTP_FILTER_DTOR(filter) \
57 http_filter_ ##filter## _dtor
58 #define HTTP_FILTER_DESTRUCTOR(filter) \
59 void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
60 #define HTTP_FILTER_FUNC(filter) \
62 #define HTTP_FILTER_FUNCTION(filter) \
63 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
65 #define NEW_BUCKET(data, length) \
68 php_stream_bucket *__buck; \
70 __data = pemalloc(length, this->is_persistent); \
72 return PSFS_ERR_FATAL; \
74 memcpy(__data, data, length); \
76 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
78 pefree(__data, this->is_persistent); \
79 return PSFS_ERR_FATAL; \
82 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
85 static HTTP_FILTER_FUNCTION(chunked_decode
)
88 php_stream_bucket
*ptr
, *nxt
;
89 http_filter_buffer
*buffer
= (http_filter_buffer
*) (this->abstract
);
95 /* new data available? */
96 if (buckets_in
->head
) {
98 /* fetch available bucket data */
99 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
101 if (bytes_consumed
) {
102 *bytes_consumed
+= ptr
->buflen
;
105 if ((size_t) -1 == phpstr_append(PHPSTR(buffer
), ptr
->buf
, ptr
->buflen
)) {
106 return PSFS_ERR_FATAL
;
109 php_stream_bucket_unlink(ptr TSRMLS_CC
);
110 php_stream_bucket_delref(ptr TSRMLS_CC
);
113 if (!phpstr_fix(PHPSTR(buffer
))) {
114 return PSFS_ERR_FATAL
;
117 /* we have data in our buffer */
118 while (PHPSTR_LEN(buffer
)) {
120 /* we already know the size of the chunk and are waiting for data */
121 if (buffer
->hexlen
) {
123 /* not enough data buffered */
124 if (PHPSTR_LEN(buffer
) < buffer
->hexlen
) {
127 if (flags
== PSFS_FLAG_FLUSH_INC
) {
129 /* flush all data (should only be chunk data) */
131 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
133 /* waiting for less data now */
134 buffer
->hexlen
-= PHPSTR_LEN(buffer
);
135 /* no more buffered data */
136 phpstr_reset(PHPSTR(buffer
));
140 /* we have too less data and don't need to flush */
146 /* we seem to have all data of the chunk */
149 NEW_BUCKET(PHPSTR_VAL(buffer
), buffer
->hexlen
);
151 /* remove outgoing data from the buffer */
152 phpstr_cut(PHPSTR(buffer
), 0, buffer
->hexlen
);
159 /* we don't know the length of the chunk yet */
163 /* ignore preceeding CRLFs (too loose?) */
164 while (off
< PHPSTR_LEN(buffer
) && (
165 PHPSTR_VAL(buffer
)[off
] == 0xa ||
166 PHPSTR_VAL(buffer
)[off
] == 0xd)) {
170 phpstr_cut(PHPSTR(buffer
), 0, off
);
173 /* still data there? */
174 if (PHPSTR_LEN(buffer
)) {
178 /* we need eol, so we can be sure we have all hex digits */
179 phpstr_fix(PHPSTR(buffer
));
180 if ((eolstr
= http_locate_eol(PHPSTR_VAL(buffer
), &eollen
))) {
183 /* read in chunk size */
184 buffer
->hexlen
= strtoul(PHPSTR_VAL(buffer
), &stop
, 16);
186 /* if strtoul() stops at the beginning of the buffered data
187 there's domething oddly wrong, i.e. bad input */
188 if (stop
== PHPSTR_VAL(buffer
)) {
189 return PSFS_ERR_FATAL
;
192 /* cut out <chunk size hex><chunk extension><eol> */
193 phpstr_cut(PHPSTR(buffer
), 0, eolstr
+ eollen
- PHPSTR_VAL(buffer
));
194 /* buffer->hexlen is 0 now or contains the size of the next chunk */
197 /* we have not enough data buffered to read in chunk size */
205 /* flush before close, but only if we are already waiting for more data */
206 if (flags
== PSFS_FLAG_FLUSH_CLOSE
&& buffer
->hexlen
&& PHPSTR_LEN(buffer
)) {
208 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
209 phpstr_reset(PHPSTR(buffer
));
213 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
216 static HTTP_FILTER_DESTRUCTOR(chunked_decode
)
218 http_filter_buffer
*b
= (http_filter_buffer
*) (this->abstract
);
220 phpstr_dtor(PHPSTR(b
));
221 pefree(b
, this->is_persistent
);
224 static HTTP_FILTER_FUNCTION(chunked_encode
)
227 php_stream_bucket
*ptr
, *nxt
;
229 if (bytes_consumed
) {
233 /* new data available? */
234 if (buckets_in
->head
) {
240 /* fetch available bucket data */
241 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
243 if (bytes_consumed
) {
244 *bytes_consumed
+= ptr
->buflen
;
247 phpstr_appendf(&buf
, "%x" HTTP_CRLF
, ptr
->buflen
);
248 phpstr_append(&buf
, ptr
->buf
, ptr
->buflen
);
249 phpstr_appends(&buf
, HTTP_CRLF
);
252 NEW_BUCKET(PHPSTR_VAL(&buf
), PHPSTR_LEN(&buf
));
256 php_stream_bucket_unlink(ptr TSRMLS_CC
);
257 php_stream_bucket_delref(ptr TSRMLS_CC
);
264 /* terminate with "0" */
265 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
267 NEW_BUCKET("0" HTTP_CRLF
, lenof("0" HTTP_CRLF
));
270 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
273 static HTTP_FILTER_OPS(chunked_decode
) = {
274 HTTP_FILTER_FUNC(chunked_decode
),
275 HTTP_FILTER_DTOR(chunked_decode
),
276 "http.chunked_decode"
279 static HTTP_FILTER_OPS(chunked_encode
) = {
280 HTTP_FILTER_FUNC(chunked_encode
),
282 "http.chunked_encode"
285 static php_stream_filter
*http_filter_create(const char *name
, zval
*params
, int p TSRMLS_DC
)
287 php_stream_filter
*f
= NULL
;
289 if (!strcasecmp(name
, "http.chunked_decode")) {
290 http_filter_buffer
*b
= NULL
;
292 if ((b
= pecalloc(1, sizeof(http_filter_buffer
), p
))) {
293 phpstr_init_ex(PHPSTR(b
), 4096, p
? PHPSTR_INIT_PERSISTENT
: 0);
294 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_decode
), b
, p
))) {
300 if (!strcasecmp(name
, "http.chunked_encode")) {
301 f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode
), NULL
, p
);
307 php_stream_filter_factory http_filter_factory
= {
311 #endif /* ZEND_ENGINE_2 */
318 * vim600: noet sw=4 ts=4 fdm=marker
319 * vim<600: noet sw=4 ts=4