- unified cache_api function names
[m6w6/ext-http] / http_message_object.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "php.h"
24
25 #include "php_http.h"
26 #include "php_http_std_defs.h"
27 #include "php_http_message_object.h"
28
29 #ifdef ZEND_ENGINE_2
30
31 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
32 static inline void _http_message_object_declare_default_properties(TSRMLS_D);
33 #define http_message_object_read_prop _http_message_object_read_prop
34 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC);
35 #define http_message_object_write_prop _http_message_object_write_prop
36 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC);
37 #define http_message_object_get_props _http_message_object_get_props
38 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
39
40 zend_class_entry *http_message_object_ce;
41 zend_function_entry http_message_object_fe[] = {
42 PHP_ME(HttpMessage, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
43 PHP_ME(HttpMessage, setRaw, NULL, ZEND_ACC_PUBLIC)
44 PHP_ME(HttpMessage, getBody, NULL, ZEND_ACC_PUBLIC)
45 PHP_ME(HttpMessage, getHeaders, NULL, ZEND_ACC_PUBLIC)
46 PHP_ME(HttpMessage, setHeaders, NULL, ZEND_ACC_PUBLIC)
47 PHP_ME(HttpMessage, addHeaders, NULL, ZEND_ACC_PUBLIC)
48 PHP_ME(HttpMessage, getType, NULL, ZEND_ACC_PUBLIC)
49 PHP_ME(HttpMessage, setType, NULL, ZEND_ACC_PUBLIC)
50 PHP_ME(HttpMessage, getResponseCode, NULL, ZEND_ACC_PUBLIC)
51 PHP_ME(HttpMessage, setResponseCode, NULL, ZEND_ACC_PUBLIC)
52 PHP_ME(HttpMessage, getRequestMethod, NULL, ZEND_ACC_PUBLIC)
53 PHP_ME(HttpMessage, setRequestMethod, NULL, ZEND_ACC_PUBLIC)
54 PHP_ME(HttpMessage, getRequestUri, NULL, ZEND_ACC_PUBLIC)
55 PHP_ME(HttpMessage, setRequestUri, NULL, ZEND_ACC_PUBLIC)
56 PHP_ME(HttpMessage, getHttpVersion, NULL, ZEND_ACC_PUBLIC)
57 PHP_ME(HttpMessage, setHttpVersion, NULL, ZEND_ACC_PUBLIC)
58 PHP_ME(HttpMessage, toString, NULL, ZEND_ACC_PUBLIC)
59
60 ZEND_MALIAS(HttpMessage, __toString, toString, NULL, ZEND_ACC_PUBLIC)
61
62 PHP_ME(HttpMessage, fromString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
63 {NULL, NULL, NULL}
64 };
65 static zend_object_handlers http_message_object_handlers;
66
67 void _http_message_object_init(INIT_FUNC_ARGS)
68 {
69 HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
70
71 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
72 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
73 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
74
75 http_message_object_handlers.read_property = http_message_object_read_prop;
76 http_message_object_handlers.write_property = http_message_object_write_prop;
77 http_message_object_handlers.get_properties = http_message_object_get_props;
78 }
79
80 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
81 {
82 return http_message_object_new_ex(ce, NULL);
83 }
84
85 zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg TSRMLS_DC)
86 {
87 zend_object_value ov;
88 http_message_object *o;
89
90 o = ecalloc(1, sizeof(http_message_object));
91 o->zo.ce = ce;
92 o->message = msg ? msg : http_message_new();
93
94 ALLOC_HASHTABLE(OBJ_PROP(o));
95 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
96
97 ov.handle = zend_objects_store_put(o, (zend_objects_store_dtor_t) zend_objects_destroy_object, http_message_object_free, NULL TSRMLS_CC);
98 ov.handlers = &http_message_object_handlers;
99
100 return ov;
101 }
102
103 static inline void _http_message_object_declare_default_properties(TSRMLS_D)
104 {
105 zend_class_entry *ce = http_message_object_ce;
106
107 DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
108
109 DCL_PROP(PROTECTED, string, raw, "");
110 DCL_PROP(PROTECTED, string, body, "");
111
112 DCL_PROP(PROTECTED, string, requestMethod, "");
113 DCL_PROP(PROTECTED, string, requestUri, "");
114 DCL_PROP(PROTECTED, long, responseCode, 0);
115
116 DCL_PROP_N(PROTECTED, httpVersion);
117 DCL_PROP_N(PROTECTED, headers);
118 DCL_PROP_N(PROTECTED, nestedMessage);
119 }
120
121 static void _http_message_object_free(zend_object *object TSRMLS_DC)
122 {
123 http_message_object *o = (http_message_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->message) {
130 http_message_free(o->message);
131 }
132 zval_dtor(&o->_tmp_property);
133 efree(o);
134 }
135
136 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
137 {
138 getObjectEx(http_message_object, obj, object);
139 http_message *msg = obj->message;
140 zval *return_value = &obj->_tmp_property;
141
142 if (!EG(scope) || !instanceof_function(EG(scope), obj->zo.ce TSRMLS_CC)) {
143 zend_error(E_WARNING, "Cannot access protected property %s::$%s", obj->zo.ce->name, Z_STRVAL_P(member));
144 return EG(uninitialized_zval_ptr);
145 }
146
147 zval_dtor(return_value);
148
149 #if 0
150 fprintf(stderr, "Reading property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member), Z_STRLEN_P(member), strlen(Z_STRVAL_P(member)),
151 zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1)
152 );
153 #endif
154
155 switch (zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1))
156 {
157 case HTTP_MSG_PROPHASH_TYPE:
158 RETVAL_LONG(msg->type);
159 break;
160
161 case HTTP_MSG_PROPHASH_HTTP_VERSION:
162 switch (msg->type)
163 {
164 case HTTP_MSG_NONE:
165 RETVAL_NULL();
166 break;
167
168 case HTTP_MSG_REQUEST:
169 RETVAL_DOUBLE(msg->info.request.http_version);
170 break;
171
172 case HTTP_MSG_RESPONSE:
173 RETVAL_DOUBLE(msg->info.response.http_version);
174 break;
175 }
176 break;
177
178 case HTTP_MSG_PROPHASH_RAW:
179 if (msg->raw) {
180 if (msg->len) {
181 RETVAL_STRINGL(msg->raw, msg->len, 1);
182 } else {
183 RETVAL_STRINGL("", 0, 1);
184 }
185 } else {
186 RETVAL_NULL();
187 }
188 break;
189
190 case HTTP_MSG_PROPHASH_BODY:
191 phpstr_fix(PHPSTR(msg));
192 RETVAL_PHPSTR(PHPSTR(msg), 0, 1);
193 break;
194
195 case HTTP_MSG_PROPHASH_HEADERS:
196 array_init(return_value);
197 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
198 break;
199
200 case HTTP_MSG_PROPHASH_NESTED_MESSAGE:
201 RETVAL_NULL();
202 break;
203
204 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
205 if (msg->type == HTTP_MSG_REQUEST && msg->info.request.method) {
206 RETVAL_STRING(msg->info.request.method, 1);
207 } else {
208 RETVAL_NULL();
209 }
210 break;
211
212 case HTTP_MSG_PROPHASH_REQUEST_URI:
213 if (msg->type == HTTP_MSG_REQUEST && msg->info.request.URI) {
214 RETVAL_STRING(msg->info.request.URI, 1);
215 } else {
216 RETVAL_NULL();
217 }
218 break;
219
220 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
221 if (msg->type == HTTP_MSG_RESPONSE) {
222 RETVAL_LONG(msg->info.response.code);
223 } else {
224 RETVAL_NULL();
225 }
226 break;
227
228 default:
229 RETVAL_NULL();
230 break;
231 }
232
233 return return_value;
234 }
235
236 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
237 {
238 getObjectEx(http_message_object, obj, object);
239 http_message *msg = obj->message;
240
241 if (!EG(scope) || !instanceof_function(EG(scope), obj->zo.ce TSRMLS_CC)) {
242 zend_error(E_WARNING, "Cannot access protected property %s::$%s", obj->zo.ce->name, Z_STRVAL_P(member));
243 }
244
245 #if 0
246 fprintf(stderr, "Writing property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member), Z_STRLEN_P(member), strlen(Z_STRVAL_P(member)),
247 zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1)
248 );
249 #endif
250
251 switch (zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1))
252 {
253 case HTTP_MSG_PROPHASH_TYPE:
254 if (Z_LVAL_P(value) != msg->type) {
255 if (msg->type == HTTP_MSG_REQUEST) {
256 if (msg->info.request.method) {
257 efree(msg->info.request.method);
258 }
259 if (msg->info.request.URI) {
260 efree(msg->info.request.URI);
261 }
262 }
263 msg->type = Z_LVAL_P(value);
264 if (msg->type == HTTP_MSG_REQUEST) {
265 msg->info.request.method = NULL;
266 msg->info.request.URI = NULL;
267 }
268 }
269
270 break;
271
272 case HTTP_MSG_PROPHASH_HTTP_VERSION:
273 switch (msg->type)
274 {
275 case HTTP_MSG_REQUEST:
276 msg->info.request.http_version = (float) Z_DVAL_P(value);
277 break;
278
279 case HTTP_MSG_RESPONSE:
280 msg->info.response.http_version = (float) Z_DVAL_P(value);
281 break;
282 }
283 break;
284
285 case HTTP_MSG_PROPHASH_RAW:
286 http_message_dtor(msg);
287 http_message_parse_ex(msg, Z_STRVAL_P(value), Z_STRLEN_P(value), 1);
288 break;
289
290 case HTTP_MSG_PROPHASH_BODY:
291 phpstr_dtor(PHPSTR(msg));
292 phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(value), Z_STRLEN_P(value));
293 break;
294
295 case HTTP_MSG_PROPHASH_HEADERS:
296 zend_hash_clean(&msg->hdrs);
297 zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(value), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
298 break;
299
300 case HTTP_MSG_PROPHASH_NESTED_MESSAGE:
301 break;
302
303 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
304 if (msg->type == HTTP_MSG_REQUEST) {
305 if (msg->info.request.method) {
306 efree(msg->info.request.method);
307 }
308 msg->info.request.method = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
309 }
310 break;
311
312 case HTTP_MSG_PROPHASH_REQUEST_URI:
313 if (msg->type == HTTP_MSG_REQUEST) {
314 if (msg->info.request.URI) {
315 efree(msg->info.request.URI);
316 }
317 msg->info.request.URI = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
318 }
319 break;
320
321 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
322 if (msg->type == HTTP_MSG_RESPONSE) {
323 msg->info.response.code = Z_LVAL_P(value);
324 }
325 break;
326 }
327 zval_dtor(value);
328 FREE_ZVAL(value);
329 }
330
331 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
332 {
333 zval *headers;
334 getObjectEx(http_message_object, obj, object);
335 http_message *msg = obj->message;
336
337 #define ASSOC_PROP(obj, ptype, name, val) \
338 { \
339 zval array; \
340 char *m_prop_name; \
341 int m_prop_len; \
342 Z_ARRVAL(array) = OBJ_PROP(obj); \
343 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
344 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
345 }
346 #define ASSOC_STRING(obj, name, val) ASSOC_STRINGL(obj, name, val, strlen(val))
347 #define ASSOC_STRINGL(obj, name, val, len) \
348 { \
349 zval array; \
350 char *m_prop_name; \
351 int m_prop_len; \
352 Z_ARRVAL(array) = OBJ_PROP(obj); \
353 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
354 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
355 }
356
357 zend_hash_clean(OBJ_PROP(obj));
358
359 ASSOC_PROP(obj, long, "type", msg->type);
360 ASSOC_STRINGL(obj, "raw", msg->raw, msg->len)
361 ASSOC_STRINGL(obj, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
362
363 MAKE_STD_ZVAL(headers);
364 array_init(headers);
365
366 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
367 ASSOC_PROP(obj, zval, "headers", headers);
368
369 switch (msg->type)
370 {
371 case HTTP_MSG_REQUEST:
372 ASSOC_PROP(obj, double, "httpVersion", msg->info.request.http_version);
373 ASSOC_PROP(obj, long, "responseCode", 0);
374 ASSOC_STRING(obj, "requestMethod", msg->info.request.method);
375 ASSOC_STRING(obj, "requestUri", msg->info.request.URI);
376 break;
377
378 case HTTP_MSG_RESPONSE:
379 ASSOC_PROP(obj, double, "httpVersion", msg->info.response.http_version);
380 ASSOC_PROP(obj, long, "responseCode", msg->info.response.code);
381 ASSOC_STRING(obj, "requestMethod", "");
382 ASSOC_STRING(obj, "requestUri", "");
383 break;
384
385 case HTTP_MSG_NONE:
386 default:
387 ASSOC_PROP(obj, double, "httpVersion", 0.0);
388 ASSOC_PROP(obj, long, "responseCode", 0);
389 ASSOC_STRING(obj, "requestMethod", "");
390 ASSOC_STRING(obj, "requestUri", "");
391 break;
392 }
393
394 return OBJ_PROP(obj);
395 }
396
397 #endif /* ZEND_ENGINE_2 */
398
399 /*
400 * Local variables:
401 * tab-width: 4
402 * c-basic-offset: 4
403 * End:
404 * vim600: noet sw=4 ts=4 fdm=marker
405 * vim<600: noet sw=4 ts=4
406 */
407