- aliases for HttpUtil
[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 #ifdef ZEND_ENGINE_2
21
22 #include "php_http_std_defs.h"
23 #include "php_http_api.h"
24 #include "php_http_filter_api.h"
25
26 #include "phpstr/phpstr.h"
27
28 #include "php_streams.h"
29
30 PHP_MINIT_FUNCTION(http_filter)
31 {
32 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC);
33 return SUCCESS;
34 }
35
36 /*
37 -
38 */
39
40 typedef struct {
41 phpstr buffer;
42 ulong hexlen;
43 } http_filter_buffer;
44
45 #define HTTP_FILTER_PARAMS \
46 php_stream *stream, \
47 php_stream_filter *this, \
48 php_stream_bucket_brigade *buckets_in, \
49 php_stream_bucket_brigade *buckets_out, \
50 size_t *bytes_consumed, int flags \
51 TSRMLS_DC
52 #define HTTP_FILTER_OP(filter) \
53 http_filter_op_ ##filter
54 #define HTTP_FILTER_OPS(filter) \
55 php_stream_filter_ops HTTP_FILTER_OP(filter)
56 #define HTTP_FILTER_DTOR(filter) \
57 http_filter_ ##filter## _dtor
58 #define HTTP_FILTER_DESTRUCTOR(filter) \
59 void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
60 #define HTTP_FILTER_FUNC(filter) \
61 http_filter_ ##filter
62 #define HTTP_FILTER_FUNCTION(filter) \
63 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
64
65 #define NEW_BUCKET(data, length) \
66 { \
67 char *__data; \
68 php_stream_bucket *__buck; \
69 \
70 __data = pemalloc(length, this->is_persistent); \
71 if (!__data) { \
72 return PSFS_ERR_FATAL; \
73 } \
74 memcpy(__data, data, length); \
75 \
76 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
77 if (!__buck) { \
78 pefree(__data, this->is_persistent); \
79 return PSFS_ERR_FATAL; \
80 } \
81 \
82 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
83 }
84
85 static HTTP_FILTER_FUNCTION(chunked_decode)
86 {
87 int out_avail = 0;
88 php_stream_bucket *ptr, *nxt;
89 http_filter_buffer *buffer = (http_filter_buffer *) (this->abstract);
90
91 if (bytes_consumed) {
92 *bytes_consumed = 0;
93 }
94
95 /* new data available? */
96 if (buckets_in->head) {
97
98 /* fetch available bucket data */
99 for (ptr = buckets_in->head; ptr; ptr = nxt) {
100 nxt = ptr->next;
101 if (bytes_consumed) {
102 *bytes_consumed += ptr->buflen;
103 }
104
105 if ((size_t) -1 == phpstr_append(PHPSTR(buffer), ptr->buf, ptr->buflen)) {
106 return PSFS_ERR_FATAL;
107 }
108
109 php_stream_bucket_unlink(ptr TSRMLS_CC);
110 php_stream_bucket_delref(ptr TSRMLS_CC);
111 }
112 }
113 if (!phpstr_fix(PHPSTR(buffer))) {
114 return PSFS_ERR_FATAL;
115 }
116
117 /* we have data in our buffer */
118 while (PHPSTR_LEN(buffer)) {
119
120 /* we already know the size of the chunk and are waiting for data */
121 if (buffer->hexlen) {
122
123 /* not enough data buffered */
124 if (PHPSTR_LEN(buffer) < buffer->hexlen) {
125
126 /* flush anyway? */
127 if (flags == PSFS_FLAG_FLUSH_INC) {
128
129 /* flush all data (should only be chunk data) */
130 out_avail = 1;
131 NEW_BUCKET(PHPSTR_VAL(buffer), PHPSTR_LEN(buffer));
132
133 /* waiting for less data now */
134 buffer->hexlen -= PHPSTR_LEN(buffer);
135 /* no more buffered data */
136 phpstr_reset(PHPSTR(buffer));
137 /* break */
138 }
139
140 /* we have too less data and don't need to flush */
141 else {
142 break;
143 }
144 }
145
146 /* we seem to have all data of the chunk */
147 else {
148 out_avail = 1;
149 NEW_BUCKET(PHPSTR_VAL(buffer), buffer->hexlen);
150
151 /* remove outgoing data from the buffer */
152 phpstr_cut(PHPSTR(buffer), 0, buffer->hexlen);
153 /* reset hexlen */
154 buffer->hexlen = 0;
155 /* continue */
156 }
157 }
158
159 /* we don't know the length of the chunk yet */
160 else {
161 size_t off = 0;
162
163 /* ignore preceeding CRLFs (too loose?) */
164 while (off < PHPSTR_LEN(buffer) && (
165 PHPSTR_VAL(buffer)[off] == 0xa ||
166 PHPSTR_VAL(buffer)[off] == 0xd)) {
167 ++off;
168 }
169 if (off) {
170 phpstr_cut(PHPSTR(buffer), 0, off);
171 }
172
173 /* still data there? */
174 if (PHPSTR_LEN(buffer)) {
175 int eollen;
176 const char *eolstr;
177
178 /* we need eol, so we can be sure we have all hex digits */
179 phpstr_fix(PHPSTR(buffer));
180 if ((eolstr = http_locate_eol(PHPSTR_VAL(buffer), &eollen))) {
181 char *stop = NULL;
182
183 /* read in chunk size */
184 buffer->hexlen = strtoul(PHPSTR_VAL(buffer), &stop, 16);
185
186 /* if strtoul() stops at the beginning of the buffered data
187 there's domething oddly wrong, i.e. bad input */
188 if (stop == PHPSTR_VAL(buffer)) {
189 return PSFS_ERR_FATAL;
190 }
191
192 /* cut out <chunk size hex><chunk extension><eol> */
193 phpstr_cut(PHPSTR(buffer), 0, eolstr + eollen - PHPSTR_VAL(buffer));
194 /* buffer->hexlen is 0 now or contains the size of the next chunk */
195 /* continue */
196 } else {
197 /* we have not enough data buffered to read in chunk size */
198 break;
199 }
200 }
201 /* break */
202 }
203 }
204
205 /* flush before close, but only if we are already waiting for more data */
206 if (flags == PSFS_FLAG_FLUSH_CLOSE && buffer->hexlen && PHPSTR_LEN(buffer)) {
207 out_avail = 1;
208 NEW_BUCKET(PHPSTR_VAL(buffer), PHPSTR_LEN(buffer));
209 phpstr_reset(PHPSTR(buffer));
210 buffer->hexlen = 0;
211 }
212
213 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
214 }
215
216 static HTTP_FILTER_DESTRUCTOR(chunked_decode)
217 {
218 http_filter_buffer *b = (http_filter_buffer *) (this->abstract);
219
220 phpstr_dtor(PHPSTR(b));
221 pefree(b, this->is_persistent);
222 }
223
224 static HTTP_FILTER_FUNCTION(chunked_encode)
225 {
226 int out_avail = 0;
227 php_stream_bucket *ptr, *nxt;
228
229 if (bytes_consumed) {
230 *bytes_consumed = 0;
231 }
232
233 /* new data available? */
234 if (buckets_in->head) {
235 phpstr buf;
236 out_avail = 1;
237
238 phpstr_init(&buf);
239
240 /* fetch available bucket data */
241 for (ptr = buckets_in->head; ptr; ptr = nxt) {
242 nxt = ptr->next;
243 if (bytes_consumed) {
244 *bytes_consumed += ptr->buflen;
245 }
246
247 phpstr_appendf(&buf, "%x" HTTP_CRLF, ptr->buflen);
248 phpstr_append(&buf, ptr->buf, ptr->buflen);
249 phpstr_appends(&buf, HTTP_CRLF);
250
251 /* pass through */
252 NEW_BUCKET(PHPSTR_VAL(&buf), PHPSTR_LEN(&buf));
253 /* reset */
254 phpstr_reset(&buf);
255
256 php_stream_bucket_unlink(ptr TSRMLS_CC);
257 php_stream_bucket_delref(ptr TSRMLS_CC);
258 }
259
260 /* free buffer */
261 phpstr_dtor(&buf);
262 }
263
264 /* terminate with "0" */
265 if (flags == PSFS_FLAG_FLUSH_CLOSE) {
266 out_avail = 1;
267 NEW_BUCKET("0" HTTP_CRLF, lenof("0" HTTP_CRLF));
268 }
269
270 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
271 }
272
273 static HTTP_FILTER_OPS(chunked_decode) = {
274 HTTP_FILTER_FUNC(chunked_decode),
275 HTTP_FILTER_DTOR(chunked_decode),
276 "http.chunked_decode"
277 };
278
279 static HTTP_FILTER_OPS(chunked_encode) = {
280 HTTP_FILTER_FUNC(chunked_encode),
281 NULL,
282 "http.chunked_encode"
283 };
284
285 static php_stream_filter *http_filter_create(const char *name, zval *params, int p TSRMLS_DC)
286 {
287 php_stream_filter *f = NULL;
288
289 if (!strcasecmp(name, "http.chunked_decode")) {
290 http_filter_buffer *b = NULL;
291
292 if ((b = pecalloc(1, sizeof(http_filter_buffer), p))) {
293 phpstr_init_ex(PHPSTR(b), 4096, p ? PHPSTR_INIT_PERSISTENT : 0);
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 #endif /* ZEND_ENGINE_2 */
312
313 /*
314 * Local variables:
315 * tab-width: 4
316 * c-basic-offset: 4
317 * End:
318 * vim600: noet sw=4 ts=4 fdm=marker
319 * vim<600: noet sw=4 ts=4
320 */