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_deflatestream_object.h"
25 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpDeflateStream, method, 0, req_args)
26 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpDeflateStream, method, 0)
27 #define HTTP_DEFLATE_ME(method, visibility) PHP_ME(HttpDeflateStream, method, HTTP_ARGS(HttpDeflateStream, 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_deflatestream_object_ce
51 zend_class_entry
*http_deflatestream_object_ce
;
52 zend_function_entry http_deflatestream_object_fe
[] = {
53 HTTP_DEFLATE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
54 HTTP_DEFLATE_ME(update
, ZEND_ACC_PUBLIC
)
55 HTTP_DEFLATE_ME(flush
, ZEND_ACC_PUBLIC
)
56 HTTP_DEFLATE_ME(finish
, ZEND_ACC_PUBLIC
)
58 HTTP_DEFLATE_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
62 static zend_object_handlers http_deflatestream_object_handlers
;
64 PHP_MINIT_FUNCTION(http_deflatestream_object
)
66 HTTP_REGISTER_CLASS_EX(HttpDeflateStream
, http_deflatestream_object
, NULL
, 0);
67 http_deflatestream_object_handlers
.clone_obj
= _http_deflatestream_object_clone_obj
;
70 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_GZIP")-1, HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC
);
71 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_ZLIB")-1, HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC
);
72 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("TYPE_RAW")-1, HTTP_DEFLATE_TYPE_RAW TSRMLS_CC
);
73 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("LEVEL_DEF")-1, HTTP_DEFLATE_LEVEL_DEF TSRMLS_CC
);
74 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("LEVEL_MIN")-1, HTTP_DEFLATE_LEVEL_MIN TSRMLS_CC
);
75 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("LEVEL_MAX")-1, HTTP_DEFLATE_LEVEL_MAX TSRMLS_CC
);
76 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("STRATEGY_DEF")-1, HTTP_DEFLATE_STRATEGY_DEF TSRMLS_CC
);
77 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("STRATEGY_FILT")-1, HTTP_DEFLATE_STRATEGY_FILT TSRMLS_CC
);
78 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("STRATEGY_HUFF")-1, HTTP_DEFLATE_STRATEGY_HUFF TSRMLS_CC
);
79 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("STRATEGY_RLE")-1, HTTP_DEFLATE_STRATEGY_RLE TSRMLS_CC
);
80 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("STRATEGY_FIXED")-1, HTTP_DEFLATE_STRATEGY_FIXED TSRMLS_CC
);
81 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("FLUSH_NONE")-1, HTTP_ENCODING_STREAM_FLUSH_NONE TSRMLS_CC
);
82 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("FLUSH_SYNC")-1, HTTP_ENCODING_STREAM_FLUSH_SYNC TSRMLS_CC
);
83 zend_declare_class_constant_long(THIS_CE
, ZEND_STRS("FLUSH_FULL")-1, HTTP_ENCODING_STREAM_FLUSH_FULL TSRMLS_CC
);
89 zend_object_value
_http_deflatestream_object_new(zend_class_entry
*ce TSRMLS_DC
)
91 return http_deflatestream_object_new_ex(ce
, NULL
, NULL
);
94 zend_object_value
_http_deflatestream_object_new_ex(zend_class_entry
*ce
, http_encoding_stream
*s
, http_deflatestream_object
**ptr TSRMLS_DC
)
97 http_deflatestream_object
*o
;
99 o
= ecalloc(1, sizeof(http_deflatestream_object
));
110 #ifdef ZEND_ENGINE_2_4
111 zend_object_std_init(o
, ce TSRMLS_CC
);
112 object_properties_init(o
, ce
);
114 ALLOC_HASHTABLE(OBJ_PROP(o
));
115 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
116 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
119 ov
.handle
= putObject(http_deflatestream_object
, o
);
120 ov
.handlers
= &http_deflatestream_object_handlers
;
125 zend_object_value
_http_deflatestream_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
127 http_encoding_stream
*s
;
128 zend_object_value new_ov
;
129 http_deflatestream_object
*new_obj
= NULL
;
130 getObject(http_deflatestream_object
, old_obj
);
132 s
= ecalloc(1, sizeof(http_encoding_stream
));
133 s
->flags
= old_obj
->stream
->flags
;
134 deflateCopy(&s
->stream
, &old_obj
->stream
->stream
);
135 s
->stream
.opaque
= phpstr_dup(s
->stream
.opaque
);
137 new_ov
= http_deflatestream_object_new_ex(old_obj
->zo
.ce
, s
, &new_obj
);
138 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
143 void _http_deflatestream_object_free(zend_object
*object TSRMLS_DC
)
145 http_deflatestream_object
*o
= (http_deflatestream_object
*) object
;
148 http_encoding_deflate_stream_free(&o
->stream
);
153 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
154 Creates a new HttpDeflateStream object instance. */
155 PHP_METHOD(HttpDeflateStream
, __construct
)
160 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &flags
)) {
161 getObject(http_deflatestream_object
, obj
);
164 obj
->stream
= http_encoding_deflate_stream_init(NULL
, flags
& 0x0fffffff);
166 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "HttpDeflateStream cannot be initialized twice");
173 /* {{{ proto HttpDeflateStream HttpDeflateStream::factory([int flags[, string class = "HttpDeflateStream"]])
174 Creates a new HttpDeflateStream object instance. */
175 PHP_METHOD(HttpDeflateStream
, factory
)
182 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|ls", &flags
, &cn
, &cl
)) {
183 zend_object_value ov
;
184 http_encoding_stream
*s
= http_encoding_deflate_stream_init(NULL
, flags
& 0x0fffffff);
186 if (SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_deflatestream_object_new_ex
, http_deflatestream_object_ce
, s
, NULL
)) {
187 RETVAL_OBJVAL(ov
, 0);
194 /* {{{ proto string HttpDeflateStream::update(string data)
195 Passes more data through the deflate stream. */
196 PHP_METHOD(HttpDeflateStream
, update
)
199 size_t encoded_len
= 0;
200 char *data
, *encoded
= NULL
;
201 getObject(http_deflatestream_object
, obj
);
203 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
207 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
211 if (SUCCESS
== http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &encoded
, &encoded_len
)) {
212 RETURN_STRINGL(encoded
, encoded_len
, 0);
219 /* {{{ proto string HttpDeflateStream::flush([string data])
220 Flushes the deflate stream. */
221 PHP_METHOD(HttpDeflateStream
, flush
)
224 size_t updated_len
= 0, encoded_len
= 0;
225 char *updated
= NULL
, *encoded
= NULL
, *data
= NULL
;
226 getObject(http_deflatestream_object
, obj
);
228 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
232 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
237 if (SUCCESS
!= http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
242 if (SUCCESS
== http_encoding_deflate_stream_flush(obj
->stream
, &encoded
, &encoded_len
)) {
244 updated
= erealloc(updated
, updated_len
+ encoded_len
+ 1);
245 updated
[updated_len
+ encoded_len
] = '\0';
246 memcpy(updated
+ updated_len
, encoded
, encoded_len
);
248 updated_len
+= encoded_len
;
249 RETURN_STRINGL(updated
, updated_len
, 0);
250 } else if (encoded
) {
251 RETVAL_STRINGL(encoded
, encoded_len
, 0);
262 /* {{{ proto string HttpDeflateStream::finish([string data])
263 Finalizes the deflate stream. The deflate stream can be reused after finalizing. */
264 PHP_METHOD(HttpDeflateStream
, finish
)
267 size_t updated_len
= 0, encoded_len
= 0;
268 char *updated
= NULL
, *encoded
= NULL
, *data
= NULL
;
269 getObject(http_deflatestream_object
, obj
);
271 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
275 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
280 if (SUCCESS
!= http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
285 if (SUCCESS
== http_encoding_deflate_stream_finish(obj
->stream
, &encoded
, &encoded_len
)) {
287 updated
= erealloc(updated
, updated_len
+ encoded_len
+ 1);
288 updated
[updated_len
+ encoded_len
] = '\0';
289 memcpy(updated
+ updated_len
, encoded
, encoded_len
);
291 updated_len
+= encoded_len
;
292 RETVAL_STRINGL(updated
, updated_len
, 0);
295 RETVAL_STRINGL(encoded
, encoded_len
, 0);
302 http_encoding_deflate_stream_dtor(obj
->stream
);
303 http_encoding_deflate_stream_init(obj
->stream
, obj
->stream
->flags
);
307 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
314 * vim600: noet sw=4 ts=4 fdm=marker
315 * vim<600: noet sw=4 ts=4