- remove debug statement
[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 } else {
195 /* we have not enough data buffered to read in chunk size */
196 break;
197 }
198 }
199 /* break */
200 }
201 }
202
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)) {
205 out_avail = 1;
206 NEW_BUCKET(PHPSTR_VAL(buffer), PHPSTR_LEN(buffer));
207 phpstr_reset(PHPSTR(buffer));
208 buffer->hexlen = 0;
209 }
210
211 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
212 }
213
214 static HTTP_FILTER_DESTRUCTOR(chunked_decode)
215 {
216 http_filter_buffer *b = (http_filter_buffer *) (this->abstract);
217
218 phpstr_dtor(PHPSTR(b));
219 pefree(b, this->is_persistent);
220 }
221
222 static HTTP_FILTER_FUNCTION(chunked_encode)
223 {
224 int out_avail = 0;
225 php_stream_bucket *ptr, *nxt;
226
227 if (bytes_consumed) {
228 *bytes_consumed = 0;
229 }
230
231 /* new data available? */
232 if (buckets_in->head) {
233 phpstr buf;
234 out_avail = 1;
235
236 phpstr_init(&buf);
237
238 /* fetch available bucket data */
239 for (ptr = buckets_in->head; ptr; ptr = nxt) {
240 nxt = ptr->next;
241 if (bytes_consumed) {
242 *bytes_consumed += ptr->buflen;
243 }
244
245 phpstr_appendf(&buf, "%x" HTTP_CRLF, ptr->buflen);
246 phpstr_append(&buf, ptr->buf, ptr->buflen);
247 phpstr_appends(&buf, HTTP_CRLF);
248
249 /* pass through */
250 NEW_BUCKET(PHPSTR_VAL(&buf), PHPSTR_LEN(&buf));
251 /* reset */
252 phpstr_reset(&buf);
253
254 php_stream_bucket_unlink(ptr TSRMLS_CC);
255 php_stream_bucket_delref(ptr TSRMLS_CC);
256 }
257
258 /* free buffer */
259 phpstr_dtor(&buf);
260 }
261
262 /* terminate with "0" */
263 if (flags == PSFS_FLAG_FLUSH_CLOSE) {
264 out_avail = 1;
265 NEW_BUCKET("0" HTTP_CRLF, lenof("0" HTTP_CRLF));
266 }
267
268 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
269 }
270
271 static HTTP_FILTER_OPS(chunked_decode) = {
272 HTTP_FILTER_FUNC(chunked_decode),
273 HTTP_FILTER_DTOR(chunked_decode),
274 "http.chunked_decode"
275 };
276
277 static HTTP_FILTER_OPS(chunked_encode) = {
278 HTTP_FILTER_FUNC(chunked_encode),
279 NULL,
280 "http.chunked_encode"
281 };
282
283 static php_stream_filter *http_filter_create(const char *name, zval *params, int p TSRMLS_DC)
284 {
285 php_stream_filter *f = NULL;
286
287 if (!strcasecmp(name, "http.chunked_decode")) {
288 http_filter_buffer *b = NULL;
289
290 /* FIXXME: allow usage with persistent streams */
291 if (p) {
292 return NULL;
293 }
294
295 if (b = pecalloc(1, sizeof(http_filter_buffer), p)) {
296 phpstr_init(PHPSTR(b));
297 if (!(f = php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_decode), b, p))) {
298 pefree(b, p);
299 }
300 }
301 } else
302
303 if (!strcasecmp(name, "http.chunked_encode")) {
304 f = php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode), NULL, p);
305 }
306
307 return f;
308 }
309
310 php_stream_filter_factory http_filter_factory = {
311 http_filter_create
312 };
313
314 /*
315 * Local variables:
316 * tab-width: 4
317 * c-basic-offset: 4
318 * End:
319 * vim600: noet sw=4 ts=4 fdm=marker
320 * vim<600: noet sw=4 ts=4
321 */