- Fixed build on php-trunk
[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 #if PHP_VERSION_ID < 50399
111 ALLOC_HASHTABLE(OBJ_PROP(o));
112 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
113 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
114 #else
115 object_properties_init(&o->zo, ce);
116 #endif
117
118 ov.handle = putObject(http_deflatestream_object, o);
119 ov.handlers = &http_deflatestream_object_handlers;
120
121 return ov;
122 }
123
124 zend_object_value _http_deflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
125 {
126 http_encoding_stream *s;
127 zend_object_value new_ov;
128 http_deflatestream_object *new_obj = NULL;
129 getObject(http_deflatestream_object, old_obj);
130
131 s = ecalloc(1, sizeof(http_encoding_stream));
132 s->flags = old_obj->stream->flags;
133 deflateCopy(&s->stream, &old_obj->stream->stream);
134 s->stream.opaque = phpstr_dup(s->stream.opaque);
135
136 new_ov = http_deflatestream_object_new_ex(old_obj->zo.ce, s, &new_obj);
137 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
138
139 return new_ov;
140 }
141
142 void _http_deflatestream_object_free(zend_object *object TSRMLS_DC)
143 {
144 http_deflatestream_object *o = (http_deflatestream_object *) object;
145
146 if (o->stream) {
147 http_encoding_deflate_stream_free(&o->stream);
148 }
149 freeObject(o);
150 }
151
152 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
153 Creates a new HttpDeflateStream object instance. */
154 PHP_METHOD(HttpDeflateStream, __construct)
155 {
156 long flags = 0;
157
158 SET_EH_THROW_HTTP();
159 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
160 getObject(http_deflatestream_object, obj);
161
162 if (!obj->stream) {
163 obj->stream = http_encoding_deflate_stream_init(NULL, flags & 0x0fffffff);
164 } else {
165 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpDeflateStream cannot be initialized twice");
166 }
167 }
168 SET_EH_NORMAL();
169 }
170 /* }}} */
171
172 /* {{{ proto HttpDeflateStream HttpDeflateStream::factory([int flags[, string class = "HttpDeflateStream"]])
173 Creates a new HttpDeflateStream object instance. */
174 PHP_METHOD(HttpDeflateStream, factory)
175 {
176 long flags = 0;
177 char *cn = NULL;
178 int cl = 0;
179
180 SET_EH_THROW_HTTP();
181 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &flags, &cn, &cl)) {
182 zend_object_value ov;
183 http_encoding_stream *s = http_encoding_deflate_stream_init(NULL, flags & 0x0fffffff);
184
185 if (SUCCESS == http_object_new(&ov, cn, cl, _http_deflatestream_object_new_ex, http_deflatestream_object_ce, s, NULL)) {
186 RETVAL_OBJVAL(ov, 0);
187 }
188 }
189 SET_EH_NORMAL();
190 }
191 /* }}} */
192
193 /* {{{ proto string HttpDeflateStream::update(string data)
194 Passes more data through the deflate stream. */
195 PHP_METHOD(HttpDeflateStream, update)
196 {
197 int data_len;
198 size_t encoded_len = 0;
199 char *data, *encoded = NULL;
200 getObject(http_deflatestream_object, obj);
201
202 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
203 RETURN_FALSE;
204 }
205
206 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
207 RETURN_FALSE;
208 }
209
210 if (SUCCESS == http_encoding_deflate_stream_update(obj->stream, data, data_len, &encoded, &encoded_len)) {
211 RETURN_STRINGL(encoded, encoded_len, 0);
212 } else {
213 RETURN_FALSE;
214 }
215 }
216 /* }}} */
217
218 /* {{{ proto string HttpDeflateStream::flush([string data])
219 Flushes the deflate stream. */
220 PHP_METHOD(HttpDeflateStream, flush)
221 {
222 int data_len = 0;
223 size_t updated_len = 0, encoded_len = 0;
224 char *updated = NULL, *encoded = NULL, *data = NULL;
225 getObject(http_deflatestream_object, obj);
226
227 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
228 RETURN_FALSE;
229 }
230
231 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
232 RETURN_FALSE;
233 }
234
235 if (data_len) {
236 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
237 RETURN_FALSE;
238 }
239 }
240
241 if (SUCCESS == http_encoding_deflate_stream_flush(obj->stream, &encoded, &encoded_len)) {
242 if (updated_len) {
243 updated = erealloc(updated, updated_len + encoded_len + 1);
244 updated[updated_len + encoded_len] = '\0';
245 memcpy(updated + updated_len, encoded, encoded_len);
246 STR_FREE(encoded);
247 updated_len += encoded_len;
248 RETURN_STRINGL(updated, updated_len, 0);
249 } else if (encoded) {
250 RETVAL_STRINGL(encoded, encoded_len, 0);
251 } else {
252 RETVAL_NULL();
253 }
254 } else {
255 RETVAL_FALSE;
256 }
257 STR_FREE(updated);
258 }
259 /* }}} */
260
261 /* {{{ proto string HttpDeflateStream::finish([string data])
262 Finalizes the deflate stream. The deflate stream can be reused after finalizing. */
263 PHP_METHOD(HttpDeflateStream, finish)
264 {
265 int data_len = 0;
266 size_t updated_len = 0, encoded_len = 0;
267 char *updated = NULL, *encoded = NULL, *data = NULL;
268 getObject(http_deflatestream_object, obj);
269
270 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
271 RETURN_FALSE;
272 }
273
274 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
275 RETURN_FALSE;
276 }
277
278 if (data_len) {
279 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
280 RETURN_FALSE;
281 }
282 }
283
284 if (SUCCESS == http_encoding_deflate_stream_finish(obj->stream, &encoded, &encoded_len)) {
285 if (updated_len) {
286 updated = erealloc(updated, updated_len + encoded_len + 1);
287 updated[updated_len + encoded_len] = '\0';
288 memcpy(updated + updated_len, encoded, encoded_len);
289 STR_FREE(encoded);
290 updated_len += encoded_len;
291 RETVAL_STRINGL(updated, updated_len, 0);
292 } else {
293 STR_FREE(updated);
294 RETVAL_STRINGL(encoded, encoded_len, 0);
295 }
296 } else {
297 STR_FREE(updated);
298 RETVAL_FALSE;
299 }
300
301 http_encoding_deflate_stream_dtor(obj->stream);
302 http_encoding_deflate_stream_init(obj->stream, obj->stream->flags);
303 }
304 /* }}} */
305
306 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
307
308 /*
309 * Local variables:
310 * tab-width: 4
311 * c-basic-offset: 4
312 * End:
313 * vim600: noet sw=4 ts=4 fdm=marker
314 * vim<600: noet sw=4 ts=4
315 */
316