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