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