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