3129193147c2404e8bb66d804f2dd43c292d5e78
[m6w6/ext-http] / http_message_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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_MAGIC
18 #include "php_http.h"
19
20 #ifdef ZEND_ENGINE_2
21
22 #include "zend_interfaces.h"
23 #include "ext/standard/url.h"
24 #include "php_variables.h"
25
26 #include "php_http_api.h"
27 #include "php_http_send_api.h"
28 #include "php_http_url_api.h"
29 #include "php_http_message_api.h"
30 #include "php_http_message_object.h"
31 #include "php_http_exception_object.h"
32 #include "php_http_response_object.h"
33 #include "php_http_request_method_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_request_object.h"
36 #include "php_http_headers_api.h"
37
38 #if defined(HTTP_HAVE_SPL) && !defined(WONKY)
39 /* SPL doesn't install its headers */
40 extern PHPAPI zend_class_entry *spl_ce_Countable;
41 #endif
42
43 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, 0, req_args)
44 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpMessage, method, 0)
45 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
46
47 HTTP_BEGIN_ARGS(__construct, 0)
48 HTTP_ARG_VAL(message, 0)
49 HTTP_END_ARGS;
50
51 HTTP_BEGIN_ARGS(factory, 0)
52 HTTP_ARG_VAL(message, 0)
53 HTTP_ARG_VAL(class_name, 0)
54 HTTP_END_ARGS;
55
56 HTTP_BEGIN_ARGS(fromEnv, 1)
57 HTTP_ARG_VAL(type, 0)
58 HTTP_ARG_VAL(class_name, 0)
59 HTTP_END_ARGS;
60
61 HTTP_EMPTY_ARGS(getBody);
62 HTTP_BEGIN_ARGS(setBody, 1)
63 HTTP_ARG_VAL(body, 0)
64 HTTP_END_ARGS;
65
66 HTTP_BEGIN_ARGS(getHeader, 1)
67 HTTP_ARG_VAL(header, 0)
68 HTTP_END_ARGS;
69
70 HTTP_EMPTY_ARGS(getHeaders);
71 HTTP_BEGIN_ARGS(setHeaders, 1)
72 HTTP_ARG_VAL(headers, 0)
73 HTTP_END_ARGS;
74
75 HTTP_BEGIN_ARGS(addHeaders, 1)
76 HTTP_ARG_VAL(headers, 0)
77 HTTP_ARG_VAL(append, 0)
78 HTTP_END_ARGS;
79
80 HTTP_EMPTY_ARGS(getType);
81 HTTP_BEGIN_ARGS(setType, 1)
82 HTTP_ARG_VAL(type, 0)
83 HTTP_END_ARGS;
84
85 HTTP_EMPTY_ARGS(getInfo);
86 HTTP_BEGIN_ARGS(setInfo, 1)
87 HTTP_ARG_VAL(http_info, 0)
88 HTTP_END_ARGS;
89
90 HTTP_EMPTY_ARGS(getResponseCode);
91 HTTP_BEGIN_ARGS(setResponseCode, 1)
92 HTTP_ARG_VAL(response_code, 0)
93 HTTP_END_ARGS;
94
95 HTTP_EMPTY_ARGS(getResponseStatus);
96 HTTP_BEGIN_ARGS(setResponseStatus, 1)
97 HTTP_ARG_VAL(response_status, 0)
98 HTTP_END_ARGS;
99
100 HTTP_EMPTY_ARGS(getRequestMethod);
101 HTTP_BEGIN_ARGS(setRequestMethod, 1)
102 HTTP_ARG_VAL(request_method, 0)
103 HTTP_END_ARGS;
104
105 HTTP_EMPTY_ARGS(getRequestUrl);
106 HTTP_BEGIN_ARGS(setRequestUrl, 1)
107 HTTP_ARG_VAL(url, 0)
108 HTTP_END_ARGS;
109
110 HTTP_EMPTY_ARGS(getHttpVersion);
111 HTTP_BEGIN_ARGS(setHttpVersion, 1)
112 HTTP_ARG_VAL(http_version, 0)
113 HTTP_END_ARGS;
114
115 HTTP_BEGIN_ARGS(guessContentType, 1)
116 HTTP_ARG_VAL(magic_file, 0)
117 HTTP_ARG_VAL(magic_mode, 0)
118 HTTP_END_ARGS;
119
120 HTTP_EMPTY_ARGS(getParentMessage);
121 HTTP_EMPTY_ARGS(send);
122 HTTP_EMPTY_ARGS(__toString);
123 HTTP_BEGIN_ARGS(toString, 0)
124 HTTP_ARG_VAL(include_parent, 0)
125 HTTP_END_ARGS;
126
127 HTTP_EMPTY_ARGS(toMessageTypeObject);
128
129 HTTP_EMPTY_ARGS(count);
130
131 HTTP_EMPTY_ARGS(serialize);
132 HTTP_BEGIN_ARGS(unserialize, 1)
133 HTTP_ARG_VAL(serialized, 0)
134 HTTP_END_ARGS;
135
136 HTTP_EMPTY_ARGS(rewind);
137 HTTP_EMPTY_ARGS(valid);
138 HTTP_EMPTY_ARGS(key);
139 HTTP_EMPTY_ARGS(current);
140 HTTP_EMPTY_ARGS(next);
141
142 HTTP_EMPTY_ARGS(detach);
143 HTTP_BEGIN_ARGS(prepend, 1)
144 HTTP_ARG_OBJ(HttpMessage, message, 0)
145 HTTP_END_ARGS;
146 HTTP_EMPTY_ARGS(reverse);
147
148 #define http_message_object_read_prop _http_message_object_read_prop
149 static zval *_http_message_object_read_prop(zval *object, zval *member, int type ZEND_LITERAL_KEY_DC TSRMLS_DC);
150 #define http_message_object_write_prop _http_message_object_write_prop
151 static void _http_message_object_write_prop(zval *object, zval *member, zval *value ZEND_LITERAL_KEY_DC TSRMLS_DC);
152 #define http_message_object_get_prop_ptr _http_message_object_get_prop_ptr
153 static zval **_http_message_object_get_prop_ptr(zval *object, zval *member ZEND_LITERAL_KEY_DC TSRMLS_DC);
154 #define http_message_object_get_props _http_message_object_get_props
155 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
156
157 #define THIS_CE http_message_object_ce
158 zend_class_entry *http_message_object_ce;
159 zend_function_entry http_message_object_fe[] = {
160 HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
161 HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC)
162 HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC)
163 HTTP_MESSAGE_ME(getHeader, ZEND_ACC_PUBLIC)
164 HTTP_MESSAGE_ME(getHeaders, ZEND_ACC_PUBLIC)
165 HTTP_MESSAGE_ME(setHeaders, ZEND_ACC_PUBLIC)
166 HTTP_MESSAGE_ME(addHeaders, ZEND_ACC_PUBLIC)
167 HTTP_MESSAGE_ME(getType, ZEND_ACC_PUBLIC)
168 HTTP_MESSAGE_ME(setType, ZEND_ACC_PUBLIC)
169 HTTP_MESSAGE_ME(getInfo, ZEND_ACC_PUBLIC)
170 HTTP_MESSAGE_ME(setInfo, ZEND_ACC_PUBLIC)
171 HTTP_MESSAGE_ME(getResponseCode, ZEND_ACC_PUBLIC)
172 HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
173 HTTP_MESSAGE_ME(getResponseStatus, ZEND_ACC_PUBLIC)
174 HTTP_MESSAGE_ME(setResponseStatus, ZEND_ACC_PUBLIC)
175 HTTP_MESSAGE_ME(getRequestMethod, ZEND_ACC_PUBLIC)
176 HTTP_MESSAGE_ME(setRequestMethod, ZEND_ACC_PUBLIC)
177 HTTP_MESSAGE_ME(getRequestUrl, ZEND_ACC_PUBLIC)
178 HTTP_MESSAGE_ME(setRequestUrl, ZEND_ACC_PUBLIC)
179 HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC)
180 HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
181 HTTP_MESSAGE_ME(guessContentType, ZEND_ACC_PUBLIC)
182 HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
183 HTTP_MESSAGE_ME(send, ZEND_ACC_PUBLIC)
184 HTTP_MESSAGE_ME(toString, ZEND_ACC_PUBLIC)
185 HTTP_MESSAGE_ME(toMessageTypeObject, ZEND_ACC_PUBLIC)
186
187 /* implements Countable */
188 HTTP_MESSAGE_ME(count, ZEND_ACC_PUBLIC)
189
190 /* implements Serializable */
191 HTTP_MESSAGE_ME(serialize, ZEND_ACC_PUBLIC)
192 HTTP_MESSAGE_ME(unserialize, ZEND_ACC_PUBLIC)
193
194 /* implements Iterator */
195 HTTP_MESSAGE_ME(rewind, ZEND_ACC_PUBLIC)
196 HTTP_MESSAGE_ME(valid, ZEND_ACC_PUBLIC)
197 HTTP_MESSAGE_ME(current, ZEND_ACC_PUBLIC)
198 HTTP_MESSAGE_ME(key, ZEND_ACC_PUBLIC)
199 HTTP_MESSAGE_ME(next, ZEND_ACC_PUBLIC)
200
201 ZEND_MALIAS(HttpMessage, __toString, toString, HTTP_ARGS(HttpMessage, __toString), ZEND_ACC_PUBLIC)
202
203 HTTP_MESSAGE_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
204 ZEND_MALIAS(HttpMessage, fromString, factory, HTTP_ARGS(HttpMessage, factory), ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
205 HTTP_MESSAGE_ME(fromEnv, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
206
207 HTTP_MESSAGE_ME(detach, ZEND_ACC_PUBLIC)
208 HTTP_MESSAGE_ME(prepend, ZEND_ACC_PUBLIC)
209 HTTP_MESSAGE_ME(reverse, ZEND_ACC_PUBLIC)
210
211 EMPTY_FUNCTION_ENTRY
212 };
213 static zend_object_handlers http_message_object_handlers;
214
215 static HashTable http_message_object_prophandlers;
216
217 typedef void (*http_message_object_prophandler_func)(http_message_object *o, zval *v TSRMLS_DC);
218
219 typedef struct _http_message_object_prophandler {
220 http_message_object_prophandler_func read;
221 http_message_object_prophandler_func write;
222 } http_message_object_prophandler;
223
224 static STATUS http_message_object_add_prophandler(const char *prop_str, size_t prop_len, http_message_object_prophandler_func read, http_message_object_prophandler_func write) {
225 http_message_object_prophandler h = { read, write };
226 return zend_hash_add(&http_message_object_prophandlers, prop_str, prop_len, (void *) &h, sizeof(h), NULL);
227 }
228 static STATUS http_message_object_get_prophandler(const char *prop_str, size_t prop_len, http_message_object_prophandler **handler) {
229 return zend_hash_find(&http_message_object_prophandlers, prop_str, prop_len, (void *) handler);
230 }
231 static void http_message_object_prophandler_get_type(http_message_object *obj, zval *return_value TSRMLS_DC) {
232 RETVAL_LONG(obj->message->type);
233 }
234 static void http_message_object_prophandler_set_type(http_message_object *obj, zval *value TSRMLS_DC) {
235 zval *cpy = http_zsep(IS_LONG, value);
236 http_message_set_type(obj->message, Z_LVAL_P(cpy));
237 zval_ptr_dtor(&cpy);
238 }
239 static void http_message_object_prophandler_get_body(http_message_object *obj, zval *return_value TSRMLS_DC) {
240 phpstr_fix(PHPSTR(obj->message));
241 RETVAL_PHPSTR(PHPSTR(obj->message), 0, 1);
242 }
243 static void http_message_object_prophandler_set_body(http_message_object *obj, zval *value TSRMLS_DC) {
244 zval *cpy = http_zsep(IS_STRING, value);
245 phpstr_dtor(PHPSTR(obj->message));
246 phpstr_from_string_ex(PHPSTR(obj->message), Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
247 zval_ptr_dtor(&cpy);
248 }
249 static void http_message_object_prophandler_get_request_method(http_message_object *obj, zval *return_value TSRMLS_DC) {
250 if (HTTP_MSG_TYPE(REQUEST, obj->message) && obj->message->http.info.request.method) {
251 RETVAL_STRING(obj->message->http.info.request.method, 1);
252 } else {
253 RETVAL_NULL();
254 }
255 }
256 static void http_message_object_prophandler_set_request_method(http_message_object *obj, zval *value TSRMLS_DC) {
257 if (HTTP_MSG_TYPE(REQUEST, obj->message)) {
258 zval *cpy = http_zsep(IS_STRING, value);
259 STR_SET(obj->message->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
260 zval_ptr_dtor(&cpy);
261 }
262 }
263 static void http_message_object_prophandler_get_request_url(http_message_object *obj, zval *return_value TSRMLS_DC) {
264 if (HTTP_MSG_TYPE(REQUEST, obj->message) && obj->message->http.info.request.url) {
265 RETVAL_STRING(obj->message->http.info.request.url, 1);
266 } else {
267 RETVAL_NULL();
268 }
269 }
270 static void http_message_object_prophandler_set_request_url(http_message_object *obj, zval *value TSRMLS_DC) {
271 if (HTTP_MSG_TYPE(REQUEST, obj->message)) {
272 zval *cpy = http_zsep(IS_STRING, value);
273 STR_SET(obj->message->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
274 zval_ptr_dtor(&cpy);
275 }
276 }
277 static void http_message_object_prophandler_get_response_status(http_message_object *obj, zval *return_value TSRMLS_DC) {
278 if (HTTP_MSG_TYPE(RESPONSE, obj->message) && obj->message->http.info.response.status) {
279 RETVAL_STRING(obj->message->http.info.response.status, 1);
280 } else {
281 RETVAL_NULL();
282 }
283 }
284 static void http_message_object_prophandler_set_response_status(http_message_object *obj, zval *value TSRMLS_DC) {
285 if (HTTP_MSG_TYPE(RESPONSE, obj->message)) {
286 zval *cpy = http_zsep(IS_STRING, value);
287 STR_SET(obj->message->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
288 zval_ptr_dtor(&cpy);
289 }
290 }
291 static void http_message_object_prophandler_get_response_code(http_message_object *obj, zval *return_value TSRMLS_DC) {
292 if (HTTP_MSG_TYPE(RESPONSE, obj->message)) {
293 RETVAL_LONG(obj->message->http.info.response.code);
294 } else {
295 RETVAL_NULL();
296 }
297 }
298 static void http_message_object_prophandler_set_response_code(http_message_object *obj, zval *value TSRMLS_DC) {
299 if (HTTP_MSG_TYPE(RESPONSE, obj->message)) {
300 zval *cpy = http_zsep(IS_LONG, value);
301 obj->message->http.info.response.code = Z_LVAL_P(cpy);
302 zval_ptr_dtor(&cpy);
303 }
304 }
305 static void http_message_object_prophandler_get_http_version(http_message_object *obj, zval *return_value TSRMLS_DC) {
306 RETVAL_DOUBLE(obj->message->http.version);
307 }
308 static void http_message_object_prophandler_set_http_version(http_message_object *obj, zval *value TSRMLS_DC) {
309 zval *cpy = http_zsep(IS_DOUBLE, value);
310 obj->message->http.version = Z_DVAL_P(cpy);
311 zval_ptr_dtor(&cpy);
312 }
313 static void http_message_object_prophandler_get_headers(http_message_object *obj, zval *return_value TSRMLS_DC) {
314 array_init(return_value);
315 zend_hash_copy(Z_ARRVAL_P(return_value), &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
316 }
317 static void http_message_object_prophandler_set_headers(http_message_object *obj, zval *value TSRMLS_DC) {
318 zval *cpy = http_zsep(IS_ARRAY, value);
319 zend_hash_clean(&obj->message->hdrs);
320 zend_hash_copy(&obj->message->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
321 zval_ptr_dtor(&cpy);
322 }
323 static void http_message_object_prophandler_get_parent_message(http_message_object *obj, zval *return_value TSRMLS_DC) {
324 if (obj->message->parent) {
325 RETVAL_OBJVAL(obj->parent, 1);
326 } else {
327 RETVAL_NULL();
328 }
329 }
330 static void http_message_object_prophandler_set_parent_message(http_message_object *obj, zval *value TSRMLS_DC) {
331 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), http_message_object_ce TSRMLS_CC)) {
332 if (obj->message->parent) {
333 zval tmp;
334 tmp.value.obj = obj->parent;
335 Z_OBJ_DELREF(tmp);
336 }
337 Z_OBJ_ADDREF_P(value);
338 obj->parent = value->value.obj;
339 }
340 }
341
342 PHP_MINIT_FUNCTION(http_message_object)
343 {
344 HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
345
346 #ifndef WONKY
347 # ifdef HTTP_HAVE_SPL
348 zend_class_implements(http_message_object_ce TSRMLS_CC, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
349 # else
350 zend_class_implements(http_message_object_ce TSRMLS_CC, 2, zend_ce_serializable, zend_ce_iterator);
351 # endif
352 #else
353 zend_class_implements(http_message_object_ce TSRMLS_CC, 1, zend_ce_iterator);
354 #endif
355
356 http_message_object_handlers.clone_obj = _http_message_object_clone_obj;
357 http_message_object_handlers.read_property = http_message_object_read_prop;
358 http_message_object_handlers.write_property = http_message_object_write_prop;
359 http_message_object_handlers.get_properties = http_message_object_get_props;
360 http_message_object_handlers.get_property_ptr_ptr = http_message_object_get_prop_ptr;
361
362 zend_hash_init(&http_message_object_prophandlers, 9, NULL, NULL, 1);
363 zend_declare_property_long(THIS_CE, ZEND_STRS("type")-1, HTTP_MSG_NONE, ZEND_ACC_PROTECTED TSRMLS_CC);
364 http_message_object_add_prophandler(ZEND_STRS("type")-1, http_message_object_prophandler_get_type, http_message_object_prophandler_set_type);
365 zend_declare_property_string(THIS_CE, ZEND_STRS("body")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
366 http_message_object_add_prophandler(ZEND_STRS("body")-1, http_message_object_prophandler_get_body, http_message_object_prophandler_set_body);
367 zend_declare_property_string(THIS_CE, ZEND_STRS("requestMethod")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
368 http_message_object_add_prophandler(ZEND_STRS("requestMethod")-1, http_message_object_prophandler_get_request_method, http_message_object_prophandler_set_request_method);
369 zend_declare_property_string(THIS_CE, ZEND_STRS("requestUrl")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
370 http_message_object_add_prophandler(ZEND_STRS("requestUrl")-1, http_message_object_prophandler_get_request_url, http_message_object_prophandler_set_request_url);
371 zend_declare_property_string(THIS_CE, ZEND_STRS("responseStatus")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
372 http_message_object_add_prophandler(ZEND_STRS("responseStatus")-1, http_message_object_prophandler_get_response_status, http_message_object_prophandler_set_response_status);
373 zend_declare_property_long(THIS_CE, ZEND_STRS("responseCode")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC);
374 http_message_object_add_prophandler(ZEND_STRS("responseCode")-1, http_message_object_prophandler_get_response_code, http_message_object_prophandler_set_response_code);
375 zend_declare_property_null(THIS_CE, ZEND_STRS("httpVersion")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
376 http_message_object_add_prophandler(ZEND_STRS("httpVersion")-1, http_message_object_prophandler_get_http_version, http_message_object_prophandler_set_http_version);
377 zend_declare_property_null(THIS_CE, ZEND_STRS("headers")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
378 http_message_object_add_prophandler(ZEND_STRS("headers")-1, http_message_object_prophandler_get_headers, http_message_object_prophandler_set_headers);
379 zend_declare_property_null(THIS_CE, ZEND_STRS("parentMessage")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
380 http_message_object_add_prophandler(ZEND_STRS("parentMessage")-1, http_message_object_prophandler_get_parent_message, http_message_object_prophandler_set_parent_message);
381
382 #ifndef WONKY
383 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_NONE")-1, HTTP_MSG_NONE TSRMLS_CC);
384 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_REQUEST")-1, HTTP_MSG_REQUEST TSRMLS_CC);
385 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_RESPONSE")-1, HTTP_MSG_RESPONSE TSRMLS_CC);
386 #endif
387
388 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
389 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
390 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
391
392 return SUCCESS;
393 }
394
395 PHP_MSHUTDOWN_FUNCTION(http_message_object)
396 {
397 zend_hash_destroy(&http_message_object_prophandlers);
398
399 return SUCCESS;
400 }
401
402 void _http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_DC)
403 {
404 int i;
405 getObject(http_message_object, obj);
406
407 /* count */
408 http_message_count(i, obj->message);
409
410 if (i > 1) {
411 zval o;
412 zend_object_value *ovalues = NULL;
413 http_message_object **objects = NULL;
414 int last = i - 1;
415
416 objects = ecalloc(i, sizeof(http_message_object *));
417 ovalues = ecalloc(i, sizeof(zend_object_value));
418
419 /* we are the first message */
420 objects[0] = obj;
421 ovalues[0] = getThis()->value.obj;
422
423 /* fetch parents */
424 INIT_PZVAL(&o);
425 o.type = IS_OBJECT;
426 for (i = 1; obj->parent.handle; ++i) {
427 o.value.obj = obj->parent;
428 ovalues[i] = o.value.obj;
429 objects[i] = obj = zend_object_store_get_object(&o TSRMLS_CC);
430 }
431
432 /* reorder parents */
433 for (last = --i; i; --i) {
434 objects[i]->message->parent = objects[i-1]->message;
435 objects[i]->parent = ovalues[i-1];
436 }
437 objects[0]->message->parent = NULL;
438 objects[0]->parent.handle = 0;
439 objects[0]->parent.handlers = NULL;
440
441 /* add ref (why?) */
442 Z_OBJ_ADDREF_P(getThis());
443 RETVAL_OBJVAL(ovalues[last], 1);
444
445 efree(objects);
446 efree(ovalues);
447 } else {
448 RETURN_ZVAL(getThis(), 1, 0);
449 }
450 }
451
452 void _http_message_object_prepend_ex(zval *this_ptr, zval *prepend, zend_bool top TSRMLS_DC)
453 {
454 zval m;
455 http_message *save_parent_msg = NULL;
456 zend_object_value save_parent_obj = {0, NULL};
457 getObject(http_message_object, obj);
458 getObjectEx(http_message_object, prepend_obj, prepend);
459
460 INIT_PZVAL(&m);
461 m.type = IS_OBJECT;
462
463 if (!top) {
464 save_parent_obj = obj->parent;
465 save_parent_msg = obj->message->parent;
466 } else {
467 /* iterate to the most parent object */
468 while (obj->parent.handle) {
469 m.value.obj = obj->parent;
470 obj = zend_object_store_get_object(&m TSRMLS_CC);
471 }
472 }
473
474 /* prepend */
475 obj->parent = prepend->value.obj;
476 obj->message->parent = prepend_obj->message;
477
478 /* add ref */
479 zend_objects_store_add_ref(prepend TSRMLS_CC);
480 while (prepend_obj->parent.handle) {
481 m.value.obj = prepend_obj->parent;
482 zend_objects_store_add_ref(&m TSRMLS_CC);
483 prepend_obj = zend_object_store_get_object(&m TSRMLS_CC);
484 }
485
486 if (!top) {
487 prepend_obj->parent = save_parent_obj;
488 prepend_obj->message->parent = save_parent_msg;
489 }
490 }
491
492 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
493 {
494 return http_message_object_new_ex(ce, NULL, NULL);
495 }
496
497 zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg, http_message_object **ptr TSRMLS_DC)
498 {
499 zend_object_value ov;
500 http_message_object *o;
501
502 o = ecalloc(1, sizeof(http_message_object));
503 o->zo.ce = ce;
504
505 if (ptr) {
506 *ptr = o;
507 }
508
509 if (msg) {
510 o->message = msg;
511 if (msg->parent) {
512 o->parent = http_message_object_new_ex(ce, msg->parent, NULL);
513 }
514 }
515
516 #if PHP_VERSION_ID < 50399
517 ALLOC_HASHTABLE(OBJ_PROP(o));
518 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
519 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
520 #else
521 zend_object_std_init(&o->zo, ce TSRMLS_CC);
522 object_properties_init(&o->zo, ce);
523 #endif
524
525 ov.handle = putObject(http_message_object, o);
526 ov.handlers = &http_message_object_handlers;
527
528 return ov;
529 }
530
531 zend_object_value _http_message_object_clone_obj(zval *this_ptr TSRMLS_DC)
532 {
533 zend_object_value new_ov;
534 http_message_object *new_obj = NULL;
535 getObject(http_message_object, old_obj);
536
537 new_ov = http_message_object_new_ex(old_obj->zo.ce, http_message_dup(old_obj->message), &new_obj);
538 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
539
540 return new_ov;
541 }
542
543 void _http_message_object_free(zend_object *object TSRMLS_DC)
544 {
545 http_message_object *o = (http_message_object *) object;
546
547 if (o->iterator) {
548 zval_ptr_dtor(&o->iterator);
549 o->iterator = NULL;
550 }
551 if (o->message) {
552 http_message_dtor(o->message);
553 efree(o->message);
554 }
555 if (o->parent.handle) {
556 zval p;
557
558 INIT_PZVAL(&p);
559 p.type = IS_OBJECT;
560 p.value.obj = o->parent;
561 zend_objects_store_del_ref(&p TSRMLS_CC);
562 }
563 freeObject(o);
564 }
565
566 static zval **_http_message_object_get_prop_ptr(zval *object, zval *member ZEND_LITERAL_KEY_DC TSRMLS_DC) {
567 getObjectEx(http_message_object, obj, object);
568 http_message_object_prophandler *handler;
569
570 if (SUCCESS == http_message_object_get_prophandler(Z_STRVAL_P(member), Z_STRLEN_P(member), &handler)) {
571 zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
572 return NULL;
573 }
574
575 return zend_get_std_object_handlers()->get_property_ptr_ptr(object, member ZEND_LITERAL_KEY_CC TSRMLS_CC);
576 }
577
578 static zval *_http_message_object_read_prop(zval *object, zval *member, int type ZEND_LITERAL_KEY_DC TSRMLS_DC)
579 {
580 getObjectEx(http_message_object, obj, object);
581 http_message_object_prophandler *handler;
582 zval *return_value, *tmp_member = NULL;
583
584 if (Z_TYPE_P(member) != IS_STRING) {
585 ALLOC_ZVAL(tmp_member);
586 MAKE_COPY_ZVAL(&member, tmp_member);
587 convert_to_string(tmp_member);
588 member = tmp_member;
589 #if PHP_VERSION_ID >= 50399
590 _zend_literal_key = NULL;
591 #endif
592 }
593
594 if (SUCCESS == http_message_object_get_prophandler(Z_STRVAL_P(member), Z_STRLEN_P(member), &handler)) {
595 if (type == BP_VAR_W) {
596 zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
597 if (tmp_member) {
598 zval_ptr_dtor(&tmp_member);
599 }
600 return NULL;
601 }
602
603 ALLOC_ZVAL(return_value);
604 #ifdef Z_SET_REFCOUNT
605 Z_SET_REFCOUNT_P(return_value, 0);
606 Z_UNSET_ISREF_P(return_value);
607 #else
608 return_value->refcount = 0;
609 return_value->is_ref = 0;
610 #endif
611
612 handler->read(obj, return_value TSRMLS_CC);
613
614 } else {
615 return_value = zend_get_std_object_handlers()->read_property(object, member, type ZEND_LITERAL_KEY_CC TSRMLS_CC);
616 }
617
618 if (tmp_member) {
619 zval_ptr_dtor(&tmp_member);
620 }
621 return return_value;
622 }
623
624 static void _http_message_object_write_prop(zval *object, zval *member, zval *value ZEND_LITERAL_KEY_DC TSRMLS_DC)
625 {
626 getObjectEx(http_message_object, obj, object);
627 http_message_object_prophandler *handler;
628 zval *tmp_member = NULL;
629
630 if (Z_TYPE_P(member) != IS_STRING) {
631 ALLOC_ZVAL(tmp_member);
632 MAKE_COPY_ZVAL(&member, tmp_member);
633 convert_to_string(tmp_member);
634 member = tmp_member;
635 #if PHP_VERSION_ID >= 50399
636 _zend_literal_key = NULL;
637 #endif
638 }
639
640 if (SUCCESS == http_message_object_get_prophandler(Z_STRVAL_P(member), Z_STRLEN_P(member), &handler)) {
641 handler->write(obj, value TSRMLS_CC);
642 } else {
643 zend_get_std_object_handlers()->write_property(object, member, value ZEND_LITERAL_KEY_CC TSRMLS_CC);
644 }
645 if (tmp_member) {
646 zval_ptr_dtor(&tmp_member);
647 }
648 }
649
650 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
651 {
652 zval *headers;
653 getObjectEx(http_message_object, obj, object);
654 http_message *msg = obj->message;
655 #if PHP_VERSION_ID < 50399
656 HashTable *props = OBJ_PROP(obj);
657 #else
658 HashTable *props = zend_std_get_properties(object TSRMLS_CC);
659 #endif
660 zval array, *parent;
661
662 INIT_ZARR(array, props);
663
664 #define ASSOC_PROP(array, ptype, name, val) \
665 { \
666 char *m_prop_name; \
667 int m_prop_len; \
668 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
669 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
670 efree(m_prop_name); \
671 }
672 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
673 #define ASSOC_STRINGL(array, name, val, len) \
674 { \
675 char *m_prop_name; \
676 int m_prop_len; \
677 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
678 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
679 efree(m_prop_name); \
680 }
681
682 ASSOC_PROP(array, long, "type", msg->type);
683 ASSOC_PROP(array, double, "httpVersion", msg->http.version);
684
685 switch (msg->type) {
686 case HTTP_MSG_REQUEST:
687 ASSOC_PROP(array, long, "responseCode", 0);
688 ASSOC_STRINGL(array, "responseStatus", "", 0);
689 ASSOC_STRING(array, "requestMethod", STR_PTR(msg->http.info.request.method));
690 ASSOC_STRING(array, "requestUrl", STR_PTR(msg->http.info.request.url));
691 break;
692
693 case HTTP_MSG_RESPONSE:
694 ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
695 ASSOC_STRING(array, "responseStatus", STR_PTR(msg->http.info.response.status));
696 ASSOC_STRINGL(array, "requestMethod", "", 0);
697 ASSOC_STRINGL(array, "requestUrl", "", 0);
698 break;
699
700 case HTTP_MSG_NONE:
701 default:
702 ASSOC_PROP(array, long, "responseCode", 0);
703 ASSOC_STRINGL(array, "responseStatus", "", 0);
704 ASSOC_STRINGL(array, "requestMethod", "", 0);
705 ASSOC_STRINGL(array, "requestUrl", "", 0);
706 break;
707 }
708
709 MAKE_STD_ZVAL(headers);
710 array_init(headers);
711 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
712 ASSOC_PROP(array, zval, "headers", headers);
713 ASSOC_STRINGL(array, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
714
715 MAKE_STD_ZVAL(parent);
716 if (msg->parent) {
717 ZVAL_OBJVAL(parent, obj->parent, 1);
718 } else {
719 ZVAL_NULL(parent);
720 }
721 ASSOC_PROP(array, zval, "parentMessage", parent);
722
723 return OBJ_PROP(obj);
724 }
725
726 /* ### USERLAND ### */
727
728 /* {{{ proto void HttpMessage::__construct([string message])
729 Create a new HttpMessage object instance. */
730 PHP_METHOD(HttpMessage, __construct)
731 {
732 int length = 0;
733 char *message = NULL;
734
735 getObject(http_message_object, obj);
736
737 SET_EH_THROW_HTTP();
738 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
739 http_message *msg = obj->message;
740
741 http_message_dtor(msg);
742 if ((obj->message = http_message_parse_ex(msg, message, length))) {
743 if (obj->message->parent) {
744 obj->parent = http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL);
745 }
746 } else {
747 obj->message = http_message_init(msg);
748 }
749 }
750 if (!obj->message) {
751 obj->message = http_message_new();
752 }
753 SET_EH_NORMAL();
754 }
755 /* }}} */
756
757 /* {{{ proto static HttpMessage HttpMessage::factory([string raw_message[, string class_name = "HttpMessage"]])
758 Create a new HttpMessage object instance. */
759 PHP_METHOD(HttpMessage, factory)
760 {
761 char *string = NULL, *cn = NULL;
762 int length = 0, cl = 0;
763 http_message *msg = NULL;
764 zend_object_value ov;
765 http_message_object *obj = NULL;
766
767 RETVAL_NULL();
768
769 SET_EH_THROW_HTTP();
770 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &string, &length, &cn, &cl)) {
771 if (length) {
772 msg = http_message_parse(string, length);
773 }
774 if ((msg || !length) && SUCCESS == http_object_new(&ov, cn, cl, _http_message_object_new_ex, http_message_object_ce, msg, &obj)) {
775 RETVAL_OBJVAL(ov, 0);
776 }
777 if (obj && !obj->message) {
778 obj->message = http_message_new();
779 }
780 }
781 SET_EH_NORMAL();
782 }
783 /* }}} */
784
785 /* {{{ proto static HttpMessage HttpMessage::fromEnv(int type[, string class_name = "HttpMessage"])
786 Create a new HttpMessage object from environment representing either current request or response */
787 PHP_METHOD(HttpMessage, fromEnv)
788 {
789 char *cn = NULL;
790 int cl = 0;
791 long type;
792 http_message_object *obj = NULL;
793 zend_object_value ov;
794
795 RETVAL_NULL();
796 SET_EH_THROW_HTTP();
797 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s", &type, &cn, &cl)) {
798 if (SUCCESS == http_object_new(&ov, cn, cl, _http_message_object_new_ex, http_message_object_ce, http_message_init_env(NULL, type), &obj)) {
799 RETVAL_OBJVAL(ov, 0);
800 }
801 if (obj && !obj->message) {
802 obj->message = http_message_new();
803 }
804 }
805 SET_EH_NORMAL();
806 }
807 /* }}} */
808
809 /* {{{ proto string HttpMessage::getBody()
810 Get the body of the parsed HttpMessage. */
811 PHP_METHOD(HttpMessage, getBody)
812 {
813 NO_ARGS;
814
815 if (return_value_used) {
816 getObject(http_message_object, obj);
817 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
818 }
819 }
820 /* }}} */
821
822 /* {{{ proto void HttpMessage::setBody(string body)
823 Set the body of the HttpMessage. NOTE: Don't forget to update any headers accordingly. */
824 PHP_METHOD(HttpMessage, setBody)
825 {
826 char *body;
827 int len;
828 getObject(http_message_object, obj);
829
830 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &body, &len)) {
831 phpstr_dtor(PHPSTR(obj->message));
832 phpstr_from_string_ex(PHPSTR(obj->message), body, len);
833 }
834 }
835 /* }}} */
836
837 /* {{{ proto string HttpMessage::getHeader(string header)
838 Get message header. */
839 PHP_METHOD(HttpMessage, getHeader)
840 {
841 zval *header;
842 char *orig_header, *nice_header;
843 int header_len;
844 getObject(http_message_object, obj);
845
846 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &orig_header, &header_len)) {
847 RETURN_FALSE;
848 }
849
850 nice_header = pretty_key(estrndup(orig_header, header_len), header_len, 1, 1);
851 if ((header = http_message_header_ex(obj->message, nice_header, header_len + 1, 0))) {
852 RETVAL_ZVAL(header, 1, 1);
853 }
854 efree(nice_header);
855 }
856 /* }}} */
857
858 /* {{{ proto array HttpMessage::getHeaders()
859 Get Message Headers. */
860 PHP_METHOD(HttpMessage, getHeaders)
861 {
862 NO_ARGS;
863
864 if (return_value_used) {
865 getObject(http_message_object, obj);
866
867 array_init(return_value);
868 array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
869 }
870 }
871 /* }}} */
872
873 /* {{{ proto void HttpMessage::setHeaders(array headers)
874 Sets new headers. */
875 PHP_METHOD(HttpMessage, setHeaders)
876 {
877 zval *new_headers = NULL;
878 getObject(http_message_object, obj);
879
880 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &new_headers)) {
881 return;
882 }
883
884 zend_hash_clean(&obj->message->hdrs);
885 if (new_headers) {
886 array_copy(Z_ARRVAL_P(new_headers), &obj->message->hdrs);
887 }
888 }
889 /* }}} */
890
891 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
892 Add headers. If append is true, headers with the same name will be separated, else overwritten. */
893 PHP_METHOD(HttpMessage, addHeaders)
894 {
895 zval *new_headers;
896 zend_bool append = 0;
897 getObject(http_message_object, obj);
898
899 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
900 return;
901 }
902
903 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
904 }
905 /* }}} */
906
907 /* {{{ proto int HttpMessage::getType()
908 Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
909 PHP_METHOD(HttpMessage, getType)
910 {
911 NO_ARGS;
912
913 if (return_value_used) {
914 getObject(http_message_object, obj);
915 RETURN_LONG(obj->message->type);
916 }
917 }
918 /* }}} */
919
920 /* {{{ proto void HttpMessage::setType(int type)
921 Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE) */
922 PHP_METHOD(HttpMessage, setType)
923 {
924 long type;
925 getObject(http_message_object, obj);
926
927 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
928 return;
929 }
930 http_message_set_type(obj->message, type);
931 }
932 /* }}} */
933
934 /* {{{ proto string HttpMessage::getInfo(void)
935 Get the HTTP request/response line */
936 PHP_METHOD(HttpMessage, getInfo)
937 {
938 NO_ARGS;
939
940 if (return_value_used) {
941 getObject(http_message_object, obj);
942
943 switch (obj->message->type) {
944 case HTTP_MSG_REQUEST:
945 Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, ""));
946 break;
947 case HTTP_MSG_RESPONSE:
948 Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, ""));
949 break;
950 default:
951 RETURN_NULL();
952 break;
953 }
954 Z_TYPE_P(return_value) = IS_STRING;
955 }
956 }
957 /* }}} */
958
959 /* {{{ proto bool HttpMessage::setInfo(string http_info)
960 Set type and request or response info with a standard HTTP request or response line */
961 PHP_METHOD(HttpMessage, setInfo)
962 {
963 char *str;
964 int len;
965 http_info inf;
966
967 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) && SUCCESS == http_info_parse_ex(str, &inf, 0)) {
968 getObject(http_message_object, obj);
969
970 http_message_set_info(obj->message, &inf);
971 http_info_dtor(&inf);
972 RETURN_TRUE;
973 }
974 RETURN_FALSE;
975 }
976 /* }}} */
977
978 /* {{{ proto int HttpMessage::getResponseCode()
979 Get the Response Code of the Message. */
980 PHP_METHOD(HttpMessage, getResponseCode)
981 {
982 NO_ARGS;
983
984 if (return_value_used) {
985 getObject(http_message_object, obj);
986 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
987 RETURN_LONG(obj->message->http.info.response.code);
988 }
989 }
990 /* }}} */
991
992 /* {{{ proto bool HttpMessage::setResponseCode(int code)
993 Set the response code of an HTTP Response Message. */
994 PHP_METHOD(HttpMessage, setResponseCode)
995 {
996 long code;
997 getObject(http_message_object, obj);
998
999 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
1000
1001 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
1002 RETURN_FALSE;
1003 }
1004 if (code < 100 || code > 599) {
1005 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid response code (100-599): %ld", code);
1006 RETURN_FALSE;
1007 }
1008
1009 obj->message->http.info.response.code = code;
1010 RETURN_TRUE;
1011 }
1012 /* }}} */
1013
1014 /* {{{ proto string HttpMessage::getResponseStatus()
1015 Get the Response Status of the message (i.e. the string following the response code). */
1016 PHP_METHOD(HttpMessage, getResponseStatus)
1017 {
1018 NO_ARGS;
1019
1020 if (return_value_used) {
1021 getObject(http_message_object, obj);
1022 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
1023 if (obj->message->http.info.response.status) {
1024 RETURN_STRING(obj->message->http.info.response.status, 1);
1025 } else {
1026 RETURN_EMPTY_STRING();
1027 }
1028 }
1029 }
1030 /* }}} */
1031
1032 /* {{{ proto bool HttpMessage::setResponseStatus(string status)
1033 Set the Response Status of the HTTP message (i.e. the string following the response code). */
1034 PHP_METHOD(HttpMessage, setResponseStatus)
1035 {
1036 char *status;
1037 int status_len;
1038 getObject(http_message_object, obj);
1039
1040 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
1041
1042 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len)) {
1043 RETURN_FALSE;
1044 }
1045 STR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
1046 RETURN_TRUE;
1047 }
1048 /* }}} */
1049
1050 /* {{{ proto string HttpMessage::getRequestMethod()
1051 Get the Request Method of the Message. */
1052 PHP_METHOD(HttpMessage, getRequestMethod)
1053 {
1054 NO_ARGS;
1055
1056 if (return_value_used) {
1057 getObject(http_message_object, obj);
1058 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1059 if (obj->message->http.info.request.method) {
1060 RETURN_STRING(obj->message->http.info.request.method, 1);
1061 } else {
1062 RETURN_EMPTY_STRING();
1063 }
1064 }
1065 }
1066 /* }}} */
1067
1068 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
1069 Set the Request Method of the HTTP Message. */
1070 PHP_METHOD(HttpMessage, setRequestMethod)
1071 {
1072 char *method;
1073 int method_len;
1074 getObject(http_message_object, obj);
1075
1076 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1077
1078 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1079 RETURN_FALSE;
1080 }
1081 if (method_len < 1) {
1082 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
1083 RETURN_FALSE;
1084 }
1085 if (!http_request_method_exists(1, 0, method)) {
1086 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unknown request method: %s", method);
1087 RETURN_FALSE;
1088 }
1089
1090 STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
1091 RETURN_TRUE;
1092 }
1093 /* }}} */
1094
1095 /* {{{ proto string HttpMessage::getRequestUrl()
1096 Get the Request URL of the Message. */
1097 PHP_METHOD(HttpMessage, getRequestUrl)
1098 {
1099 NO_ARGS;
1100
1101 if (return_value_used) {
1102 getObject(http_message_object, obj);
1103 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1104 if (obj->message->http.info.request.url) {
1105 RETURN_STRING(obj->message->http.info.request.url, 1);
1106 } else {
1107 RETURN_EMPTY_STRING();
1108 }
1109 }
1110 }
1111 /* }}} */
1112
1113 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
1114 Set the Request URL of the HTTP Message. */
1115 PHP_METHOD(HttpMessage, setRequestUrl)
1116 {
1117 char *URI;
1118 int URIlen;
1119 getObject(http_message_object, obj);
1120
1121 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
1122 RETURN_FALSE;
1123 }
1124 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
1125 if (URIlen < 1) {
1126 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUrl to an empty string");
1127 RETURN_FALSE;
1128 }
1129
1130 STR_SET(obj->message->http.info.request.url, estrndup(URI, URIlen));
1131 RETURN_TRUE;
1132 }
1133 /* }}} */
1134
1135 /* {{{ proto string HttpMessage::getHttpVersion()
1136 Get the HTTP Protocol Version of the Message. */
1137 PHP_METHOD(HttpMessage, getHttpVersion)
1138 {
1139 NO_ARGS;
1140
1141 if (return_value_used) {
1142 char *version;
1143 getObject(http_message_object, obj);
1144
1145 spprintf(&version, 0, "%1.1F", obj->message->http.version);
1146 RETURN_STRING(version, 0);
1147 }
1148 }
1149 /* }}} */
1150
1151 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
1152 Set the HTTP Protocol version of the Message. */
1153 PHP_METHOD(HttpMessage, setHttpVersion)
1154 {
1155 zval *zv;
1156 char *version;
1157 getObject(http_message_object, obj);
1158
1159 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
1160 return;
1161 }
1162
1163 convert_to_double(zv);
1164 spprintf(&version, 0, "%1.1F", Z_DVAL_P(zv));
1165 if (strcmp(version, "1.0") && strcmp(version, "1.1")) {
1166 efree(version);
1167 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %g", Z_DVAL_P(zv));
1168 RETURN_FALSE;
1169 }
1170 efree(version);
1171 obj->message->http.version = Z_DVAL_P(zv);
1172 RETURN_TRUE;
1173 }
1174 /* }}} */
1175
1176 /* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
1177 Attempts to guess the content type of supplied payload through libmagic. */
1178 PHP_METHOD(HttpMessage, guessContentType)
1179 {
1180 #ifdef HTTP_HAVE_MAGIC
1181 char *magic_file, *ct = NULL;
1182 int magic_file_len;
1183 long magic_mode = MAGIC_MIME;
1184
1185 RETVAL_FALSE;
1186 SET_EH_THROW_HTTP();
1187 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) {
1188 getObject(http_message_object, obj);
1189 if ((ct = http_guess_content_type(magic_file, magic_mode, PHPSTR_VAL(&obj->message->body), PHPSTR_LEN(&obj->message->body), SEND_DATA))) {
1190 RETVAL_STRING(ct, 0);
1191 }
1192 }
1193 SET_EH_NORMAL();
1194 #else
1195 http_error(HE_THROW, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
1196 RETURN_FALSE;
1197 #endif
1198 }
1199 /* }}} */
1200
1201 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
1202 Get parent Message. */
1203 PHP_METHOD(HttpMessage, getParentMessage)
1204 {
1205 SET_EH_THROW_HTTP();
1206 NO_ARGS {
1207 getObject(http_message_object, obj);
1208
1209 if (obj->message->parent) {
1210 RETVAL_OBJVAL(obj->parent, 1);
1211 } else {
1212 http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpMessage does not have a parent message");
1213 }
1214 }
1215 SET_EH_NORMAL();
1216 }
1217 /* }}} */
1218
1219 /* {{{ proto bool HttpMessage::send()
1220 Send the Message according to its type as Response or Request. */
1221 PHP_METHOD(HttpMessage, send)
1222 {
1223 getObject(http_message_object, obj);
1224
1225 NO_ARGS;
1226
1227 RETURN_SUCCESS(http_message_send(obj->message));
1228 }
1229 /* }}} */
1230
1231 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1232 Get the string representation of the Message. */
1233 PHP_METHOD(HttpMessage, toString)
1234 {
1235 if (return_value_used) {
1236 char *string;
1237 size_t length;
1238 zend_bool include_parent = 0;
1239 getObject(http_message_object, obj);
1240
1241 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
1242 RETURN_FALSE;
1243 }
1244
1245 if (include_parent) {
1246 http_message_serialize(obj->message, &string, &length);
1247 } else {
1248 http_message_tostring(obj->message, &string, &length);
1249 }
1250 RETURN_STRINGL(string, length, 0);
1251 }
1252 }
1253 /* }}} */
1254
1255 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1256 Creates an object regarding to the type of the message. Returns either an HttpRequest or HttpResponse object on success, or NULL on failure. */
1257 PHP_METHOD(HttpMessage, toMessageTypeObject)
1258 {
1259 SET_EH_THROW_HTTP();
1260
1261 NO_ARGS;
1262
1263 if (return_value_used) {
1264 getObject(http_message_object, obj);
1265
1266 switch (obj->message->type) {
1267 case HTTP_MSG_REQUEST:
1268 {
1269 #ifdef HTTP_HAVE_CURL
1270 int method;
1271 char *url;
1272 zval post, body, *array, *headers, *host = http_message_header(obj->message, "Host");
1273 php_url hurl, *purl = php_url_parse(STR_PTR(obj->message->http.info.request.url));
1274
1275 MAKE_STD_ZVAL(array);
1276 array_init(array);
1277
1278 memset(&hurl, 0, sizeof(php_url));
1279 if (host) {
1280 hurl.host = Z_STRVAL_P(host);
1281 zval_ptr_dtor(&host);
1282 }
1283 http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
1284 php_url_free(purl);
1285 add_assoc_string(array, "url", url, 0);
1286
1287 if ( obj->message->http.info.request.method &&
1288 ((method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
1289 (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method))))) {
1290 add_assoc_long(array, "method", method);
1291 }
1292
1293 if (10 == (int) (obj->message->http.version * 10)) {
1294 add_assoc_long(array, "protocol", CURL_HTTP_VERSION_1_0);
1295 }
1296
1297 MAKE_STD_ZVAL(headers);
1298 array_init(headers);
1299 array_copy(&obj->message->hdrs, Z_ARRVAL_P(headers));
1300 add_assoc_zval(array, "headers", headers);
1301
1302 object_init_ex(return_value, http_request_object_ce);
1303 zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setoptions", NULL, array);
1304 zval_ptr_dtor(&array);
1305
1306 if (PHPSTR_VAL(obj->message) && PHPSTR_LEN(obj->message)) {
1307 phpstr_fix(PHPSTR(obj->message));
1308 INIT_PZVAL(&body);
1309 ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
1310 if (method != HTTP_POST) {
1311 zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setbody", NULL, &body);
1312 } else {
1313 INIT_PZVAL(&post);
1314 array_init(&post);
1315
1316 zval_copy_ctor(&body);
1317 sapi_module.treat_data(PARSE_STRING, Z_STRVAL(body), &post TSRMLS_CC);
1318 zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setpostfields", NULL, &post);
1319 zval_dtor(&post);
1320 }
1321 }
1322 #else
1323 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1324 #endif
1325 break;
1326 }
1327
1328 case HTTP_MSG_RESPONSE:
1329 {
1330 #ifndef WONKY
1331 HashPosition pos1, pos2;
1332 HashKey key = initHashKey(0);
1333 zval **header, **h, *body;
1334
1335 if (obj->message->http.info.response.code) {
1336 http_send_status(obj->message->http.info.response.code);
1337 }
1338
1339 object_init_ex(return_value, http_response_object_ce);
1340
1341 FOREACH_HASH_KEYVAL(pos1, &obj->message->hdrs, key, header) {
1342 if (key.type == HASH_KEY_IS_STRING) {
1343 zval *zkey;
1344
1345 MAKE_STD_ZVAL(zkey);
1346 ZVAL_STRINGL(zkey, key.str, key.len - 1, 1);
1347
1348 switch (Z_TYPE_PP(header)) {
1349 case IS_ARRAY:
1350 case IS_OBJECT:
1351 FOREACH_HASH_VAL(pos2, HASH_OF(*header), h) {
1352 ZVAL_ADDREF(*h);
1353 zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, zkey, *h);
1354 zval_ptr_dtor(h);
1355 }
1356 break;
1357
1358 default:
1359 ZVAL_ADDREF(*header);
1360 zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, zkey, *header);
1361 zval_ptr_dtor(header);
1362 break;
1363 }
1364 zval_ptr_dtor(&zkey);
1365 }
1366 }
1367
1368 MAKE_STD_ZVAL(body);
1369 ZVAL_STRINGL(body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 1);
1370 zend_call_method_with_1_params(&return_value, http_response_object_ce, NULL, "setdata", NULL, body);
1371 zval_ptr_dtor(&body);
1372 #else
1373 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1374 #endif
1375 break;
1376 }
1377
1378 default:
1379 http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1380 break;
1381 }
1382 }
1383 SET_EH_NORMAL();
1384 }
1385 /* }}} */
1386
1387 /* {{{ proto int HttpMessage::count()
1388 Implements Countable::count(). Returns the number of parent messages + 1. */
1389 PHP_METHOD(HttpMessage, count)
1390 {
1391 NO_ARGS {
1392 long i;
1393 getObject(http_message_object, obj);
1394
1395 http_message_count(i, obj->message);
1396 RETURN_LONG(i);
1397 }
1398 }
1399 /* }}} */
1400
1401 /* {{{ proto string HttpMessage::serialize()
1402 Implements Serializable::serialize(). Returns the serialized representation of the HttpMessage. */
1403 PHP_METHOD(HttpMessage, serialize)
1404 {
1405 NO_ARGS {
1406 char *string;
1407 size_t length;
1408 getObject(http_message_object, obj);
1409
1410 http_message_serialize(obj->message, &string, &length);
1411 RETURN_STRINGL(string, length, 0);
1412 }
1413 }
1414 /* }}} */
1415
1416 /* {{{ proto void HttpMessage::unserialize(string serialized)
1417 Implements Serializable::unserialize(). Re-constructs the HttpMessage based upon the serialized string. */
1418 PHP_METHOD(HttpMessage, unserialize)
1419 {
1420 int length;
1421 char *serialized;
1422 getObject(http_message_object, obj);
1423
1424 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
1425 http_message *msg;
1426
1427 http_message_dtor(obj->message);
1428 if ((msg = http_message_parse_ex(obj->message, serialized, (size_t) length))) {
1429 obj->message = msg;
1430 } else {
1431 http_message_init(obj->message);
1432 http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
1433 }
1434 }
1435 }
1436 /* }}} */
1437
1438 /* {{{ proto HttpMessage HttpMessage::detach(void)
1439 Returns a clone of an HttpMessage object detached from any parent messages. */
1440 PHP_METHOD(HttpMessage, detach)
1441 {
1442 http_info info;
1443 http_message *msg;
1444 getObject(http_message_object, obj);
1445
1446 NO_ARGS;
1447
1448 info.type = obj->message->type;
1449 memcpy(&HTTP_INFO(&info), &HTTP_INFO(obj->message), sizeof(struct http_info));
1450
1451 msg = http_message_new();
1452 http_message_set_info(msg, &info);
1453
1454 zend_hash_copy(&msg->hdrs, &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1455 phpstr_append(&msg->body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message));
1456
1457 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg, NULL), 0);
1458 }
1459 /* }}} */
1460
1461 /* {{{ proto void HttpMessage::prepend(HttpMessage message[, bool top = true])
1462 Prepends message(s) to the HTTP message. Throws HttpInvalidParamException if the message is located within the same message chain. */
1463 PHP_METHOD(HttpMessage, prepend)
1464 {
1465 zval *prepend;
1466 zend_bool top = 1;
1467
1468 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, http_message_object_ce, &top)) {
1469 http_message *msg[2];
1470 getObject(http_message_object, obj);
1471 getObjectEx(http_message_object, prepend_obj, prepend);
1472
1473 /* safety check */
1474 for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
1475 for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
1476 if (msg[0] == msg[1]) {
1477 http_error(HE_THROW, HTTP_E_INVALID_PARAM, "Cannot prepend a message located within the same message chain");
1478 return;
1479 }
1480 }
1481 }
1482
1483 http_message_object_prepend_ex(getThis(), prepend, top);
1484 }
1485 }
1486 /* }}} */
1487
1488 /* {{{ proto HttpMessage HttpMessage::reverse()
1489 Reorders the message chain in reverse order. Returns the most parent HttpMessage object. */
1490 PHP_METHOD(HttpMessage, reverse)
1491 {
1492 NO_ARGS {
1493 http_message_object_reverse(getThis(), return_value);
1494 }
1495 }
1496 /* }}} */
1497
1498 /* {{{ proto void HttpMessage::rewind(void)
1499 Implements Iterator::rewind(). */
1500 PHP_METHOD(HttpMessage, rewind)
1501 {
1502 NO_ARGS {
1503 getObject(http_message_object, obj);
1504
1505 if (obj->iterator) {
1506 zval_ptr_dtor(&obj->iterator);
1507 }
1508 ZVAL_ADDREF(getThis());
1509 obj->iterator = getThis();
1510 }
1511 }
1512 /* }}} */
1513
1514 /* {{{ proto bool HttpMessage::valid(void)
1515 Implements Iterator::valid(). */
1516 PHP_METHOD(HttpMessage, valid)
1517 {
1518 NO_ARGS {
1519 getObject(http_message_object, obj);
1520
1521 RETURN_BOOL(obj->iterator != NULL);
1522 }
1523 }
1524 /* }}} */
1525
1526 /* {{{ proto void HttpMessage::next(void)
1527 Implements Iterator::next(). */
1528 PHP_METHOD(HttpMessage, next)
1529 {
1530 NO_ARGS {
1531 getObject(http_message_object, obj);
1532 if (obj->iterator) {
1533 getObjectEx(http_message_object, itr, obj->iterator);
1534
1535 if (itr && itr->parent.handle) {
1536 zval *old = obj->iterator;
1537 MAKE_STD_ZVAL(obj->iterator);
1538 ZVAL_OBJVAL(obj->iterator, itr->parent, 1);
1539 zval_ptr_dtor(&old);
1540 } else {
1541 zval_ptr_dtor(&obj->iterator);
1542 obj->iterator = NULL;
1543 }
1544 }
1545 }
1546 }
1547 /* }}} */
1548
1549 /* {{{ proto int HttpMessage::key(void)
1550 Implements Iterator::key(). */
1551 PHP_METHOD(HttpMessage, key)
1552 {
1553 NO_ARGS {
1554 getObject(http_message_object, obj);
1555
1556 RETURN_LONG(obj->iterator ? obj->iterator->value.obj.handle:0);
1557 }
1558 }
1559 /* }}} */
1560
1561 /* {{{ proto HttpMessage HttpMessage::current(void)
1562 Implements Iterator::current(). */
1563 PHP_METHOD(HttpMessage, current)
1564 {
1565 NO_ARGS {
1566 getObject(http_message_object, obj);
1567
1568 if (obj->iterator) {
1569 RETURN_ZVAL(obj->iterator, 1, 0);
1570 }
1571 }
1572 }
1573 /* }}} */
1574
1575 #endif /* ZEND_ENGINE_2 */
1576
1577 /*
1578 * Local variables:
1579 * tab-width: 4
1580 * c-basic-offset: 4
1581 * End:
1582 * vim600: noet sw=4 ts=4 fdm=marker
1583 * vim<600: noet sw=4 ts=4
1584 */
1585