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