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