- win32 build now has libcurl v7.16.0
[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-2006, 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(update, 1)
34 HTTP_ARG_VAL(data, 0)
35 HTTP_END_ARGS;
36
37 HTTP_BEGIN_ARGS(flush, 0)
38 HTTP_ARG_VAL(data, 0)
39 HTTP_END_ARGS;
40
41 HTTP_BEGIN_ARGS(finish, 0)
42 HTTP_ARG_VAL(data, 0)
43 HTTP_END_ARGS;
44
45 #define OBJ_PROP_CE http_deflatestream_object_ce
46 zend_class_entry *http_deflatestream_object_ce;
47 zend_function_entry http_deflatestream_object_fe[] = {
48 HTTP_DEFLATE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
49 HTTP_DEFLATE_ME(update, ZEND_ACC_PUBLIC)
50 HTTP_DEFLATE_ME(flush, ZEND_ACC_PUBLIC)
51 HTTP_DEFLATE_ME(finish, ZEND_ACC_PUBLIC)
52
53 EMPTY_FUNCTION_ENTRY
54 };
55 static zend_object_handlers http_deflatestream_object_handlers;
56
57 PHP_MINIT_FUNCTION(http_deflatestream_object)
58 {
59 HTTP_REGISTER_CLASS_EX(HttpDeflateStream, http_deflatestream_object, NULL, 0);
60 http_deflatestream_object_handlers.clone_obj = _http_deflatestream_object_clone_obj;
61
62 #ifndef WONKY
63 DCL_CONST(long, "TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP);
64 DCL_CONST(long, "TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB);
65 DCL_CONST(long, "TYPE_RAW", HTTP_DEFLATE_TYPE_RAW);
66 DCL_CONST(long, "LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF);
67 DCL_CONST(long, "LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN);
68 DCL_CONST(long, "LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX);
69 DCL_CONST(long, "STRATEGY_DEF", HTTP_DEFLATE_STRATEGY_DEF);
70 DCL_CONST(long, "STRATEGY_FILT", HTTP_DEFLATE_STRATEGY_FILT);
71 DCL_CONST(long, "STRATEGY_HUFF", HTTP_DEFLATE_STRATEGY_HUFF);
72 DCL_CONST(long, "STRATEGY_RLE", HTTP_DEFLATE_STRATEGY_RLE);
73 DCL_CONST(long, "STRATEGY_FIXED", HTTP_DEFLATE_STRATEGY_FIXED);
74 DCL_CONST(long, "FLUSH_NONE", HTTP_ENCODING_STREAM_FLUSH_NONE);
75 DCL_CONST(long, "FLUSH_SYNC", HTTP_ENCODING_STREAM_FLUSH_SYNC);
76 DCL_CONST(long, "FLUSH_FULL", HTTP_ENCODING_STREAM_FLUSH_FULL);
77 #endif
78
79 return SUCCESS;
80 }
81
82 zend_object_value _http_deflatestream_object_new(zend_class_entry *ce TSRMLS_DC)
83 {
84 return http_deflatestream_object_new_ex(ce, NULL, NULL);
85 }
86
87 zend_object_value _http_deflatestream_object_new_ex(zend_class_entry *ce, http_encoding_stream *s, http_deflatestream_object **ptr TSRMLS_DC)
88 {
89 zend_object_value ov;
90 http_deflatestream_object *o;
91
92 o = ecalloc(1, sizeof(http_deflatestream_object));
93 o->zo.ce = ce;
94
95 if (ptr) {
96 *ptr = o;
97 }
98
99 if (s) {
100 o->stream = s;
101 }
102
103 ALLOC_HASHTABLE(OBJ_PROP(o));
104 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
105 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
106
107 ov.handle = putObject(http_deflatestream_object, o);
108 ov.handlers = &http_deflatestream_object_handlers;
109
110 return ov;
111 }
112
113 zend_object_value _http_deflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
114 {
115 http_encoding_stream *s;
116 zend_object_value new_ov;
117 http_deflatestream_object *new_obj = NULL;
118 getObject(http_deflatestream_object, old_obj);
119
120 s = ecalloc(1, sizeof(http_encoding_stream));
121 s->flags = old_obj->stream->flags;
122 deflateCopy(&s->stream, &old_obj->stream->stream);
123 s->stream.opaque = phpstr_dup(s->stream.opaque);
124
125 new_ov = http_deflatestream_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);
127
128 return new_ov;
129 }
130
131 void _http_deflatestream_object_free(zend_object *object TSRMLS_DC)
132 {
133 http_deflatestream_object *o = (http_deflatestream_object *) object;
134
135 if (o->stream) {
136 http_encoding_deflate_stream_free(&o->stream);
137 }
138 freeObject(o);
139 }
140
141 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
142 *
143 * Creates a new HttpDeflateStream object instance.
144 *
145 * Accepts an optional int parameter specifying how to initialize the deflate stream.
146 */
147 PHP_METHOD(HttpDeflateStream, __construct)
148 {
149 long flags = 0;
150
151 SET_EH_THROW_HTTP();
152 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
153 getObject(http_deflatestream_object, obj);
154
155 if (!obj->stream) {
156 obj->stream = http_encoding_deflate_stream_init(NULL, flags & 0x0fffffff);
157 } else {
158 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpDeflateStream cannot be initialized twice");
159 }
160 }
161 SET_EH_NORMAL();
162 }
163 /* }}} */
164
165 /* {{{ proto string HttpDeflateStream::update(string data)
166 *
167 * Passes more data through the deflate stream.
168 *
169 * Expects a string parameter containing (a part of) the data to deflate.
170 *
171 * Returns deflated data on success or FALSE on failure.
172 */
173 PHP_METHOD(HttpDeflateStream, update)
174 {
175 int data_len;
176 size_t encoded_len = 0;
177 char *data, *encoded = NULL;
178 getObject(http_deflatestream_object, obj);
179
180 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
181 RETURN_FALSE;
182 }
183
184 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
185 RETURN_FALSE;
186 }
187
188 if (SUCCESS == http_encoding_deflate_stream_update(obj->stream, data, data_len, &encoded, &encoded_len)) {
189 RETURN_STRINGL(encoded, encoded_len, 0);
190 } else {
191 RETURN_FALSE;
192 }
193 }
194 /* }}} */
195
196 /* {{{ proto string HttpDeflateStream::flush([string data])
197 *
198 * Flushes the deflate stream.
199 *
200 * Returns some deflated data as string on success or FALSE on failure.
201 */
202 PHP_METHOD(HttpDeflateStream, flush)
203 {
204 int data_len = 0;
205 size_t updated_len = 0, encoded_len = 0;
206 char *updated = NULL, *encoded = NULL, *data = NULL;
207 getObject(http_deflatestream_object, obj);
208
209 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
210 RETURN_FALSE;
211 }
212
213 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
214 RETURN_FALSE;
215 }
216
217 if (data_len) {
218 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
219 RETURN_FALSE;
220 }
221 }
222
223 if (SUCCESS == http_encoding_deflate_stream_flush(obj->stream, &encoded, &encoded_len)) {
224 if (updated_len) {
225 updated = erealloc(updated, updated_len + encoded_len + 1);
226 updated[updated_len + encoded_len] = '\0';
227 memcpy(updated + updated_len, encoded, encoded_len);
228 STR_FREE(encoded);
229 updated_len += encoded_len;
230 RETURN_STRINGL(updated, updated_len, 0);
231 } else {
232 RETVAL_STRINGL(encoded, encoded_len, 0);
233 }
234 } else {
235 RETVAL_FALSE;
236 }
237 STR_FREE(updated);
238 }
239 /* }}} */
240
241 /* {{{ proto string HttpDeflateStream::finish([string data])
242 *
243 * Finalizes the deflate stream. The deflate stream can be reused after finalizing.
244 *
245 * Returns the final part of deflated data.
246 */
247 PHP_METHOD(HttpDeflateStream, finish)
248 {
249 int data_len = 0;
250 size_t updated_len = 0, encoded_len = 0;
251 char *updated = NULL, *encoded = NULL, *data = NULL;
252 getObject(http_deflatestream_object, obj);
253
254 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
255 RETURN_FALSE;
256 }
257
258 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
259 RETURN_FALSE;
260 }
261
262 if (data_len) {
263 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
264 RETURN_FALSE;
265 }
266 }
267
268 if (SUCCESS == http_encoding_deflate_stream_finish(obj->stream, &encoded, &encoded_len)) {
269 if (updated_len) {
270 updated = erealloc(updated, updated_len + encoded_len + 1);
271 updated[updated_len + encoded_len] = '\0';
272 memcpy(updated + updated_len, encoded, encoded_len);
273 STR_FREE(encoded);
274 updated_len += encoded_len;
275 RETVAL_STRINGL(updated, updated_len, 0);
276 } else {
277 STR_FREE(updated);
278 RETVAL_STRINGL(encoded, encoded_len, 0);
279 }
280 } else {
281 STR_FREE(updated);
282 RETVAL_FALSE;
283 }
284
285 http_encoding_deflate_stream_dtor(obj->stream);
286 http_encoding_deflate_stream_init(obj->stream, obj->stream->flags);
287 }
288 /* }}} */
289
290
291 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
292
293 /*
294 * Local variables:
295 * tab-width: 4
296 * c-basic-offset: 4
297 * End:
298 * vim600: noet sw=4 ts=4 fdm=marker
299 * vim<600: noet sw=4 ts=4
300 */
301