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