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