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