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"
28 PHP_MINIT_FUNCTION(http_filter
)
30 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC
);
43 #define HTTP_FILTER_PARAMS \
45 php_stream_filter *this, \
46 php_stream_bucket_brigade *buckets_in, \
47 php_stream_bucket_brigade *buckets_out, \
48 size_t *bytes_consumed, int flags \
50 #define HTTP_FILTER_OP(filter) \
51 http_filter_op_ ##filter
52 #define HTTP_FILTER_OPS(filter) \
53 php_stream_filter_ops HTTP_FILTER_OP(filter)
54 #define HTTP_FILTER_DTOR(filter) \
55 http_filter_ ##filter## _dtor
56 #define HTTP_FILTER_DESTRUCTOR(filter) \
57 void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
58 #define HTTP_FILTER_FUNC(filter) \
60 #define HTTP_FILTER_FUNCTION(filter) \
61 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
63 #define NEW_BUCKET(data, length) \
66 php_stream_bucket *__buck; \
68 __data = pemalloc(length, this->is_persistent); \
70 return PSFS_ERR_FATAL; \
72 memcpy(__data, data, length); \
74 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
76 pefree(__data, this->is_persistent); \
77 return PSFS_ERR_FATAL; \
80 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
83 static HTTP_FILTER_FUNCTION(chunked_decode
)
86 php_stream_bucket
*ptr
, *nxt
;
87 http_filter_buffer
*buffer
= (http_filter_buffer
*) (this->abstract
);
93 /* new data available? */
94 if (buckets_in
->head
) {
96 /* fetch available bucket data */
97 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
100 *bytes_consumed
+= ptr
->buflen
;
103 if ((size_t) -1 == phpstr_append(PHPSTR(buffer
), ptr
->buf
, ptr
->buflen
)) {
104 return PSFS_ERR_FATAL
;
107 php_stream_bucket_unlink(ptr TSRMLS_CC
);
108 php_stream_bucket_delref(ptr TSRMLS_CC
);
111 if (!phpstr_fix(PHPSTR(buffer
))) {
112 return PSFS_ERR_FATAL
;
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 */
195 /* we have not enough data buffered to read in chunk size */
203 /* flush before close, but only if we are already waiting for more data */
204 if (flags
== PSFS_FLAG_FLUSH_CLOSE
&& buffer
->hexlen
&& PHPSTR_LEN(buffer
)) {
206 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
207 phpstr_reset(PHPSTR(buffer
));
211 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
214 static HTTP_FILTER_DESTRUCTOR(chunked_decode
)
216 http_filter_buffer
*b
= (http_filter_buffer
*) (this->abstract
);
218 phpstr_dtor(PHPSTR(b
));
219 pefree(b
, this->is_persistent
);
222 static HTTP_FILTER_FUNCTION(chunked_encode
)
225 php_stream_bucket
*ptr
, *nxt
;
227 if (bytes_consumed
) {
231 /* new data available? */
232 if (buckets_in
->head
) {
238 /* fetch available bucket data */
239 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
241 if (bytes_consumed
) {
242 *bytes_consumed
+= ptr
->buflen
;
245 phpstr_appendf(&buf
, "%x" HTTP_CRLF
, ptr
->buflen
);
246 phpstr_append(&buf
, ptr
->buf
, ptr
->buflen
);
247 phpstr_appends(&buf
, HTTP_CRLF
);
250 NEW_BUCKET(PHPSTR_VAL(&buf
), PHPSTR_LEN(&buf
));
254 php_stream_bucket_unlink(ptr TSRMLS_CC
);
255 php_stream_bucket_delref(ptr TSRMLS_CC
);
262 /* terminate with "0" */
263 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
265 NEW_BUCKET("0" HTTP_CRLF
, lenof("0" HTTP_CRLF
));
268 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
271 static HTTP_FILTER_OPS(chunked_decode
) = {
272 HTTP_FILTER_FUNC(chunked_decode
),
273 HTTP_FILTER_DTOR(chunked_decode
),
274 "http.chunked_decode"
277 static HTTP_FILTER_OPS(chunked_encode
) = {
278 HTTP_FILTER_FUNC(chunked_encode
),
280 "http.chunked_encode"
283 static php_stream_filter
*http_filter_create(const char *name
, zval
*params
, int p TSRMLS_DC
)
285 php_stream_filter
*f
= NULL
;
287 if (!strcasecmp(name
, "http.chunked_decode")) {
288 http_filter_buffer
*b
= NULL
;
290 if (b
= pecalloc(1, sizeof(http_filter_buffer
), p
)) {
291 phpstr_init_ex(PHPSTR(b
), 4096, p
? PHPSTR_INIT_PERSISTENT
: 0);
292 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_decode
), b
, p
))) {
298 if (!strcasecmp(name
, "http.chunked_encode")) {
299 f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode
), NULL
, p
);
305 php_stream_filter_factory http_filter_factory
= {
314 * vim600: noet sw=4 ts=4 fdm=marker
315 * vim<600: noet sw=4 ts=4