- solve that another way
[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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #include "php_http.h"
18
19 #ifdef ZEND_ENGINE_2
20
21 #include "ext/standard/url.h"
22
23 #include "php_http_api.h"
24 #include "php_http_send_api.h"
25 #include "php_http_url_api.h"
26 #include "php_http_message_api.h"
27 #include "php_http_message_object.h"
28 #include "php_http_exception_object.h"
29 #include "php_http_response_object.h"
30 #include "php_http_request_method_api.h"
31 #include "php_http_request_api.h"
32 #include "php_http_request_object.h"
33
34 #ifndef WONKY
35 # include "zend_interfaces.h"
36 # ifdef HAVE_SPL
37 /* SPL doesn't install its headers */
38 extern PHPAPI zend_class_entry *spl_ce_Countable;
39 # endif
40 #endif
41
42 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
43 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
44 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
45
46 HTTP_BEGIN_ARGS(__construct, 0, 0)
47 HTTP_ARG_VAL(message, 0)
48 HTTP_END_ARGS;
49
50 HTTP_BEGIN_ARGS(fromString, 1, 1)
51 HTTP_ARG_VAL(message, 0)
52 HTTP_END_ARGS;
53
54 HTTP_EMPTY_ARGS(getBody, 0);
55 HTTP_BEGIN_ARGS(setBody, 0, 1)
56 HTTP_ARG_VAL(body, 0)
57 HTTP_END_ARGS;
58
59 HTTP_EMPTY_ARGS(getHeaders, 0);
60 HTTP_BEGIN_ARGS(setHeaders, 0, 1)
61 HTTP_ARG_VAL(headers, 0)
62 HTTP_END_ARGS;
63
64 HTTP_BEGIN_ARGS(addHeaders, 0, 1)
65 HTTP_ARG_VAL(headers, 0)
66 HTTP_ARG_VAL(append, 0)
67 HTTP_END_ARGS;
68
69 HTTP_EMPTY_ARGS(getType, 0);
70 HTTP_BEGIN_ARGS(setType, 0, 1)
71 HTTP_ARG_VAL(type, 0)
72 HTTP_END_ARGS;
73
74 HTTP_EMPTY_ARGS(getResponseCode, 0);
75 HTTP_BEGIN_ARGS(setResponseCode, 0, 1)
76 HTTP_ARG_VAL(response_code, 0)
77 HTTP_END_ARGS;
78
79 HTTP_EMPTY_ARGS(getRequestMethod, 0);
80 HTTP_BEGIN_ARGS(setRequestMethod, 0, 1)
81 HTTP_ARG_VAL(request_method, 0)
82 HTTP_END_ARGS;
83
84 HTTP_EMPTY_ARGS(getRequestUrl, 0);
85 HTTP_BEGIN_ARGS(setRequestUrl, 0, 1)
86 HTTP_ARG_VAL(url, 0)
87 HTTP_END_ARGS;
88
89 HTTP_EMPTY_ARGS(getHttpVersion, 0);
90 HTTP_BEGIN_ARGS(setHttpVersion, 0, 1)
91 HTTP_ARG_VAL(http_version, 0)
92 HTTP_END_ARGS;
93
94 HTTP_EMPTY_ARGS(getParentMessage, 0);
95 HTTP_EMPTY_ARGS(send, 0);
96 HTTP_BEGIN_ARGS(toString, 0, 0)
97 HTTP_ARG_VAL(include_parent, 0)
98 HTTP_END_ARGS;
99
100 HTTP_EMPTY_ARGS(toMessageTypeObject, 0);
101
102 HTTP_EMPTY_ARGS(count, 0);
103
104 HTTP_EMPTY_ARGS(serialize, 0);
105 HTTP_BEGIN_ARGS(unserialize, 0, 1)
106 HTTP_ARG_VAL(serialized, 0)
107 HTTP_END_ARGS;
108
109 HTTP_EMPTY_ARGS(rewind, 0);
110 HTTP_EMPTY_ARGS(valid, 0);
111 HTTP_EMPTY_ARGS(key, 0);
112 HTTP_EMPTY_ARGS(current, 0);
113 HTTP_EMPTY_ARGS(next, 0);
114
115 HTTP_EMPTY_ARGS(detach, 0);
116 HTTP_BEGIN_ARGS(prepend, 0, 1)
117 HTTP_ARG_OBJ(HttpMessage, message, 0)
118 HTTP_END_ARGS;
119
120 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
121 static inline void _http_message_object_declare_default_properties(TSRMLS_D);
122 #define http_message_object_read_prop _http_message_object_read_prop
123 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC);
124 #define http_message_object_write_prop _http_message_object_write_prop
125 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC);
126 #define http_message_object_get_props _http_message_object_get_props
127 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
128
129 zend_class_entry *http_message_object_ce;
130 zend_function_entry http_message_object_fe[] = {
131 HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
132 HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC)
133 HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC)
134 HTTP_MESSAGE_ME(getHeaders, ZEND_ACC_PUBLIC)
135 HTTP_MESSAGE_ME(setHeaders, ZEND_ACC_PUBLIC)
136 HTTP_MESSAGE_ME(addHeaders, ZEND_ACC_PUBLIC)
137 HTTP_MESSAGE_ME(getType, ZEND_ACC_PUBLIC)
138 HTTP_MESSAGE_ME(setType, ZEND_ACC_PUBLIC)
139 HTTP_MESSAGE_ME(getResponseCode, ZEND_ACC_PUBLIC)
140 HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
141 HTTP_MESSAGE_ME(getRequestMethod, ZEND_ACC_PUBLIC)
142 HTTP_MESSAGE_ME(setRequestMethod, ZEND_ACC_PUBLIC)
143 HTTP_MESSAGE_ME(getRequestUrl, ZEND_ACC_PUBLIC)
144 HTTP_MESSAGE_ME(setRequestUrl, ZEND_ACC_PUBLIC)
145 HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC)
146 HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
147 HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
148 HTTP_MESSAGE_ME(send, ZEND_ACC_PUBLIC)
149 HTTP_MESSAGE_ME(toString, ZEND_ACC_PUBLIC)
150 HTTP_MESSAGE_ME(toMessageTypeObject, ZEND_ACC_PUBLIC)
151
152 /* implements Countable */
153 HTTP_MESSAGE_ME(count, ZEND_ACC_PUBLIC)
154
155 /* implements Serializable */
156 HTTP_MESSAGE_ME(serialize, ZEND_ACC_PUBLIC)
157 HTTP_MESSAGE_ME(unserialize, ZEND_ACC_PUBLIC)
158
159 /* implements Iterator */
160 HTTP_MESSAGE_ME(rewind, ZEND_ACC_PUBLIC)
161 HTTP_MESSAGE_ME(valid, ZEND_ACC_PUBLIC)
162 HTTP_MESSAGE_ME(current, ZEND_ACC_PUBLIC)
163 HTTP_MESSAGE_ME(key, ZEND_ACC_PUBLIC)
164 HTTP_MESSAGE_ME(next, ZEND_ACC_PUBLIC)
165
166 ZEND_MALIAS(HttpMessage, __toString, toString, HTTP_ARGS(HttpMessage, toString), ZEND_ACC_PUBLIC)
167
168 HTTP_MESSAGE_ME(fromString, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
169
170 HTTP_MESSAGE_ME(detach, ZEND_ACC_PUBLIC)
171 HTTP_MESSAGE_ME(prepend, ZEND_ACC_PUBLIC)
172
173 EMPTY_FUNCTION_ENTRY
174 };
175 static zend_object_handlers http_message_object_handlers;
176
177 PHP_MINIT_FUNCTION(http_message_object)
178 {
179 HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
180 #ifndef WONKY
181 # ifdef HAVE_SPL
182 zend_class_implements(http_message_object_ce TSRMLS_CC, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
183 # else
184 zend_class_implements(http_message_object_ce TSRMLS_CC, 2, zend_ce_serializable, zend_ce_iterator);
185 # endif
186 #else
187 zend_class_implements(http_message_object_ce TSRMLS_CC, 1, zend_ce_iterator);
188 #endif
189
190 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
191 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
192 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
193
194 http_message_object_handlers.clone_obj = _http_message_object_clone_obj;
195 http_message_object_handlers.read_property = http_message_object_read_prop;
196 http_message_object_handlers.write_property = http_message_object_write_prop;
197 http_message_object_handlers.get_properties = http_message_object_get_props;
198 http_message_object_handlers.get_property_ptr_ptr = NULL;
199
200 return SUCCESS;
201 }
202
203 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
204 {
205 return http_message_object_new_ex(ce, NULL, NULL);
206 }
207
208 zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg, http_message_object **ptr TSRMLS_DC)
209 {
210 zend_object_value ov;
211 http_message_object *o;
212
213 o = ecalloc(1, sizeof(http_message_object));
214 o->zo.ce = ce;
215
216 if (ptr) {
217 *ptr = o;
218 }
219
220 if (msg) {
221 o->message = msg;
222 if (msg->parent) {
223 o->parent = http_message_object_new_ex(ce, msg->parent, NULL);
224 }
225 }
226
227 ALLOC_HASHTABLE(OBJ_PROP(o));
228 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
229
230 ov.handle = putObject(http_message_object, o);
231 ov.handlers = &http_message_object_handlers;
232
233 return ov;
234 }
235
236 zend_object_value _http_message_object_clone_obj(zval *this_ptr TSRMLS_DC)
237 {
238 getObject(http_message_object, obj);
239 return http_message_object_new_ex(Z_OBJCE_P(this_ptr), http_message_dup(obj->message), NULL);
240 }
241
242 static inline void _http_message_object_declare_default_properties(TSRMLS_D)
243 {
244 zend_class_entry *ce = http_message_object_ce;
245
246 #ifndef WONKY
247 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE);
248 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST);
249 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE);
250 #endif
251
252 DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
253 DCL_PROP(PROTECTED, string, body, "");
254 DCL_PROP(PROTECTED, string, requestMethod, "");
255 DCL_PROP(PROTECTED, string, requestUrl, "");
256 DCL_PROP(PROTECTED, long, responseCode, 0);
257 DCL_PROP_N(PROTECTED, httpVersion);
258 DCL_PROP_N(PROTECTED, headers);
259 DCL_PROP_N(PROTECTED, parentMessage);
260 }
261
262 void _http_message_object_free(zend_object *object TSRMLS_DC)
263 {
264 http_message_object *o = (http_message_object *) object;
265
266 if (OBJ_PROP(o)) {
267 zend_hash_destroy(OBJ_PROP(o));
268 FREE_HASHTABLE(OBJ_PROP(o));
269 }
270 if (o->message) {
271 http_message_dtor(o->message);
272 efree(o->message);
273 }
274 if (o->parent.handle) {
275 zval p;
276
277 INIT_PZVAL(&p);
278 p.type = IS_OBJECT;
279 p.value.obj = o->parent;
280 zend_objects_store_del_ref(&p TSRMLS_CC);
281 }
282 efree(o);
283 }
284
285 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
286 {
287 getObjectEx(http_message_object, obj, object);
288 http_message *msg = obj->message;
289 zval *return_value;
290 #ifdef WONKY
291 ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
292 #else
293 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
294
295 if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
296 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
297 }
298 #endif
299
300 if (type == BP_VAR_W) {
301 zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
302 return NULL;
303 }
304
305 ALLOC_ZVAL(return_value);
306 return_value->refcount = 0;
307 return_value->is_ref = 0;
308
309 #ifdef WONKY
310 switch (h)
311 #else
312 switch (pinfo->h)
313 #endif
314 {
315 case HTTP_MSG_PROPHASH_TYPE:
316 case HTTP_MSG_CHILD_PROPHASH_TYPE:
317 RETVAL_LONG(msg->type);
318 break;
319
320 case HTTP_MSG_PROPHASH_HTTP_VERSION:
321 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
322 RETVAL_DOUBLE(msg->http.version);
323 break;
324
325 case HTTP_MSG_PROPHASH_BODY:
326 case HTTP_MSG_CHILD_PROPHASH_BODY:
327 phpstr_fix(PHPSTR(msg));
328 RETVAL_PHPSTR(PHPSTR(msg), 0, 1);
329 break;
330
331 case HTTP_MSG_PROPHASH_HEADERS:
332 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
333 array_init(return_value);
334 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
335 break;
336
337 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
338 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
339 if (msg->parent) {
340 RETVAL_OBJVAL(obj->parent, 1);
341 } else {
342 RETVAL_NULL();
343 }
344 break;
345
346 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
347 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
348 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.method) {
349 RETVAL_STRING(msg->http.info.request.method, 1);
350 } else {
351 RETVAL_NULL();
352 }
353 break;
354
355 case HTTP_MSG_PROPHASH_REQUEST_URL:
356 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
357 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.url) {
358 RETVAL_STRING(msg->http.info.request.url, 1);
359 } else {
360 RETVAL_NULL();
361 }
362 break;
363
364 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
365 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
366 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
367 RETVAL_LONG(msg->http.info.response.code);
368 } else {
369 RETVAL_NULL();
370 }
371 break;
372
373 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
374 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
375 if (HTTP_MSG_TYPE(RESPONSE, msg) && msg->http.info.response.status) {
376 RETVAL_STRING(msg->http.info.response.status, 1);
377 } else {
378 RETVAL_NULL();
379 }
380 break;
381
382 default:
383 #ifdef WONKY
384 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
385 #else
386 RETVAL_NULL();
387 #endif
388 break;
389 }
390
391 return return_value;
392 }
393
394 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
395 {
396 getObjectEx(http_message_object, obj, object);
397 http_message *msg = obj->message;
398 zval *cpy = NULL;
399 #ifdef WONKY
400 ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
401 #else
402 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
403
404 if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
405 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
406 return;
407 }
408 #endif
409
410 cpy = zval_copy(Z_TYPE_P(value), value);
411
412 #ifdef WONKY
413 switch (h)
414 #else
415 switch (pinfo->h)
416 #endif
417 {
418 case HTTP_MSG_PROPHASH_TYPE:
419 case HTTP_MSG_CHILD_PROPHASH_TYPE:
420 convert_to_long(cpy);
421 http_message_set_type(msg, Z_LVAL_P(cpy));
422 break;
423
424 case HTTP_MSG_PROPHASH_HTTP_VERSION:
425 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
426 convert_to_double(cpy);
427 msg->http.version = Z_DVAL_P(cpy);
428 break;
429
430 case HTTP_MSG_PROPHASH_BODY:
431 case HTTP_MSG_CHILD_PROPHASH_BODY:
432 convert_to_string(cpy);
433 phpstr_dtor(PHPSTR(msg));
434 phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
435 break;
436
437 case HTTP_MSG_PROPHASH_HEADERS:
438 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
439 convert_to_array(cpy);
440 zend_hash_clean(&msg->hdrs);
441 zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
442 break;
443
444 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
445 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
446 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), http_message_object_ce TSRMLS_CC)) {
447 if (msg->parent) {
448 zval tmp;
449 tmp.value.obj = obj->parent;
450 Z_OBJ_DELREF(tmp);
451 }
452 Z_OBJ_ADDREF_P(value);
453 obj->parent = value->value.obj;
454 }
455 break;
456
457 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
458 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
459 if (HTTP_MSG_TYPE(REQUEST, msg)) {
460 convert_to_string(cpy);
461 STR_SET(msg->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
462 }
463 break;
464
465 case HTTP_MSG_PROPHASH_REQUEST_URL:
466 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
467 if (HTTP_MSG_TYPE(REQUEST, msg)) {
468 convert_to_string(cpy);
469 STR_SET(msg->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
470 }
471 break;
472
473 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
474 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
475 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
476 convert_to_long(cpy);
477 msg->http.info.response.code = Z_LVAL_P(cpy);
478 }
479 break;
480
481 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
482 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
483 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
484 convert_to_string(cpy);
485 STR_SET(msg->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
486 }
487 break;
488
489 default:
490 #ifdef WONKY
491 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
492 #endif
493 break;
494 }
495 zval_free(&cpy);
496 }
497
498 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
499 {
500 zval *headers;
501 getObjectEx(http_message_object, obj, object);
502 http_message *msg = obj->message;
503 HashTable *props = OBJ_PROP(obj);
504 zval array;
505
506 INIT_ZARR(array, props);
507
508 #define ASSOC_PROP(array, ptype, name, val) \
509 { \
510 char *m_prop_name; \
511 int m_prop_len; \
512 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
513 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
514 efree(m_prop_name); \
515 }
516 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
517 #define ASSOC_STRINGL(array, name, val, len) \
518 { \
519 char *m_prop_name; \
520 int m_prop_len; \
521 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
522 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
523 efree(m_prop_name); \
524 }
525
526 ASSOC_PROP(array, long, "type", msg->type);
527 ASSOC_PROP(array, double, "httpVersion", msg->http.version);
528
529 switch (msg->type)
530 {
531 case HTTP_MSG_REQUEST:
532 ASSOC_PROP(array, long, "responseCode", 0);
533 ASSOC_STRINGL(array, "responseStatus", "", 0);
534 ASSOC_STRING(array, "requestMethod", msg->http.info.request.method);
535 ASSOC_STRING(array, "requestUrl", msg->http.info.request.url);
536 break;
537
538 case HTTP_MSG_RESPONSE:
539 ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
540 ASSOC_STRING(array, "responseStatus", msg->http.info.response.status);
541 ASSOC_STRINGL(array, "requestMethod", "", 0);
542 ASSOC_STRINGL(array, "requestUrl", "", 0);
543 break;
544
545 case HTTP_MSG_NONE:
546 default:
547 ASSOC_PROP(array, long, "responseCode", 0);
548 ASSOC_STRINGL(array, "responseStatus", "", 0);
549 ASSOC_STRINGL(array, "requestMethod", "", 0);
550 ASSOC_STRINGL(array, "requestUrl", "", 0);
551 break;
552 }
553
554 MAKE_STD_ZVAL(headers);
555 array_init(headers);
556 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
557 ASSOC_PROP(array, zval, "headers", headers);
558 ASSOC_STRINGL(array, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
559
560 return OBJ_PROP(obj);
561 }
562
563 /* ### USERLAND ### */
564
565 /* {{{ proto void HttpMessage::__construct([string message])
566 *
567 * Instantiate a new HttpMessage object.
568 *
569 * Accepts an optional string parameter containing a single or several
570 * consecutive HTTP messages. The constructed object will actually
571 * represent the *last* message of the passed string. If there were
572 * prior messages, those can be accessed by HttpMessage::getParentMessage().
573 *
574 * Throws HttpMalformedHeaderException.
575 */
576 PHP_METHOD(HttpMessage, __construct)
577 {
578 int length = 0;
579 char *message = NULL;
580
581 getObject(http_message_object, obj);
582
583 SET_EH_THROW_HTTP();
584 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
585 http_message *msg = obj->message;
586
587 http_message_dtor(msg);
588 if ((obj->message = http_message_parse_ex(msg, message, length))) {
589 if (obj->message->parent) {
590 obj->parent = http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL);
591 }
592 } else {
593 obj->message = http_message_init(msg);
594 }
595 }
596 if (!obj->message) {
597 obj->message = http_message_new();
598 }
599 SET_EH_NORMAL();
600 }
601 /* }}} */
602
603 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message[, string class_name = "HttpMessage"])
604 *
605 * Create an HttpMessage object from a string. Kind of a static constructor.
606 *
607 * Expects a string parameter containing a sinlge or several consecutive
608 * HTTP messages. Accepts an optionsl string parameter specifying the class to use.
609 *
610 * Returns an HttpMessage object on success or NULL on failure.
611 *
612 * Throws HttpMalformedHeadersException.
613 */
614 PHP_METHOD(HttpMessage, fromString)
615 {
616 char *string = NULL, *class_name = NULL;
617 int length = 0, class_length = 0;
618 http_message *msg = NULL;
619
620 RETVAL_NULL();
621
622 SET_EH_THROW_HTTP();
623 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &string, &length, &class_name, &class_length)) {
624 if ((msg = http_message_parse(string, length))) {
625 zend_class_entry *ce = http_message_object_ce;
626
627 if (class_name && *class_name) {
628 ce = zend_fetch_class(class_name, class_length, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
629 if (ce && !instanceof_function(ce, http_message_object_ce TSRMLS_CC)) {
630 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Class %s does not extend HttpMessage", class_name);
631 ce = NULL;
632 }
633 }
634 if (ce) {
635 RETVAL_OBJVAL(http_message_object_new_ex(ce, msg, NULL), 0);
636 }
637 }
638 }
639 SET_EH_NORMAL();
640 }
641 /* }}} */
642
643 /* {{{ proto string HttpMessage::getBody()
644 *
645 * Get the body of the parsed HttpMessage.
646 *
647 * Returns the message body as string.
648 */
649 PHP_METHOD(HttpMessage, getBody)
650 {
651 NO_ARGS;
652
653 IF_RETVAL_USED {
654 getObject(http_message_object, obj);
655 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
656 }
657 }
658 /* }}} */
659
660 /* {{{ proto void HttpMessage::setBody(string body)
661 *
662 * Set the body of the HttpMessage.
663 * NOTE: Don't forget to update any headers accordingly.
664 *
665 * Expects a string parameter containing the new body of the message.
666 */
667 PHP_METHOD(HttpMessage, setBody)
668 {
669 char *body;
670 int len;
671 getObject(http_message_object, obj);
672
673 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &body, &len)) {
674 phpstr_dtor(PHPSTR(obj->message));
675 phpstr_from_string_ex(PHPSTR(obj->message), body, len);
676 }
677 }
678 /* }}} */
679
680 /* {{{ proto array HttpMessage::getHeaders()
681 *
682 * Get Message Headers.
683 *
684 * Returns an associative array containing the messages HTTP headers.
685 */
686 PHP_METHOD(HttpMessage, getHeaders)
687 {
688 NO_ARGS;
689
690 IF_RETVAL_USED {
691 zval headers;
692 getObject(http_message_object, obj);
693
694 INIT_ZARR(headers, &obj->message->hdrs);
695 array_init(return_value);
696 array_copy(&headers, return_value);
697 }
698 }
699 /* }}} */
700
701 /* {{{ proto void HttpMessage::setHeaders(array headers)
702 *
703 * Sets new headers.
704 *
705 * Expects an associative array as parameter containing the new HTTP headers,
706 * which will replace *all* previous HTTP headers of the message.
707 */
708 PHP_METHOD(HttpMessage, setHeaders)
709 {
710 zval *new_headers, old_headers;
711 getObject(http_message_object, obj);
712
713 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
714 return;
715 }
716
717 zend_hash_clean(&obj->message->hdrs);
718 INIT_ZARR(old_headers, &obj->message->hdrs);
719 array_copy(new_headers, &old_headers);
720 }
721 /* }}} */
722
723 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
724 *
725 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
726 *
727 * Expects an associative array as parameter containing the additional HTTP headers
728 * to add to the messages existing headers. If the optional bool parameter is true,
729 * and a header with the same name of one to add exists already, this respective
730 * header will be converted to an array containing both header values, otherwise
731 * it will be overwritten with the new header value.
732 */
733 PHP_METHOD(HttpMessage, addHeaders)
734 {
735 zval old_headers, *new_headers;
736 zend_bool append = 0;
737 getObject(http_message_object, obj);
738
739 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
740 return;
741 }
742
743 INIT_ZARR(old_headers, &obj->message->hdrs);
744 if (append) {
745 array_append(new_headers, &old_headers);
746 } else {
747 array_merge(new_headers, &old_headers);
748 }
749 }
750 /* }}} */
751
752 /* {{{ proto int HttpMessage::getType()
753 *
754 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
755 *
756 * Returns the HttpMessage::TYPE.
757 */
758 PHP_METHOD(HttpMessage, getType)
759 {
760 NO_ARGS;
761
762 IF_RETVAL_USED {
763 getObject(http_message_object, obj);
764 RETURN_LONG(obj->message->type);
765 }
766 }
767 /* }}} */
768
769 /* {{{ proto void HttpMessage::setType(int type)
770 *
771 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
772 *
773 * Exptects an int parameter, the HttpMessage::TYPE.
774 */
775 PHP_METHOD(HttpMessage, setType)
776 {
777 long type;
778 getObject(http_message_object, obj);
779
780 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
781 return;
782 }
783 http_message_set_type(obj->message, type);
784 }
785 /* }}} */
786
787 /* {{{ proto int HttpMessage::getResponseCode()
788 *
789 * Get the Response Code of the Message.
790 *
791 * Returns the HTTP response code if the message is of type
792 * HttpMessage::TYPE_RESPONSE, else FALSE.
793 */
794 PHP_METHOD(HttpMessage, getResponseCode)
795 {
796 NO_ARGS;
797
798 IF_RETVAL_USED {
799 getObject(http_message_object, obj);
800 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
801 RETURN_LONG(obj->message->http.info.response.code);
802 }
803 }
804 /* }}} */
805
806 /* {{{ proto bool HttpMessage::setResponseCode(int code)
807 *
808 * Set the response code of an HTTP Response Message.
809 *
810 * Expects an int parameter with the HTTP response code.
811 *
812 * Returns TRUE on success, or FALSE if the message is not of type
813 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
814 */
815 PHP_METHOD(HttpMessage, setResponseCode)
816 {
817 long code;
818 getObject(http_message_object, obj);
819
820 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
821
822 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
823 RETURN_FALSE;
824 }
825 if (code < 100 || code > 510) {
826 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid response code (100-510): %ld", code);
827 RETURN_FALSE;
828 }
829
830 obj->message->http.info.response.code = code;
831 RETURN_TRUE;
832 }
833 /* }}} */
834
835 /* {{{ proto string HttpMessage::getRequestMethod()
836 *
837 * Get the Request Method of the Message.
838 *
839 * Returns the request method name on success, or FALSE if the message is
840 * not of type HttpMessage::TYPE_REQUEST.
841 */
842 PHP_METHOD(HttpMessage, getRequestMethod)
843 {
844 NO_ARGS;
845
846 IF_RETVAL_USED {
847 getObject(http_message_object, obj);
848 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
849 RETURN_STRING(obj->message->http.info.request.method, 1);
850 }
851 }
852 /* }}} */
853
854 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
855 *
856 * Set the Request Method of the HTTP Message.
857 *
858 * Expects a string parameter containing the request method name.
859 *
860 * Returns TRUE on success, or FALSE if the message is not of type
861 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
862 */
863 PHP_METHOD(HttpMessage, setRequestMethod)
864 {
865 char *method;
866 int method_len;
867 getObject(http_message_object, obj);
868
869 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
870
871 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
872 RETURN_FALSE;
873 }
874 if (method_len < 1) {
875 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
876 RETURN_FALSE;
877 }
878 if (SUCCESS != http_check_method(method)) {
879 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unkown request method: %s", method);
880 RETURN_FALSE;
881 }
882
883 STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
884 RETURN_TRUE;
885 }
886 /* }}} */
887
888 /* {{{ proto string HttpMessage::getRequestUrl()
889 *
890 * Get the Request URL of the Message.
891 *
892 * Returns the request url as string on success, or FALSE if the message
893 * is not of type HttpMessage::TYPE_REQUEST.
894 */
895 PHP_METHOD(HttpMessage, getRequestUrl)
896 {
897 NO_ARGS;
898
899 IF_RETVAL_USED {
900 getObject(http_message_object, obj);
901 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
902 RETURN_STRING(obj->message->http.info.request.url, 1);
903 }
904 }
905 /* }}} */
906
907 /* {{{ proto bool HttpMessage::setRequestUrl(string url)
908 *
909 * Set the Request URL of the HTTP Message.
910 *
911 * Expects a string parameters containing the request url.
912 *
913 * Returns TRUE on success, or FALSE if the message is not of type
914 * HttpMessage::TYPE_REQUEST or supplied URL was empty.
915 */
916 PHP_METHOD(HttpMessage, setRequestUrl)
917 {
918 char *URI;
919 int URIlen;
920 getObject(http_message_object, obj);
921
922 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
923 RETURN_FALSE;
924 }
925 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
926 if (URIlen < 1) {
927 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUrl to an empty string");
928 RETURN_FALSE;
929 }
930
931 STR_SET(obj->message->http.info.request.url, estrndup(URI, URIlen));
932 RETURN_TRUE;
933 }
934 /* }}} */
935
936 /* {{{ proto string HttpMessage::getHttpVersion()
937 *
938 * Get the HTTP Protocol Version of the Message.
939 *
940 * Returns the HTTP protocol version as string.
941 */
942 PHP_METHOD(HttpMessage, getHttpVersion)
943 {
944 NO_ARGS;
945
946 IF_RETVAL_USED {
947 char ver[4] = {0};
948 getObject(http_message_object, obj);
949
950 sprintf(ver, "%1.1lf", obj->message->http.version);
951 RETURN_STRINGL(ver, 3, 1);
952 }
953 }
954 /* }}} */
955
956 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
957 *
958 * Set the HTTP Protocol version of the Message.
959 *
960 * Expects a string parameter containing the HTTP protocol version.
961 *
962 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
963 */
964 PHP_METHOD(HttpMessage, setHttpVersion)
965 {
966 char v[4];
967 zval *zv;
968 getObject(http_message_object, obj);
969
970 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
971 return;
972 }
973
974 convert_to_double(zv);
975 sprintf(v, "%1.1lf", Z_DVAL_P(zv));
976 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
977 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
978 RETURN_FALSE;
979 }
980
981 obj->message->http.version = Z_DVAL_P(zv);
982 RETURN_TRUE;
983 }
984 /* }}} */
985
986 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
987 *
988 * Get parent Message.
989 *
990 * Returns the parent HttpMessage on success, or NULL if there's none.
991 */
992 PHP_METHOD(HttpMessage, getParentMessage)
993 {
994 NO_ARGS;
995
996 IF_RETVAL_USED {
997 getObject(http_message_object, obj);
998
999 if (obj->message->parent) {
1000 RETVAL_OBJVAL(obj->parent, 1);
1001 } else {
1002 RETVAL_NULL();
1003 }
1004 }
1005 }
1006 /* }}} */
1007
1008 /* {{{ proto bool HttpMessage::send()
1009 *
1010 * Send the Message according to its type as Response or Request.
1011 * This provides limited functionality compared to HttpRequest and HttpResponse.
1012 *
1013 * Returns TRUE on success, or FALSE on failure.
1014 */
1015 PHP_METHOD(HttpMessage, send)
1016 {
1017 getObject(http_message_object, obj);
1018
1019 NO_ARGS;
1020
1021 RETURN_SUCCESS(http_message_send(obj->message));
1022 }
1023 /* }}} */
1024
1025 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
1026 *
1027 * Get the string representation of the Message.
1028 *
1029 * Accepts a bool parameter which specifies whether the returned string
1030 * should also contain any parent messages.
1031 *
1032 * Returns the full message as string.
1033 */
1034 PHP_METHOD(HttpMessage, toString)
1035 {
1036 IF_RETVAL_USED {
1037 char *string;
1038 size_t length;
1039 zend_bool include_parent = 0;
1040 getObject(http_message_object, obj);
1041
1042 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
1043 RETURN_FALSE;
1044 }
1045
1046 if (include_parent) {
1047 http_message_serialize(obj->message, &string, &length);
1048 } else {
1049 http_message_tostring(obj->message, &string, &length);
1050 }
1051 RETURN_STRINGL(string, length, 0);
1052 }
1053 }
1054 /* }}} */
1055
1056 /* {{{ proto HttpRequest|HttpResponse HttpMessage::toMessageTypeObject(void)
1057 *
1058 * Creates an object regarding to the type of the message.
1059 *
1060 * Returns either an HttpRequest or HttpResponse object on success, or NULL on failure.
1061 *
1062 * Throws HttpRuntimeException, HttpMessageTypeException, HttpHeaderException.
1063 */
1064 PHP_METHOD(HttpMessage, toMessageTypeObject)
1065 {
1066 SET_EH_THROW_HTTP();
1067
1068 NO_ARGS;
1069
1070 IF_RETVAL_USED {
1071 getObject(http_message_object, obj);
1072
1073 switch (obj->message->type)
1074 {
1075 case HTTP_MSG_REQUEST:
1076 {
1077 #ifdef HTTP_HAVE_CURL
1078 int method;
1079 char *url;
1080 zval tmp, body, *array, *headers, *host = http_message_header(obj->message, "Host");
1081 php_url hurl, *purl = php_url_parse(obj->message->http.info.request.url);
1082
1083 MAKE_STD_ZVAL(array);
1084 array_init(array);
1085
1086 memset(&hurl, 0, sizeof(php_url));
1087 hurl.host = host ? Z_STRVAL_P(host) : NULL;
1088 http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL);
1089 php_url_free(purl);
1090 add_assoc_string(array, "url", url, 0);
1091
1092 if ( (method = http_request_method_exists(1, 0, obj->message->http.info.request.method)) ||
1093 (method = http_request_method_register(obj->message->http.info.request.method, strlen(obj->message->http.info.request.method)))) {
1094 add_assoc_long(array, "method", method);
1095 }
1096
1097 if (10 == (int) (obj->message->http.version * 10)) {
1098 add_assoc_long(array, "protocol", CURL_HTTP_VERSION_1_0);
1099 }
1100
1101 MAKE_STD_ZVAL(headers);
1102 array_init(headers);
1103 INIT_ZARR(tmp, &obj->message->hdrs);
1104 array_copy(&tmp, headers);
1105 add_assoc_zval(array, "headers", headers);
1106
1107 object_init_ex(return_value, http_request_object_ce);
1108 zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setoptions", NULL, array);
1109 zval_ptr_dtor(&array);
1110
1111 INIT_PZVAL(&body);
1112 ZVAL_STRINGL(&body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 0);
1113 zend_call_method_with_1_params(&return_value, http_request_object_ce, NULL, "setrawpostdata", NULL, &body);
1114 #else
1115 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpRequest (missing curl support)");
1116 #endif
1117 }
1118 break;
1119
1120 case HTTP_MSG_RESPONSE:
1121 {
1122 #ifndef WONKY
1123 HashPosition pos1, pos2;
1124 ulong idx;
1125 uint key_len;
1126 char *key = NULL;
1127 zval **header, **h, *body;
1128
1129 if (obj->message->http.info.response.code) {
1130 http_send_status(obj->message->http.info.response.code);
1131 }
1132
1133 object_init_ex(return_value, http_response_object_ce);
1134
1135 FOREACH_HASH_KEYLENVAL(pos1, &obj->message->hdrs, key, key_len, idx, header) {
1136 if (key) {
1137 zval zkey;
1138
1139 INIT_PZVAL(&zkey);
1140 ZVAL_STRINGL(&zkey, key, key_len, 0);
1141
1142 switch (Z_TYPE_PP(header))
1143 {
1144 case IS_ARRAY:
1145 case IS_OBJECT:
1146 FOREACH_HASH_VAL(pos2, HASH_OF(*header), h) {
1147 ZVAL_ADDREF(*h);
1148 zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, &zkey, *h);
1149 zval_ptr_dtor(h);
1150 }
1151 break;
1152
1153 default:
1154 ZVAL_ADDREF(*header);
1155 zend_call_method_with_2_params(&return_value, http_response_object_ce, NULL, "setheader", NULL, &zkey, *header);
1156 zval_ptr_dtor(header);
1157 break;
1158 }
1159 key = NULL;
1160 }
1161 }
1162
1163 MAKE_STD_ZVAL(body);
1164 ZVAL_STRINGL(body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message), 1);
1165 zend_call_method_with_1_params(&return_value, http_response_object_ce, NULL, "setdata", NULL, body);
1166 zval_ptr_dtor(&body);
1167 #else
1168 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot transform HttpMessage to HttpResponse (need PHP 5.1+)");
1169 #endif
1170 }
1171 break;
1172
1173 default:
1174 http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HttpMessage::TYPE_REQUEST nor HttpMessage::TYPE_RESPONSE");
1175 break;
1176 }
1177 }
1178 SET_EH_NORMAL();
1179 }
1180 /* }}} */
1181
1182 /* {{{ proto int HttpMessage::count()
1183 *
1184 * Implements Countable.
1185 *
1186 * Returns the number of parent messages + 1.
1187 */
1188 PHP_METHOD(HttpMessage, count)
1189 {
1190 NO_ARGS {
1191 long i;
1192 http_message *msg;
1193 getObject(http_message_object, obj);
1194
1195 for (i = 0, msg = obj->message; msg; msg = msg->parent, ++i);
1196 RETURN_LONG(i);
1197 }
1198 }
1199 /* }}} */
1200
1201 /* {{{ proto string HttpMessage::serialize()
1202 *
1203 * Implements Serializable.
1204 *
1205 * Returns the serialized representation of the HttpMessage.
1206 */
1207 PHP_METHOD(HttpMessage, serialize)
1208 {
1209 NO_ARGS {
1210 char *string;
1211 size_t length;
1212 getObject(http_message_object, obj);
1213
1214 http_message_serialize(obj->message, &string, &length);
1215 RETURN_STRINGL(string, length, 0);
1216 }
1217 }
1218 /* }}} */
1219
1220 /* {{{ proto void HttpMessage::unserialize(string serialized)
1221 *
1222 * Implements Serializable.
1223 *
1224 * Re-constructs the HttpMessage based upon the serialized string.
1225 */
1226 PHP_METHOD(HttpMessage, unserialize)
1227 {
1228 int length;
1229 char *serialized;
1230 getObject(http_message_object, obj);
1231
1232 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
1233 http_message_dtor(obj->message);
1234 if (!http_message_parse_ex(obj->message, serialized, (size_t) length)) {
1235 http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
1236 http_message_init(obj->message);
1237 }
1238 }
1239 }
1240 /* }}} */
1241
1242 /* {{{ proto HttpMessage HttpMessage::detach(void)
1243 *
1244 * Returns a clone of an HttpMessage object detached from any parent messages.
1245 */
1246 PHP_METHOD(HttpMessage, detach)
1247 {
1248 http_info info;
1249 http_message *msg;
1250 getObject(http_message_object, obj);
1251
1252 NO_ARGS;
1253
1254 info.type = obj->message->type;
1255 memcpy(&HTTP_INFO(&info), &HTTP_INFO(obj->message), sizeof(struct http_info));
1256
1257 msg = http_message_new();
1258 http_message_set_info(msg, &info);
1259
1260 zend_hash_copy(&msg->hdrs, &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1261 phpstr_append(&msg->body, PHPSTR_VAL(obj->message), PHPSTR_LEN(obj->message));
1262
1263 RETVAL_OBJVAL(http_message_object_new_ex(Z_OBJCE_P(getThis()), msg, NULL), 0);
1264 }
1265 /* }}} */
1266
1267 /* {{{ proto void HttpMessage::prepend(HttpMessage message)
1268 *
1269 * Prepends message(s) to the HTTP message.
1270 *
1271 * Expects an HttpMessage object as parameter.
1272 */
1273 PHP_METHOD(HttpMessage, prepend)
1274 {
1275 zval *prepend;
1276 zend_bool top = 1;
1277
1278 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, http_message_object_ce, &top)) {
1279 zval m;
1280 http_message *save_parent_msg;
1281 zend_object_value save_parent_obj;
1282 getObject(http_message_object, obj);
1283 getObjectEx(http_message_object, prepend_obj, prepend);
1284
1285 INIT_PZVAL(&m);
1286 m.type = IS_OBJECT;
1287
1288 if (!top) {
1289 save_parent_obj = obj->parent;
1290 save_parent_msg = obj->message->parent;
1291 } else {
1292 /* iterate to the most parent object */
1293 while (obj->parent.handle) {
1294 m.value.obj = obj->parent;
1295 obj = zend_object_store_get_object(&m TSRMLS_CC);
1296 }
1297 }
1298
1299 /* prepend */
1300 obj->parent = prepend->value.obj;
1301 obj->message->parent = prepend_obj->message;
1302
1303 /* add ref */
1304 zend_objects_store_add_ref(prepend TSRMLS_CC);
1305 while (prepend_obj->parent.handle) {
1306 m.value.obj = prepend_obj->parent;
1307 zend_objects_store_add_ref(&m TSRMLS_CC);
1308 prepend_obj = zend_object_store_get_object(&m TSRMLS_CC);
1309 }
1310
1311 if (!top) {
1312 prepend_obj->parent = save_parent_obj;
1313 prepend_obj->message->parent = save_parent_msg;
1314 }
1315 }
1316 }
1317 /* }}} */
1318
1319 /* {{{ proto void HttpMessage::rewind(void)
1320 *
1321 * Implements Iterator.
1322 */
1323 PHP_METHOD(HttpMessage, rewind)
1324 {
1325 NO_ARGS {
1326 getObject(http_message_object, obj);
1327
1328 if (obj->iterator) {
1329 zval_ptr_dtor(&obj->iterator);
1330 }
1331 ZVAL_ADDREF(getThis());
1332 obj->iterator = getThis();
1333 }
1334 }
1335 /* }}} */
1336
1337 /* {{{ proto bool HttpMessage::valid(void)
1338 *
1339 * Implements Iterator.
1340 */
1341 PHP_METHOD(HttpMessage, valid)
1342 {
1343 NO_ARGS {
1344 getObject(http_message_object, obj);
1345
1346 RETURN_BOOL(obj->iterator != NULL);
1347 }
1348 }
1349 /* }}} */
1350
1351 /* {{{ proto void HttpMessage::next(void)
1352 *
1353 * Implements Iterator.
1354 */
1355 PHP_METHOD(HttpMessage, next)
1356 {
1357 NO_ARGS {
1358 getObject(http_message_object, obj);
1359 getObjectEx(http_message_object, itr, obj->iterator);
1360
1361 if (itr && itr->parent.handle) {
1362 zval *old = obj->iterator;
1363 MAKE_STD_ZVAL(obj->iterator);
1364 ZVAL_OBJVAL(obj->iterator, itr->parent, 1);
1365 zval_ptr_dtor(&old);
1366 } else {
1367 zval_ptr_dtor(&obj->iterator);
1368 obj->iterator = NULL;
1369 }
1370 }
1371 }
1372 /* }}} */
1373
1374 /* {{{ proto int HttpMessage::key(void)
1375 *
1376 * Implements Iterator.
1377 */
1378 PHP_METHOD(HttpMessage, key)
1379 {
1380 NO_ARGS {
1381 getObject(http_message_object, obj);
1382
1383 RETURN_LONG(obj->iterator ? obj->iterator->value.obj.handle:0);
1384 }
1385 }
1386 /* }}} */
1387
1388 /* {{{ proto HttpMessage HttpMessage::current(void)
1389 *
1390 * Implements Iterator.
1391 */
1392 PHP_METHOD(HttpMessage, current)
1393 {
1394 NO_ARGS {
1395 getObject(http_message_object, obj);
1396
1397 if (obj->iterator) {
1398 RETURN_ZVAL(obj->iterator, 1, 0);
1399 }
1400 }
1401 }
1402 /* }}} */
1403
1404 #endif /* ZEND_ENGINE_2 */
1405
1406 /*
1407 * Local variables:
1408 * tab-width: 4
1409 * c-basic-offset: 4
1410 * End:
1411 * vim600: noet sw=4 ts=4 fdm=marker
1412 * vim<600: noet sw=4 ts=4
1413 */
1414