X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_message.c;h=e6cdbc8f143782b56784239e6ebde8e16b552477;hp=f391d5fd989019a71c962aa80b18dbb265fcfc43;hb=4bf1b4570329514fa00dc68c6e02f581c3792d73;hpb=1f934bf6f8bf9343eac1fdc7fdba5959b8d062a1 diff --git a/php_http_message.c b/php_http_message.c index f391d5f..e6cdbc8 100644 --- a/php_http_message.c +++ b/php_http_message.c @@ -86,7 +86,7 @@ php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_h php_http_env_get_response_headers(&message->hdrs); if (php_output_get_level()) { if (php_output_get_status() & PHP_OUTPUT_SENT) { - php_error_docref(NULL, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C)); + php_error_docref(NULL, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(), php_output_get_start_lineno()); goto error; } else if (SUCCESS != php_output_get_contents(&tval)) { php_error_docref(NULL, E_WARNING, "Could not fetch response body"); @@ -275,6 +275,8 @@ void php_http_message_update_headers(php_http_message_t *msg) if (php_http_message_body_stream(msg->body)->readfilters.head) { /* if a read stream filter is attached to the body the caller must also care for the headers */ + } else if (php_http_message_header(msg, ZEND_STRL("Content-Range"))) { + /* don't mess around with a Content-Range message */ } else if ((size = php_http_message_body_size(msg->body))) { ZVAL_LONG(&h, size); zend_hash_str_update(&msg->hdrs, "Content-Length", lenof("Content-Length"), &h); @@ -299,6 +301,7 @@ void php_http_message_update_headers(php_http_message_t *msg) } } else if ((cl = php_http_message_header_string(msg, ZEND_STRL("Content-Length")))) { if (!zend_string_equals_literal(cl, "0")) { + /* body->size == 0, so get rid of old Content-Length */ zend_hash_str_del(&msg->hdrs, ZEND_STRL("Content-Length")); } zend_string_release(cl); @@ -493,7 +496,6 @@ void php_http_message_free(php_http_message_t **message) static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *rv); static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot); -static HashTable *php_http_message_object_get_props(zval *object); static zend_object_handlers php_http_message_object_handlers; static HashTable php_http_message_object_prophandlers; @@ -604,9 +606,8 @@ static void php_http_message_object_prophandler_set_headers(php_http_message_obj if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) { convert_to_array_ex(value); - } else { - headers = HASH_OF(value); } + headers = HASH_OF(value); zend_hash_clean(&obj->message->hdrs); array_copy(headers, &obj->message->hdrs); @@ -688,11 +689,12 @@ void php_http_message_object_reverse(zval *zmsg, zval *return_value) /* add ref, because we previously have not been a parent message */ Z_ADDREF_P(zmsg); - RETVAL_OBJ(&objects[last]->zo); + /* no addref, because we've been a parent message previously */ + RETVAL_OBJECT(&objects[last]->zo, 0); efree(objects); } else { - RETURN_ZVAL_FAST(zmsg); + RETURN_ZVAL(zmsg, 1, 0); } } @@ -774,10 +776,11 @@ ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg } if (msg_obj->message) { php_http_message_body_free(&msg_obj->message->body); - msg_obj->message->body = php_http_message_body_init(&body_obj->body, NULL); + msg_obj->message->body = body_obj->body; } else { - msg_obj->message = php_http_message_init(NULL, 0, php_http_message_body_init(&body_obj->body, NULL)); + msg_obj->message = php_http_message_init(NULL, 0, body_obj->body); } + php_http_message_body_addref(body_obj->body); msg_obj->body = body_obj; return SUCCESS; @@ -798,7 +801,7 @@ php_http_message_object_t *php_http_message_object_new_ex(zend_class_entry *ce, { php_http_message_object_t *o; - o = ecalloc(1, sizeof(php_http_message_object_t) + (ce->default_properties_count - 1) * sizeof(zval)); + o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); zend_object_std_init(&o->zo, ce); object_properties_init(&o->zo, ce); @@ -815,7 +818,7 @@ php_http_message_object_t *php_http_message_object_new_ex(zend_class_entry *ce, return o; } -zend_object *php_http_message_object_clone(zval *this_ptr TSRMLS_DC) +zend_object *php_http_message_object_clone(zval *this_ptr) { php_http_message_object_t *new_obj = NULL; php_http_message_object_t *old_obj = PHP_HTTP_OBJ(NULL, this_ptr); @@ -841,41 +844,53 @@ void php_http_message_object_free(zend_object *object) o->message = NULL; } if (o->parent) { + if (GC_REFCOUNT(&o->parent->zo) == 1) { + zend_objects_store_del(&o->parent->zo); + } zend_objects_store_del(&o->parent->zo); o->parent = NULL; } if (o->body) { + if (GC_REFCOUNT(&o->body->zo) == 1) { + zend_objects_store_del(&o->body->zo); + } zend_objects_store_del(&o->body->zo); o->body = NULL; } zend_object_std_dtor(object); } -static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *return_value) +static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp) { - php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object); - php_http_message_object_prophandler_t *handler; + zval *return_value; zend_string *member_name = zval_get_string(member); + php_http_message_object_prophandler_t *handler = php_http_message_object_get_prophandler(member_name); - PHP_HTTP_MESSAGE_OBJECT_INIT(obj); + if (!handler || type == BP_VAR_R || type == BP_VAR_IS) { + return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp); - /* supplied retun_value lives on the stack of zend_read_property! */ - return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, return_value); + if (handler) { + php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object); - if ((handler = php_http_message_object_get_prophandler(member_name))) { - zval_dtor(return_value); + PHP_HTTP_MESSAGE_OBJECT_INIT(obj); + handler->read(obj, tmp); - if (type == BP_VAR_R) { - handler->read(obj, return_value); - } else { - php_property_proxy_t *proxy = php_property_proxy_init(object, member_name); - RETVAL_OBJ(&php_property_proxy_object_new_ex(php_property_proxy_get_class_entry(), proxy)->zo); + zval_ptr_dtor(return_value); + ZVAL_COPY_VALUE(return_value, tmp); } - } + zend_string_release(member_name); + return return_value; + } else { + php_property_proxy_t *proxy; + php_property_proxy_object_t *proxy_obj; - zend_string_release(member_name); + proxy = php_property_proxy_init(object, member_name); + proxy_obj = php_property_proxy_object_new_ex(NULL, proxy); - return return_value; + ZVAL_OBJ(tmp, &proxy_obj->zo); + zend_string_release(member_name); + return tmp; + } } static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot) @@ -895,7 +910,7 @@ static void php_http_message_object_write_prop(zval *object, zval *member, zval zend_string_release(member_name); } -static HashTable *php_http_message_object_get_props(zval *object) +static HashTable *php_http_message_object_get_debug_info(zval *object, int *is_temp) { zval tmp; php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object); @@ -904,13 +919,14 @@ static HashTable *php_http_message_object_get_props(zval *object) size_t ver_len, url_len = 0; PHP_HTTP_MESSAGE_OBJECT_INIT(obj); + *is_temp = 0; #define UPDATE_PROP(name_str, action_with_tmp) \ do { \ zend_property_info *pi; \ if ((pi = zend_hash_str_find_ptr(&obj->zo.ce->properties_info, name_str, lenof(name_str)))) { \ action_with_tmp; \ - zend_hash_update(props, pi->name, &tmp); \ + zend_hash_update_ind(props, pi->name, &tmp); \ } \ } while(0) @@ -1069,7 +1085,7 @@ static PHP_METHOD(HttpMessage, setBody) PHP_HTTP_MESSAGE_OBJECT_INIT(obj); php_http_message_object_prophandler_set_body(obj, zbody); } - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody, 0, 0, 1) @@ -1086,7 +1102,7 @@ static PHP_METHOD(HttpMessage, addBody) PHP_HTTP_MESSAGE_OBJECT_INIT(obj); php_http_message_body_to_callback(new_obj->body, (php_http_pass_callback_t) php_http_message_body_append, obj->message->body, 0, 0); } - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader, 0, 0, 1) @@ -1107,15 +1123,18 @@ static PHP_METHOD(HttpMessage, getHeader) if ((header = php_http_message_header(obj->message, header_str, header_len))) { if (!header_ce) { - RETURN_ZVAL(header, 1, 1); + RETURN_ZVAL(header, 1, 0); } else if (instanceof_function(header_ce, php_http_header_class_entry)) { + php_http_object_method_t cb; zval argv[2]; ZVAL_STRINGL(&argv[0], header_str, header_len); ZVAL_COPY(&argv[1], header); object_init_ex(return_value, header_ce); - php_http_method_call(return_value, ZEND_STRL("__construct"), 2, argv, NULL); + php_http_object_method_init(&cb, return_value, ZEND_STRL("__construct")); + php_http_object_method_call(&cb, return_value, NULL, 2, argv); + php_http_object_method_dtor(&cb); zval_ptr_dtor(&argv[0]); zval_ptr_dtor(&argv[1]); @@ -1167,7 +1186,7 @@ static PHP_METHOD(HttpMessage, setHeader) } efree(name); } - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders, 0, 0, 1) @@ -1187,7 +1206,7 @@ static PHP_METHOD(HttpMessage, setHeaders) array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY); } } - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader, 0, 0, 2) @@ -1216,7 +1235,7 @@ static PHP_METHOD(HttpMessage, addHeader) } efree(name); } - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders, 0, 0, 1) @@ -1235,7 +1254,7 @@ static PHP_METHOD(HttpMessage, addHeaders) array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY); } - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType, 0, 0, 0) @@ -1265,7 +1284,7 @@ static PHP_METHOD(HttpMessage, setType) php_http_message_set_type(obj->message, type); } - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo, 0, 0, 0) @@ -1320,7 +1339,7 @@ static PHP_METHOD(HttpMessage, setInfo) php_http_message_set_info(obj->message, &inf); php_http_info_dtor(&inf); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion, 0, 0, 0) @@ -1358,7 +1377,7 @@ static PHP_METHOD(HttpMessage, setHttpVersion) obj->message->http.version = version; - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode, 0, 0, 0) @@ -1389,7 +1408,7 @@ static PHP_METHOD(HttpMessage, setResponseCode) zend_bool strict = 1; php_http_message_object_t *obj; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &code, &strict), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &code, &strict), invalid_arg, return); obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_OBJECT_INIT(obj); @@ -1407,7 +1426,7 @@ static PHP_METHOD(HttpMessage, setResponseCode) obj->message->http.info.response.code = code; PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(code))); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus, 0, 0, 0) @@ -1440,7 +1459,7 @@ static PHP_METHOD(HttpMessage, setResponseStatus) size_t status_len; php_http_message_object_t *obj; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &status, &status_len), invalid_arg, return); obj = PHP_HTTP_OBJ(NULL, getThis()); @@ -1451,7 +1470,7 @@ static PHP_METHOD(HttpMessage, setResponseStatus) } PTR_SET(obj->message->http.info.response.status, estrndup(status, status_len)); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod, 0, 0, 0) @@ -1502,7 +1521,7 @@ static PHP_METHOD(HttpMessage, setRequestMethod) } PTR_SET(obj->message->http.info.request.method, estrndup(method, method_len)); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl, 0, 0, 0) @@ -1556,14 +1575,14 @@ static PHP_METHOD(HttpMessage, setRequestUrl) url = php_http_url_from_zval(zurl, ~0); zend_restore_error_handling(&zeh); - if (php_http_url_is_empty(url)) { + if (url && php_http_url_is_empty(url)) { php_http_url_free(&url); php_http_throw(invalid_arg, "Cannot set http\\Message's request url to an empty string", NULL); - } else { + } else if (url) { PTR_SET(obj->message->http.info.request.url, url); } - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage, 0, 0, 0) @@ -1648,7 +1667,7 @@ static PHP_METHOD(HttpMessage, toCallback) zend_fcall_info_args_clear(&fcd.fci, 1); zval_ptr_dtor(&fcd.fcz); - RETURN_ZVAL_FAST(getThis()); + RETURN_ZVAL(getThis(), 1, 0); } } @@ -1690,7 +1709,7 @@ static PHP_METHOD(HttpMessage, unserialize) obj->message = msg; } else { obj->message = php_http_message_init(NULL, 0, NULL); - php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not unserialize http\\Message"); + php_error_docref(NULL, E_ERROR, "Could not unserialize http\\Message"); } } } @@ -1742,7 +1761,7 @@ static PHP_METHOD(HttpMessage, prepend) } php_http_message_object_prepend(getThis(), prepend, top); - RETURN_ZVAL_FAST(getThis()); + RETURN_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse, 0, 0, 0) @@ -1812,7 +1831,7 @@ static PHP_METHOD(HttpMessage, count) { zend_long count_mode = -1; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &count_mode)) { php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_OBJECT_INIT(obj); @@ -1890,7 +1909,7 @@ static PHP_METHOD(HttpMessage, current) php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); if (!Z_ISUNDEF(obj->iterator)) { - RETURN_ZVAL_FAST(&obj->iterator); + RETURN_ZVAL(&obj->iterator, 1, 0); } } } @@ -1966,7 +1985,7 @@ PHP_MINIT_FUNCTION(http_message) php_http_message_object_handlers.free_obj = php_http_message_object_free; php_http_message_object_handlers.read_property = php_http_message_object_read_prop; php_http_message_object_handlers.write_property = php_http_message_object_write_prop; - php_http_message_object_handlers.get_properties = php_http_message_object_get_props; + php_http_message_object_handlers.get_debug_info = php_http_message_object_get_debug_info; php_http_message_object_handlers.get_property_ptr_ptr = NULL; zend_class_implements(php_http_message_class_entry, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);