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