- fix occurrence of PHPSTR_LEN(x) = 0;
[m6w6/ext-http] / http_filter_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include "php.h"
19
20 #include "php_http_std_defs.h"
21 #include "php_http_api.h"
22 #include "php_http_filter_api.h"
23
24 #include "phpstr/phpstr.h"
25
26 #include "php_streams.h"
27
28 /*
29 * TODO: allow use with persistent streams
30 */
31
32 PHP_MINIT_FUNCTION(http_filter)
33 {
34 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC);
35 return SUCCESS;
36 }
37
38 /*
39 -
40 */
41
42 typedef struct {
43 phpstr buffer;
44 ulong hexlen;
45 } http_filter_buffer;
46
47 #define HTTP_FILTER_PARAMS \
48 php_stream *stream, \
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 \
53 TSRMLS_DC
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) \
63 http_filter_ ##filter
64 #define HTTP_FILTER_FUNCTION(filter) \
65 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
66
67 #define NEW_BUCKET(data, length) \
68 { \
69 char *__data; \
70 php_stream_bucket *__buck; \
71 \
72 __data = pemalloc(length, this->is_persistent); \
73 if (!__data) { \
74 return PSFS_ERR_FATAL; \
75 } \
76 memcpy(__data, data, length); \
77 \
78 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
79 if (!__buck) { \
80 pefree(__data, this->is_persistent); \
81 return PSFS_ERR_FATAL; \
82 } \
83 \
84 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
85 }
86
87 static HTTP_FILTER_FUNCTION(chunked_decode)
88 {
89 int out_avail = 0;
90 php_stream_bucket *ptr, *nxt;
91 http_filter_buffer *buffer = (http_filter_buffer *) (this->abstract);
92
93 if (bytes_consumed) {
94 *bytes_consumed = 0;
95 }
96
97 /* new data available? */
98 if (buckets_in->head) {
99
100 /* fetch available bucket data */
101 for (ptr = buckets_in->head; ptr; ptr = nxt) {
102 nxt = ptr->next;
103 if (bytes_consumed) {
104 *bytes_consumed += ptr->buflen;
105 }
106
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);
110
111 }
112 }
113 phpstr_fix(PHPSTR(buffer));
114
115 /* we have data in our buffer */
116 while (PHPSTR_LEN(buffer)) {
117
118 /* we already know the size of the chunk and are waiting for data */
119 if (buffer->hexlen) {
120
121 /* not enough data buffered */
122 if (PHPSTR_LEN(buffer) < buffer->hexlen) {
123
124 /* flush anyway? */
125 if (flags == PSFS_FLAG_FLUSH_INC) {
126
127 /* flush all data (should only be chunk data) */
128 out_avail = 1;
129 NEW_BUCKET(PHPSTR_VAL(buffer), PHPSTR_LEN(buffer));
130
131 /* waiting for less data now */
132 buffer->hexlen -= PHPSTR_LEN(buffer);
133 /* no more buffered data */
134 phpstr_reset(PHPSTR(buffer));
135 /* break */
136 }
137
138 /* we have too less data and don't need to flush */
139 else {
140 break;
141 }
142 }
143
144 /* we seem to have all data of the chunk */
145 else {
146 out_avail = 1;
147 NEW_BUCKET(PHPSTR_VAL(buffer), buffer->hexlen);
148
149 /* remove outgoing data from the buffer */
150 phpstr_cut(PHPSTR(buffer), 0, buffer->hexlen);
151 /* reset hexlen */
152 buffer->hexlen = 0;
153 /* continue */
154 }
155 }
156
157 /* we don't know the length of the chunk yet */
158 else {
159 size_t off = 0;
160
161 /* ignore preceeding CRLFs (too loose?) */
162 while (off < PHPSTR_LEN(buffer) && (
163 PHPSTR_VAL(buffer)[off] == 0xa ||
164 PHPSTR_VAL(buffer)[off] == 0xd)) {
165 ++off;
166 }
167 if (off) {
168 phpstr_cut(PHPSTR(buffer), 0, off);
169 }
170
171 /* still data there? */
172 if (PHPSTR_LEN(buffer)) {
173 int eollen;
174 const char *eolstr;
175
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)) {
179 char *stop = NULL;
180
181 /* read in chunk size */
182 buffer->hexlen = strtoul(PHPSTR_VAL(buffer), &stop, 16);
183
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;
188 }
189
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 */
193 /* continue */
194 }
195 }
196 /* break */
197 }
198 }
199
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)) {
202 out_avail = 1;
203 NEW_BUCKET(PHPSTR_VAL(buffer), PHPSTR_LEN(buffer));
204 phpstr_reset(PHPSTR(buffer));
205 buffer->hexlen = 0;
206 }
207
208 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
209 }
210
211 static HTTP_FILTER_DESTRUCTOR(chunked_decode)
212 {
213 http_filter_buffer *b = (http_filter_buffer *) (this->abstract);
214
215 phpstr_dtor(PHPSTR(b));
216 pefree(b, this->is_persistent);
217 }
218
219 static HTTP_FILTER_FUNCTION(chunked_encode)
220 {
221 int out_avail = 0;
222 php_stream_bucket *ptr, *nxt;
223
224 if (bytes_consumed) {
225 *bytes_consumed = 0;
226 }
227
228 /* new data available? */
229 if (buckets_in->head) {
230 phpstr buf;
231 out_avail = 1;
232
233 phpstr_init(&buf);
234
235 /* fetch available bucket data */
236 for (ptr = buckets_in->head; ptr; ptr = nxt) {
237 nxt = ptr->next;
238 if (bytes_consumed) {
239 *bytes_consumed += ptr->buflen;
240 }
241
242 phpstr_appendf(&buf, "%x" HTTP_CRLF, ptr->buflen);
243 phpstr_append(&buf, ptr->buf, ptr->buflen);
244 phpstr_appends(&buf, HTTP_CRLF);
245
246 /* pass through */
247 NEW_BUCKET(PHPSTR_VAL(&buf), PHPSTR_LEN(&buf));
248 /* reset */
249 phpstr_reset(&buf);
250
251 php_stream_bucket_unlink(ptr TSRMLS_CC);
252 php_stream_bucket_delref(ptr TSRMLS_CC);
253 }
254
255 /* free buffer */
256 phpstr_dtor(&buf);
257 }
258
259 /* terminate with "0" */
260 if (flags == PSFS_FLAG_FLUSH_CLOSE) {
261 out_avail = 1;
262 NEW_BUCKET("0" HTTP_CRLF, lenof("0" HTTP_CRLF));
263 }
264
265 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
266 }
267
268 static HTTP_FILTER_OPS(chunked_decode) = {
269 HTTP_FILTER_FUNC(chunked_decode),
270 HTTP_FILTER_DTOR(chunked_decode),
271 "http.chunked_decode"
272 };
273
274 static HTTP_FILTER_OPS(chunked_encode) = {
275 HTTP_FILTER_FUNC(chunked_encode),
276 NULL,
277 "http.chunked_encode"
278 };
279
280 static php_stream_filter *http_filter_create(const char *name, zval *params, int p TSRMLS_DC)
281 {
282 php_stream_filter *f = NULL;
283
284 if (!strcasecmp(name, "http.chunked_decode")) {
285 http_filter_buffer *b = NULL;
286
287 /* FIXXME: allow usage with persistent streams */
288 if (p) {
289 return NULL;
290 }
291
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))) {
295 pefree(b, p);
296 }
297 }
298 } else
299
300 if (!strcasecmp(name, "http.chunked_encode")) {
301 f = php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode), NULL, p);
302 }
303
304 return f;
305 }
306
307 php_stream_filter_factory http_filter_factory = {
308 http_filter_create
309 };
310
311 /*
312 * Local variables:
313 * tab-width: 4
314 * c-basic-offset: 4
315 * End:
316 * vim600: noet sw=4 ts=4 fdm=marker
317 * vim<600: noet sw=4 ts=4
318 */