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