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