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