- win32 build now has libcurl v7.16.0
[m6w6/ext-http] / http_inflatestream_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_inflatestream_object.h"
24
25 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpInflateStream, method, 0, req_args)
26 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpInflateStream, method, 0)
27 #define HTTP_INFLATE_ME(method, visibility) PHP_ME(HttpInflateStream, method, HTTP_ARGS(HttpInflateStream, 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_inflatestream_object_ce
46 zend_class_entry *http_inflatestream_object_ce;
47 zend_function_entry http_inflatestream_object_fe[] = {
48 HTTP_INFLATE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
49 HTTP_INFLATE_ME(update, ZEND_ACC_PUBLIC)
50 HTTP_INFLATE_ME(flush, ZEND_ACC_PUBLIC)
51 HTTP_INFLATE_ME(finish, ZEND_ACC_PUBLIC)
52
53 EMPTY_FUNCTION_ENTRY
54 };
55 static zend_object_handlers http_inflatestream_object_handlers;
56
57 PHP_MINIT_FUNCTION(http_inflatestream_object)
58 {
59 HTTP_REGISTER_CLASS_EX(HttpInflateStream, http_inflatestream_object, NULL, 0);
60 http_inflatestream_object_handlers.clone_obj = _http_inflatestream_object_clone_obj;
61
62 #ifndef WONKY
63 DCL_CONST(long, "FLUSH_NONE", HTTP_ENCODING_STREAM_FLUSH_NONE);
64 DCL_CONST(long, "FLUSH_SYNC", HTTP_ENCODING_STREAM_FLUSH_SYNC);
65 DCL_CONST(long, "FLUSH_FULL", HTTP_ENCODING_STREAM_FLUSH_FULL);
66 #endif
67
68 return SUCCESS;
69 }
70
71 zend_object_value _http_inflatestream_object_new(zend_class_entry *ce TSRMLS_DC)
72 {
73 return http_inflatestream_object_new_ex(ce, NULL, NULL);
74 }
75
76 zend_object_value _http_inflatestream_object_new_ex(zend_class_entry *ce, http_encoding_stream *s, http_inflatestream_object **ptr TSRMLS_DC)
77 {
78 zend_object_value ov;
79 http_inflatestream_object *o;
80
81 o = ecalloc(1, sizeof(http_inflatestream_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), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
94 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
95
96 ov.handle = putObject(http_inflatestream_object, o);
97 ov.handlers = &http_inflatestream_object_handlers;
98
99 return ov;
100 }
101
102 zend_object_value _http_inflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
103 {
104 http_encoding_stream *s;
105 zend_object_value new_ov;
106 http_inflatestream_object *new_obj = NULL;
107 getObject(http_inflatestream_object, old_obj);
108
109 s = ecalloc(1, sizeof(http_encoding_stream));
110 s->flags = old_obj->stream->flags;
111 inflateCopy(&s->stream, &old_obj->stream->stream);
112 s->stream.opaque = phpstr_dup(s->stream.opaque);
113
114 new_ov = http_inflatestream_object_new_ex(old_obj->zo.ce, s, &new_obj);
115 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
116
117 return new_ov;
118 }
119
120 void _http_inflatestream_object_free(zend_object *object TSRMLS_DC)
121 {
122 http_inflatestream_object *o = (http_inflatestream_object *) object;
123
124 if (o->stream) {
125 http_encoding_inflate_stream_free(&o->stream);
126 }
127 freeObject(o);
128 }
129
130 /* {{{ proto void HttpInflateStream::__construct([int flags = 0])
131 *
132 * Creates a new HttpInflateStream object instance.
133 *
134 * Accepts an optional int parameter specifying how to initialize the inflate stream.
135 */
136 PHP_METHOD(HttpInflateStream, __construct)
137 {
138 long flags = 0;
139
140 SET_EH_THROW_HTTP();
141 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
142 getObject(http_inflatestream_object, obj);
143
144 if (!obj->stream) {
145 obj->stream = http_encoding_inflate_stream_init(NULL, flags & 0x0fffffff);
146 } else {
147 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpInflateStream cannot be initialized twice");
148 }
149 }
150 SET_EH_NORMAL();
151 }
152 /* }}} */
153
154 /* {{{ proto string HttpInflateStream::update(string data)
155 *
156 * Passes more data through the inflate stream.
157 *
158 * Expects a string parameter containing (a part of) the data to inflate.
159 *
160 * Returns inflated data on success or FALSE on failure.
161 */
162 PHP_METHOD(HttpInflateStream, update)
163 {
164 int data_len;
165 size_t decoded_len = 0;
166 char *data, *decoded = NULL;
167 getObject(http_inflatestream_object, obj);
168
169 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
170 RETURN_FALSE;
171 }
172
173 if (!data_len) {
174 RETURN_STRING("", 1);
175 }
176
177 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
178 RETURN_FALSE;
179 }
180
181 if (SUCCESS == http_encoding_inflate_stream_update(obj->stream, data, data_len, &decoded, &decoded_len)) {
182 RETURN_STRINGL(decoded, decoded_len, 0);
183 } else {
184 RETURN_FALSE;
185 }
186 }
187 /* }}} */
188
189 /* {{{ proto string HttpInflateStream::flush([string data])
190 *
191 * Flush the inflate stream.
192 *
193 * Returns some inflated data as string on success or FALSE on failure.
194 */
195 PHP_METHOD(HttpInflateStream, flush)
196 {
197 int data_len = 0;
198 size_t decoded_len = 0;
199 char *decoded = NULL, *data = NULL;
200 getObject(http_inflatestream_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_inflate_stream_init(NULL, 0))) {
207 RETURN_FALSE;
208 }
209
210 /* flushing the inflate stream is a no-op */
211 if (!data_len) {
212 RETURN_STRINGL("", 0, 1);
213 } else if (SUCCESS == http_encoding_inflate_stream_update(obj->stream, data, data_len, &decoded, &decoded_len)) {
214 RETURN_STRINGL(decoded, decoded_len, 0);
215 } else {
216 RETURN_FALSE;
217 }
218 }
219 /* }}} */
220
221 /* {{{ proto string HttpInflateStream::finish([string data])
222 *
223 * Finalizes the inflate stream. The inflate stream can be reused after finalizing.
224 *
225 * Returns the final part of inflated data.
226 */
227 PHP_METHOD(HttpInflateStream, finish)
228 {
229 int data_len = 0;
230 size_t updated_len = 0, decoded_len = 0;
231 char *updated = NULL, *decoded = NULL, *data = NULL;
232 getObject(http_inflatestream_object, obj);
233
234 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
235 RETURN_FALSE;
236 }
237
238 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
239 RETURN_FALSE;
240 }
241
242 if (data_len) {
243 if (SUCCESS != http_encoding_inflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
244 RETURN_FALSE;
245 }
246 }
247
248 if (SUCCESS == http_encoding_inflate_stream_finish(obj->stream, &decoded, &decoded_len)) {
249 if (updated_len) {
250 updated = erealloc(updated, updated_len + decoded_len + 1);
251 updated[updated_len + decoded_len] = '\0';
252 memcpy(updated + updated_len, decoded, decoded_len);
253 STR_FREE(decoded);
254 updated_len += decoded_len;
255 RETVAL_STRINGL(updated, updated_len, 0);
256 } else {
257 STR_FREE(updated);
258 RETVAL_STRINGL(decoded, decoded_len, 0);
259 }
260 } else {
261 STR_FREE(updated);
262 RETVAL_FALSE;
263 }
264
265 http_encoding_inflate_stream_dtor(obj->stream);
266 http_encoding_inflate_stream_init(obj->stream, obj->stream->flags);
267 }
268 /* }}} */
269
270
271 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
272
273 /*
274 * Local variables:
275 * tab-width: 4
276 * c-basic-offset: 4
277 * End:
278 * vim600: noet sw=4 ts=4 fdm=marker
279 * vim<600: noet sw=4 ts=4
280 */
281