X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=http_request_object.c;h=f763b482898c408613dcb5fade3891035e51909c;hb=43923eca4429ed3ce5fb85407345d7030a1879f1;hp=bf271105162e66c6c2e5f4fe717977bf0740bb3a;hpb=b9045f99a675bcdf7d8f96a8ffb4885459f39f63;p=m6w6%2Fext-http diff --git a/http_request_object.c b/http_request_object.c index bf27110..f763b48 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -32,8 +32,6 @@ #include "php_http_request_pool_api.h" #include "php_http_url_api.h" -ZEND_EXTERN_MODULE_GLOBALS(http); - #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, ret_ref, req_args) #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequest, method, ret_ref) #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility) @@ -55,6 +53,10 @@ HTTP_BEGIN_ARGS(setSslOptions, 0, 0) HTTP_ARG_VAL(ssl_options, 0) HTTP_END_ARGS; +HTTP_BEGIN_ARGS(addSslOptions, 0, 0) + HTTP_ARG_VAL(ssl_optins, 0) +HTTP_END_ARGS; + HTTP_EMPTY_ARGS(getHeaders, 0); HTTP_BEGIN_ARGS(setHeaders, 0, 0) HTTP_ARG_VAL(headers, 0) @@ -221,6 +223,7 @@ zend_function_entry http_request_object_fe[] = { HTTP_REQUEST_ME(getOptions, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(setSslOptions, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getSslOptions, ZEND_ACC_PUBLIC) + HTTP_REQUEST_ME(addSslOptions, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(addHeaders, ZEND_ACC_PUBLIC) HTTP_REQUEST_ME(getHeaders, ZEND_ACC_PUBLIC) @@ -640,7 +643,8 @@ static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, array_init(return_value); - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options)) { + if ( (Z_TYPE_P(opts) == IS_ARRAY) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options))) { convert_to_array(*options); array_copy(*options, return_value); } @@ -700,7 +704,7 @@ PHP_METHOD(HttpRequest, setOptions) char *key = NULL; ulong idx = 0; HashPosition pos; - zval *opts = NULL, *old_opts, *new_opts, **opt; + zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt; getObject(http_request_object, obj); if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) { @@ -712,61 +716,42 @@ PHP_METHOD(HttpRequest, setOptions) if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) { SET_PROP(obj, options, new_opts); + zval_ptr_dtor(&new_opts); RETURN_TRUE; } - - old_opts = GET_PROP(obj, options); - if (Z_TYPE_P(old_opts) == IS_ARRAY) { - array_copy(old_opts, new_opts); - } + MAKE_STD_ZVAL(add_opts); + array_init(add_opts); /* some options need extra attention -- thus cannot use array_merge() directly */ FOREACH_KEYVAL(pos, opts, key, idx, opt) { if (key) { if (!strcmp(key, "headers")) { - zval **headers; - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), "headers", sizeof("headers"), (void **) &headers)) { - convert_to_array_ex(opt); - convert_to_array(*headers); - array_merge(*opt, *headers); - continue; - } + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt); } else if (!strcmp(key, "cookies")) { - zval **cookies; - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), "cookies", sizeof("cookies"), (void **) &cookies)) { - convert_to_array_ex(opt); - convert_to_array(*cookies); - array_merge(*opt, *cookies); - continue; - } + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt); } else if (!strcmp(key, "ssl")) { - zval **ssl; - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), "ssl", sizeof("ssl"), (void **) &ssl)) { - convert_to_array_ex(opt); - convert_to_array(*ssl); - array_merge(*opt, *ssl); - continue; - } - } else if ((Z_TYPE_PP(opt) == IS_STRING) && ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri")))) { - UPD_STRL(obj, url, Z_STRVAL_PP(opt), Z_STRLEN_PP(opt)); - continue; + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt); + } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) { + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt); } else if (!strcmp(key, "method")) { - if (Z_TYPE_PP(opt) != IS_LONG) { - convert_to_long_ex(opt); - } - UPD_PROP(obj, long, method, Z_LVAL_PP(opt)); - continue; + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt); + } else { + ZVAL_ADDREF(*opt); + add_assoc_zval(add_opts, key, *opt); } - - ZVAL_ADDREF(*opt); - add_assoc_zval(new_opts, key, *opt); - /* reset */ key = NULL; } } + + old_opts = GET_PROP(obj, options); + if (Z_TYPE_P(old_opts) == IS_ARRAY) { + array_copy(old_opts, new_opts); + } + array_merge(add_opts, new_opts); SET_PROP(obj, options, new_opts); zval_ptr_dtor(&new_opts); + zval_ptr_dtor(&add_opts); RETURN_TRUE; } @@ -1276,18 +1261,13 @@ PHP_METHOD(HttpRequest, addRawPostData) } if (data_len) { - zval *data, *zdata = GET_PROP(obj, rawPostData); + zval *data = zval_copy(IS_STRING, GET_PROP(obj, rawPostData)); - ALLOC_ZVAL(data); - *data = *zdata; - zval_copy_ctor(data); - INIT_PZVAL(data); - convert_to_string(data); Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1); Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0'; memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len); SET_PROP(obj, rawPostData, data); - zval_ptr_dtor(&data); + zval_free(&data); } RETURN_TRUE; @@ -1359,6 +1339,7 @@ PHP_METHOD(HttpRequest, addPostFile) } add_next_index_zval(new_post, entry); SET_PROP(obj, postFiles, new_post); + zval_ptr_dtor(&new_post); RETURN_TRUE; } @@ -1390,6 +1371,7 @@ PHP_METHOD(HttpRequest, setPostFiles) array_copy(files, post); } SET_PROP(obj, postFiles, post); + zval_ptr_dtor(&post); RETURN_TRUE; } @@ -1407,9 +1389,9 @@ PHP_METHOD(HttpRequest, getPostFiles) IF_RETVAL_USED { getObject(http_request_object, obj); - - array_init(return_value); - array_copy(GET_PROP(obj, postFiles), return_value); + zval *files = GET_PROP(obj, postFiles); + + RETURN_ZVAL(files, 1, 0); } } /* }}} */ @@ -1474,9 +1456,9 @@ PHP_METHOD(HttpRequest, getResponseData) IF_RETVAL_USED { getObject(http_request_object, obj); + zval *data = GET_PROP(obj, responseData); - array_init(return_value); - array_copy(GET_PROP(obj, responseData), return_value); + RETURN_ZVAL(data, 1, 0); } } /* }}} */ @@ -1507,8 +1489,9 @@ PHP_METHOD(HttpRequest, getResponseHeader) } data = GET_PROP(obj, responseData); - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) { - convert_to_array(*headers); + if ( (Z_TYPE_P(data) == IS_ARRAY) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) && + (Z_TYPE_PP(headers) == IS_ARRAY)) { if (!header_len || !header_name) { RETVAL_ZVAL(*headers, 1, 0); } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) { @@ -1553,7 +1536,9 @@ PHP_METHOD(HttpRequest, getResponseCookie) array_init(return_value); data = GET_PROP(obj, responseData); - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) { + if ( (Z_TYPE_P(data) == IS_ARRAY) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) && + (Z_TYPE_PP(headers) == IS_ARRAY)) { ulong idx = 0; char *key = NULL; zval **header = NULL; @@ -1646,7 +1631,8 @@ PHP_METHOD(HttpRequest, getResponseBody) getObject(http_request_object, obj); zval *data = GET_PROP(obj, responseData); - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) { + if ( (Z_TYPE_P(data) == IS_ARRAY) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body))) { RETURN_ZVAL(*body, 1, 0); } else { RETURN_FALSE; @@ -1706,6 +1692,10 @@ PHP_METHOD(HttpRequest, getResponseInfo) } info = GET_PROP(obj, responseInfo); + + if (Z_TYPE_P(info) != IS_ARRAY) { + RETURN_FALSE; + } if (info_len && info_name) { if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {