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