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