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