- ditch some warnings
[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 PHP_MINIT_FUNCTION(http_filter)
29 {
30 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC);
31 return SUCCESS;
32 }
33
34 /*
35 -
36 */
37
38 typedef struct {
39 phpstr buffer;
40 ulong hexlen;
41 } http_filter_buffer;
42
43 #define HTTP_FILTER_PARAMS \
44 php_stream *stream, \
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 \
49 TSRMLS_DC
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) \
59 http_filter_ ##filter
60 #define HTTP_FILTER_FUNCTION(filter) \
61 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
62
63 #define NEW_BUCKET(data, length) \
64 { \
65 char *__data; \
66 php_stream_bucket *__buck; \
67 \
68 __data = pemalloc(length, this->is_persistent); \
69 if (!__data) { \
70 return PSFS_ERR_FATAL; \
71 } \
72 memcpy(__data, data, length); \
73 \
74 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
75 if (!__buck) { \
76 pefree(__data, this->is_persistent); \
77 return PSFS_ERR_FATAL; \
78 } \
79 \
80 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
81 }
82
83 static HTTP_FILTER_FUNCTION(chunked_decode)
84 {
85 int out_avail = 0;
86 php_stream_bucket *ptr, *nxt;
87 http_filter_buffer *buffer = (http_filter_buffer *) (this->abstract);
88
89 if (bytes_consumed) {
90 *bytes_consumed = 0;
91 }
92
93 /* new data available? */
94 if (buckets_in->head) {
95
96 /* fetch available bucket data */
97 for (ptr = buckets_in->head; ptr; ptr = nxt) {
98 nxt = ptr->next;
99 if (bytes_consumed) {
100 *bytes_consumed += ptr->buflen;
101 }
102
103 if ((size_t) -1 == phpstr_append(PHPSTR(buffer), ptr->buf, ptr->buflen)) {
104 return PSFS_ERR_FATAL;
105 }
106
107 php_stream_bucket_unlink(ptr TSRMLS_CC);
108 php_stream_bucket_delref(ptr TSRMLS_CC);
109 }
110 }
111 if (!phpstr_fix(PHPSTR(buffer))) {
112 return PSFS_ERR_FATAL;
113 }
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 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))) {
293 pefree(b, p);
294 }
295 }
296 } else
297
298 if (!strcasecmp(name, "http.chunked_encode")) {
299 f = php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode), NULL, p);
300 }
301
302 return f;
303 }
304
305 php_stream_filter_factory http_filter_factory = {
306 http_filter_create
307 };
308
309 /*
310 * Local variables:
311 * tab-width: 4
312 * c-basic-offset: 4
313 * End:
314 * vim600: noet sw=4 ts=4 fdm=marker
315 * vim<600: noet sw=4 ts=4
316 */