2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
19 #define HTTP_WANT_ZLIB
24 #include "php_streams.h"
25 #include "php_http_api.h"
26 #include "php_http_encoding_api.h"
27 #include "php_http_filter_api.h"
29 PHP_MINIT_FUNCTION(http_filter
)
31 php_stream_filter_register_factory("http.*", &http_filter_factory TSRMLS_CC
);
39 #define HTTP_FILTER_PARAMS \
41 php_stream_filter *this, \
42 php_stream_bucket_brigade *buckets_in, \
43 php_stream_bucket_brigade *buckets_out, \
44 size_t *bytes_consumed, int flags \
46 #define HTTP_FILTER_OP(filter) \
47 http_filter_op_ ##filter
48 #define HTTP_FILTER_OPS(filter) \
49 php_stream_filter_ops HTTP_FILTER_OP(filter)
50 #define HTTP_FILTER_DTOR(filter) \
51 http_filter_ ##filter## _dtor
52 #define HTTP_FILTER_DESTRUCTOR(filter) \
53 void HTTP_FILTER_DTOR(filter)(php_stream_filter *this TSRMLS_DC)
54 #define HTTP_FILTER_FUNC(filter) \
56 #define HTTP_FILTER_FUNCTION(filter) \
57 php_stream_filter_status_t HTTP_FILTER_FUNC(filter)(HTTP_FILTER_PARAMS)
58 #define HTTP_FILTER_BUFFER(filter) \
59 http_filter_ ##filter## _buffer
61 #define NEW_BUCKET(data, length) \
64 php_stream_bucket *__buck; \
66 __data = pemalloc(length, this->is_persistent); \
68 return PSFS_ERR_FATAL; \
70 memcpy(__data, data, length); \
72 __buck = php_stream_bucket_new(stream, __data, length, 1, this->is_persistent TSRMLS_CC); \
74 pefree(__data, this->is_persistent); \
75 return PSFS_ERR_FATAL; \
78 php_stream_bucket_append(buckets_out, __buck TSRMLS_CC); \
84 } HTTP_FILTER_BUFFER(chunked_decode
);
90 http_encoding_stream stream
;
91 } HTTP_FILTER_BUFFER(gzip
);
92 #endif /* HTTP_HAVE_ZLIB */
95 static HTTP_FILTER_FUNCTION(chunked_decode
)
98 php_stream_bucket
*ptr
, *nxt
;
99 HTTP_FILTER_BUFFER(chunked_decode
) *buffer
= (HTTP_FILTER_BUFFER(chunked_decode
) *) (this->abstract
);
101 if (bytes_consumed
) {
105 /* new data available? */
106 if (buckets_in
->head
) {
108 /* fetch available bucket data */
109 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
111 if (bytes_consumed
) {
112 *bytes_consumed
+= ptr
->buflen
;
115 if ((size_t) -1 == phpstr_append(PHPSTR(buffer
), ptr
->buf
, ptr
->buflen
)) {
116 return PSFS_ERR_FATAL
;
119 php_stream_bucket_unlink(ptr TSRMLS_CC
);
120 php_stream_bucket_delref(ptr TSRMLS_CC
);
123 if (!phpstr_fix(PHPSTR(buffer
))) {
124 return PSFS_ERR_FATAL
;
127 /* we have data in our buffer */
128 while (PHPSTR_LEN(buffer
)) {
130 /* we already know the size of the chunk and are waiting for data */
131 if (buffer
->hexlen
) {
133 /* not enough data buffered */
134 if (PHPSTR_LEN(buffer
) < buffer
->hexlen
) {
137 if (flags
== PSFS_FLAG_FLUSH_INC
) {
139 /* flush all data (should only be chunk data) */
141 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
143 /* waiting for less data now */
144 buffer
->hexlen
-= PHPSTR_LEN(buffer
);
145 /* no more buffered data */
146 phpstr_reset(PHPSTR(buffer
));
150 /* we have too less data and don't need to flush */
156 /* we seem to have all data of the chunk */
159 NEW_BUCKET(PHPSTR_VAL(buffer
), buffer
->hexlen
);
161 /* remove outgoing data from the buffer */
162 phpstr_cut(PHPSTR(buffer
), 0, buffer
->hexlen
);
169 /* we don't know the length of the chunk yet */
173 /* ignore preceeding CRLFs (too loose?) */
174 while (off
< PHPSTR_LEN(buffer
) && (
175 PHPSTR_VAL(buffer
)[off
] == 0xa ||
176 PHPSTR_VAL(buffer
)[off
] == 0xd)) {
180 phpstr_cut(PHPSTR(buffer
), 0, off
);
183 /* still data there? */
184 if (PHPSTR_LEN(buffer
)) {
188 /* we need eol, so we can be sure we have all hex digits */
189 phpstr_fix(PHPSTR(buffer
));
190 if ((eolstr
= http_locate_eol(PHPSTR_VAL(buffer
), &eollen
))) {
193 /* read in chunk size */
194 buffer
->hexlen
= strtoul(PHPSTR_VAL(buffer
), &stop
, 16);
196 /* if strtoul() stops at the beginning of the buffered data
197 there's domething oddly wrong, i.e. bad input */
198 if (stop
== PHPSTR_VAL(buffer
)) {
199 return PSFS_ERR_FATAL
;
202 /* cut out <chunk size hex><chunk extension><eol> */
203 phpstr_cut(PHPSTR(buffer
), 0, eolstr
+ eollen
- PHPSTR_VAL(buffer
));
204 /* buffer->hexlen is 0 now or contains the size of the next chunk */
207 /* we have not enough data buffered to read in chunk size */
215 /* flush before close, but only if we are already waiting for more data */
216 if (flags
== PSFS_FLAG_FLUSH_CLOSE
&& buffer
->hexlen
&& PHPSTR_LEN(buffer
)) {
218 NEW_BUCKET(PHPSTR_VAL(buffer
), PHPSTR_LEN(buffer
));
219 phpstr_reset(PHPSTR(buffer
));
223 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
226 static HTTP_FILTER_DESTRUCTOR(chunked_decode
)
228 HTTP_FILTER_BUFFER(chunked_decode
) *b
= (HTTP_FILTER_BUFFER(chunked_decode
) *) (this->abstract
);
230 phpstr_dtor(PHPSTR(b
));
231 pefree(b
, this->is_persistent
);
234 static HTTP_FILTER_FUNCTION(chunked_encode
)
237 php_stream_bucket
*ptr
, *nxt
;
239 if (bytes_consumed
) {
243 /* new data available? */
244 if (buckets_in
->head
) {
250 /* fetch available bucket data */
251 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
253 if (bytes_consumed
) {
254 *bytes_consumed
+= ptr
->buflen
;
257 phpstr_appendf(&buf
, "%x" HTTP_CRLF
, ptr
->buflen
);
258 phpstr_append(&buf
, ptr
->buf
, ptr
->buflen
);
259 phpstr_appends(&buf
, HTTP_CRLF
);
262 NEW_BUCKET(PHPSTR_VAL(&buf
), PHPSTR_LEN(&buf
));
266 php_stream_bucket_unlink(ptr TSRMLS_CC
);
267 php_stream_bucket_delref(ptr TSRMLS_CC
);
274 /* terminate with "0" */
275 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
277 NEW_BUCKET("0" HTTP_CRLF
, lenof("0" HTTP_CRLF
));
280 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
283 static HTTP_FILTER_OPS(chunked_decode
) = {
284 HTTP_FILTER_FUNC(chunked_decode
),
285 HTTP_FILTER_DTOR(chunked_decode
),
286 "http.chunked_decode"
289 static HTTP_FILTER_OPS(chunked_encode
) = {
290 HTTP_FILTER_FUNC(chunked_encode
),
292 "http.chunked_encode"
295 #ifdef HTTP_HAVE_ZLIB
296 static HTTP_FILTER_FUNCTION(gzip
)
299 php_stream_bucket
*ptr
, *nxt
;
300 HTTP_FILTER_BUFFER(gzip
) *buffer
= (HTTP_FILTER_BUFFER(gzip
) *) this->abstract
;
302 if (bytes_consumed
) {
308 char *encoded
= NULL
;
309 size_t encoded_len
= 0;
312 http_encoding_stream_init(&buffer
->stream
, buffer
->flags
, -1, &encoded
, &encoded_len
);
316 NEW_BUCKET(encoded
, encoded_len
);
317 pefree(encoded
, this->is_persistent
);
321 /* new data available? */
322 if (buckets_in
->head
) {
324 /* fetch available bucket data */
325 for (ptr
= buckets_in
->head
; ptr
; ptr
= nxt
) {
326 char *encoded
= NULL
;
327 size_t encoded_len
= 0;
330 if (bytes_consumed
) {
331 *bytes_consumed
+= ptr
->buflen
;
334 /* this is actually flushing implicitly */
335 http_encoding_stream_update(&buffer
->stream
, ptr
->buf
, ptr
->buflen
, &encoded
, &encoded_len
);
338 NEW_BUCKET(encoded
, encoded_len
);
339 pefree(encoded
, this->is_persistent
);
342 php_stream_bucket_unlink(ptr TSRMLS_CC
);
343 php_stream_bucket_delref(ptr TSRMLS_CC
);
348 if (flags
== PSFS_FLAG_FLUSH_CLOSE
) {
349 char *encoded
= NULL
;
350 size_t encoded_len
= 0;
352 http_encoding_stream_finish(&buffer
->stream
, &encoded
, &encoded_len
);
355 NEW_BUCKET(encoded
, encoded_len
);
359 return out_avail
? PSFS_PASS_ON
: PSFS_FEED_ME
;
362 static HTTP_FILTER_DESTRUCTOR(gzip
)
364 HTTP_FILTER_BUFFER(gzip
) *buffer
= (HTTP_FILTER_BUFFER(gzip
) *) this->abstract
;
366 pefree(buffer
, this->is_persistent
);
369 static HTTP_FILTER_OPS(gzencode
) = {
370 HTTP_FILTER_FUNC(gzip
),
371 HTTP_FILTER_DTOR(gzip
),
375 static HTTP_FILTER_OPS(deflate
) = {
376 HTTP_FILTER_FUNC(gzip
),
377 HTTP_FILTER_DTOR(gzip
),
380 #endif /* HTTP_HAVE_ZLIB */
382 static php_stream_filter
*http_filter_create(const char *name
, zval
*params
, int p TSRMLS_DC
)
384 php_stream_filter
*f
= NULL
;
386 if (!strcasecmp(name
, "http.chunked_decode")) {
387 HTTP_FILTER_BUFFER(chunked_decode
) *b
= NULL
;
389 if ((b
= pecalloc(1, sizeof(HTTP_FILTER_BUFFER(chunked_decode
)), p
))) {
390 phpstr_init_ex(PHPSTR(b
), 4096, p
? PHPSTR_INIT_PERSISTENT
: 0);
391 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_decode
), b
, p
))) {
397 if (!strcasecmp(name
, "http.chunked_encode")) {
398 f
= php_stream_filter_alloc(&HTTP_FILTER_OP(chunked_encode
), NULL
, p
);
399 #ifdef HTTP_HAVE_ZLIB
402 if (!strcasecmp(name
, "http.gzencode")) {
403 HTTP_FILTER_BUFFER(gzip
) *b
= NULL
;
405 if ((b
= pecalloc(1, sizeof(HTTP_FILTER_BUFFER(gzip
)), p
))) {
406 b
->flags
= HTTP_ENCODING_STREAM_GZIP_HEADER
;
408 b
->flags
|= HTTP_ENCODING_STREAM_PERSISTENT
;
410 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(gzencode
), b
, p
))) {
416 if (!strcasecmp(name
, "http.deflate")) {
417 HTTP_FILTER_BUFFER(gzip
) *b
= NULL
;
419 if ((b
= pecalloc(1, sizeof(HTTP_FILTER_BUFFER(gzip
)), p
))) {
421 b
->flags
|= HTTP_ENCODING_STREAM_PERSISTENT
;
423 if (!(f
= php_stream_filter_alloc(&HTTP_FILTER_OP(deflate
), b
, p
))) {
427 #endif /* HTTP_HAVE_ZLIB */
433 php_stream_filter_factory http_filter_factory
= {
437 #endif /* ZEND_ENGINE_2 */
444 * vim600: noet sw=4 ts=4 fdm=marker
445 * vim<600: noet sw=4 ts=4