X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_message.c;h=6436a1cf6a913ec45faec3b8f7a611af8a87439e;hp=0d11d0168ae0b53fe15988bd6748265ad7727022;hb=61c5a47e7530b142df6b67332e60a339998c2492;hpb=4743c5114bcdcf8d478e140beb63fecfeccd01a4 diff --git a/php_http_message.c b/php_http_message.c index 0d11d01..6436a1c 100644 --- a/php_http_message.c +++ b/php_http_message.c @@ -6,17 +6,11 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2010, Michael Wallner | + | Copyright (c) 2004-2011, Michael Wallner | +--------------------------------------------------------------------+ */ -/* $Id: http_message_api.c 298689 2010-04-28 06:50:06Z mike $ */ - -#include "php_http.h" - -#include
-#include -#include +#include "php_http_api.h" PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC) { @@ -130,17 +124,25 @@ PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, { php_http_message_parser_t p; php_http_buffer_t buf; + int free_msg; - if (!msg) { - msg = php_http_message_init(NULL, 0 TSRMLS_CC); - } php_http_buffer_from_string_ex(&buf, str, len); php_http_message_parser_init(&p TSRMLS_CC); - php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg); + + if ((free_msg = !msg)) { + msg = php_http_message_init(NULL, 0 TSRMLS_CC); + } + + if (FAILURE == php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg)) { + if (free_msg) { + php_http_message_free(&msg); + } + msg = NULL; + } + php_http_message_parser_dtor(&p); php_http_buffer_dtor(&buf); - /* FIXME */ return msg; } @@ -149,7 +151,7 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st zval *ret = NULL, **header; char *key = php_http_pretty_key(estrndup(key_str, key_len), key_len, 1, 1); - if (SUCCESS == zend_hash_find(&msg->hdrs, key, key_len + 1, (void *) &header)) { + if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) { if (join && Z_TYPE_PP(header) == IS_ARRAY) { zval *header_str, **val; HashPosition pos; @@ -382,8 +384,9 @@ PHP_HTTP_API php_http_message_t *php_http_message_reverse(php_http_message_t *ms php_http_message_count(c, msg); if (c > 1) { - php_http_message_t *tmp = msg, **arr = ecalloc(c, sizeof(**arr)); - + php_http_message_t *tmp = msg, **arr; + + arr = ecalloc(c, sizeof(**arr)); for (i = 0; i < c; ++i) { arr[i] = tmp; tmp = tmp->parent; @@ -709,20 +712,12 @@ PHP_HTTP_BEGIN_ARGS(setHttpVersion, 1) PHP_HTTP_ARG_VAL(http_version, 0) PHP_HTTP_END_ARGS; -PHP_HTTP_BEGIN_ARGS(guessContentType, 1) - PHP_HTTP_ARG_VAL(magic_file, 0) - PHP_HTTP_ARG_VAL(magic_mode, 0) -PHP_HTTP_END_ARGS; - PHP_HTTP_EMPTY_ARGS(getParentMessage); -PHP_HTTP_EMPTY_ARGS(send); PHP_HTTP_EMPTY_ARGS(__toString); PHP_HTTP_BEGIN_ARGS(toString, 0) PHP_HTTP_ARG_VAL(include_parent, 0) PHP_HTTP_END_ARGS; -PHP_HTTP_EMPTY_ARGS(toMessageTypeObject); - PHP_HTTP_EMPTY_ARGS(count); PHP_HTTP_EMPTY_ARGS(serialize); @@ -811,9 +806,11 @@ static STATUS php_http_message_object_add_prophandler(const char *prop_str, size php_http_message_object_prophandler_t h = { read, write }; return zend_hash_add(&php_http_message_object_prophandlers, prop_str, prop_len + 1, (void *) &h, sizeof(h), NULL); } +/* static int php_http_message_object_has_prophandler(const char *prop_str, size_t prop_len) { return zend_hash_exists(&php_http_message_object_prophandlers, prop_str, prop_len + 1); } +*/ static STATUS php_http_message_object_get_prophandler(const char *prop_str, size_t prop_len, php_http_message_object_prophandler_t **handler) { return zend_hash_find(&php_http_message_object_prophandlers, prop_str, prop_len + 1, (void *) handler); } @@ -1150,7 +1147,7 @@ static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, c if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) { zval_ptr_dtor(©); - return &php_http_property_proxy_init(NULL, object, member TSRMLS_CC)->myself; + return &php_http_property_proxy_init(NULL, object, member, NULL TSRMLS_CC)->myself; } zval_ptr_dtor(©); @@ -1164,18 +1161,16 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t zval *return_value, *copy = php_http_ztyp(IS_STRING, member); if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) { - if (type == BP_VAR_W) { - zval_ptr_dtor(©); + if (type == BP_VAR_R) { + ALLOC_ZVAL(return_value); + Z_SET_REFCOUNT_P(return_value, 0); + Z_UNSET_ISREF_P(return_value); + + handler->read(obj, return_value TSRMLS_CC); + } else { zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index"); - return NULL; + return_value = NULL; } - - ALLOC_ZVAL(return_value); - Z_SET_REFCOUNT_P(return_value, 0); - Z_UNSET_ISREF_P(return_value); - - handler->read(obj, return_value TSRMLS_CC); - } else { return_value = zend_get_std_object_handlers()->read_property(object, member, type, literal_key TSRMLS_CC); } @@ -1291,16 +1286,17 @@ PHP_METHOD(HttpMessage, __construct) with_error_handling(EH_THROW, php_http_exception_class_entry) { if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) { - php_http_message_t *msg = obj->message; + if (message && length) { + php_http_message_t *msg = php_http_message_parse(NULL, message, length TSRMLS_CC); - php_http_message_dtor(msg); - with_error_handling(EH_THROW, php_http_exception_class_entry) { - if ((obj->message = php_http_message_parse(msg, message, length TSRMLS_CC))) { + if (!msg) { + php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", 25, message); + } else { + php_http_message_dtor(obj->message); + obj->message = msg;{ if (obj->message->parent) { obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC); } - } else { - obj->message = php_http_message_init(msg, 0 TSRMLS_CC); } } end_error_handling(); } @@ -1397,10 +1393,10 @@ PHP_METHOD(HttpMessage, setHeader) } if (!zvalue) { - zend_hash_del(&obj->message->hdrs, name, name_len + 1); + zend_symtable_del(&obj->message->hdrs, name, name_len + 1); } else { Z_ADDREF_P(zvalue); - zend_hash_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL); + zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL); } efree(name); } @@ -1446,7 +1442,7 @@ PHP_METHOD(HttpMessage, addHeader) convert_to_array(header); zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL); } else { - zend_hash_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL); + zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL); } efree(name); }