- add HttpDeflateStream and HttpInflateStream objects
[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_EMPTY_ARGS(finish, 0);
43
44 #define http_deflatestream_object_declare_default_properties() _http_deflatestream_object_declare_default_properties(TSRMLS_C)
45 static inline void _http_deflatestream_object_declare_default_properties(TSRMLS_D);
46
47 zend_class_entry *http_deflatestream_object_ce;
48 zend_function_entry http_deflatestream_object_fe[] = {
49 HTTP_DEFLATE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
50 HTTP_DEFLATE_ME(update, 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 return SUCCESS;
62 }
63
64 zend_object_value _http_deflatestream_object_new(zend_class_entry *ce TSRMLS_DC)
65 {
66 return http_deflatestream_object_new_ex(ce, NULL, NULL);
67 }
68
69 zend_object_value _http_deflatestream_object_new_ex(zend_class_entry *ce, http_encoding_stream *s, http_deflatestream_object **ptr TSRMLS_DC)
70 {
71 zend_object_value ov;
72 http_deflatestream_object *o;
73
74 o = ecalloc(1, sizeof(http_deflatestream_object));
75 o->zo.ce = ce;
76
77 if (ptr) {
78 *ptr = o;
79 }
80
81 if (s) {
82 o->stream = s;
83 }
84
85 ALLOC_HASHTABLE(OBJ_PROP(o));
86 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
87
88 ov.handle = putObject(http_deflatestream_object, o);
89 ov.handlers = &http_deflatestream_object_handlers;
90
91 return ov;
92 }
93
94 zend_object_value _http_deflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
95 {
96 http_encoding_stream *s;
97 getObject(http_deflatestream_object, obj);
98
99 s = ecalloc(1, sizeof(http_encoding_stream));
100 s->flags = obj->stream->flags;
101 deflateCopy(&s->stream, &obj->stream->stream);
102 s->stream.opaque = phpstr_dup(s->stream.opaque);
103
104 return http_deflatestream_object_new_ex(Z_OBJCE_P(this_ptr), s, NULL);
105 }
106
107 static inline void _http_deflatestream_object_declare_default_properties(TSRMLS_D)
108 {
109 zend_class_entry *ce = http_deflatestream_object_ce;
110
111 #ifndef WONKY
112 DCL_CONST(long, "TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP);
113 DCL_CONST(long, "TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB);
114 DCL_CONST(long, "TYPE_RAW", HTTP_DEFLATE_TYPE_RAW);
115 DCL_CONST(long, "LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF);
116 DCL_CONST(long, "LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN);
117 DCL_CONST(long, "LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX);
118 #endif
119 }
120
121 void _http_deflatestream_object_free(zend_object *object TSRMLS_DC)
122 {
123 http_deflatestream_object *o = (http_deflatestream_object *) object;
124
125 if (OBJ_PROP(o)) {
126 zend_hash_destroy(OBJ_PROP(o));
127 FREE_HASHTABLE(OBJ_PROP(o));
128 }
129 if (o->stream) {
130 http_encoding_deflate_stream_free(&o->stream);
131 }
132 efree(o);
133 }
134
135 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
136 *
137 * Creates a new HttpDeflateStream object instance.
138 *
139 * Accepts an optional int parameter specifying how to initialize the deflate stream.
140 */
141 PHP_METHOD(HttpDeflateStream, __construct)
142 {
143 long flags = 0;
144
145 SET_EH_THROW_HTTP();
146 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
147 getObject(http_deflatestream_object, obj);
148
149 if (!obj->stream) {
150 obj->stream = http_encoding_deflate_stream_init(NULL, flags);
151 } else {
152 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpDeflateStream cannot be initialized twice");
153 }
154 }
155 SET_EH_NORMAL();
156 }
157 /* }}} */
158
159 /* {{{ proto string HttpDeflateStream::update(string data)
160 *
161 * Passes more data through the deflate stream.
162 *
163 * Expects a string parameter containing (a part of) the data to deflate.
164 *
165 * Returns deflated data on success or FALSE on failure.
166 */
167 PHP_METHOD(HttpDeflateStream, update)
168 {
169 int data_len;
170 size_t encoded_len = 0;
171 char *data, *encoded = NULL;
172 getObject(http_deflatestream_object, obj);
173
174 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
175 RETURN_FALSE;
176 }
177
178 if (!obj->stream) {
179 if (!(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
180 RETURN_FALSE;
181 }
182 }
183
184 if (SUCCESS == http_encoding_deflate_stream_update(obj->stream, data, data_len, &encoded, &encoded_len)) {
185 RETURN_STRINGL(encoded, encoded_len, 0);
186 } else {
187 RETURN_FALSE;
188 }
189 }
190 /* }}} */
191
192 /* {{{ proto string HttpDeflateStream::finish()
193 *
194 * Finalizes the deflate stream. The deflate stream can be reused after finalizing.
195 *
196 * Returns the final part of deflated data.
197 */
198 PHP_METHOD(HttpDeflateStream, finish)
199 {
200 size_t encoded_len = 0;
201 char *encoded = NULL;
202 getObject(http_deflatestream_object, obj);
203
204 NO_ARGS;
205
206 if (!obj->stream) {
207 RETURN_FALSE;
208 }
209
210 if (SUCCESS == http_encoding_deflate_stream_finish(obj->stream, &encoded, &encoded_len)) {
211 RETVAL_STRINGL(encoded, encoded_len, 0);
212 } else {
213 RETVAL_FALSE;
214 }
215
216 http_encoding_deflate_stream_dtor(obj->stream);
217 http_encoding_deflate_stream_init(obj->stream, obj->stream->flags);
218 }
219 /* }}} */
220
221
222 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
223
224 /*
225 * Local variables:
226 * tab-width: 4
227 * c-basic-offset: 4
228 * End:
229 * vim600: noet sw=4 ts=4 fdm=marker
230 * vim<600: noet sw=4 ts=4
231 */
232