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