ef687d4eb175ab8fe739d28bf40db8aa42a68b67
[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), 0, NULL, ZVAL_PTR_DTOR, 0);
94
95 ov.handle = putObject(http_inflatestream_object, o);
96 ov.handlers = &http_inflatestream_object_handlers;
97
98 return ov;
99 }
100
101 zend_object_value _http_inflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
102 {
103 http_encoding_stream *s;
104 getObject(http_inflatestream_object, obj);
105
106 s = ecalloc(1, sizeof(http_encoding_stream));
107 s->flags = obj->stream->flags;
108 inflateCopy(&s->stream, &obj->stream->stream);
109 s->stream.opaque = phpstr_dup(s->stream.opaque);
110
111 return http_inflatestream_object_new_ex(Z_OBJCE_P(this_ptr), s, NULL);
112 }
113
114 void _http_inflatestream_object_free(zend_object *object TSRMLS_DC)
115 {
116 http_inflatestream_object *o = (http_inflatestream_object *) object;
117
118 if (o->stream) {
119 http_encoding_inflate_stream_free(&o->stream);
120 }
121 freeObject(o);
122 }
123
124 /* {{{ proto void HttpInflateStream::__construct([int flags = 0])
125 *
126 * Creates a new HttpInflateStream object instance.
127 *
128 * Accepts an optional int parameter specifying how to initialize the inflate stream.
129 */
130 PHP_METHOD(HttpInflateStream, __construct)
131 {
132 long flags = 0;
133
134 SET_EH_THROW_HTTP();
135 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
136 getObject(http_inflatestream_object, obj);
137
138 if (!obj->stream) {
139 obj->stream = http_encoding_inflate_stream_init(NULL, flags & 0x0fffffff);
140 } else {
141 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpInflateStream cannot be initialized twice");
142 }
143 }
144 SET_EH_NORMAL();
145 }
146 /* }}} */
147
148 /* {{{ proto string HttpInflateStream::update(string data)
149 *
150 * Passes more data through the inflate stream.
151 *
152 * Expects a string parameter containing (a part of) the data to inflate.
153 *
154 * Returns inflated data on success or FALSE on failure.
155 */
156 PHP_METHOD(HttpInflateStream, update)
157 {
158 int data_len;
159 size_t decoded_len = 0;
160 char *data, *decoded = NULL;
161 getObject(http_inflatestream_object, obj);
162
163 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
164 RETURN_FALSE;
165 }
166
167 if (!data_len) {
168 RETURN_STRING("", 1);
169 }
170
171 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
172 RETURN_FALSE;
173 }
174
175 if (SUCCESS == http_encoding_inflate_stream_update(obj->stream, data, data_len, &decoded, &decoded_len)) {
176 RETURN_STRINGL(decoded, decoded_len, 0);
177 } else {
178 RETURN_FALSE;
179 }
180 }
181 /* }}} */
182
183 /* {{{ proto string HttpInflateStream::flush([string data])
184 *
185 * Flush the inflate stream.
186 *
187 * Returns some inflated data as string on success or FALSE on failure.
188 */
189 PHP_METHOD(HttpInflateStream, flush)
190 {
191 int data_len = 0;
192 size_t decoded_len = 0;
193 char *decoded = NULL, *data = NULL;
194 getObject(http_inflatestream_object, obj);
195
196 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
197 RETURN_FALSE;
198 }
199
200 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
201 RETURN_FALSE;
202 }
203
204 /* flushing the inflate stream is a no-op */
205 if (!data_len) {
206 RETURN_STRINGL("", 0, 1);
207 } else if (SUCCESS == http_encoding_inflate_stream_update(obj->stream, data, data_len, &decoded, &decoded_len)) {
208 RETURN_STRINGL(decoded, decoded_len, 0);
209 } else {
210 RETURN_FALSE;
211 }
212 }
213 /* }}} */
214
215 /* {{{ proto string HttpInflateStream::finish([string data])
216 *
217 * Finalizes the inflate stream. The inflate stream can be reused after finalizing.
218 *
219 * Returns the final part of inflated data.
220 */
221 PHP_METHOD(HttpInflateStream, finish)
222 {
223 int data_len = 0;
224 size_t updated_len = 0, decoded_len = 0;
225 char *updated = NULL, *decoded = NULL, *data = NULL;
226 getObject(http_inflatestream_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_inflate_stream_init(NULL, 0))) {
233 RETURN_FALSE;
234 }
235
236 if (data_len) {
237 if (SUCCESS != http_encoding_inflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
238 RETURN_FALSE;
239 }
240 }
241
242 if (SUCCESS == http_encoding_inflate_stream_finish(obj->stream, &decoded, &decoded_len)) {
243 if (updated_len) {
244 updated = erealloc(updated, updated_len + decoded_len + 1);
245 updated[updated_len + decoded_len] = '\0';
246 memcpy(updated + updated_len, decoded, decoded_len);
247 STR_FREE(decoded);
248 updated_len += decoded_len;
249 RETVAL_STRINGL(updated, updated_len, 0);
250 } else {
251 STR_FREE(updated);
252 RETVAL_STRINGL(decoded, decoded_len, 0);
253 }
254 } else {
255 STR_FREE(updated);
256 RETVAL_FALSE;
257 }
258
259 http_encoding_inflate_stream_dtor(obj->stream);
260 http_encoding_inflate_stream_init(obj->stream, obj->stream->flags);
261 }
262 /* }}} */
263
264
265 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
266
267 /*
268 * Local variables:
269 * tab-width: 4
270 * c-basic-offset: 4
271 * End:
272 * vim600: noet sw=4 ts=4 fdm=marker
273 * vim<600: noet sw=4 ts=4
274 */
275