tag 1.7.4
[m6w6/ext-http] / http_deflatestream_object.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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_ZLIB
16 #include "php_http.h"
17
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_ZLIB)
19
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"
24
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)
28
29 HTTP_BEGIN_ARGS(__construct, 0)
30 HTTP_ARG_VAL(flags, 0)
31 HTTP_END_ARGS;
32
33 HTTP_BEGIN_ARGS(factory, 0)
34 HTTP_ARG_VAL(flags, 0)
35 HTTP_ARG_VAL(class_name, 0)
36 HTTP_END_ARGS;
37
38 HTTP_BEGIN_ARGS(update, 1)
39 HTTP_ARG_VAL(data, 0)
40 HTTP_END_ARGS;
41
42 HTTP_BEGIN_ARGS(flush, 0)
43 HTTP_ARG_VAL(data, 0)
44 HTTP_END_ARGS;
45
46 HTTP_BEGIN_ARGS(finish, 0)
47 HTTP_ARG_VAL(data, 0)
48 HTTP_END_ARGS;
49
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)
57
58 HTTP_DEFLATE_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
59
60 EMPTY_FUNCTION_ENTRY
61 };
62 static zend_object_handlers http_deflatestream_object_handlers;
63
64 PHP_MINIT_FUNCTION(http_deflatestream_object)
65 {
66 HTTP_REGISTER_CLASS_EX(HttpDeflateStream, http_deflatestream_object, NULL, 0);
67 http_deflatestream_object_handlers.clone_obj = _http_deflatestream_object_clone_obj;
68
69 #ifndef WONKY
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);
84 #endif
85
86 return SUCCESS;
87 }
88
89 zend_object_value _http_deflatestream_object_new(zend_class_entry *ce TSRMLS_DC)
90 {
91 return http_deflatestream_object_new_ex(ce, NULL, NULL);
92 }
93
94 zend_object_value _http_deflatestream_object_new_ex(zend_class_entry *ce, http_encoding_stream *s, http_deflatestream_object **ptr TSRMLS_DC)
95 {
96 zend_object_value ov;
97 http_deflatestream_object *o;
98
99 o = ecalloc(1, sizeof(http_deflatestream_object));
100 o->zo.ce = ce;
101
102 if (ptr) {
103 *ptr = o;
104 }
105
106 if (s) {
107 o->stream = s;
108 }
109
110 #ifdef ZEND_ENGINE_2_4
111 zend_object_std_init(o, ce TSRMLS_CC);
112 object_properties_init(o, ce);
113 #else
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 *));
117 #endif
118
119 ov.handle = putObject(http_deflatestream_object, o);
120 ov.handlers = &http_deflatestream_object_handlers;
121
122 return ov;
123 }
124
125 zend_object_value _http_deflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
126 {
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);
131
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);
136
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);
139
140 return new_ov;
141 }
142
143 void _http_deflatestream_object_free(zend_object *object TSRMLS_DC)
144 {
145 http_deflatestream_object *o = (http_deflatestream_object *) object;
146
147 if (o->stream) {
148 http_encoding_deflate_stream_free(&o->stream);
149 }
150 freeObject(o);
151 }
152
153 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
154 Creates a new HttpDeflateStream object instance. */
155 PHP_METHOD(HttpDeflateStream, __construct)
156 {
157 long flags = 0;
158
159 SET_EH_THROW_HTTP();
160 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
161 getObject(http_deflatestream_object, obj);
162
163 if (!obj->stream) {
164 obj->stream = http_encoding_deflate_stream_init(NULL, flags & 0x0fffffff);
165 } else {
166 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpDeflateStream cannot be initialized twice");
167 }
168 }
169 SET_EH_NORMAL();
170 }
171 /* }}} */
172
173 /* {{{ proto HttpDeflateStream HttpDeflateStream::factory([int flags[, string class = "HttpDeflateStream"]])
174 Creates a new HttpDeflateStream object instance. */
175 PHP_METHOD(HttpDeflateStream, factory)
176 {
177 long flags = 0;
178 char *cn = NULL;
179 int cl = 0;
180
181 SET_EH_THROW_HTTP();
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);
185
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);
188 }
189 }
190 SET_EH_NORMAL();
191 }
192 /* }}} */
193
194 /* {{{ proto string HttpDeflateStream::update(string data)
195 Passes more data through the deflate stream. */
196 PHP_METHOD(HttpDeflateStream, update)
197 {
198 int data_len;
199 size_t encoded_len = 0;
200 char *data, *encoded = NULL;
201 getObject(http_deflatestream_object, obj);
202
203 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
204 RETURN_FALSE;
205 }
206
207 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
208 RETURN_FALSE;
209 }
210
211 if (SUCCESS == http_encoding_deflate_stream_update(obj->stream, data, data_len, &encoded, &encoded_len)) {
212 RETURN_STRINGL(encoded, encoded_len, 0);
213 } else {
214 RETURN_FALSE;
215 }
216 }
217 /* }}} */
218
219 /* {{{ proto string HttpDeflateStream::flush([string data])
220 Flushes the deflate stream. */
221 PHP_METHOD(HttpDeflateStream, flush)
222 {
223 int data_len = 0;
224 size_t updated_len = 0, encoded_len = 0;
225 char *updated = NULL, *encoded = NULL, *data = NULL;
226 getObject(http_deflatestream_object, obj);
227
228 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
229 RETURN_FALSE;
230 }
231
232 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
233 RETURN_FALSE;
234 }
235
236 if (data_len) {
237 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
238 RETURN_FALSE;
239 }
240 }
241
242 if (SUCCESS == http_encoding_deflate_stream_flush(obj->stream, &encoded, &encoded_len)) {
243 if (updated_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);
247 STR_FREE(encoded);
248 updated_len += encoded_len;
249 RETURN_STRINGL(updated, updated_len, 0);
250 } else if (encoded) {
251 RETVAL_STRINGL(encoded, encoded_len, 0);
252 } else {
253 RETVAL_NULL();
254 }
255 } else {
256 RETVAL_FALSE;
257 }
258 STR_FREE(updated);
259 }
260 /* }}} */
261
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)
265 {
266 int data_len = 0;
267 size_t updated_len = 0, encoded_len = 0;
268 char *updated = NULL, *encoded = NULL, *data = NULL;
269 getObject(http_deflatestream_object, obj);
270
271 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
272 RETURN_FALSE;
273 }
274
275 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
276 RETURN_FALSE;
277 }
278
279 if (data_len) {
280 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
281 RETURN_FALSE;
282 }
283 }
284
285 if (SUCCESS == http_encoding_deflate_stream_finish(obj->stream, &encoded, &encoded_len)) {
286 if (updated_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);
290 STR_FREE(encoded);
291 updated_len += encoded_len;
292 RETVAL_STRINGL(updated, updated_len, 0);
293 } else {
294 STR_FREE(updated);
295 RETVAL_STRINGL(encoded, encoded_len, 0);
296 }
297 } else {
298 STR_FREE(updated);
299 RETVAL_FALSE;
300 }
301
302 http_encoding_deflate_stream_dtor(obj->stream);
303 http_encoding_deflate_stream_init(obj->stream, obj->stream->flags);
304 }
305 /* }}} */
306
307 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
308
309 /*
310 * Local variables:
311 * tab-width: 4
312 * c-basic-offset: 4
313 * End:
314 * vim600: noet sw=4 ts=4 fdm=marker
315 * vim<600: noet sw=4 ts=4
316 */
317