install tweaks
[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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19 #include "php.h"
20
21 #ifdef ZEND_ENGINE_2
22
23 #include "php_http.h"
24 #include "php_http_api.h"
25 #include "php_http_std_defs.h"
26 #include "php_http_message_object.h"
27 #include "php_http_exception_object.h"
28
29 #include "phpstr/phpstr.h"
30 #include "missing.h"
31
32 #ifndef WONKY
33 # include "zend_interfaces.h"
34 # if defined(HAVE_SPL)
35 # include "ext/spl/spl_array.h"
36 # endif
37 #endif
38
39 ZEND_EXTERN_MODULE_GLOBALS(http);
40
41 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
42 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
43 #define HTTP_MESSAGE_ME(method, visibility) PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
44
45 HTTP_BEGIN_ARGS(__construct, 0, 0)
46 HTTP_ARG_VAL(message, 0)
47 HTTP_END_ARGS;
48
49 HTTP_BEGIN_ARGS(fromString, 1, 1)
50 HTTP_ARG_VAL(message, 0)
51 HTTP_END_ARGS;
52
53 HTTP_EMPTY_ARGS(getBody, 0);
54 HTTP_BEGIN_ARGS(setBody, 0, 1)
55 HTTP_ARG_VAL(body, 0)
56 HTTP_END_ARGS;
57
58 HTTP_EMPTY_ARGS(getHeaders, 0);
59 HTTP_BEGIN_ARGS(setHeaders, 0, 1)
60 HTTP_ARG_VAL(headers, 0)
61 HTTP_END_ARGS;
62
63 HTTP_BEGIN_ARGS(addHeaders, 0, 1)
64 HTTP_ARG_VAL(headers, 0)
65 HTTP_ARG_VAL(append, 0)
66 HTTP_END_ARGS;
67
68 HTTP_EMPTY_ARGS(getType, 0);
69 HTTP_BEGIN_ARGS(setType, 0, 1)
70 HTTP_ARG_VAL(type, 0)
71 HTTP_END_ARGS;
72
73 HTTP_EMPTY_ARGS(getResponseCode, 0);
74 HTTP_BEGIN_ARGS(setResponseCode, 0, 1)
75 HTTP_ARG_VAL(response_code, 0)
76 HTTP_END_ARGS;
77
78 HTTP_EMPTY_ARGS(getRequestMethod, 0);
79 HTTP_BEGIN_ARGS(setRequestMethod, 0, 1)
80 HTTP_ARG_VAL(request_method, 0)
81 HTTP_END_ARGS;
82
83 HTTP_EMPTY_ARGS(getRequestUri, 0);
84 HTTP_BEGIN_ARGS(setRequestUri, 0, 1)
85 HTTP_ARG_VAL(uri, 0)
86 HTTP_END_ARGS;
87
88 HTTP_EMPTY_ARGS(getHttpVersion, 0);
89 HTTP_BEGIN_ARGS(setHttpVersion, 0, 1)
90 HTTP_ARG_VAL(http_version, 0)
91 HTTP_END_ARGS;
92
93 HTTP_EMPTY_ARGS(getParentMessage, 1);
94 HTTP_EMPTY_ARGS(send, 0);
95 HTTP_BEGIN_ARGS(toString, 0, 0)
96 HTTP_ARG_VAL(include_parent, 0)
97 HTTP_END_ARGS;
98
99 HTTP_EMPTY_ARGS(count, 0);
100
101 HTTP_EMPTY_ARGS(serialize, 0);
102 HTTP_BEGIN_ARGS(unserialize, 0, 1)
103 HTTP_ARG_VAL(serialized, 0)
104 HTTP_END_ARGS;
105
106 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C)
107 static inline void _http_message_object_declare_default_properties(TSRMLS_D);
108 #define http_message_object_read_prop _http_message_object_read_prop
109 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC);
110 #define http_message_object_write_prop _http_message_object_write_prop
111 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC);
112 #define http_message_object_get_props _http_message_object_get_props
113 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
114
115 zend_class_entry *http_message_object_ce;
116 zend_function_entry http_message_object_fe[] = {
117 HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
118 HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC)
119 HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC)
120 HTTP_MESSAGE_ME(getHeaders, ZEND_ACC_PUBLIC)
121 HTTP_MESSAGE_ME(setHeaders, ZEND_ACC_PUBLIC)
122 HTTP_MESSAGE_ME(addHeaders, ZEND_ACC_PUBLIC)
123 HTTP_MESSAGE_ME(getType, ZEND_ACC_PUBLIC)
124 HTTP_MESSAGE_ME(setType, ZEND_ACC_PUBLIC)
125 HTTP_MESSAGE_ME(getResponseCode, ZEND_ACC_PUBLIC)
126 HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
127 HTTP_MESSAGE_ME(getRequestMethod, ZEND_ACC_PUBLIC)
128 HTTP_MESSAGE_ME(setRequestMethod, ZEND_ACC_PUBLIC)
129 HTTP_MESSAGE_ME(getRequestUri, ZEND_ACC_PUBLIC)
130 HTTP_MESSAGE_ME(setRequestUri, ZEND_ACC_PUBLIC)
131 HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC)
132 HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
133 HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
134 HTTP_MESSAGE_ME(send, ZEND_ACC_PUBLIC)
135 HTTP_MESSAGE_ME(toString, ZEND_ACC_PUBLIC)
136
137 /* implements Countable */
138 HTTP_MESSAGE_ME(count, ZEND_ACC_PUBLIC)
139
140 /* implements Serializable */
141 HTTP_MESSAGE_ME(serialize, ZEND_ACC_PUBLIC)
142 HTTP_MESSAGE_ME(unserialize, ZEND_ACC_PUBLIC)
143
144 ZEND_MALIAS(HttpMessage, __toString, toString, HTTP_ARGS(HttpMessage, toString), ZEND_ACC_PUBLIC)
145
146 HTTP_MESSAGE_ME(fromString, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
147
148 EMPTY_FUNCTION_ENTRY
149 };
150 static zend_object_handlers http_message_object_handlers;
151
152 PHP_MINIT_FUNCTION(http_message_object)
153 {
154 HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
155 #ifndef WONKY
156 # ifdef HAVE_SPL
157 zend_class_implements(http_message_object_ce TSRMLS_CC, 2, spl_ce_Countable, zend_ce_serializable);
158 # else
159 zend_class_implements(http_message_object_ce TSRMLS_CC, 1, zend_ce_serializable);
160 # endif
161 #endif
162
163 HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
164 HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
165 HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
166
167 http_message_object_handlers.clone_obj = _http_message_object_clone_obj;
168 http_message_object_handlers.read_property = http_message_object_read_prop;
169 http_message_object_handlers.write_property = http_message_object_write_prop;
170 http_message_object_handlers.get_properties = http_message_object_get_props;
171 http_message_object_handlers.get_property_ptr_ptr = NULL;
172
173 return SUCCESS;
174 }
175
176 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
177 {
178 return http_message_object_new_ex(ce, NULL, NULL);
179 }
180
181 zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg, http_message_object **ptr TSRMLS_DC)
182 {
183 zend_object_value ov;
184 http_message_object *o;
185
186 o = ecalloc(1, sizeof(http_message_object));
187 o->zo.ce = ce;
188
189 if (ptr) {
190 *ptr = o;
191 }
192
193 if (msg) {
194 o->message = msg;
195 if (msg->parent) {
196 o->parent = http_message_object_new_ex(ce, msg->parent, NULL);
197 }
198 }
199
200 ALLOC_HASHTABLE(OBJ_PROP(o));
201 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
202
203 ov.handle = putObject(http_message_object, o);
204 ov.handlers = &http_message_object_handlers;
205
206 return ov;
207 }
208
209 zend_object_value _http_message_object_clone_obj(zval *this_ptr TSRMLS_DC)
210 {
211 getObject(http_message_object, obj);
212 return http_message_object_new_ex(Z_OBJCE_P(this_ptr), http_message_dup(obj->message), NULL);
213 }
214
215 static inline void _http_message_object_declare_default_properties(TSRMLS_D)
216 {
217 zend_class_entry *ce = http_message_object_ce;
218
219 #ifndef WONKY
220 DCL_CONST(long, "TYPE_NONE", HTTP_MSG_NONE);
221 DCL_CONST(long, "TYPE_REQUEST", HTTP_MSG_REQUEST);
222 DCL_CONST(long, "TYPE_RESPONSE", HTTP_MSG_RESPONSE);
223 #endif
224
225 DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
226 DCL_PROP(PROTECTED, string, body, "");
227 DCL_PROP(PROTECTED, string, requestMethod, "");
228 DCL_PROP(PROTECTED, string, requestUri, "");
229 DCL_PROP(PROTECTED, long, responseCode, 0);
230 DCL_PROP_N(PROTECTED, httpVersion);
231 DCL_PROP_N(PROTECTED, headers);
232 DCL_PROP_N(PROTECTED, parentMessage);
233 }
234
235 void _http_message_object_free(zend_object *object TSRMLS_DC)
236 {
237 http_message_object *o = (http_message_object *) object;
238
239 if (OBJ_PROP(o)) {
240 zend_hash_destroy(OBJ_PROP(o));
241 FREE_HASHTABLE(OBJ_PROP(o));
242 }
243 if (o->message) {
244 http_message_dtor(o->message);
245 efree(o->message);
246 }
247 efree(o);
248 }
249
250 static zval *_http_message_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
251 {
252 getObjectEx(http_message_object, obj, object);
253 http_message *msg = obj->message;
254 zval *return_value;
255 #ifdef WONKY
256 ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member)+1);
257 #else
258 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
259
260 if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
261 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
262 }
263 #endif
264
265 if (type == BP_VAR_W) {
266 zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
267 return NULL;
268 }
269
270 ALLOC_ZVAL(return_value);
271 return_value->refcount = 0;
272 return_value->is_ref = 0;
273
274 #ifdef WONKY
275 switch (h)
276 #else
277 switch (pinfo->h)
278 #endif
279 {
280 case HTTP_MSG_PROPHASH_TYPE:
281 case HTTP_MSG_CHILD_PROPHASH_TYPE:
282 RETVAL_LONG(msg->type);
283 break;
284
285 case HTTP_MSG_PROPHASH_HTTP_VERSION:
286 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
287 RETVAL_DOUBLE(msg->http.version);
288 break;
289
290 case HTTP_MSG_PROPHASH_BODY:
291 case HTTP_MSG_CHILD_PROPHASH_BODY:
292 phpstr_fix(PHPSTR(msg));
293 RETVAL_PHPSTR(PHPSTR(msg), 0, 1);
294 break;
295
296 case HTTP_MSG_PROPHASH_HEADERS:
297 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
298 array_init(return_value);
299 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
300 break;
301
302 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
303 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
304 if (msg->parent) {
305 RETVAL_OBJVAL(obj->parent);
306 } else {
307 RETVAL_NULL();
308 }
309 break;
310
311 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
312 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
313 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.method) {
314 RETVAL_STRING(msg->http.info.request.method, 1);
315 } else {
316 RETVAL_NULL();
317 }
318 break;
319
320 case HTTP_MSG_PROPHASH_REQUEST_URI:
321 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI:
322 if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.URI) {
323 RETVAL_STRING(msg->http.info.request.URI, 1);
324 } else {
325 RETVAL_NULL();
326 }
327 break;
328
329 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
330 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
331 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
332 RETVAL_LONG(msg->http.info.response.code);
333 } else {
334 RETVAL_NULL();
335 }
336 break;
337
338 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
339 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
340 if (HTTP_MSG_TYPE(RESPONSE, msg) && msg->http.info.response.status) {
341 RETVAL_STRING(msg->http.info.response.status, 1);
342 } else {
343 RETVAL_NULL();
344 }
345 break;
346
347 default:
348 #ifdef WONKY
349 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
350 #else
351 RETVAL_NULL();
352 #endif
353 break;
354 }
355
356 return return_value;
357 }
358
359 static void _http_message_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
360 {
361 getObjectEx(http_message_object, obj, object);
362 http_message *msg = obj->message;
363 #ifdef WONKY
364 ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
365 #else
366 zend_property_info *pinfo = zend_get_property_info(obj->zo.ce, member, 1 TSRMLS_CC);
367
368 if (!pinfo || ACC_PROP_PUBLIC(pinfo->flags)) {
369 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
370 return;
371 }
372 #endif
373
374 #ifdef WONKY
375 switch (h)
376 #else
377 switch (pinfo->h)
378 #endif
379 {
380 case HTTP_MSG_PROPHASH_TYPE:
381 case HTTP_MSG_CHILD_PROPHASH_TYPE:
382 convert_to_long_ex(&value);
383 http_message_set_type(msg, Z_LVAL_P(value));
384 break;
385
386 case HTTP_MSG_PROPHASH_HTTP_VERSION:
387 case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
388 convert_to_double_ex(&value);
389 msg->http.version = Z_DVAL_P(value);
390 break;
391
392 case HTTP_MSG_PROPHASH_BODY:
393 case HTTP_MSG_CHILD_PROPHASH_BODY:
394 convert_to_string_ex(&value);
395 phpstr_dtor(PHPSTR(msg));
396 phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(value), Z_STRLEN_P(value));
397 break;
398
399 case HTTP_MSG_PROPHASH_HEADERS:
400 case HTTP_MSG_CHILD_PROPHASH_HEADERS:
401 convert_to_array_ex(&value);
402 zend_hash_clean(&msg->hdrs);
403 zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(value), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
404 break;
405
406 case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
407 case HTTP_MSG_CHILD_PROPHASH_PARENT_MESSAGE:
408 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), http_message_object_ce TSRMLS_CC)) {
409 if (msg->parent) {
410 zval tmp;
411 tmp.value.obj = obj->parent;
412 Z_OBJ_DELREF(tmp);
413 }
414 Z_OBJ_ADDREF_P(value);
415 obj->parent = value->value.obj;
416 }
417 break;
418
419 case HTTP_MSG_PROPHASH_REQUEST_METHOD:
420 case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
421 if (HTTP_MSG_TYPE(REQUEST, msg)) {
422 convert_to_string_ex(&value);
423 STR_SET(msg->http.info.request.method, estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value)));
424 }
425 break;
426
427 case HTTP_MSG_PROPHASH_REQUEST_URI:
428 case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI:
429 if (HTTP_MSG_TYPE(REQUEST, msg)) {
430 convert_to_string_ex(&value);
431 STR_SET(msg->http.info.request.URI, estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value)));
432 }
433 break;
434
435 case HTTP_MSG_PROPHASH_RESPONSE_CODE:
436 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
437 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
438 convert_to_long_ex(&value);
439 msg->http.info.response.code = Z_LVAL_P(value);
440 }
441 break;
442
443 case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
444 case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
445 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
446 convert_to_string_ex(&value);
447 STR_SET(msg->http.info.response.status, estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value)));
448 }
449 break;
450
451 default:
452 #ifdef WONKY
453 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
454 #endif
455 break;
456 }
457 }
458
459 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
460 {
461 zval *headers;
462 getObjectEx(http_message_object, obj, object);
463 http_message *msg = obj->message;
464 HashTable *props = OBJ_PROP(obj);
465 zval array;
466
467 INIT_ZARR(array, props);
468
469 #define ASSOC_PROP(array, ptype, name, val) \
470 { \
471 char *m_prop_name; \
472 int m_prop_len; \
473 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
474 add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
475 efree(m_prop_name); \
476 }
477 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
478 #define ASSOC_STRINGL(array, name, val, len) \
479 { \
480 char *m_prop_name; \
481 int m_prop_len; \
482 zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
483 add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
484 efree(m_prop_name); \
485 }
486
487 ASSOC_PROP(array, long, "type", msg->type);
488 ASSOC_PROP(array, double, "httpVersion", msg->http.version);
489
490 switch (msg->type)
491 {
492 case HTTP_MSG_REQUEST:
493 ASSOC_PROP(array, long, "responseCode", 0);
494 ASSOC_STRINGL(array, "responseStatus", "", 0);
495 ASSOC_STRING(array, "requestMethod", msg->http.info.request.method);
496 ASSOC_STRING(array, "requestUri", msg->http.info.request.URI);
497 break;
498
499 case HTTP_MSG_RESPONSE:
500 ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
501 ASSOC_STRING(array, "responseStatus", msg->http.info.response.status);
502 ASSOC_STRINGL(array, "requestMethod", "", 0);
503 ASSOC_STRINGL(array, "requestUri", "", 0);
504 break;
505
506 case HTTP_MSG_NONE:
507 default:
508 ASSOC_PROP(array, long, "responseCode", 0);
509 ASSOC_STRINGL(array, "responseStatus", "", 0);
510 ASSOC_STRINGL(array, "requestMethod", "", 0);
511 ASSOC_STRINGL(array, "requestUri", "", 0);
512 break;
513 }
514
515 MAKE_STD_ZVAL(headers);
516 array_init(headers);
517 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
518 ASSOC_PROP(array, zval, "headers", headers);
519 ASSOC_STRINGL(array, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
520
521 return OBJ_PROP(obj);
522 }
523
524 /* ### USERLAND ### */
525
526 /* {{{ proto void HttpMessage::__construct([string message])
527 *
528 * Instantiate a new HttpMessage object.
529 *
530 * Accepts an optional string parameter containing a single or several
531 * consecutive HTTP messages. The constructed object will actually
532 * represent the *last* message of the passed string. If there were
533 * prior messages, those can be accessed by HttpMessage::getParentMessage().
534 *
535 * Throws HttpMalformedHeaderException.
536 */
537 PHP_METHOD(HttpMessage, __construct)
538 {
539 int length = 0;
540 char *message = NULL;
541
542 getObject(http_message_object, obj);
543
544 SET_EH_THROW_HTTP();
545 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
546 http_message *msg = obj->message;
547
548 http_message_dtor(msg);
549 if (obj->message = http_message_parse_ex(msg, message, length)) {
550 if (obj->message->parent) {
551 obj->parent = http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL);
552 }
553 } else {
554 obj->message = http_message_init(msg);
555 }
556 }
557 if (!obj->message) {
558 obj->message = http_message_new();
559 }
560 SET_EH_NORMAL();
561 }
562 /* }}} */
563
564 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
565 *
566 * Create an HttpMessage object from a string. Kind of a static constructor.
567 *
568 * Expects a string parameter containing a sinlge or several consecutive
569 * HTTP messages.
570 *
571 * Returns an HttpMessage object on success or NULL on failure.
572 *
573 * Throws HttpMalformedHeadersException.
574 */
575 PHP_METHOD(HttpMessage, fromString)
576 {
577 char *string = NULL;
578 int length = 0;
579 http_message *msg = NULL;
580
581 RETVAL_NULL();
582
583 SET_EH_THROW_HTTP();
584 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
585 if (msg = http_message_parse(string, length)) {
586 ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL));
587 }
588 }
589 SET_EH_NORMAL();
590 }
591 /* }}} */
592
593 /* {{{ proto string HttpMessage::getBody()
594 *
595 * Get the body of the parsed HttpMessage.
596 *
597 * Returns the message body as string.
598 */
599 PHP_METHOD(HttpMessage, getBody)
600 {
601 NO_ARGS;
602
603 IF_RETVAL_USED {
604 getObject(http_message_object, obj);
605 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
606 }
607 }
608 /* }}} */
609
610 /* {{{ proto void HttpMessage::setBody(string body)
611 *
612 * Set the body of the HttpMessage.
613 * NOTE: Don't forget to update any headers accordingly.
614 *
615 * Expects a string parameter containing the new body of the message.
616 */
617 PHP_METHOD(HttpMessage, setBody)
618 {
619 char *body;
620 int len;
621 getObject(http_message_object, obj);
622
623 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &body, &len)) {
624 phpstr_dtor(PHPSTR(obj->message));
625 phpstr_from_string_ex(PHPSTR(obj->message), body, len);
626 }
627 }
628 /* }}} */
629
630 /* {{{ proto array HttpMessage::getHeaders()
631 *
632 * Get Message Headers.
633 *
634 * Returns an associative array containing the messages HTTP headers.
635 */
636 PHP_METHOD(HttpMessage, getHeaders)
637 {
638 NO_ARGS;
639
640 IF_RETVAL_USED {
641 zval headers;
642 getObject(http_message_object, obj);
643
644 INIT_ZARR(headers, &obj->message->hdrs);
645 array_init(return_value);
646 array_copy(&headers, return_value);
647 }
648 }
649 /* }}} */
650
651 /* {{{ proto void HttpMessage::setHeaders(array headers)
652 *
653 * Sets new headers.
654 *
655 * Expects an associative array as parameter containing the new HTTP headers,
656 * which will replace *all* previous HTTP headers of the message.
657 */
658 PHP_METHOD(HttpMessage, setHeaders)
659 {
660 zval *new_headers, old_headers;
661 getObject(http_message_object, obj);
662
663 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
664 return;
665 }
666
667 zend_hash_clean(&obj->message->hdrs);
668 INIT_ZARR(old_headers, &obj->message->hdrs);
669 array_copy(new_headers, &old_headers);
670 }
671 /* }}} */
672
673 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
674 *
675 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
676 *
677 * Expects an associative array as parameter containing the additional HTTP headers
678 * to add to the messages existing headers. If the optional bool parameter is true,
679 * and a header with the same name of one to add exists already, this respective
680 * header will be converted to an array containing both header values, otherwise
681 * it will be overwritten with the new header value.
682 */
683 PHP_METHOD(HttpMessage, addHeaders)
684 {
685 zval old_headers, *new_headers;
686 zend_bool append = 0;
687 getObject(http_message_object, obj);
688
689 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
690 return;
691 }
692
693 INIT_ZARR(old_headers, &obj->message->hdrs);
694 if (append) {
695 array_append(new_headers, &old_headers);
696 } else {
697 array_merge(new_headers, &old_headers);
698 }
699 }
700 /* }}} */
701
702 /* {{{ proto int HttpMessage::getType()
703 *
704 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
705 *
706 * Returns the HttpMessage::TYPE.
707 */
708 PHP_METHOD(HttpMessage, getType)
709 {
710 NO_ARGS;
711
712 IF_RETVAL_USED {
713 getObject(http_message_object, obj);
714 RETURN_LONG(obj->message->type);
715 }
716 }
717 /* }}} */
718
719 /* {{{ proto void HttpMessage::setType(int type)
720 *
721 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
722 *
723 * Exptects an int parameter, the HttpMessage::TYPE.
724 */
725 PHP_METHOD(HttpMessage, setType)
726 {
727 long type;
728 getObject(http_message_object, obj);
729
730 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
731 return;
732 }
733 http_message_set_type(obj->message, type);
734 }
735 /* }}} */
736
737 /* {{{ proto int HttpMessage::getResponseCode()
738 *
739 * Get the Response Code of the Message.
740 *
741 * Returns the HTTP response code if the message is of type
742 * HttpMessage::TYPE_RESPONSE, else FALSE.
743 */
744 PHP_METHOD(HttpMessage, getResponseCode)
745 {
746 NO_ARGS;
747
748 IF_RETVAL_USED {
749 getObject(http_message_object, obj);
750 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
751 RETURN_LONG(obj->message->http.info.response.code);
752 }
753 }
754 /* }}} */
755
756 /* {{{ proto bool HttpMessage::setResponseCode(int code)
757 *
758 * Set the response code of an HTTP Response Message.
759 *
760 * Expects an int parameter with the HTTP response code.
761 *
762 * Returns TRUE on success, or FALSE if the message is not of type
763 * HttpMessage::TYPE_RESPONSE or the response code is out of range (100-510).
764 */
765 PHP_METHOD(HttpMessage, setResponseCode)
766 {
767 long code;
768 getObject(http_message_object, obj);
769
770 HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
771
772 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
773 RETURN_FALSE;
774 }
775 if (code < 100 || code > 510) {
776 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid response code (100-510): %ld", code);
777 RETURN_FALSE;
778 }
779
780 obj->message->http.info.response.code = code;
781 RETURN_TRUE;
782 }
783 /* }}} */
784
785 /* {{{ proto string HttpMessage::getRequestMethod()
786 *
787 * Get the Request Method of the Message.
788 *
789 * Returns the request method name on success, or FALSE if the message is
790 * not of type HttpMessage::TYPE_REQUEST.
791 */
792 PHP_METHOD(HttpMessage, getRequestMethod)
793 {
794 NO_ARGS;
795
796 IF_RETVAL_USED {
797 getObject(http_message_object, obj);
798 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
799 RETURN_STRING(obj->message->http.info.request.method, 1);
800 }
801 }
802 /* }}} */
803
804 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
805 *
806 * Set the Request Method of the HTTP Message.
807 *
808 * Expects a string parameter containing the request method name.
809 *
810 * Returns TRUE on success, or FALSE if the message is not of type
811 * HttpMessage::TYPE_REQUEST or an invalid request method was supplied.
812 */
813 PHP_METHOD(HttpMessage, setRequestMethod)
814 {
815 char *method;
816 int method_len;
817 getObject(http_message_object, obj);
818
819 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
820
821 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
822 RETURN_FALSE;
823 }
824 if (method_len < 1) {
825 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
826 RETURN_FALSE;
827 }
828 if (SUCCESS != http_check_method(method)) {
829 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unkown request method: %s", method);
830 RETURN_FALSE;
831 }
832
833 STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
834 RETURN_TRUE;
835 }
836 /* }}} */
837
838 /* {{{ proto string HttpMessage::getRequestUri()
839 *
840 * Get the Request URI of the Message.
841 *
842 * Returns the request uri as string on success, or FALSE if the message
843 * is not of type HttpMessage::TYPE_REQUEST.
844 */
845 PHP_METHOD(HttpMessage, getRequestUri)
846 {
847 NO_ARGS;
848
849 IF_RETVAL_USED {
850 getObject(http_message_object, obj);
851 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
852 RETURN_STRING(obj->message->http.info.request.URI, 1);
853 }
854 }
855 /* }}} */
856
857 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
858 *
859 * Set the Request URI of the HTTP Message.
860 *
861 * Expects a string parameters containing the request uri.
862 *
863 * Returns TRUE on success, or FALSE if the message is not of type
864 * HttpMessage::TYPE_REQUEST or supplied URI was empty.
865 */
866 PHP_METHOD(HttpMessage, setRequestUri)
867 {
868 char *URI;
869 int URIlen;
870 getObject(http_message_object, obj);
871
872 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
873 RETURN_FALSE;
874 }
875 HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
876 if (URIlen < 1) {
877 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
878 RETURN_FALSE;
879 }
880
881 STR_SET(obj->message->http.info.request.URI, estrndup(URI, URIlen));
882 RETURN_TRUE;
883 }
884 /* }}} */
885
886 /* {{{ proto string HttpMessage::getHttpVersion()
887 *
888 * Get the HTTP Protocol Version of the Message.
889 *
890 * Returns the HTTP protocol version as string.
891 */
892 PHP_METHOD(HttpMessage, getHttpVersion)
893 {
894 NO_ARGS;
895
896 IF_RETVAL_USED {
897 char ver[4] = {0};
898 getObject(http_message_object, obj);
899
900 sprintf(ver, "%1.1lf", obj->message->http.version);
901 RETURN_STRINGL(ver, 3, 1);
902 }
903 }
904 /* }}} */
905
906 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
907 *
908 * Set the HTTP Protocol version of the Message.
909 *
910 * Expects a string parameter containing the HTTP protocol version.
911 *
912 * Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).
913 */
914 PHP_METHOD(HttpMessage, setHttpVersion)
915 {
916 char v[4];
917 zval *zv;
918 getObject(http_message_object, obj);
919
920 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
921 return;
922 }
923
924 convert_to_double(zv);
925 sprintf(v, "%1.1lf", Z_DVAL_P(zv));
926 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
927 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
928 RETURN_FALSE;
929 }
930
931 obj->message->http.version = Z_DVAL_P(zv);
932 RETURN_TRUE;
933 }
934 /* }}} */
935
936 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
937 *
938 * Get parent Message.
939 *
940 * Returns the parent HttpMessage on success, or NULL if there's none.
941 */
942 PHP_METHOD(HttpMessage, getParentMessage)
943 {
944 NO_ARGS;
945
946 IF_RETVAL_USED {
947 getObject(http_message_object, obj);
948
949 if (obj->message->parent) {
950 RETVAL_OBJVAL(obj->parent);
951 } else {
952 RETVAL_NULL();
953 }
954 }
955 }
956 /* }}} */
957
958 /* {{{ proto bool HttpMessage::send()
959 *
960 * Send the Message according to its type as Response or Request.
961 * This provides limited functionality compared to HttpRequest and HttpResponse.
962 *
963 * Returns TRUE on success, or FALSE on failure.
964 */
965 PHP_METHOD(HttpMessage, send)
966 {
967 getObject(http_message_object, obj);
968
969 NO_ARGS;
970
971 RETURN_SUCCESS(http_message_send(obj->message));
972 }
973 /* }}} */
974
975 /* {{{ proto string HttpMessage::toString([bool include_parent = false])
976 *
977 * Get the string representation of the Message.
978 *
979 * Accepts a bool parameter which specifies whether the returned string
980 * should also contain any parent messages.
981 *
982 * Returns the full message as string.
983 */
984 PHP_METHOD(HttpMessage, toString)
985 {
986 IF_RETVAL_USED {
987 char *string;
988 size_t length;
989 zend_bool include_parent = 0;
990 getObject(http_message_object, obj);
991
992 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
993 RETURN_FALSE;
994 }
995
996 if (include_parent) {
997 http_message_serialize(obj->message, &string, &length);
998 } else {
999 http_message_tostring(obj->message, &string, &length);
1000 }
1001 RETURN_STRINGL(string, length, 0);
1002 }
1003 }
1004 /* }}} */
1005
1006 /* {{{ proto int HttpMessage::count()
1007 *
1008 * Implements Countable.
1009 *
1010 * Returns the number of parent messages + 1.
1011 */
1012 PHP_METHOD(HttpMessage, count)
1013 {
1014 NO_ARGS {
1015 long i;
1016 http_message *msg;
1017 getObject(http_message_object, obj);
1018
1019 for (i = 0, msg = obj->message; msg; msg = msg->parent, ++i);
1020 RETURN_LONG(i);
1021 }
1022 }
1023 /* }}} */
1024
1025 /* {{{ proto string HttpMessage::serialize()
1026 *
1027 * Implements Serializable.
1028 *
1029 * Returns the serialized representation of the HttpMessage.
1030 */
1031 PHP_METHOD(HttpMessage, serialize)
1032 {
1033 NO_ARGS {
1034 char *string;
1035 size_t length;
1036 getObject(http_message_object, obj);
1037
1038 http_message_serialize(obj->message, &string, &length);
1039 RETURN_STRINGL(string, length, 0);
1040 }
1041 }
1042 /* }}} */
1043
1044 /* {{{ proto void HttpMessage::unserialize(string serialized)
1045 *
1046 * Implements Serializable.
1047 *
1048 * Re-constructs the HttpMessage based upon the serialized string.
1049 */
1050 PHP_METHOD(HttpMessage, unserialize)
1051 {
1052 int length;
1053 char *serialized;
1054 getObject(http_message_object, obj);
1055
1056 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
1057 http_message_dtor(obj->message);
1058 if (!http_message_parse_ex(obj->message, serialized, (size_t) length)) {
1059 http_error(HE_ERROR, HTTP_E_RUNTIME, "Could not unserialize HttpMessage");
1060 http_message_init(obj->message);
1061 }
1062 }
1063 }
1064 /* }}} */
1065
1066 #endif /* ZEND_ENGINE_2 */
1067
1068 /*
1069 * Local variables:
1070 * tab-width: 4
1071 * c-basic-offset: 4
1072 * End:
1073 * vim600: noet sw=4 ts=4 fdm=marker
1074 * vim<600: noet sw=4 ts=4
1075 */
1076