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