f4e55325ef637053207c446f15b9fb0573002751
[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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19
20 #define HTTP_WANT_ZLIB
21 #include "php_http.h"
22
23 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_ZLIB)
24
25 #include "php_http_api.h"
26 #include "php_http_encoding_api.h"
27 #include "php_http_exception_object.h"
28 #include "php_http_deflatestream_object.h"
29
30 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpDeflateStream, method, ret_ref, req_args)
31 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpDeflateStream, method, ret_ref)
32 #define HTTP_DEFLATE_ME(method, visibility) PHP_ME(HttpDeflateStream, method, HTTP_ARGS(HttpDeflateStream, method), visibility)
33
34 HTTP_BEGIN_ARGS(__construct, 0, 0)
35 HTTP_ARG_VAL(flags, 0)
36 HTTP_END_ARGS;
37
38 HTTP_BEGIN_ARGS(update, 0, 1)
39 HTTP_ARG_VAL(data, 0)
40 HTTP_END_ARGS;
41
42 HTTP_BEGIN_ARGS(flush, 0, 0)
43 HTTP_ARG_VAL(data, 0)
44 HTTP_END_ARGS;
45
46 HTTP_BEGIN_ARGS(finish, 0, 0)
47 HTTP_ARG_VAL(data, 0)
48 HTTP_END_ARGS;
49
50 #define http_deflatestream_object_declare_default_properties() _http_deflatestream_object_declare_default_properties(TSRMLS_C)
51 static inline void _http_deflatestream_object_declare_default_properties(TSRMLS_D);
52
53 zend_class_entry *http_deflatestream_object_ce;
54 zend_function_entry http_deflatestream_object_fe[] = {
55 HTTP_DEFLATE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
56 HTTP_DEFLATE_ME(update, ZEND_ACC_PUBLIC)
57 HTTP_DEFLATE_ME(flush, ZEND_ACC_PUBLIC)
58 HTTP_DEFLATE_ME(finish, ZEND_ACC_PUBLIC)
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 return SUCCESS;
69 }
70
71 zend_object_value _http_deflatestream_object_new(zend_class_entry *ce TSRMLS_DC)
72 {
73 return http_deflatestream_object_new_ex(ce, NULL, NULL);
74 }
75
76 zend_object_value _http_deflatestream_object_new_ex(zend_class_entry *ce, http_encoding_stream *s, http_deflatestream_object **ptr TSRMLS_DC)
77 {
78 zend_object_value ov;
79 http_deflatestream_object *o;
80
81 o = ecalloc(1, sizeof(http_deflatestream_object));
82 o->zo.ce = ce;
83
84 if (ptr) {
85 *ptr = o;
86 }
87
88 if (s) {
89 o->stream = s;
90 }
91
92 ALLOC_HASHTABLE(OBJ_PROP(o));
93 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
94
95 ov.handle = putObject(http_deflatestream_object, o);
96 ov.handlers = &http_deflatestream_object_handlers;
97
98 return ov;
99 }
100
101 zend_object_value _http_deflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
102 {
103 http_encoding_stream *s;
104 getObject(http_deflatestream_object, obj);
105
106 s = ecalloc(1, sizeof(http_encoding_stream));
107 s->flags = obj->stream->flags;
108 deflateCopy(&s->stream, &obj->stream->stream);
109 s->stream.opaque = phpstr_dup(s->stream.opaque);
110
111 return http_deflatestream_object_new_ex(Z_OBJCE_P(this_ptr), s, NULL);
112 }
113
114 static inline void _http_deflatestream_object_declare_default_properties(TSRMLS_D)
115 {
116 zend_class_entry *ce = http_deflatestream_object_ce;
117
118 #ifndef WONKY
119 DCL_CONST(long, "TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP);
120 DCL_CONST(long, "TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB);
121 DCL_CONST(long, "TYPE_RAW", HTTP_DEFLATE_TYPE_RAW);
122 DCL_CONST(long, "LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF);
123 DCL_CONST(long, "LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN);
124 DCL_CONST(long, "LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX);
125 #endif
126 }
127
128 void _http_deflatestream_object_free(zend_object *object TSRMLS_DC)
129 {
130 http_deflatestream_object *o = (http_deflatestream_object *) object;
131
132 if (OBJ_PROP(o)) {
133 zend_hash_destroy(OBJ_PROP(o));
134 FREE_HASHTABLE(OBJ_PROP(o));
135 }
136 if (o->stream) {
137 http_encoding_deflate_stream_free(&o->stream);
138 }
139 efree(o);
140 }
141
142 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
143 *
144 * Creates a new HttpDeflateStream object instance.
145 *
146 * Accepts an optional int parameter specifying how to initialize the deflate stream.
147 */
148 PHP_METHOD(HttpDeflateStream, __construct)
149 {
150 long flags = 0;
151
152 SET_EH_THROW_HTTP();
153 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
154 getObject(http_deflatestream_object, obj);
155
156 if (!obj->stream) {
157 obj->stream = http_encoding_deflate_stream_init(NULL, flags);
158 } else {
159 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpDeflateStream cannot be initialized twice");
160 }
161 }
162 SET_EH_NORMAL();
163 }
164 /* }}} */
165
166 /* {{{ proto string HttpDeflateStream::update(string data)
167 *
168 * Passes more data through the deflate stream.
169 *
170 * Expects a string parameter containing (a part of) the data to deflate.
171 *
172 * Returns deflated data on success or FALSE on failure.
173 */
174 PHP_METHOD(HttpDeflateStream, update)
175 {
176 int data_len;
177 size_t encoded_len = 0;
178 char *data, *encoded = NULL;
179 getObject(http_deflatestream_object, obj);
180
181 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
182 RETURN_FALSE;
183 }
184
185 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
186 RETURN_FALSE;
187 }
188
189 if (SUCCESS == http_encoding_deflate_stream_update(obj->stream, data, data_len, &encoded, &encoded_len)) {
190 RETURN_STRINGL(encoded, encoded_len, 0);
191 } else {
192 RETURN_FALSE;
193 }
194 }
195 /* }}} */
196
197 /* {{{ proto string HttpDeflateStream::flush([string data])
198 *
199 * Flushes the deflate stream.
200 *
201 * Returns some deflated data as string on success or FALSE on failure.
202 */
203 PHP_METHOD(HttpDeflateStream, flush)
204 {
205 int data_len = 0;
206 size_t updated_len = 0, encoded_len = 0;
207 char *updated = NULL, *encoded = NULL, *data = NULL;
208 getObject(http_deflatestream_object, obj);
209
210 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
211 RETURN_FALSE;
212 }
213
214 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
215 RETURN_FALSE;
216 }
217
218 if (data_len) {
219 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
220 RETURN_FALSE;
221 }
222 }
223
224 if (SUCCESS == http_encoding_deflate_stream_flush(obj->stream, &encoded, &encoded_len)) {
225 if (updated_len) {
226 updated = erealloc(updated, updated_len + encoded_len + 1);
227 updated[updated_len + encoded_len] = '\0';
228 memcpy(updated + updated_len, encoded, encoded_len);
229 STR_FREE(encoded);
230 updated_len += encoded_len;
231 RETURN_STRINGL(updated, updated_len, 0);
232 } else {
233 RETVAL_STRINGL(encoded, encoded_len, 0);
234 }
235 } else {
236 RETVAL_FALSE;
237 }
238 STR_FREE(updated);
239 }
240 /* }}} */
241
242 /* {{{ proto string HttpDeflateStream::finish([string data])
243 *
244 * Finalizes the deflate stream. The deflate stream can be reused after finalizing.
245 *
246 * Returns the final part of deflated data.
247 */
248 PHP_METHOD(HttpDeflateStream, finish)
249 {
250 int data_len = 0;
251 size_t updated_len = 0, encoded_len = 0;
252 char *updated = NULL, *encoded = NULL, *data = NULL;
253 getObject(http_deflatestream_object, obj);
254
255 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
256 RETURN_FALSE;
257 }
258
259 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
260 RETURN_FALSE;
261 }
262
263 if (data_len) {
264 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
265 RETURN_FALSE;
266 }
267 }
268
269 if (SUCCESS == http_encoding_deflate_stream_finish(obj->stream, &encoded, &encoded_len)) {
270 if (updated_len) {
271 updated = erealloc(updated, updated_len + encoded_len + 1);
272 updated[updated_len + encoded_len] = '\0';
273 memcpy(updated + updated_len, encoded, encoded_len);
274 STR_FREE(encoded);
275 updated_len += encoded_len;
276 RETVAL_STRINGL(updated, updated_len, 0);
277 } else {
278 STR_FREE(updated);
279 RETVAL_STRINGL(encoded, encoded_len, 0);
280 }
281 } else {
282 STR_FREE(updated);
283 RETVAL_FALSE;
284 }
285
286 http_encoding_deflate_stream_dtor(obj->stream);
287 http_encoding_deflate_stream_init(obj->stream, obj->stream->flags);
288 }
289 /* }}} */
290
291
292 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
293
294 /*
295 * Local variables:
296 * tab-width: 4
297 * c-basic-offset: 4
298 * End:
299 * vim600: noet sw=4 ts=4 fdm=marker
300 * vim<600: noet sw=4 ts=4
301 */
302