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