- add flag parameter to http_build_url(); slightly breaks parameter order
[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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_ZLIB
16 #include "php_http.h"
17
18 #ifdef ZEND_ENGINE_2
19
20 #include "php_streams.h"
21 #include "php_http_api.h"
22 #include "php_http_encoding_api.h"
23 #include "php_http_filter_api.h"
24
25 PHP_MINIT_FUNCTION(http_filter)
26 {
27 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC);
28 return SUCCESS;
29 }
30
31 /*
32 -
33 */
34
35 #define HTTP_FILTER_PARAMS \
36 php_stream *stream, \
37 php_stream_filter *this, \
38 php_stream_bucket_brigade *buckets_in, \
39 php_stream_bucket_brigade *buckets_out, \
40 size_t *bytes_consumed, int flags \
41 TSRMLS_DC
42 #define HTTP_FILTER_OP(filter) \
43 http_filter_op_ ##filter
44 #define HTTP_FILTER_OPS(filter) \
45 php_stream_filter_ops HTTP_FILTER_OP(filter)
46 #define HTTP_FILTER_DTOR(filter) \
47 http_filter_ ##filter## _dtor
48 #define HTTP_FILTER_DESTRUCTOR(filter) \
49 void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
50 #define HTTP_FILTER_FUNC(filter) \
51 http_filter_ ##filter
52 #define HTTP_FILTER_FUNCTION(filter) \
53 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
54 #define HTTP_FILTER_BUFFER(filter) \
55 http_filter_ ##filter## _buffer
56
57 #define NEW_BUCKET(data, length) \
58 { \
59 char *__data; \
60 php_stream_bucket *__buck; \
61 \
62 __data = pemalloc(length, this->is_persistent); \
63 if (!__data) { \
64 return PSFS_ERR_FATAL; \
65 } \
66 memcpy(__data, data, length); \
67 \
68 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
69 if (!__buck) { \
70 pefree(__data, this->is_persistent); \
71 return PSFS_ERR_FATAL; \
72 } \
73 \
74 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
75 }
76
77 typedef struct {
78 phpstr buffer;
79 ulong hexlen;
80 } HTTP_FILTER_BUFFER(chunked_decode);
81
82 #ifdef HTTP_HAVE_ZLIB
83 typedef http_encoding_stream HTTP_FILTER_BUFFER(deflate);
84 typedef http_encoding_stream HTTP_FILTER_BUFFER(inflate);
85 #endif /* HTTP_HAVE_ZLIB */
86
87
88 static HTTP_FILTER_FUNCTION(chunked_decode)
89 {
90 int out_avail = 0;
91 php_stream_bucket *ptr, *nxt;
92 HTTP_FILTER_BUFFER(chunked_decode) *buffer = (HTTP_FILTER_BUFFER(chunked_decode) *) (this->abstract);
93
94 if (bytes_consumed) {
95 *bytes_consumed = 0;
96 }
97
98 /* new data available? */
99 if (buckets_in->head) {
100
101 /* fetch available bucket data */
102 for (ptr = buckets_in->head; ptr; ptr = nxt) {
103 nxt = ptr->next;
104 if (bytes_consumed) {
105 *bytes_consumed += ptr->buflen;
106 }
107
108 if ((size_t) -1 == phpstr_append(PHPSTR(buffer), ptr->buf, ptr->buflen)) {
109 return PSFS_ERR_FATAL;
110 }
111
112 php_stream_bucket_unlink(ptr TSRMLS_CC);
113 php_stream_bucket_delref(ptr TSRMLS_CC);
114 }
115 }
116 if (!phpstr_fix(PHPSTR(buffer))) {
117 return PSFS_ERR_FATAL;
118 }
119
120 /* we have data in our buffer */
121 while (PHPSTR_LEN(buffer)) {
122
123 /* we already know the size of the chunk and are waiting for data */
124 if (buffer->hexlen) {
125
126 /* not enough data buffered */
127 if (PHPSTR_LEN(buffer) < buffer->hexlen) {
128
129 /* flush anyway? */
130 if (flags & PSFS_FLAG_FLUSH_INC) {
131
132 /* flush all data (should only be chunk data) */
133 out_avail = 1;
134 NEW_BUCKET(PHPSTR_VAL(buffer), PHPSTR_LEN(buffer));
135
136 /* waiting for less data now */
137 buffer->hexlen -= PHPSTR_LEN(buffer);
138 /* no more buffered data */
139 phpstr_reset(PHPSTR(buffer));
140 /* break */
141 }
142
143 /* we have too less data and don't need to flush */
144 else {
145 break;
146 }
147 }
148
149 /* we seem to have all data of the chunk */
150 else {
151 out_avail = 1;
152 NEW_BUCKET(PHPSTR_VAL(buffer), buffer->hexlen);
153
154 /* remove outgoing data from the buffer */
155 phpstr_cut(PHPSTR(buffer), 0, buffer->hexlen);
156 /* reset hexlen */
157 buffer->hexlen = 0;
158 /* continue */
159 }
160 }
161
162 /* we don't know the length of the chunk yet */
163 else {
164 size_t off = 0;
165
166 /* ignore preceeding CRLFs (too loose?) */
167 while (off < PHPSTR_LEN(buffer) && (
168 PHPSTR_VAL(buffer)[off] == 0xa ||
169 PHPSTR_VAL(buffer)[off] == 0xd)) {
170 ++off;
171 }
172 if (off) {
173 phpstr_cut(PHPSTR(buffer), 0, off);
174 }
175
176 /* still data there? */
177 if (PHPSTR_LEN(buffer)) {
178 int eollen;
179 const char *eolstr;
180
181 /* we need eol, so we can be sure we have all hex digits */
182 phpstr_fix(PHPSTR(buffer));
183 if ((eolstr = http_locate_eol(PHPSTR_VAL(buffer), &eollen))) {
184 char *stop = NULL;
185
186 /* read in chunk size */
187 buffer->hexlen = strtoul(PHPSTR_VAL(buffer), &stop, 16);
188
189 /* if strtoul() stops at the beginning of the buffered data
190 there's domething oddly wrong, i.e. bad input */
191 if (stop == PHPSTR_VAL(buffer)) {
192 return PSFS_ERR_FATAL;
193 }
194
195 /* cut out <chunk size hex><chunk extension><eol> */
196 phpstr_cut(PHPSTR(buffer), 0, eolstr + eollen - PHPSTR_VAL(buffer));
197 /* buffer->hexlen is 0 now or contains the size of the next chunk */
198 /* continue */
199 } else {
200 /* we have not enough data buffered to read in chunk size */
201 break;
202 }
203 }
204 /* break */
205 }
206 }
207
208 /* flush before close, but only if we are already waiting for more data */
209 if ((flags & PSFS_FLAG_FLUSH_CLOSE) && buffer->hexlen && PHPSTR_LEN(buffer)) {
210 out_avail = 1;
211 NEW_BUCKET(PHPSTR_VAL(buffer), PHPSTR_LEN(buffer));
212 phpstr_reset(PHPSTR(buffer));
213 buffer->hexlen = 0;
214 }
215
216 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
217 }
218
219 static HTTP_FILTER_DESTRUCTOR(chunked_decode)
220 {
221 HTTP_FILTER_BUFFER(chunked_decode) *b = (HTTP_FILTER_BUFFER(chunked_decode) *) (this->abstract);
222
223 phpstr_dtor(PHPSTR(b));
224 pefree(b, this->is_persistent);
225 }
226
227 static HTTP_FILTER_FUNCTION(chunked_encode)
228 {
229 int out_avail = 0;
230 php_stream_bucket *ptr, *nxt;
231
232 if (bytes_consumed) {
233 *bytes_consumed = 0;
234 }
235
236 /* new data available? */
237 if (buckets_in->head) {
238 phpstr buf;
239 out_avail = 1;
240
241 phpstr_init(&buf);
242
243 /* fetch available bucket data */
244 for (ptr = buckets_in->head; ptr; ptr = nxt) {
245 nxt = ptr->next;
246 if (bytes_consumed) {
247 *bytes_consumed += ptr->buflen;
248 }
249
250 phpstr_appendf(&buf, "%x" HTTP_CRLF, ptr->buflen);
251 phpstr_append(&buf, ptr->buf, ptr->buflen);
252 phpstr_appends(&buf, HTTP_CRLF);
253
254 /* pass through */
255 NEW_BUCKET(PHPSTR_VAL(&buf), PHPSTR_LEN(&buf));
256 /* reset */
257 phpstr_reset(&buf);
258
259 php_stream_bucket_unlink(ptr TSRMLS_CC);
260 php_stream_bucket_delref(ptr TSRMLS_CC);
261 }
262
263 /* free buffer */
264 phpstr_dtor(&buf);
265 }
266
267 /* terminate with "0" */
268 if (flags & PSFS_FLAG_FLUSH_CLOSE) {
269 out_avail = 1;
270 NEW_BUCKET("0" HTTP_CRLF, lenof("0" HTTP_CRLF));
271 }
272
273 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
274 }
275
276 static HTTP_FILTER_OPS(chunked_decode) = {
277 HTTP_FILTER_FUNC(chunked_decode),
278 HTTP_FILTER_DTOR(chunked_decode),
279 "http.chunked_decode"
280 };
281
282 static HTTP_FILTER_OPS(chunked_encode) = {
283 HTTP_FILTER_FUNC(chunked_encode),
284 NULL,
285 "http.chunked_encode"
286 };
287
288 #ifdef HTTP_HAVE_ZLIB
289
290 static HTTP_FILTER_FUNCTION(deflate)
291 {
292 int out_avail = 0;
293 php_stream_bucket *ptr, *nxt;
294 HTTP_FILTER_BUFFER(inflate) *buffer = (HTTP_FILTER_BUFFER(deflate) *) this->abstract;
295
296 if (bytes_consumed) {
297 *bytes_consumed = 0;
298 }
299
300 /* new data available? */
301 if (buckets_in->head) {
302
303 /* fetch available bucket data */
304 for (ptr = buckets_in->head; ptr; ptr = nxt) {
305 char *encoded = NULL;
306 size_t encoded_len = 0;
307
308 nxt = ptr->next;
309 if (bytes_consumed) {
310 *bytes_consumed += ptr->buflen;
311 }
312
313 if (ptr->buflen) {
314 http_encoding_deflate_stream_update(buffer, ptr->buf, ptr->buflen, &encoded, &encoded_len);
315 if (encoded) {
316 out_avail = 1;
317 NEW_BUCKET(encoded, encoded_len);
318 efree(encoded);
319 }
320 }
321
322 php_stream_bucket_unlink(ptr TSRMLS_CC);
323 php_stream_bucket_delref(ptr TSRMLS_CC);
324 }
325 }
326
327 /* flush & close */
328 if (flags & PSFS_FLAG_FLUSH_INC) {
329 char *encoded = NULL;
330 size_t encoded_len = 0;
331
332 http_encoding_deflate_stream_flush(buffer, &encoded, &encoded_len);
333 if (encoded) {
334 out_avail = 1;
335 NEW_BUCKET(encoded, encoded_len);
336 efree(encoded);
337 }
338 }
339
340 if (flags & PSFS_FLAG_FLUSH_CLOSE) {
341 char *encoded = NULL;
342 size_t encoded_len = 0;
343
344 http_encoding_deflate_stream_finish(buffer, &encoded, &encoded_len);
345 if (encoded) {
346 out_avail = 1;
347 NEW_BUCKET(encoded, encoded_len);
348 efree(encoded);
349 }
350 }
351
352 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
353 }
354
355 static HTTP_FILTER_FUNCTION(inflate)
356 {
357 int out_avail = 0;
358 php_stream_bucket *ptr, *nxt;
359 HTTP_FILTER_BUFFER(inflate) *buffer = (HTTP_FILTER_BUFFER(inflate) *) this->abstract;
360
361 if (bytes_consumed) {
362 *bytes_consumed = 0;
363 }
364
365 /* new data available? */
366 if (buckets_in->head) {
367
368 /* fetch available bucket data */
369 for (ptr = buckets_in->head; ptr; ptr = nxt) {
370 char *decoded = NULL;
371 size_t decoded_len = 0;
372
373 nxt = ptr->next;
374 if (bytes_consumed) {
375 *bytes_consumed += ptr->buflen;
376 }
377
378 if (ptr->buflen) {
379 http_encoding_inflate_stream_update(buffer, ptr->buf, ptr->buflen, &decoded, &decoded_len);
380 if (decoded) {
381 out_avail = 1;
382 NEW_BUCKET(decoded, decoded_len);
383 efree(decoded);
384 }
385 }
386
387 php_stream_bucket_unlink(ptr TSRMLS_CC);
388 php_stream_bucket_delref(ptr TSRMLS_CC);
389 }
390 }
391
392 /* flush & close */
393 if (flags & PSFS_FLAG_FLUSH_INC) {
394 char *decoded = NULL;
395 size_t decoded_len = 0;
396
397 http_encoding_inflate_stream_flush(buffer, &decoded, &decoded_len);
398 if (decoded) {
399 out_avail = 1;
400 NEW_BUCKET(decoded, decoded_len);
401 efree(decoded);
402 }
403 }
404
405 if (flags & PSFS_FLAG_FLUSH_CLOSE) {
406 char *decoded = NULL;
407 size_t decoded_len = 0;
408
409 http_encoding_inflate_stream_finish(buffer, &decoded, &decoded_len);
410 if (decoded) {
411 out_avail = 1;
412 NEW_BUCKET(decoded, decoded_len);
413 efree(decoded);
414 }
415 }
416
417 return out_avail ? PSFS_PASS_ON : PSFS_FEED_ME;
418 }
419
420 static HTTP_FILTER_DESTRUCTOR(deflate)
421 {
422 HTTP_FILTER_BUFFER(deflate) *buffer = (HTTP_FILTER_BUFFER(deflate) *) this->abstract;
423 http_encoding_deflate_stream_free(&buffer);
424 }
425
426 static HTTP_FILTER_DESTRUCTOR(inflate)
427 {
428 HTTP_FILTER_BUFFER(inflate) *buffer = (HTTP_FILTER_BUFFER(inflate) *) this->abstract;
429 http_encoding_inflate_stream_free(&buffer);
430 }
431
432 static HTTP_FILTER_OPS(deflate) = {
433 HTTP_FILTER_FUNC(deflate),
434 HTTP_FILTER_DTOR(deflate),
435 "http.deflate"
436 };
437
438 static HTTP_FILTER_OPS(inflate) = {
439 HTTP_FILTER_FUNC(inflate),
440 HTTP_FILTER_DTOR(inflate),
441 "http.inflate"
442 };
443
444 #endif /* HTTP_HAVE_ZLIB */
445
446 static php_stream_filter *http_filter_create(const char *name, zval *params, int p TSRMLS_DC)
447 {
448 zval **tmp = &params;
449 php_stream_filter *f = NULL;
450
451 if (!strcasecmp(name, "http.chunked_decode")) {
452 HTTP_FILTER_BUFFER(chunked_decode) *b = NULL;
453
454 if ((b = pecalloc(1, sizeof(HTTP_FILTER_BUFFER(chunked_decode)), p))) {
455 phpstr_init_ex(PHPSTR(b), 4096, p ? PHPSTR_INIT_PERSISTENT : 0);
456 if (!(f = php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_decode), b, p))) {
457 pefree(b, p);
458 }
459 }
460 } else
461
462 if (!strcasecmp(name, "http.chunked_encode")) {
463 f = php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode), NULL, p);
464 #ifdef HTTP_HAVE_ZLIB
465 } else
466
467 if (!strcasecmp(name, "http.inflate")) {
468 int flags = p ? HTTP_ENCODING_STREAM_PERSISTENT : 0;
469 HTTP_FILTER_BUFFER(inflate) *b = NULL;
470
471 if ((b = http_encoding_inflate_stream_init(NULL, flags))) {
472 if (!(f = php_stream_filter_alloc(&HTTP_FILTER_OP(inflate), b, p))) {
473 http_encoding_inflate_stream_free(&b);
474 }
475 }
476 } else
477
478 if (!strcasecmp(name, "http.deflate")) {
479 int flags = p ? HTTP_ENCODING_STREAM_PERSISTENT : 0;
480 HTTP_FILTER_BUFFER(deflate) *b = NULL;
481
482 if (params) {
483 switch (Z_TYPE_P(params))
484 {
485 case IS_ARRAY:
486 case IS_OBJECT:
487 if (SUCCESS != zend_hash_find(HASH_OF(params), "flags", sizeof("flags"), (void **) &tmp)) {
488 break;
489 }
490 default:
491 {
492 zval *orig = *tmp;
493
494 convert_to_long_ex(tmp);
495 flags |= (Z_LVAL_PP(tmp) & 0x0fffffff);
496 if (orig != *tmp) zval_ptr_dtor(tmp);
497 }
498 }
499 }
500 if ((b = http_encoding_deflate_stream_init(NULL, flags))) {
501 if (!(f = php_stream_filter_alloc(&HTTP_FILTER_OP(deflate), b, p))) {
502 http_encoding_deflate_stream_free(&b);
503 }
504 }
505 #endif /* HTTP_HAVE_ZLIB */
506 }
507
508 return f;
509 }
510
511 php_stream_filter_factory http_filter_factory = {
512 http_filter_create
513 };
514
515 #endif /* ZEND_ENGINE_2 */
516
517 /*
518 * Local variables:
519 * tab-width: 4
520 * c-basic-offset: 4
521 * End:
522 * vim600: noet sw=4 ts=4 fdm=marker
523 * vim<600: noet sw=4 ts=4
524 */