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