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 +--------------------------------------------------------------------+
20 #include "php_http_std_defs.h"
21 #include "php_http_api.h"
22 #include "php_http_filter_api.h"
24 #include "phpstr/phpstr.h"
26 #include "php_streams.h"
29 * TODO: allow use with persistent streams
32 PHP_MINIT_FUNCTION(http_filter
)
34 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC
);
47 #define HTTP_FILTER_PARAMS \
49 php_stream_filter *this, \
50 php_stream_bucket_brigade *buckets_in, \
51 php_stream_bucket_brigade *buckets_out, \
52 size_t *bytes_consumed, int flags \
54 #define HTTP_FILTER_OP(filter) \
55 http_filter_op_ ##filter
56 #define HTTP_FILTER_OPS(filter) \
57 php_stream_filter_ops HTTP_FILTER_OP(filter)
58 #define HTTP_FILTER_DTOR(filter) \
59 http_filter_ ##filter## _dtor
60 #define HTTP_FILTER_DESTRUCTOR(filter) \
61 void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
62 #define HTTP_FILTER_FUNC(filter) \
64 #define HTTP_FILTER_FUNCTION(filter) \
65 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
67 #define NEW_BUCKET(data, length) \
70 php_stream_bucket *__buck; \
72 __data = pemalloc(length, this->is_persistent); \
74 return PSFS_ERR_FATAL; \
76 memcpy(__data, data, length); \
78 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
80 pefree(__data, this->is_persistent); \
81 return PSFS_ERR_FATAL; \
84 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
87 static HTTP_FILTER_FUNCTION(chunked_decode
)
90 php_stream_bucket
*ptr
, *nxt
;
91 http_filter_buffer
*buffer
= (http_filter_buffer
*) (this->abstract
);
97 /* new data available? */
98 if (buckets_in
->head
) {
100 /* fetch available bucket data */
101 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
103 if (bytes_consumed
) {
104 *bytes_consumed
+= ptr
->buflen
;
107 phpstr_append(PHPSTR(buffer
), ptr
->buf
, ptr
->buflen
);
108 php_stream_bucket_unlink(ptr TSRMLS_CC
);
109 php_stream_bucket_delref(ptr TSRMLS_CC
);
113 phpstr_fix(PHPSTR(buffer
));
115 /* we have data in our buffer */
116 while (PHPSTR_LEN(buffer
)) {
118 /* we already know the size of the chunk and are waiting for data */
119 if (buffer
->hexlen
) {
121 /* not enough data buffered */
122 if (PHPSTR_LEN(buffer
) < buffer
->hexlen
) {
125 if (flags
== PSFS_FLAG_FLUSH_INC
) {
127 /* flush all data (should only be chunk data) */
129 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
131 /* waiting for less data now */
132 buffer
->hexlen
-= PHPSTR_LEN(buffer
);
133 /* no more buffered data */
134 phpstr_reset(PHPSTR(buffer
));
138 /* we have too less data and don't need to flush */
144 /* we seem to have all data of the chunk */
147 NEW_BUCKET(PHPSTR_VAL(buffer
), buffer
->hexlen
);
149 /* remove outgoing data from the buffer */
150 phpstr_cut(PHPSTR(buffer
), 0, buffer
->hexlen
);
157 /* we don't know the length of the chunk yet */
161 /* ignore preceeding CRLFs (too loose?) */
162 while (off
< PHPSTR_LEN(buffer
) && (
163 PHPSTR_VAL(buffer
)[off
] == 0xa ||
164 PHPSTR_VAL(buffer
)[off
] == 0xd)) {
168 phpstr_cut(PHPSTR(buffer
), 0, off
);
171 /* still data there? */
172 if (PHPSTR_LEN(buffer
)) {
176 /* we need eol, so we can be sure we have all hex digits */
177 phpstr_fix(PHPSTR(buffer
));
178 if (eolstr
= http_locate_eol(PHPSTR_VAL(buffer
), &eollen
)) {
181 /* read in chunk size */
182 buffer
->hexlen
= strtoul(PHPSTR_VAL(buffer
), &stop
, 16);
184 /* if strtoul() stops at the beginning of the buffered data
185 there's domething oddly wrong, i.e. bad input */
186 if (stop
== PHPSTR_VAL(buffer
)) {
187 return PSFS_ERR_FATAL
;
190 /* cut out <chunk size hex><chunk extension><eol> */
191 phpstr_cut(PHPSTR(buffer
), 0, eolstr
+ eollen
- PHPSTR_VAL(buffer
));
192 /* buffer->hexlen is 0 now or contains the size of the next chunk */
200 /* flush before close, but only if we are already waiting for more data */
201 if (flags
== PSFS_FLAG_FLUSH_CLOSE
&& buffer
->hexlen
&& PHPSTR_LEN(buffer
)) {
203 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
204 phpstr_reset(PHPSTR(buffer
));
208 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
211 static HTTP_FILTER_DESTRUCTOR(chunked_decode
)
213 http_filter_buffer
*b
= (http_filter_buffer
*) (this->abstract
);
215 phpstr_dtor(PHPSTR(b
));
216 pefree(b
, this->is_persistent
);
219 static HTTP_FILTER_FUNCTION(chunked_encode
)
222 php_stream_bucket
*ptr
, *nxt
;
224 if (bytes_consumed
) {
228 /* new data available? */
229 if (buckets_in
->head
) {
235 /* fetch available bucket data */
236 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
238 if (bytes_consumed
) {
239 *bytes_consumed
+= ptr
->buflen
;
242 phpstr_appendf(&buf
, "%x" HTTP_CRLF
, ptr
->buflen
);
243 phpstr_append(&buf
, ptr
->buf
, ptr
->buflen
);
244 phpstr_appends(&buf
, HTTP_CRLF
);
247 NEW_BUCKET(PHPSTR_VAL(&buf
), PHPSTR_LEN(&buf
));
251 php_stream_bucket_unlink(ptr TSRMLS_CC
);
252 php_stream_bucket_delref(ptr TSRMLS_CC
);
259 /* terminate with "0" */
260 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
262 NEW_BUCKET("0" HTTP_CRLF
, lenof("0" HTTP_CRLF
));
265 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
268 static HTTP_FILTER_OPS(chunked_decode
) = {
269 HTTP_FILTER_FUNC(chunked_decode
),
270 HTTP_FILTER_DTOR(chunked_decode
),
271 "http.chunked_decode"
274 static HTTP_FILTER_OPS(chunked_encode
) = {
275 HTTP_FILTER_FUNC(chunked_encode
),
277 "http.chunked_encode"
280 static php_stream_filter
*http_filter_create(const char *name
, zval
*params
, int p TSRMLS_DC
)
282 php_stream_filter
*f
= NULL
;
284 if (!strcasecmp(name
, "http.chunked_decode")) {
285 http_filter_buffer
*b
= NULL
;
287 /* FIXXME: allow usage with persistent streams */
292 if (b
= pecalloc(1, sizeof(http_filter_buffer
), p
)) {
293 phpstr_init(PHPSTR(b
));
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
= {
316 * vim600: noet sw=4 ts=4 fdm=marker
317 * vim<600: noet sw=4 ts=4