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