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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_ZLIB
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_ZLIB)
20 #include "php_http_api.h"
21 #include "php_http_encoding_api.h"
22 #include "php_http_exception_object.h"
23 #include "php_http_inflatestream_object.h"
25 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpInflateStream, method, 0, req_args)
26 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpInflateStream, method, 0)
27 #define HTTP_INFLATE_ME(method, visibility) PHP_ME(HttpInflateStream, method, HTTP_ARGS(HttpInflateStream, method), visibility)
29 HTTP_BEGIN_ARGS(__construct
, 0)
30 HTTP_ARG_VAL(flags
, 0)
33 HTTP_BEGIN_ARGS(factory
, 0)
34 HTTP_ARG_VAL(flags
, 0)
35 HTTP_ARG_VAL(class_name
, 0)
38 HTTP_BEGIN_ARGS(update
, 1)
42 HTTP_BEGIN_ARGS(flush
, 0)
46 HTTP_BEGIN_ARGS(finish
, 0)
50 #define THIS_CE http_inflatestream_object_ce
51 zend_class_entry
*http_inflatestream_object_ce
;
52 zend_function_entry http_inflatestream_object_fe
[] = {
53 HTTP_INFLATE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
54 HTTP_INFLATE_ME(update
, ZEND_ACC_PUBLIC
)
55 HTTP_INFLATE_ME(flush
, ZEND_ACC_PUBLIC
)
56 HTTP_INFLATE_ME(finish
, ZEND_ACC_PUBLIC
)
58 HTTP_INFLATE_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
62 static zend_object_handlers http_inflatestream_object_handlers
;
64 PHP_MINIT_FUNCTION(http_inflatestream_object
)
66 HTTP_REGISTER_CLASS_EX(HttpInflateStream
, http_inflatestream_object
, NULL
, 0);
67 http_inflatestream_object_handlers
.clone_obj
= _http_inflatestream_object_clone_obj
;
70 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("FLUSH_NONE")-1, HTTP_ENCODING_STREAM_FLUSH_NONE TSRMLS_CC
);
71 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("FLUSH_SYNC")-1, HTTP_ENCODING_STREAM_FLUSH_SYNC TSRMLS_CC
);
72 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("FLUSH_FULL")-1, HTTP_ENCODING_STREAM_FLUSH_FULL TSRMLS_CC
);
78 zend_object_value
_http_inflatestream_object_new(zend_class_entry
*ce TSRMLS_DC
)
80 return http_inflatestream_object_new_ex(ce
, NULL
, NULL
);
83 zend_object_value
_http_inflatestream_object_new_ex(zend_class_entry
*ce
, http_encoding_stream
*s
, http_inflatestream_object
**ptr TSRMLS_DC
)
86 http_inflatestream_object
*o
;
88 o
= ecalloc(1, sizeof(http_inflatestream_object
));
99 #if PHP_VERSION_ID < 50399
100 ALLOC_HASHTABLE(OBJ_PROP(o
));
101 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
102 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
104 object_properties_init(&o
->zo
, ce
);
107 ov
.handle
= putObject(http_inflatestream_object
, o
);
108 ov
.handlers
= &http_inflatestream_object_handlers
;
113 zend_object_value
_http_inflatestream_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
115 http_encoding_stream
*s
;
116 zend_object_value new_ov
;
117 http_inflatestream_object
*new_obj
= NULL
;
118 getObject(http_inflatestream_object
, old_obj
);
120 s
= ecalloc(1, sizeof(http_encoding_stream
));
121 s
->flags
= old_obj
->stream
->flags
;
122 inflateCopy(&s
->stream
, &old_obj
->stream
->stream
);
123 s
->stream
.opaque
= phpstr_dup(s
->stream
.opaque
);
125 new_ov
= http_inflatestream_object_new_ex(old_obj
->zo
.ce
, s
, &new_obj
);
126 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
131 void _http_inflatestream_object_free(zend_object
*object TSRMLS_DC
)
133 http_inflatestream_object
*o
= (http_inflatestream_object
*) object
;
136 http_encoding_inflate_stream_free(&o
->stream
);
141 /* {{{ proto void HttpInflateStream::__construct([int flags = 0])
142 Creates a new HttpInflateStream object instance. */
143 PHP_METHOD(HttpInflateStream
, __construct
)
148 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &flags
)) {
149 getObject(http_inflatestream_object
, obj
);
152 obj
->stream
= http_encoding_inflate_stream_init(NULL
, flags
& 0x0fffffff);
154 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "HttpInflateStream cannot be initialized twice");
161 /* {{{ proto HttpInflateStream HttpInflateStream::factory([int flags[, string class = "HttpInflateStream"]])
162 Creates a new HttpInflateStream object instance. */
163 PHP_METHOD(HttpInflateStream
, factory
)
170 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|ls", &flags
, &cn
, &cl
)) {
171 zend_object_value ov
;
172 http_encoding_stream
*s
= http_encoding_inflate_stream_init(NULL
, flags
& 0x0fffffff);
174 if (SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_inflatestream_object_new_ex
, http_inflatestream_object_ce
, s
, NULL
)) {
175 RETVAL_OBJVAL(ov
, 0);
182 /* {{{ proto string HttpInflateStream::update(string data)
183 Passes more data through the inflate stream. */
184 PHP_METHOD(HttpInflateStream
, update
)
187 size_t decoded_len
= 0;
188 char *data
, *decoded
= NULL
;
189 getObject(http_inflatestream_object
, obj
);
191 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
196 RETURN_STRING("", 1);
199 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
203 if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
204 RETURN_STRINGL(decoded
, decoded_len
, 0);
211 /* {{{ proto string HttpInflateStream::flush([string data])
212 Flush the inflate stream. */
213 PHP_METHOD(HttpInflateStream
, flush
)
216 size_t decoded_len
= 0;
217 char *decoded
= NULL
, *data
= NULL
;
218 getObject(http_inflatestream_object
, obj
);
220 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
224 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
228 /* flushing the inflate stream is a no-op */
230 RETURN_STRINGL("", 0, 1);
231 } else if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
232 RETURN_STRINGL(decoded
, decoded_len
, 0);
239 /* {{{ proto string HttpInflateStream::finish([string data])
240 Finalizes the inflate stream. The inflate stream can be reused after finalizing. */
241 PHP_METHOD(HttpInflateStream
, finish
)
244 size_t updated_len
= 0, decoded_len
= 0;
245 char *updated
= NULL
, *decoded
= NULL
, *data
= NULL
;
246 getObject(http_inflatestream_object
, obj
);
248 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
252 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
257 if (SUCCESS
!= http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
262 if (SUCCESS
== http_encoding_inflate_stream_finish(obj
->stream
, &decoded
, &decoded_len
)) {
264 updated
= erealloc(updated
, updated_len
+ decoded_len
+ 1);
265 updated
[updated_len
+ decoded_len
] = '\0';
266 memcpy(updated
+ updated_len
, decoded
, decoded_len
);
268 updated_len
+= decoded_len
;
269 RETVAL_STRINGL(updated
, updated_len
, 0);
270 } else if (decoded
) {
272 RETVAL_STRINGL(decoded
, decoded_len
, 0);
281 http_encoding_inflate_stream_dtor(obj
->stream
);
282 http_encoding_inflate_stream_init(obj
->stream
, obj
->stream
->flags
);
286 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
293 * vim600: noet sw=4 ts=4 fdm=marker
294 * vim<600: noet sw=4 ts=4