From c5e63f8a63b03b52cae35906625497bd17592c4e Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Fri, 9 May 2014 12:17:09 +0800 Subject: [PATCH] Refactoring ext/curl (only compilable now) --- ext/curl/curl_file.c | 4 +- ext/curl/interface.c | 856 ++++++++++++++++++------------------------- ext/curl/multi.c | 82 ++--- ext/curl/php_curl.h | 58 +-- ext/curl/share.c | 18 +- 5 files changed, 438 insertions(+), 580 deletions(-) diff --git a/ext/curl/curl_file.c b/ext/curl/curl_file.c index 9f371df538..f48480593c 100644 --- a/ext/curl/curl_file.c +++ b/ext/curl/curl_file.c @@ -77,9 +77,7 @@ static void curlfile_get_property(char *name, INTERNAL_FUNCTION_PARAMETERS) return; } res = zend_read_property(curl_CURLFile_class, getThis(), name, strlen(name), 1 TSRMLS_CC); - *return_value = *res; - zval_copy_ctor(return_value); - INIT_PZVAL(return_value); + RETURN_ZVAL(res, 1, 0); } static void curlfile_set_property(char *name, INTERNAL_FUNCTION_PARAMETERS) diff --git a/ext/curl/interface.c b/ext/curl/interface.c index 6ed31553a8..28edf0b61d 100644 --- a/ext/curl/interface.c +++ b/ext/curl/interface.c @@ -149,15 +149,15 @@ static struct gcry_thread_cbs php_curl_gnutls_tsl = { /* }}} */ static void _php_curl_close_ex(php_curl *ch TSRMLS_DC); -static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC); +static void _php_curl_close(zend_resource *rsrc TSRMLS_DC); #define SAVE_CURL_ERROR(__handle, __err) (__handle)->err.no = (int) __err; -#define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s), (long) v); -#define CAAD(s, v) add_assoc_double_ex(return_value, s, sizeof(s), (double) v); -#define CAAS(s, v) add_assoc_string_ex(return_value, s, sizeof(s), (char *) (v ? v : "")); -#define CAAZ(s, v) add_assoc_zval_ex(return_value, s, sizeof(s), (zval *) v); +#define CAAL(s, v) add_assoc_long_ex(return_value, s, sizeof(s) - 1, (long) v); +#define CAAD(s, v) add_assoc_double_ex(return_value, s, sizeof(s) - 1, (double) v); +#define CAAS(s, v) add_assoc_string_ex(return_value, s, sizeof(s) - 1, (char *) (v ? v : "")); +#define CAAZ(s, v) add_assoc_zval_ex(return_value, s, sizeof(s) -1 , (zval *) v); #if defined(PHP_WIN32) || defined(__GNUC__) # define php_curl_ret(__ret) RETVAL_FALSE; return __ret; @@ -223,55 +223,55 @@ void _php_curl_verify_handlers(php_curl *ch, int reporterror TSRMLS_DC) /* {{{ * return; } - if (ch->handlers->std_err) { - stream = (php_stream *) zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (!Z_ISUNDEF(ch->handlers->std_err)) { + stream = zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); if (stream == NULL) { if (reporterror) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_STDERR resource has gone away, resetting to stderr"); } zval_ptr_dtor(&ch->handlers->std_err); - ch->handlers->std_err = NULL; + ZVAL_UNDEF(&ch->handlers->std_err); curl_easy_setopt(ch->cp, CURLOPT_STDERR, stderr); } } - if (ch->handlers->read && ch->handlers->read->stream) { - stream = (php_stream *) zend_fetch_resource(&ch->handlers->read->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (ch->handlers->read && !Z_ISUNDEF(ch->handlers->read->stream)) { + stream = zend_fetch_resource(&ch->handlers->read->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); if (stream == NULL) { if (reporterror) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_INFILE resource has gone away, resetting to default"); } zval_ptr_dtor(&ch->handlers->read->stream); - ch->handlers->read->fd = 0; + ZVAL_UNDEF(&ch->handlers->read->stream); + ch->handlers->read->res = NULL; ch->handlers->read->fp = 0; - ch->handlers->read->stream = NULL; curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch); } } - if (ch->handlers->write_header && ch->handlers->write_header->stream) { - stream = (php_stream *) zend_fetch_resource(&ch->handlers->write_header->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (ch->handlers->write_header && !Z_ISUNDEF(ch->handlers->write_header->stream)) { + stream = zend_fetch_resource(&ch->handlers->write_header->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); if (stream == NULL) { if (reporterror) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_WRITEHEADER resource has gone away, resetting to default"); } zval_ptr_dtor(&ch->handlers->write_header->stream); + ZVAL_UNDEF(&ch->handlers->write_header->stream); ch->handlers->write_header->fp = 0; - ch->handlers->write_header->stream = NULL; ch->handlers->write_header->method = PHP_CURL_IGNORE; curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch); } } - if (ch->handlers->write && ch->handlers->write->stream) { - stream = (php_stream *) zend_fetch_resource(&ch->handlers->write->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + if (ch->handlers->write && !Z_ISUNDEF(ch->handlers->write->stream)) { + stream = zend_fetch_resource(&ch->handlers->write->stream TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); if (stream == NULL) { if (reporterror) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FILE resource has gone away, resetting to default"); } zval_ptr_dtor(&ch->handlers->write->stream); + ZVAL_UNDEF(&ch->handlers->write->stream); ch->handlers->write->fp = 0; - ch->handlers->write->stream = NULL; ch->handlers->write->method = PHP_CURL_STDOUT; curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch); @@ -1269,9 +1269,9 @@ static size_t curl_write_nothing(char *data, size_t size, size_t nmemb, void *ct */ static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx) { - php_curl *ch = (php_curl *) ctx; - php_curl_write *t = ch->handlers->write; - size_t length = size * nmemb; + php_curl *ch = (php_curl *) ctx; + php_curl_write *t = ch->handlers->write; + size_t length = size * nmemb; TSRMLS_FETCH_FROM_CTX(ch->thread_ctx); #if PHP_CURL_DEBUG @@ -1291,27 +1291,20 @@ static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx) } break; case PHP_CURL_USER: { - zval **argv[2]; - zval *retval_ptr = NULL; - zval *handle = NULL; - zval *zdata = NULL; - int error; + zval argv[2]; + zval retval; + int error; zend_fcall_info fci; - MAKE_STD_ZVAL(handle); - ZVAL_RESOURCE(handle, ch->id); - zend_list_addref(ch->id); - argv[0] = &handle; - - MAKE_STD_ZVAL(zdata); - ZVAL_STRINGL(zdata, data, length, 1); - argv[1] = &zdata; + ZVAL_RES(&argv[0], ch->res); + Z_ADDREF(argv[0]); + ZVAL_STRINGL(&argv[1], data, length); fci.size = sizeof(fci); fci.function_table = EG(function_table); - fci.object_ptr = NULL; - fci.function_name = t->func_name; - fci.retval_ptr_ptr = &retval_ptr; + fci.object = NULL; + ZVAL_COPY_VALUE(&fci.function_name, &t->func_name); + fci.retval = &retval; fci.param_count = 2; fci.params = argv; fci.no_separation = 0; @@ -1323,16 +1316,15 @@ static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx) if (error == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_WRITEFUNCTION"); length = -1; - } else if (retval_ptr) { - if (Z_TYPE_P(retval_ptr) != IS_LONG) { - convert_to_long_ex(&retval_ptr); + } else if (!Z_ISUNDEF(retval)) { + if (Z_TYPE(retval) != IS_LONG) { + convert_to_long_ex(&retval); } - length = Z_LVAL_P(retval_ptr); - zval_ptr_dtor(&retval_ptr); + length = Z_LVAL(retval); } - zval_ptr_dtor(argv[0]); - zval_ptr_dtor(argv[1]); + zval_ptr_dtor(&argv[0]); + zval_ptr_dtor(&argv[1]); break; } } @@ -1346,17 +1338,14 @@ static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx) */ static int curl_fnmatch(void *ctx, const char *pattern, const char *string) { - php_curl *ch = (php_curl *) ctx; + php_curl *ch = (php_curl *) ctx; php_curl_fnmatch *t = ch->handlers->fnmatch; int rval = CURL_FNMATCHFUNC_FAIL; switch (t->method) { case PHP_CURL_USER: { - zval **argv[3]; - zval *zhandle = NULL; - zval *zpattern = NULL; - zval *zstring = NULL; - zval *retval_ptr; - int error; + zval argv[3]; + zval retval; + int error; zend_fcall_info fci; TSRMLS_FETCH_FROM_CTX(ch->thread_ctx); @@ -1364,20 +1353,16 @@ static int curl_fnmatch(void *ctx, const char *pattern, const char *string) MAKE_STD_ZVAL(zpattern); MAKE_STD_ZVAL(zstring); - ZVAL_RESOURCE(zhandle, ch->id); - zend_list_addref(ch->id); - ZVAL_STRING(zpattern, pattern, 1); - ZVAL_STRING(zstring, string, 1); - - argv[0] = &zhandle; - argv[1] = &zpattern; - argv[2] = &zstring; + ZVAL_RES(&argv[0], ch->res); + Z_ADDREF(argv[0]); + ZVAL_STRING(&argv[1], pattern); + ZVAL_STRING(&argv[2], string); fci.size = sizeof(fci); fci.function_table = EG(function_table); - fci.function_name = t->func_name; - fci.object_ptr = NULL; - fci.retval_ptr_ptr = &retval_ptr; + ZVAL_COPY_VALUE(&fci.function_name, &t->func_name); + fci.object = NULL; + fci.retval = &retval; fci.param_count = 3; fci.params = argv; fci.no_separation = 0; @@ -1388,16 +1373,15 @@ static int curl_fnmatch(void *ctx, const char *pattern, const char *string) ch->in_callback = 0; if (error == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_FNMATCH_FUNCTION"); - } else if (retval_ptr) { - if (Z_TYPE_P(retval_ptr) != IS_LONG) { - convert_to_long_ex(&retval_ptr); + } else if (!Z_ISUNDEF(retval)) { + if (Z_TYPE(retval) != IS_LONG) { + convert_to_long_ex(&retval); } - rval = Z_LVAL_P(retval_ptr); - zval_ptr_dtor(&retval_ptr); + rval = Z_LVAL(retval); } - zval_ptr_dtor(argv[0]); - zval_ptr_dtor(argv[1]); - zval_ptr_dtor(argv[2]); + zval_ptr_dtor(&argv[0]); + zval_ptr_dtor(&argv[1]); + zval_ptr_dtor(&argv[2]); break; } } @@ -1410,8 +1394,8 @@ static int curl_fnmatch(void *ctx, const char *pattern, const char *string) */ static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) { - php_curl *ch = (php_curl *) clientp; - php_curl_progress *t = ch->handlers->progress; + php_curl *ch = (php_curl *)clientp; + php_curl_progress *t = ch->handlers->progress; size_t rval = 0; #if PHP_CURL_DEBUG @@ -1421,41 +1405,24 @@ static size_t curl_progress(void *clientp, double dltotal, double dlnow, double switch (t->method) { case PHP_CURL_USER: { - zval **argv[5]; - zval *handle = NULL; - zval *zdltotal = NULL; - zval *zdlnow = NULL; - zval *zultotal = NULL; - zval *zulnow = NULL; - zval *retval_ptr; - int error; + zval argv[5]; + zval retval; + int error; zend_fcall_info fci; TSRMLS_FETCH_FROM_CTX(ch->thread_ctx); - MAKE_STD_ZVAL(handle); - MAKE_STD_ZVAL(zdltotal); - MAKE_STD_ZVAL(zdlnow); - MAKE_STD_ZVAL(zultotal); - MAKE_STD_ZVAL(zulnow); - - ZVAL_RESOURCE(handle, ch->id); - zend_list_addref(ch->id); - ZVAL_LONG(zdltotal, (long) dltotal); - ZVAL_LONG(zdlnow, (long) dlnow); - ZVAL_LONG(zultotal, (long) ultotal); - ZVAL_LONG(zulnow, (long) ulnow); - - argv[0] = &handle; - argv[1] = &zdltotal; - argv[2] = &zdlnow; - argv[3] = &zultotal; - argv[4] = &zulnow; + ZVAL_RES(&argv[0], ch->res); + Z_ADDREF(argv[0]); + ZVAL_LONG(&argv[1], (long)dltotal); + ZVAL_LONG(&argv[2], (long)dlnow); + ZVAL_LONG(&argv[3], (long)ultotal); + ZVAL_LONG(&argv[4], (long)ulnow); fci.size = sizeof(fci); fci.function_table = EG(function_table); - fci.function_name = t->func_name; - fci.object_ptr = NULL; - fci.retval_ptr_ptr = &retval_ptr; + ZVAL_COPY_VALUE(&fci.function_name, &t->func_name); + fci.object = NULL; + fci.retval = &retval; fci.param_count = 5; fci.params = argv; fci.no_separation = 0; @@ -1466,20 +1433,19 @@ static size_t curl_progress(void *clientp, double dltotal, double dlnow, double ch->in_callback = 0; if (error == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_PROGRESSFUNCTION"); - } else if (retval_ptr) { - if (Z_TYPE_P(retval_ptr) != IS_LONG) { - convert_to_long_ex(&retval_ptr); + } else if (!Z_ISUNDEF(retval)) { + if (Z_TYPE(retval) != IS_LONG) { + convert_to_long_ex(&retval); } - if (0 != Z_LVAL_P(retval_ptr)) { + if (0 != Z_LVAL(retval)) { rval = 1; } - zval_ptr_dtor(&retval_ptr); } - zval_ptr_dtor(argv[0]); - zval_ptr_dtor(argv[1]); - zval_ptr_dtor(argv[2]); - zval_ptr_dtor(argv[3]); - zval_ptr_dtor(argv[4]); + zval_ptr_dtor(&argv[0]); + zval_ptr_dtor(&argv[1]); + zval_ptr_dtor(&argv[2]); + zval_ptr_dtor(&argv[3]); + zval_ptr_dtor(&argv[4]); break; } } @@ -1491,9 +1457,9 @@ static size_t curl_progress(void *clientp, double dltotal, double dlnow, double */ static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx) { - php_curl *ch = (php_curl *) ctx; - php_curl_read *t = ch->handlers->read; - int length = 0; + php_curl *ch = (php_curl *)ctx; + php_curl_read *t = ch->handlers->read; + int length = 0; switch (t->method) { case PHP_CURL_DIRECT: @@ -1502,34 +1468,23 @@ static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx) } break; case PHP_CURL_USER: { - zval **argv[3]; - zval *handle = NULL; - zval *zfd = NULL; - zval *zlength = NULL; - zval *retval_ptr; - int error; + zval argv[3]; + zval retval; + int error; zend_fcall_info fci; TSRMLS_FETCH_FROM_CTX(ch->thread_ctx); - MAKE_STD_ZVAL(handle); - MAKE_STD_ZVAL(zfd); - MAKE_STD_ZVAL(zlength); - - ZVAL_RESOURCE(handle, ch->id); - zend_list_addref(ch->id); - ZVAL_RESOURCE(zfd, t->fd); - zend_list_addref(t->fd); - ZVAL_LONG(zlength, (int) size * nmemb); - - argv[0] = &handle; - argv[1] = &zfd; - argv[2] = &zlength; + ZVAL_RES(&argv[0], ch->res); + Z_ADDREF(argv[0]); + ZVAL_RES(&argv[1], t->res); + Z_ADDREF(argv[1]); + ZVAL_LONG(&argv[2], (int)size * nmemb); fci.size = sizeof(fci); fci.function_table = EG(function_table); - fci.function_name = t->func_name; - fci.object_ptr = NULL; - fci.retval_ptr_ptr = &retval_ptr; + ZVAL_COPY_VALUE(&fci.function_name, &t->func_name); + fci.object = NULL; + fci.retval = &retval; fci.param_count = 3; fci.params = argv; fci.no_separation = 0; @@ -1543,17 +1498,17 @@ static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx) #if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */ length = CURL_READFUNC_ABORT; #endif - } else if (retval_ptr) { - if (Z_TYPE_P(retval_ptr) == IS_STRING) { - length = MIN((int) (size * nmemb), Z_STRLEN_P(retval_ptr)); - memcpy(data, Z_STRVAL_P(retval_ptr), length); + } else if (!Z_ISUNDEF(retval)) { + if (Z_TYPE(retval) == IS_STRING) { + length = MIN((int) (size * nmemb), Z_STRLEN(retval)); + memcpy(data, Z_STRVAL(retval), length); } - zval_ptr_dtor(&retval_ptr); + zval_ptr_dtor(&retval); } - zval_ptr_dtor(argv[0]); - zval_ptr_dtor(argv[1]); - zval_ptr_dtor(argv[2]); + zval_ptr_dtor(&argv[0]); + zval_ptr_dtor(&argv[1]); + zval_ptr_dtor(&argv[2]); break; } } @@ -1566,9 +1521,9 @@ static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx) */ static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx) { - php_curl *ch = (php_curl *) ctx; - php_curl_write *t = ch->handlers->write_header; - size_t length = size * nmemb; + php_curl *ch = (php_curl *) ctx; + php_curl_write *t = ch->handlers->write_header; + size_t length = size * nmemb; TSRMLS_FETCH_FROM_CTX(ch->thread_ctx); switch (t->method) { @@ -1584,29 +1539,21 @@ static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx case PHP_CURL_FILE: return fwrite(data, size, nmemb, t->fp); case PHP_CURL_USER: { - zval **argv[2]; - zval *handle = NULL; - zval *zdata = NULL; - zval *retval_ptr; - int error; + zval argv[2]; + zval retval; + int error; zend_fcall_info fci; - MAKE_STD_ZVAL(handle); - MAKE_STD_ZVAL(zdata); - - ZVAL_RESOURCE(handle, ch->id); - zend_list_addref(ch->id); - ZVAL_STRINGL(zdata, data, length, 1); - - argv[0] = &handle; - argv[1] = &zdata; + ZVAL_RES(&argv[0], ch->res); + Z_ADDREF(argv[0]); + ZVAL_STRINGL(&argv[1], data, length); fci.size = sizeof(fci); fci.function_table = EG(function_table); - fci.function_name = t->func_name; + ZVAL_COPY_VALUE(&fci.function_name, &t->func_name); fci.symbol_table = NULL; - fci.object_ptr = NULL; - fci.retval_ptr_ptr = &retval_ptr; + fci.object = NULL; + fci.retval = &retval; fci.param_count = 2; fci.params = argv; fci.no_separation = 0; @@ -1617,15 +1564,14 @@ static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx if (error == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_HEADERFUNCTION"); length = -1; - } else if (retval_ptr) { - if (Z_TYPE_P(retval_ptr) != IS_LONG) { - convert_to_long_ex(&retval_ptr); + } else if (!Z_ISUNDEF(retval)) { + if (Z_TYPE(retval) != IS_LONG) { + convert_to_long_ex(&retval); } - length = Z_LVAL_P(retval_ptr); - zval_ptr_dtor(&retval_ptr); + length = Z_LVAL(retval); } - zval_ptr_dtor(argv[0]); - zval_ptr_dtor(argv[1]); + zval_ptr_dtor(&argv[0]); + zval_ptr_dtor(&argv[1]); break; } @@ -1642,7 +1588,7 @@ static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, void *ctx) /* {{{ */ { - php_curl *ch = (php_curl *) ctx; + php_curl *ch = (php_curl *)ctx; if (type == CURLINFO_HEADER_OUT) { if (ch->header.str_len) { @@ -1663,31 +1609,27 @@ static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, v */ static size_t curl_passwd(void *ctx, char *prompt, char *buf, int buflen) { - php_curl *ch = (php_curl *) ctx; - zval *func = ch->handlers->passwd; - zval *argv[3]; - zval *retval = NULL; - int error; - int ret = -1; + php_curl *ch = (php_curl *) ctx; + zval *func = &ch->handlers->passwd; + zval argv[3]; + zval retval; + int error; + int ret = -1; TSRMLS_FETCH_FROM_CTX(ch->thread_ctx); - MAKE_STD_ZVAL(argv[0]); - MAKE_STD_ZVAL(argv[1]); - MAKE_STD_ZVAL(argv[2]); - - ZVAL_RESOURCE(argv[0], ch->id); - zend_list_addref(ch->id); - ZVAL_STRING(argv[1], prompt, 1); - ZVAL_LONG(argv[2], buflen); + ZVAL_RES(&argv[0], ch->res); + Z_ADDREF(argv[0]); + ZVAL_STRING(&argv[1], prompt); + ZVAL_LONG(&argv[2], buflen); - error = call_user_function(EG(function_table), NULL, func, retval, 2, argv TSRMLS_CC); + error = call_user_function(EG(function_table), NULL, func, &retval, 2, argv TSRMLS_CC); if (error == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not call the CURLOPT_PASSWDFUNCTION"); - } else if (Z_TYPE_P(retval) == IS_STRING) { - if (Z_STRLEN_P(retval) > buflen) { + } else if (Z_TYPE(retval) == IS_STRING) { + if (Z_STRLEN(retval) > buflen) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Returned password is too long for libcurl to handle"); } else { - strlcpy(buf, Z_STRVAL_P(retval), Z_STRLEN_P(retval)); + memcpy(buf, Z_STRVAL(retval), Z_STRLEN(retval) + 1); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "User handler '%s' did not return a string", Z_STRVAL_P(func)); @@ -1721,9 +1663,9 @@ static void curl_free_post(void **post) /* {{{ curl_free_slist */ -static void curl_free_slist(void *slist) +static void curl_free_slist(zval *el) { - curl_slist_free_all(*((struct curl_slist **) slist)); + curl_slist_free_all(((struct curl_slist *)Z_PTR_P(el))); } /* }}} */ @@ -1756,16 +1698,15 @@ PHP_FUNCTION(curl_version) /* Add an array of protocols */ { char **p = (char **) d->protocols; - zval *protocol_list = NULL; + zval protocol_list; - MAKE_STD_ZVAL(protocol_list); - array_init(protocol_list); + array_init(&protocol_list); while (*p != NULL) { - add_next_index_string(protocol_list, *p); + add_next_index_string(&protocol_list, *p); p++; } - CAAZ("protocols", protocol_list); + CAAZ("protocols", &protocol_list); } } /* }}} */ @@ -1774,7 +1715,7 @@ PHP_FUNCTION(curl_version) */ static void alloc_curl_handle(php_curl **ch) { - *ch = emalloc(sizeof(php_curl)); + *ch = ecalloc(1, sizeof(php_curl)); (*ch)->to_free = ecalloc(1, sizeof(struct _php_curl_free)); (*ch)->handlers = ecalloc(1, sizeof(php_curl_handlers)); (*ch)->handlers->write = ecalloc(1, sizeof(php_curl_write)); @@ -1785,13 +1726,9 @@ static void alloc_curl_handle(php_curl **ch) (*ch)->handlers->fnmatch = NULL; #endif - (*ch)->in_callback = 0; (*ch)->header.str_len = 0; - memset(&(*ch)->err, 0, sizeof((*ch)->err)); - (*ch)->handlers->write->stream = NULL; - (*ch)->handlers->write_header->stream = NULL; - (*ch)->handlers->read->stream = NULL; + memset(&(*ch)->err, 0, sizeof(struct _php_curl_error)); zend_llist_init(&(*ch)->to_free->str, sizeof(char *), (llist_dtor_func_t) curl_free_string, 0); zend_llist_init(&(*ch)->to_free->post, sizeof(struct HttpPost), (llist_dtor_func_t) curl_free_post, 0); @@ -1841,15 +1778,14 @@ static void create_certinfo(struct curl_certinfo *ci, zval *listcode TSRMLS_DC) { int i; - if(ci) { - zval *certhash = NULL; + if (ci) { + zval certhash; - for(i=0; inum_of_certs; i++) { + for (i=0; inum_of_certs; i++) { struct curl_slist *slist; - MAKE_STD_ZVAL(certhash); - array_init(certhash); - for(slist = ci->certinfo[i]; slist; slist = slist->next) { + array_init(&certhash); + for (slist = ci->certinfo[i]; slist; slist = slist->next) { int len; char s[64]; char *tmp; @@ -1858,22 +1794,21 @@ static void create_certinfo(struct curl_certinfo *ci, zval *listcode TSRMLS_DC) if(tmp) { *tmp = '\0'; len = strlen(s); - if(!strcmp(s, "Subject") || !strcmp(s, "Issuer")) { - zval *hash; + if (!strcmp(s, "Subject") || !strcmp(s, "Issuer")) { + zval hash; - MAKE_STD_ZVAL(hash); - array_init(hash); + array_init(&hash); - split_certinfo(&slist->data[len+1], hash); - add_assoc_zval(certhash, s, hash); + split_certinfo(&slist->data[len+1], &hash); + add_assoc_zval(&certhash, s, &hash); } else { - add_assoc_string(certhash, s, &slist->data[len+1]); + add_assoc_string(&certhash, s, &slist->data[len+1]); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not extract hash key from certificate info"); } } - add_next_index_zval(listcode, certhash); + add_next_index_zval(listcode, &certhash); } } } @@ -1917,11 +1852,10 @@ static void _php_curl_set_default_options(php_curl *ch) Initialize a cURL session */ PHP_FUNCTION(curl_init) { - php_curl *ch; - CURL *cp; - zval *clone; - char *url = NULL; - int url_len = 0; + php_curl *ch; + CURL *cp; + char *url = NULL; + int url_len = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &url, &url_len) == FAILURE) { return; @@ -1942,9 +1876,6 @@ PHP_FUNCTION(curl_init) ch->handlers->read->method = PHP_CURL_DIRECT; ch->handlers->write_header->method = PHP_CURL_IGNORE; - MAKE_STD_ZVAL(clone); - ch->clone = clone; - _php_curl_set_default_options(ch); if (url) { @@ -1955,7 +1886,7 @@ PHP_FUNCTION(curl_init) } ZEND_REGISTER_RESOURCE(return_value, ch, le_curl); - ch->id = Z_LVAL_P(return_value); + ch->res = Z_RES_P(return_value); } /* }}} */ @@ -1971,7 +1902,7 @@ PHP_FUNCTION(curl_copy_handle) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); cp = curl_easy_duphandle(ch->cp); if (!cp) { @@ -1983,45 +1914,41 @@ PHP_FUNCTION(curl_copy_handle) TSRMLS_SET_CTX(dupch->thread_ctx); dupch->cp = cp; - zend_list_addref(Z_LVAL_P(zid)); - if (ch->handlers->write->stream) { - Z_ADDREF_P(ch->handlers->write->stream); + Z_ADDREF_P(zid); + if (!Z_ISUNDEF(ch->handlers->write->stream)) { + Z_ADDREF(ch->handlers->write->stream); } dupch->handlers->write->stream = ch->handlers->write->stream; dupch->handlers->write->method = ch->handlers->write->method; - if (ch->handlers->read->stream) { - Z_ADDREF_P(ch->handlers->read->stream); + if (!Z_ISUNDEF(ch->handlers->read->stream)) { + Z_ADDREF(ch->handlers->read->stream); } dupch->handlers->read->stream = ch->handlers->read->stream; dupch->handlers->read->method = ch->handlers->read->method; dupch->handlers->write_header->method = ch->handlers->write_header->method; - if (ch->handlers->write_header->stream) { - Z_ADDREF_P(ch->handlers->write_header->stream); + if (!Z_ISUNDEF(ch->handlers->write_header->stream)) { + Z_ADDREF(ch->handlers->write_header->stream); } dupch->handlers->write_header->stream = ch->handlers->write_header->stream; dupch->handlers->write->fp = ch->handlers->write->fp; dupch->handlers->write_header->fp = ch->handlers->write_header->fp; dupch->handlers->read->fp = ch->handlers->read->fp; - dupch->handlers->read->fd = ch->handlers->read->fd; + dupch->handlers->read->res = ch->handlers->read->res; #if CURLOPT_PASSWDDATA != 0 - if (ch->handlers->passwd) { - zval_add_ref(&ch->handlers->passwd); - dupch->handlers->passwd = ch->handlers->passwd; + if (!Z_ISUNDEF(ch->handlers->passwd)) { + ZVAL_COPY(&dupch->handlers->passwd, &ch->handlers->passwd); curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) dupch); } #endif - if (ch->handlers->write->func_name) { - zval_add_ref(&ch->handlers->write->func_name); - dupch->handlers->write->func_name = ch->handlers->write->func_name; + if (!Z_ISUNDEF(ch->handlers->write->func_name)) { + ZVAL_COPY(&dupch->handlers->write->func_name, &ch->handlers->write->func_name); } - if (ch->handlers->read->func_name) { - zval_add_ref(&ch->handlers->read->func_name); - dupch->handlers->read->func_name = ch->handlers->read->func_name; + if (!Z_ISUNDEF(ch->handlers->read->func_name)) { + ZVAL_COPY(&dupch->handlers->read->func_name, &ch->handlers->read->func_name); } - if (ch->handlers->write_header->func_name) { - zval_add_ref(&ch->handlers->write_header->func_name); - dupch->handlers->write_header->func_name = ch->handlers->write_header->func_name; + if (!Z_ISUNDEF(ch->handlers->write_header->func_name)) { + ZVAL_COPY(&dupch->handlers->write_header->func_name, &ch->handlers->write_header->func_name); } curl_easy_setopt(dupch->cp, CURLOPT_ERRORBUFFER, dupch->err.str); @@ -2031,9 +1958,8 @@ PHP_FUNCTION(curl_copy_handle) if (ch->handlers->progress) { dupch->handlers->progress = ecalloc(1, sizeof(php_curl_progress)); - if (ch->handlers->progress->func_name) { - zval_add_ref(&ch->handlers->progress->func_name); - dupch->handlers->progress->func_name = ch->handlers->progress->func_name; + if (!Z_ISUNDEF(ch->handlers->progress->func_name)) { + ZVAL_COPY(&dupch->handlers->progress->func_name, &ch->handlers->progress->func_name); } dupch->handlers->progress->method = ch->handlers->progress->method; curl_easy_setopt(dupch->cp, CURLOPT_PROGRESSDATA, (void *) dupch); @@ -2043,9 +1969,8 @@ PHP_FUNCTION(curl_copy_handle) #if LIBCURL_VERSION_NUM >= 0x071500 if (ch->handlers->fnmatch) { dupch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch)); - if (ch->handlers->fnmatch->func_name) { - zval_add_ref(&ch->handlers->fnmatch->func_name); - dupch->handlers->fnmatch->func_name = ch->handlers->fnmatch->func_name; + if (!Z_ISUNDEF(ch->handlers->fnmatch->func_name)) { + ZVAL_COPY(&dupch->handlers->fnmatch->func_name, ch->handlers->fnmatch->func_name); } dupch->handlers->fnmatch->method = ch->handlers->fnmatch->method; curl_easy_setopt(dupch->cp, CURLOPT_FNMATCH_DATA, (void *) dupch); @@ -2057,22 +1982,22 @@ PHP_FUNCTION(curl_copy_handle) dupch->to_free = ch->to_free; /* Keep track of cloned copies to avoid invoking curl destructors for every clone */ - Z_ADDREF_P(ch->clone); - dupch->clone = ch->clone; + ZVAL_COPY(&dupch->clone, &ch->clone); ZEND_REGISTER_RESOURCE(return_value, dupch, le_curl); - dupch->id = Z_LVAL_P(return_value); + dupch->res = Z_RES_P(return_value); } /* }}} */ -static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) /* {{{ */ +static int _php_curl_setopt(php_curl *ch, long option, zval *zvalue TSRMLS_DC) /* {{{ */ { - CURLcode error=CURLE_OK; + CURLcode error = CURLE_OK; switch (option) { /* Long options */ case CURLOPT_SSL_VERIFYHOST: - if(Z_BVAL_PP(zvalue) == 1) { + convert_to_boolean(zvalue); + if (Z_TYPE_P(zvalue) == IS_TRUE) { #if LIBCURL_VERSION_NUM <= 0x071c00 /* 7.28.0 */ php_error_docref(NULL TSRMLS_CC, E_NOTICE, "CURLOPT_SSL_VERIFYHOST with value 1 is deprecated and will be removed as of libcurl 7.28.1. It is recommended to use value 2 instead"); #else @@ -2229,16 +2154,16 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) convert_to_long_ex(zvalue); #if LIBCURL_VERSION_NUM >= 0x71304 if ((option == CURLOPT_PROTOCOLS || option == CURLOPT_REDIR_PROTOCOLS) && - (PG(open_basedir) && *PG(open_basedir)) && (Z_LVAL_PP(zvalue) & CURLPROTO_FILE)) { + (PG(open_basedir) && *PG(open_basedir)) && (Z_LVAL_P(zvalue) & CURLPROTO_FILE)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLPROTO_FILE cannot be activated when an open_basedir is set"); return 1; } #endif - error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue)); + error = curl_easy_setopt(ch->cp, option, Z_LVAL_P(zvalue)); break; case CURLOPT_SAFE_UPLOAD: convert_to_long_ex(zvalue); - ch->safe_upload = (Z_LVAL_PP(zvalue) != 0); + ch->safe_upload = (Z_LVAL_P(zvalue) != 0); break; /* String options */ @@ -2300,7 +2225,7 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) #endif { convert_to_string_ex(zvalue); - return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 0 TSRMLS_CC); + return php_curl_option_str(ch, option, Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue), 0 TSRMLS_CC); } /* Curl nullable string options */ @@ -2319,11 +2244,11 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) case CURLOPT_KRB4LEVEL: #endif { - if (Z_TYPE_PP(zvalue) == IS_NULL) { + if (Z_ISNULL_P(zvalue)) { error = curl_easy_setopt(ch->cp, option, NULL); } else { convert_to_string_ex(zvalue); - return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 0 TSRMLS_CC); + return php_curl_option_str(ch, option, Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue), 0 TSRMLS_CC); } break; } @@ -2331,12 +2256,12 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) /* Curl private option */ case CURLOPT_PRIVATE: convert_to_string_ex(zvalue); - return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 1 TSRMLS_CC); + return php_curl_option_str(ch, option, Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue), 1 TSRMLS_CC); /* Curl url option */ case CURLOPT_URL: convert_to_string_ex(zvalue); - return php_curl_option_url(ch, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue) TSRMLS_CC); + return php_curl_option_url(ch, Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue) TSRMLS_CC); /* Curl file handle options */ case CURLOPT_FILE: @@ -2345,15 +2270,15 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) case CURLOPT_WRITEHEADER: { FILE *fp = NULL; int type; - void *what = NULL; + php_stream *what = NULL; - if (Z_TYPE_PP(zvalue) != IS_NULL) { + if (Z_TYPE_P(zvalue) != IS_NULL) { what = zend_fetch_resource(zvalue TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream(), php_file_le_pstream()); if (!what) { return FAILURE; } - if (FAILURE == php_stream_cast((php_stream *) what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) { + if (FAILURE == php_stream_cast(what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) { return FAILURE; } @@ -2366,20 +2291,17 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) switch (option) { case CURLOPT_FILE: if (!what) { - if (ch->handlers->write->stream) { - Z_DELREF_P(ch->handlers->write->stream); - ch->handlers->write->stream = NULL; + if (!Z_ISUNDEF(ch->handlers->write->stream)) { + zval_ptr_dtor(&ch->handlers->write->stream); + ZVAL_UNDEF(&ch->handlers->write->stream); } ch->handlers->write->fp = NULL; ch->handlers->write->method = PHP_CURL_STDOUT; - } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') { - if (ch->handlers->write->stream) { - Z_DELREF_P(ch->handlers->write->stream); - } - Z_ADDREF_PP(zvalue); + } else if (what->mode[0] != 'r' || what->mode[1] == '+') { + zval_ptr_dtor(&ch->handlers->write->stream); ch->handlers->write->fp = fp; ch->handlers->write->method = PHP_CURL_FILE; - ch->handlers->write->stream = *zvalue; + ZVAL_COPY(&ch->handlers->write->stream, zvalue); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable"); return FAILURE; @@ -2387,20 +2309,17 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) break; case CURLOPT_WRITEHEADER: if (!what) { - if (ch->handlers->write_header->stream) { - Z_DELREF_P(ch->handlers->write_header->stream); - ch->handlers->write_header->stream = NULL; + if (!Z_ISUNDEF(ch->handlers->write_header->stream)) { + zval_ptr_dtor(&ch->handlers->write_header->stream); + ZVAL_UNDEF(&ch->handlers->write_header->stream); } ch->handlers->write_header->fp = NULL; ch->handlers->write_header->method = PHP_CURL_IGNORE; - } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') { - if (ch->handlers->write_header->stream) { - Z_DELREF_P(ch->handlers->write_header->stream); - } - Z_ADDREF_PP(zvalue); + } else if (what->mode[0] != 'r' || what->mode[1] == '+') { + zval_ptr_dtor(&ch->handlers->write_header->stream); ch->handlers->write_header->fp = fp; ch->handlers->write_header->method = PHP_CURL_FILE; - ch->handlers->write_header->stream = *zvalue; + ZVAL_COPY(&ch->handlers->write_header->stream, zvalue);; } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable"); return FAILURE; @@ -2408,34 +2327,28 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) break; case CURLOPT_INFILE: if (!what) { - if (ch->handlers->read->stream) { - Z_DELREF_P(ch->handlers->read->stream); - ch->handlers->read->stream = NULL; + if (!Z_ISUNDEF(ch->handlers->read->stream)) { + zval_ptr_dtor(&ch->handlers->read->stream); + ZVAL_UNDEF(&ch->handlers->read->stream); } ch->handlers->read->fp = NULL; - ch->handlers->read->fd = 0; + ch->handlers->read->res = NULL; } else { - if (ch->handlers->read->stream) { - Z_DELREF_P(ch->handlers->read->stream); - } - Z_ADDREF_PP(zvalue); + zval_ptr_dtor(&ch->handlers->read->stream); ch->handlers->read->fp = fp; - ch->handlers->read->fd = Z_LVAL_PP(zvalue); - ch->handlers->read->stream = *zvalue; + ch->handlers->read->res = Z_RES_P(zvalue); + ZVAL_COPY(&ch->handlers->read->stream, zvalue); } break; case CURLOPT_STDERR: if (!what) { - if (ch->handlers->std_err) { - zval_ptr_dtor(&ch->handlers->std_err); - ch->handlers->std_err = NULL; - } - } else if (((php_stream *) what)->mode[0] != 'r' || ((php_stream *) what)->mode[1] == '+') { - if (ch->handlers->std_err) { + if (!Z_ISUNDEF(ch->handlers->std_err)) { zval_ptr_dtor(&ch->handlers->std_err); + ZVAL_UNDEF(&ch->handlers->std_err); } - zval_add_ref(zvalue); - ch->handlers->std_err = *zvalue; + } else if (what->mode[0] != 'r' || what->mode[1] == '+') { + zval_ptr_dtor(&ch->handlers->std_err); + ZVAL_COPY(&ch->handlers->std_err, zvalue); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "the provided file handle is not writable"); return FAILURE; @@ -2462,11 +2375,11 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) case CURLOPT_RESOLVE: #endif { - zval **current; - HashTable *ph; - struct curl_slist *slist = NULL; + zval *current; + HashTable *ph; + struct curl_slist *slist = NULL; - ph = HASH_OF(*zvalue); + ph = HASH_OF(zvalue); if (!ph) { char *name = NULL; switch (option) { @@ -2503,20 +2416,18 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) return FAILURE; } - for (zend_hash_internal_pointer_reset(ph); - zend_hash_get_current_data(ph, (void **) ¤t) == SUCCESS; - zend_hash_move_forward(ph) - ) { + ZEND_HASH_FOREACH_VAL(ph, current) { SEPARATE_ZVAL(current); convert_to_string_ex(current); - slist = curl_slist_append(slist, Z_STRVAL_PP(current)); + slist = curl_slist_append(slist, Z_STRVAL_P(current)); if (!slist) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not build curl_slist"); return 1; } - } - zend_hash_index_update(ch->to_free->slist, (ulong) option, &slist, sizeof(struct curl_slist *), NULL); + } ZEND_HASH_FOREACH_END(); + + zend_hash_index_update_ptr(ch->to_free->slist, option, slist); error = curl_easy_setopt(ch->cp, option, slist); @@ -2531,67 +2442,60 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) convert_to_long_ex(zvalue); #if LIBCURL_VERSION_NUM < 0x071304 if (PG(open_basedir) && *PG(open_basedir)) { - if (Z_LVAL_PP(zvalue) != 0) { + if (Z_LVAL_P(zvalue) != 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set"); return FAILURE; } } #endif - error = curl_easy_setopt(ch->cp, option, Z_LVAL_PP(zvalue)); + error = curl_easy_setopt(ch->cp, option, Z_LVAL_P(zvalue)); break; case CURLOPT_HEADERFUNCTION: - if (ch->handlers->write_header->func_name) { + if (!Z_ISUNDEF(ch->handlers->write_header->func_name)) { zval_ptr_dtor(&ch->handlers->write_header->func_name); ch->handlers->write_header->fci_cache = empty_fcall_info_cache; } - zval_add_ref(zvalue); - ch->handlers->write_header->func_name = *zvalue; + ZVAL_COPY(&ch->handlers->write_header->func_name, zvalue); ch->handlers->write_header->method = PHP_CURL_USER; break; case CURLOPT_POSTFIELDS: - if (Z_TYPE_PP(zvalue) == IS_ARRAY || Z_TYPE_PP(zvalue) == IS_OBJECT) { - zval **current; - HashTable *postfields; + if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) { + zval *current; + HashTable *postfields; + zend_string *string_key; + ulong num_key; struct HttpPost *first = NULL; struct HttpPost *last = NULL; - postfields = HASH_OF(*zvalue); + postfields = HASH_OF(zvalue); if (!postfields) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS"); return FAILURE; } - for (zend_hash_internal_pointer_reset(postfields); - zend_hash_get_current_data(postfields, (void **) ¤t) == SUCCESS; - zend_hash_move_forward(postfields) - ) { + ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) { char *postval; - char *string_key = NULL; - uint string_key_len; - ulong num_key; int numeric_key; - - zend_hash_get_current_key_ex(postfields, &string_key, &string_key_len, &num_key, 0, NULL); - /* Pretend we have a string_key here */ - if(!string_key) { - spprintf(&string_key, 0, "%ld", num_key); - string_key_len = strlen(string_key)+1; + if (!string_key) { + string_key = STR_ALLOC(MAX_LENGTH_OF_LONG, 0); + string_key->len = snprintf(string_key->val, string_key->len, "%ld", num_key); numeric_key = 1; } else { numeric_key = 0; } - if(Z_TYPE_PP(current) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(current), curl_CURLFile_class TSRMLS_CC)) { + if (Z_TYPE_P(current) == IS_OBJECT && + instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class TSRMLS_CC)) { /* new-style file upload */ zval *prop; char *type = NULL, *filename = NULL; - prop = zend_read_property(curl_CURLFile_class, *current, "name", sizeof("name")-1, 0 TSRMLS_CC); - if(Z_TYPE_P(prop) != IS_STRING) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filename for key %s", string_key); + prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0 TSRMLS_CC); + if (Z_TYPE_P(prop) != IS_STRING) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filename for key %s", string_key->val); } else { postval = Z_STRVAL_P(prop); @@ -2599,17 +2503,17 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) return 1; } - prop = zend_read_property(curl_CURLFile_class, *current, "mime", sizeof("mime")-1, 0 TSRMLS_CC); - if(Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) { + prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0 TSRMLS_CC); + if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) { type = Z_STRVAL_P(prop); } - prop = zend_read_property(curl_CURLFile_class, *current, "postname", sizeof("postname")-1, 0 TSRMLS_CC); - if(Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) { + prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0 TSRMLS_CC); + if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) { filename = Z_STRVAL_P(prop); } error = curl_formadd(&first, &last, - CURLFORM_COPYNAME, string_key, - CURLFORM_NAMELENGTH, (long)string_key_len - 1, + CURLFORM_COPYNAME, string_key->val, + CURLFORM_NAMELENGTH, string_key->len, CURLFORM_FILENAME, filename ? filename : postval, CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream", CURLFORM_FILE, postval, @@ -2617,7 +2521,7 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) } if (numeric_key) { - efree(string_key); + STR_RELEASE(string_key); } continue; } @@ -2625,7 +2529,7 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) SEPARATE_ZVAL(current); convert_to_string_ex(current); - postval = Z_STRVAL_PP(current); + postval = Z_STRVAL_P(current); /* The arguments after _NAMELENGTH and _CONTENTSLENGTH * must be explicitly cast to long in curl_formadd @@ -2634,12 +2538,15 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) char *type, *filename; ++postval; - php_error_docref("curl.curlfile" TSRMLS_CC, E_DEPRECATED, "The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead"); + php_error_docref("curl.curlfile" TSRMLS_CC, E_DEPRECATED, + "The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead"); - if ((type = php_memnstr(postval, ";type=", sizeof(";type=") - 1, postval + Z_STRLEN_PP(current)))) { + if ((type = php_memnstr(postval, ";type=", sizeof(";type=") - 1, + postval + Z_STRLEN_P(current)))) { *type = '\0'; } - if ((filename = php_memnstr(postval, ";filename=", sizeof(";filename=") - 1, postval + Z_STRLEN_PP(current)))) { + if ((filename = php_memnstr(postval, ";filename=", sizeof(";filename=") - 1, + postval + Z_STRLEN_P(current)))) { *filename = '\0'; } /* open_basedir check */ @@ -2647,8 +2554,8 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) return FAILURE; } error = curl_formadd(&first, &last, - CURLFORM_COPYNAME, string_key, - CURLFORM_NAMELENGTH, (long)string_key_len - 1, + CURLFORM_COPYNAME, string_key->val, + CURLFORM_NAMELENGTH, string_key->len, CURLFORM_FILENAME, filename ? filename + sizeof(";filename=") - 1 : postval, CURLFORM_CONTENTTYPE, type ? type + sizeof(";type=") - 1 : "application/octet-stream", CURLFORM_FILE, postval, @@ -2661,44 +2568,43 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) } } else { error = curl_formadd(&first, &last, - CURLFORM_COPYNAME, string_key, - CURLFORM_NAMELENGTH, (long)string_key_len - 1, + CURLFORM_COPYNAME, string_key->val, + CURLFORM_NAMELENGTH, string_key->len, CURLFORM_COPYCONTENTS, postval, - CURLFORM_CONTENTSLENGTH, (long)Z_STRLEN_PP(current), + CURLFORM_CONTENTSLENGTH, Z_STRLEN_P(current), CURLFORM_END); } if (numeric_key) { - efree(string_key); + STR_RELEASE(string_key); } - } + } ZEND_HASH_FOREACH_END(); SAVE_CURL_ERROR(ch, error); if (error != CURLE_OK) { return FAILURE; } - if (Z_REFCOUNT_P(ch->clone) <= 1) { + if (Z_REFCOUNTED(ch->clone) && Z_REFCOUNT(ch->clone) <= 1) { zend_llist_clean(&ch->to_free->post); } zend_llist_add_element(&ch->to_free->post, &first); error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first); - } else { #if LIBCURL_VERSION_NUM >= 0x071101 convert_to_string_ex(zvalue); /* with curl 7.17.0 and later, we can use COPYPOSTFIELDS, but we have to provide size before */ - error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue)); - error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, Z_STRVAL_PP(zvalue)); + error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_P(zvalue)); + error = curl_easy_setopt(ch->cp, CURLOPT_COPYPOSTFIELDS, Z_STRVAL_P(zvalue)); #else char *post = NULL; convert_to_string_ex(zvalue); - post = estrndup(Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue)); + post = estrndup(Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue)); zend_llist_add_element(&ch->to_free->str, &post); curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, post); - error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_PP(zvalue)); + error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, Z_STRLEN_P(zvalue)); #endif } break; @@ -2708,28 +2614,26 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, ch); if (ch->handlers->progress == NULL) { ch->handlers->progress = ecalloc(1, sizeof(php_curl_progress)); - } else if (ch->handlers->progress->func_name) { + } else if (!Z_ISUNDEF(ch->handlers->progress->func_name)) { zval_ptr_dtor(&ch->handlers->progress->func_name); ch->handlers->progress->fci_cache = empty_fcall_info_cache; } - zval_add_ref(zvalue); - ch->handlers->progress->func_name = *zvalue; + ZVAL_COPY(&ch->handlers->progress->func_name, zvalue); ch->handlers->progress->method = PHP_CURL_USER; break; case CURLOPT_READFUNCTION: - if (ch->handlers->read->func_name) { + if (!Z_ISUNDEF(ch->handlers->read->func_name)) { zval_ptr_dtor(&ch->handlers->read->func_name); ch->handlers->read->fci_cache = empty_fcall_info_cache; } - zval_add_ref(zvalue); - ch->handlers->read->func_name = *zvalue; + ZVAL_COPY(&ch->handlers->read->func_name, zvalue); ch->handlers->read->method = PHP_CURL_USER; break; case CURLOPT_RETURNTRANSFER: convert_to_long_ex(zvalue); - if (Z_LVAL_PP(zvalue)) { + if (Z_LVAL_P(zvalue)) { ch->handlers->write->method = PHP_CURL_RETURN; } else { ch->handlers->write->method = PHP_CURL_STDOUT; @@ -2737,12 +2641,11 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) break; case CURLOPT_WRITEFUNCTION: - if (ch->handlers->write->func_name) { + if (!Z_ISUNDEF(ch->handlers->write->func_name)) { zval_ptr_dtor(&ch->handlers->write->func_name); ch->handlers->write->fci_cache = empty_fcall_info_cache; } - zval_add_ref(zvalue); - ch->handlers->write->func_name = *zvalue; + ZVAL_COPY(&ch->handlers->write->func_name, zvalue); ch->handlers->write->method = PHP_CURL_USER; break; @@ -2750,7 +2653,7 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) case CURLOPT_MAX_RECV_SPEED_LARGE: case CURLOPT_MAX_SEND_SPEED_LARGE: convert_to_long_ex(zvalue); - error = curl_easy_setopt(ch->cp, option, (curl_off_t)Z_LVAL_PP(zvalue)); + error = curl_easy_setopt(ch->cp, option, (curl_off_t)Z_LVAL_P(zvalue)); break; #endif @@ -2763,11 +2666,8 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) #if CURLOPT_PASSWDFUNCTION != 0 case CURLOPT_PASSWDFUNCTION: - if (ch->handlers->passwd) { - zval_ptr_dtor(&ch->handlers->passwd); - } - zval_add_ref(zvalue); - ch->handlers->passwd = *zvalue; + zval_ptr_dtor(&ch->handlers->passwd); + ZVAL_COPY(&ch->handlers->passwd, zvalue); error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDFUNCTION, curl_passwd); error = curl_easy_setopt(ch->cp, CURLOPT_PASSWDDATA, (void *) ch); break; @@ -2797,16 +2697,16 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) { convert_to_string_ex(zvalue); - if (Z_STRLEN_PP(zvalue) && php_check_open_basedir(Z_STRVAL_PP(zvalue) TSRMLS_CC)) { + if (Z_STRLEN_P(zvalue) && php_check_open_basedir(Z_STRVAL_P(zvalue) TSRMLS_CC)) { return FAILURE; } - return php_curl_option_str(ch, option, Z_STRVAL_PP(zvalue), Z_STRLEN_PP(zvalue), 0 TSRMLS_CC); + return php_curl_option_str(ch, option, Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue), 0 TSRMLS_CC); } case CURLINFO_HEADER_OUT: convert_to_long_ex(zvalue); - if (Z_LVAL_PP(zvalue) == 1) { + if (Z_LVAL_P(zvalue) == 1) { curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug); curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch); curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1); @@ -2832,12 +2732,11 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, ch); if (ch->handlers->fnmatch == NULL) { ch->handlers->fnmatch = ecalloc(1, sizeof(php_curl_fnmatch)); - } else if (ch->handlers->fnmatch->func_name) { + } else if (!Z_ISUNDEF(ch->handlers->fnmatch->func_name)) { zval_ptr_dtor(&ch->handlers->fnmatch->func_name); ch->handlers->fnmatch->fci_cache = empty_fcall_info_cache; } - zval_add_ref(zvalue); - ch->handlers->fnmatch->func_name = *zvalue; + ZVAL_COPY(&ch->handlers->fnmatch->func_name, zvalue); ch->handlers->fnmatch->method = PHP_CURL_USER; break; #endif @@ -2857,15 +2756,15 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue TSRMLS_DC) Set an option for a cURL transfer */ PHP_FUNCTION(curl_setopt) { - zval *zid, **zvalue; + zval *zid, *zvalue; long options; php_curl *ch; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &zid, &options, &zvalue) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &zid, &options, &zvalue) == FAILURE) { return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); if (options <= 0 && options != CURLOPT_SAFE_UPLOAD) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl configuration option"); @@ -2884,30 +2783,28 @@ PHP_FUNCTION(curl_setopt) Set an array of option for a cURL transfer */ PHP_FUNCTION(curl_setopt_array) { - zval *zid, *arr, **entry; + zval *zid, *arr, *entry; php_curl *ch; ulong option; - HashPosition pos; - char *string_key; - uint str_key_len; + zend_string *string_key; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &zid, &arr) == FAILURE) { return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); - zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos); - while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) { - if (zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &string_key, &str_key_len, &option, 0, &pos) != HASH_KEY_IS_LONG) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys must be CURLOPT constants or equivalent integer values"); + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arr), option, string_key, entry) { + if (string_key) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "Array keys must be CURLOPT constants or equivalent integer values"); RETURN_FALSE; } if (_php_curl_setopt(ch, (long) option, entry TSRMLS_CC) == FAILURE) { RETURN_FALSE; } - zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos); - } + } ZEND_HASH_FOREACH_END(); + RETURN_TRUE; } /* }}} */ @@ -2916,9 +2813,7 @@ PHP_FUNCTION(curl_setopt_array) Cleanup an execution phase */ void _php_curl_cleanup_handle(php_curl *ch) { - if (ch->handlers->write->buf.len > 0) { - smart_str_free(&ch->handlers->write->buf); - } + smart_str_free(&ch->handlers->write->buf); if (ch->header.str_len) { efree(ch->header.str); ch->header.str_len = 0; @@ -2941,7 +2836,7 @@ PHP_FUNCTION(curl_exec) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); _php_curl_verify_handlers(ch, 1 TSRMLS_CC); @@ -2951,23 +2846,21 @@ PHP_FUNCTION(curl_exec) SAVE_CURL_ERROR(ch, error); /* CURLE_PARTIAL_FILE is returned by HEAD requests */ if (error != CURLE_OK && error != CURLE_PARTIAL_FILE) { - if (ch->handlers->write->buf.len > 0) { - smart_str_free(&ch->handlers->write->buf); - } + smart_str_free(&ch->handlers->write->buf); RETURN_FALSE; } - if (ch->handlers->std_err) { + if (!Z_ISUNDEF(ch->handlers->std_err)) { php_stream *stream; - stream = (php_stream*)zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); + stream = zend_fetch_resource(&ch->handlers->std_err TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream()); if (stream) { php_stream_flush(stream); } } - if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) { + if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.s) { smart_str_0(&ch->handlers->write->buf); - RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1); + RETURN_STR(STR_COPY(ch->handlers->write->buf.s)); } /* flush the file handle, so any remaining data is synched to disk */ @@ -2998,15 +2891,15 @@ PHP_FUNCTION(curl_getinfo) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); if (ZEND_NUM_ARGS() < 2) { - char *s_code; - long l_code; - double d_code; + char *s_code; + long l_code; + double d_code; #if LIBCURL_VERSION_NUM > 0x071301 struct curl_certinfo *ci = NULL; - zval *listcode; + zval listcode; #endif array_init(return_value); @@ -3018,10 +2911,9 @@ PHP_FUNCTION(curl_getinfo) if (s_code != NULL) { CAAS("content_type", s_code); } else { - zval *retnull; - MAKE_STD_ZVAL(retnull); - ZVAL_NULL(retnull); - CAAZ("content_type", retnull); + zval retnull; + ZVAL_NULL(&retnull); + CAAZ("content_type", &retnull); } } if (curl_easy_getinfo(ch->cp, CURLINFO_HTTP_CODE, &l_code) == CURLE_OK) { @@ -3090,10 +2982,9 @@ PHP_FUNCTION(curl_getinfo) #endif #if LIBCURL_VERSION_NUM >= 0x071301 /* Available since 7.19.1 */ if (curl_easy_getinfo(ch->cp, CURLINFO_CERTINFO, &ci) == CURLE_OK) { - MAKE_STD_ZVAL(listcode); - array_init(listcode); - create_certinfo(ci, listcode TSRMLS_CC); - CAAZ("certinfo", listcode); + array_init(&listcode); + create_certinfo(ci, &listcode TSRMLS_CC); + CAAZ("certinfo", &listcode); } #endif #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */ @@ -3114,7 +3005,7 @@ PHP_FUNCTION(curl_getinfo) switch (option) { case CURLINFO_HEADER_OUT: if (ch->header.str_len > 0) { - RETURN_STRINGL(ch->header.str, ch->header.str_len, 1); + RETURN_STRINGL(ch->header.str, ch->header.str_len); } else { RETURN_FALSE; } @@ -3140,7 +3031,7 @@ PHP_FUNCTION(curl_getinfo) char *s_code = NULL; if (curl_easy_getinfo(ch->cp, option, &s_code) == CURLE_OK && s_code) { - RETURN_STRING(s_code, 1); + RETURN_STRING(s_code); } else { RETURN_FALSE; } @@ -3205,10 +3096,10 @@ PHP_FUNCTION(curl_error) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); ch->err.str[CURL_ERROR_SIZE] = 0; - RETURN_STRING(ch->err.str, 1); + RETURN_STRING(ch->err.str); } /* }}} */ @@ -3223,7 +3114,7 @@ PHP_FUNCTION(curl_errno) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); RETURN_LONG(ch->err.no); } @@ -3240,14 +3131,14 @@ PHP_FUNCTION(curl_close) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); if (ch->in_callback) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to close cURL handle from a callback"); return; } - zend_list_delete(Z_LVAL_P(zid)); + zend_list_close(Z_RES_P(zid)); } /* }}} */ @@ -3278,67 +3169,44 @@ static void _php_curl_close_ex(php_curl *ch TSRMLS_DC) curl_easy_cleanup(ch->cp); /* cURL destructors should be invoked only by last curl handle */ - if (Z_REFCOUNT_P(ch->clone) <= 1) { + if (Z_REFCOUNTED(ch->clone) && Z_REFCOUNT(ch->clone) <= 1) { zend_llist_clean(&ch->to_free->str); zend_llist_clean(&ch->to_free->post); zend_hash_destroy(ch->to_free->slist); efree(ch->to_free->slist); efree(ch->to_free); - FREE_ZVAL(ch->clone); } else { - Z_DELREF_P(ch->clone); + //??? Z_DELREF(ch->clone); } - if (ch->handlers->write->buf.len > 0) { - smart_str_free(&ch->handlers->write->buf); - } - if (ch->handlers->write->func_name) { - zval_ptr_dtor(&ch->handlers->write->func_name); - } - if (ch->handlers->read->func_name) { - zval_ptr_dtor(&ch->handlers->read->func_name); - } - if (ch->handlers->write_header->func_name) { - zval_ptr_dtor(&ch->handlers->write_header->func_name); - } + smart_str_free(&ch->handlers->write->buf); + zval_ptr_dtor(&ch->handlers->write->func_name); + zval_ptr_dtor(&ch->handlers->read->func_name); + zval_ptr_dtor(&ch->handlers->write_header->func_name); #if CURLOPT_PASSWDFUNCTION != 0 - if (ch->handlers->passwd) { - zval_ptr_dtor(&ch->handlers->passwd); - } + zval_ptr_dtor(&ch->handlers->passwd); #endif - if (ch->handlers->std_err) { - zval_ptr_dtor(&ch->handlers->std_err); - } + zval_ptr_dtor(&ch->handlers->std_err); if (ch->header.str_len > 0) { efree(ch->header.str); } - if (ch->handlers->write_header->stream) { - zval_ptr_dtor(&ch->handlers->write_header->stream); - } - if (ch->handlers->write->stream) { - zval_ptr_dtor(&ch->handlers->write->stream); - } - if (ch->handlers->read->stream) { - zval_ptr_dtor(&ch->handlers->read->stream); - } + zval_ptr_dtor(&ch->handlers->write_header->stream); + zval_ptr_dtor(&ch->handlers->write->stream); + zval_ptr_dtor(&ch->handlers->read->stream); efree(ch->handlers->write); efree(ch->handlers->write_header); efree(ch->handlers->read); if (ch->handlers->progress) { - if (ch->handlers->progress->func_name) { - zval_ptr_dtor(&ch->handlers->progress->func_name); - } + zval_ptr_dtor(&ch->handlers->progress->func_name); efree(ch->handlers->progress); } #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */ if (ch->handlers->fnmatch) { - if (ch->handlers->fnmatch->func_name) { - zval_ptr_dtor(&ch->handlers->fnmatch->func_name); - } + zval_ptr_dtor(&ch->handlers->fnmatch->func_name); efree(ch->handlers->fnmatch); } #endif @@ -3350,7 +3218,7 @@ static void _php_curl_close_ex(php_curl *ch TSRMLS_DC) /* {{{ _php_curl_close() List destructor for curl handles */ -static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC) +static void _php_curl_close(zend_resource *rsrc TSRMLS_DC) { php_curl *ch = (php_curl *) rsrc->ptr; _php_curl_close_ex(ch TSRMLS_CC); @@ -3371,7 +3239,7 @@ PHP_FUNCTION(curl_strerror) str = curl_easy_strerror(code); if (str) { - RETURN_STRING(str, 1); + RETURN_STRING(str); } else { RETURN_NULL(); } @@ -3384,46 +3252,42 @@ PHP_FUNCTION(curl_strerror) Reset all handlers of a given php_curl */ static void _php_curl_reset_handlers(php_curl *ch) { - if (ch->handlers->write->stream) { - Z_DELREF_P(ch->handlers->write->stream); - ch->handlers->write->stream = NULL; + if (!Z_ISUNDEF(ch->handlers->write->stream)) { + zval_ptr_dtor(&ch->handlers->write->stream); + ZVAL_UNDEF(&ch->handlers->write->stream); } ch->handlers->write->fp = NULL; ch->handlers->write->method = PHP_CURL_STDOUT; - if (ch->handlers->write_header->stream) { - Z_DELREF_P(ch->handlers->write_header->stream); - ch->handlers->write_header->stream = NULL; + if (!Z_ISUNDEF(ch->handlers->write_header->stream)) { + zval_ptr_dtor(&ch->handlers->write_header->stream); + ZVAL_UNDEF(&ch->handlers->write_header->stream); } ch->handlers->write_header->fp = NULL; ch->handlers->write_header->method = PHP_CURL_IGNORE; - if (ch->handlers->read->stream) { - Z_DELREF_P(ch->handlers->read->stream); - ch->handlers->read->stream = NULL; + if (!Z_ISUNDEF(ch->handlers->read->stream)) { + zval_ptr_dtor(&ch->handlers->read->stream); + ZVAL_UNDEF(&ch->handlers->read->stream); } ch->handlers->read->fp = NULL; - ch->handlers->read->fd = 0; + ch->handlers->read->res = NULL; ch->handlers->read->method = PHP_CURL_DIRECT; - if (ch->handlers->std_err) { + if (!Z_ISUNDEF(ch->handlers->std_err)) { zval_ptr_dtor(&ch->handlers->std_err); - ch->handlers->std_err = NULL; + ZVAL_UNDEF(&ch->handlers->std_err); } if (ch->handlers->progress) { - if (ch->handlers->progress->func_name) { - zval_ptr_dtor(&ch->handlers->progress->func_name); - } + zval_ptr_dtor(&ch->handlers->progress->func_name); efree(ch->handlers->progress); ch->handlers->progress = NULL; } #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */ if (ch->handlers->fnmatch) { - if (ch->handlers->fnmatch->func_name) { - zval_ptr_dtor(&ch->handlers->fnmatch->func_name); - } + zval_ptr_dtor(&ch->handlers->fnmatch->func_name); efree(ch->handlers->fnmatch); ch->handlers->fnmatch = NULL; } @@ -3443,7 +3307,7 @@ PHP_FUNCTION(curl_reset) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); if (ch->in_callback) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to reset cURL handle from a callback"); @@ -3471,10 +3335,10 @@ PHP_FUNCTION(curl_escape) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); if ((res = curl_easy_escape(ch->cp, str, str_len))) { - RETVAL_STRING(res, 1); + RETVAL_STRING(res); curl_free(res); } else { RETURN_FALSE; @@ -3495,10 +3359,10 @@ PHP_FUNCTION(curl_unescape) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); if ((out = curl_easy_unescape(ch->cp, str, str_len, &out_len))) { - RETVAL_STRINGL(out, out_len, 1); + RETVAL_STRINGL(out, out_len); curl_free(out); } else { RETURN_FALSE; @@ -3520,7 +3384,7 @@ PHP_FUNCTION(curl_pause) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl); RETURN_LONG(curl_easy_pause(ch->cp, bitmask)); } diff --git a/ext/curl/multi.c b/ext/curl/multi.c index 9fbea5518f..74d4b3fc4c 100644 --- a/ext/curl/multi.c +++ b/ext/curl/multi.c @@ -82,18 +82,17 @@ PHP_FUNCTION(curl_multi_add_handle) return; } - ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); - ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); + ZEND_FETCH_RESOURCE(ch, php_curl *, z_ch, -1, le_curl_name, le_curl); _php_curl_cleanup_handle(ch); /* we want to create a copy of this zval that we store in the multihandle structure element "easyh" */ - tmp_val = *z_ch; - zval_copy_ctor(&tmp_val); + ZVAL_DUP(&tmp_val, z_ch); zend_llist_add_element(&mh->easyh, &tmp_val); - RETURN_LONG((long) curl_multi_add_handle(mh->multi, ch->cp)); + RETURN_LONG((long)curl_multi_add_handle(mh->multi, ch->cp)); } /* }}} */ @@ -107,21 +106,21 @@ void _php_curl_multi_cleanup_list(void *data) /* {{{ */ return; } - ch = (php_curl *) zend_fetch_resource(&z_ch TSRMLS_CC, -1, le_curl_name, NULL, 1, le_curl); + ch = zend_fetch_resource(z_ch TSRMLS_CC, -1, le_curl_name, NULL, 1, le_curl); if (!ch) { return; } - zend_list_delete(Z_LVAL_P(z_ch)); + zend_list_close(Z_RES_P(z_ch)); } /* }}} */ /* Used internally as comparison routine passed to zend_list_del_element */ -static int curl_compare_resources( zval *z1, zval **z2 ) /* {{{ */ +static int curl_compare_resources( zval *z1, zval *z2 ) /* {{{ */ { - return (Z_TYPE_P( z1 ) == Z_TYPE_PP( z2 ) && - Z_TYPE_P( z1 ) == IS_RESOURCE && - Z_LVAL_P( z1 ) == Z_LVAL_PP( z2 ) ); + return (Z_TYPE_P(z1) == Z_TYPE_P(z2) && + Z_TYPE_P(z1) == IS_RESOURCE && + Z_RES_P(z1) == Z_RES_P(z2)); } /* }}} */ @@ -138,14 +137,11 @@ PHP_FUNCTION(curl_multi_remove_handle) return; } - ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); - ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); + ZEND_FETCH_RESOURCE(ch, php_curl *, z_ch, -1, le_curl_name, le_curl); - - - RETVAL_LONG((long) curl_multi_remove_handle(mh->multi, ch->cp)); - zend_llist_del_element( &mh->easyh, &z_ch, - (int (*)(void *, void *)) curl_compare_resources ); + RETVAL_LONG((long)curl_multi_remove_handle(mh->multi, ch->cp)); + zend_llist_del_element(&mh->easyh, &z_ch, (int (*)(void *, void *))curl_compare_resources); } /* }}} */ @@ -177,7 +173,7 @@ PHP_FUNCTION(curl_multi_select) return; } - ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); + ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); _make_timeval_struct(&to, timeout); @@ -207,7 +203,7 @@ PHP_FUNCTION(curl_multi_exec) return; } - ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); + ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); { zend_llist_position pos; @@ -217,12 +213,12 @@ PHP_FUNCTION(curl_multi_exec) for(pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch; pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) { - ZEND_FETCH_RESOURCE(ch, php_curl *, &pz_ch, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, pz_ch, -1, le_curl_name, le_curl); _php_curl_verify_handlers(ch, 1 TSRMLS_CC); } } - convert_to_long_ex(&z_still_running); + convert_to_long_ex(z_still_running); still_running = Z_LVAL_P(z_still_running); result = curl_multi_perform(mh->multi, &still_running); ZVAL_LONG(z_still_running, still_running); @@ -242,14 +238,14 @@ PHP_FUNCTION(curl_multi_getcontent) return; } - ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl); + ZEND_FETCH_RESOURCE(ch, php_curl *, z_ch, -1, le_curl_name, le_curl); - if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) { + if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.s) { smart_str_0(&ch->handlers->write->buf); - RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1); + RETURN_STR(STR_COPY(ch->handlers->write->buf.s)); } - RETURN_EMPTY_STRING(); + RETURN_EMPTY_STRING(); } /* }}} */ @@ -267,7 +263,7 @@ PHP_FUNCTION(curl_multi_info_read) return; } - ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); + ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); tmp_msg = curl_multi_info_read(mh->multi, &queued_msgs); if (tmp_msg == NULL) { @@ -291,7 +287,8 @@ PHP_FUNCTION(curl_multi_info_read) /* search the list of easy handles hanging off the multi-handle */ for(pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch; pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) { - ZEND_FETCH_RESOURCE(ch, php_curl *, &pz_ch, -1, le_curl_name, le_curl); + + ZEND_FETCH_RESOURCE(ch, php_curl *, pz_ch, -1, le_curl_name, le_curl); if (ch->cp == tmp_msg->easy_handle) { /* we are adding a reference to the underlying php_curl @@ -301,13 +298,12 @@ PHP_FUNCTION(curl_multi_info_read) Normally you would call zval_copy_ctor( pz_ch ), or SEPARATE_ZVAL, but those create new zvals, which is already being done in add_assoc_resource */ - - zend_list_addref( Z_RESVAL_P( pz_ch ) ); + Z_ADDREF_P(pz_ch); /* add_assoc_resource automatically creates a new zval to wrap the "resource" represented by the current pz_ch */ - add_assoc_resource(return_value, "handle", Z_RESVAL_P(pz_ch)); + add_assoc_zval(return_value, "handle", pz_ch); break; } @@ -327,24 +323,24 @@ PHP_FUNCTION(curl_multi_close) return; } - ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); + ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); - zend_list_delete(Z_LVAL_P(z_mh)); + zend_list_close(Z_RES_P(z_mh)); } /* }}} */ -void _php_curl_multi_close(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */ +void _php_curl_multi_close(zend_resource *rsrc TSRMLS_DC) /* {{{ */ { - php_curlm *mh = (php_curlm *) rsrc->ptr; + php_curlm *mh = (php_curlm *)rsrc->ptr; if (mh) { zend_llist_position pos; php_curl *ch; zval *pz_ch; - for(pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch; + for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch; pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) { - ch = (php_curl *) zend_fetch_resource(&pz_ch TSRMLS_CC, -1, le_curl_name, NULL, 1, le_curl); + ch = (php_curl *) zend_fetch_resource(pz_ch TSRMLS_CC, -1, le_curl_name, NULL, 1, le_curl); _php_curl_verify_handlers(ch, 0 TSRMLS_CC); } @@ -370,7 +366,7 @@ PHP_FUNCTION(curl_multi_strerror) str = curl_multi_strerror(code); if (str) { - RETURN_STRING(str, 1); + RETURN_STRING(str); } else { RETURN_NULL(); } @@ -379,7 +375,7 @@ PHP_FUNCTION(curl_multi_strerror) #endif #if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */ -static int _php_curl_multi_setopt(php_curlm *mh, long option, zval **zvalue, zval *return_value TSRMLS_DC) /* {{{ */ +static int _php_curl_multi_setopt(php_curlm *mh, long option, zval *zvalue, zval *return_value TSRMLS_DC) /* {{{ */ { CURLMcode error = CURLM_OK; @@ -391,7 +387,7 @@ static int _php_curl_multi_setopt(php_curlm *mh, long option, zval **zvalue, zva case CURLMOPT_MAXCONNECTS: #endif convert_to_long_ex(zvalue); - error = curl_multi_setopt(mh->multi, option, Z_LVAL_PP(zvalue)); + error = curl_multi_setopt(mh->multi, option, Z_LVAL_P(zvalue)); break; default: @@ -412,15 +408,15 @@ static int _php_curl_multi_setopt(php_curlm *mh, long option, zval **zvalue, zva Set an option for the curl multi handle */ PHP_FUNCTION(curl_multi_setopt) { - zval *z_mh, **zvalue; + zval *z_mh, *zvalue; long options; php_curlm *mh; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &z_mh, &options, &zvalue) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &z_mh, &options, &zvalue) == FAILURE) { return; } - ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); + ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle); if (!_php_curl_multi_setopt(mh, options, zvalue, return_value TSRMLS_CC)) { RETURN_TRUE; diff --git a/ext/curl/php_curl.h b/ext/curl/php_curl.h index 96edb9ae03..018add502b 100644 --- a/ext/curl/php_curl.h +++ b/ext/curl/php_curl.h @@ -114,44 +114,44 @@ PHP_FUNCTION(curl_pause); PHP_FUNCTION(curl_file_create); -void _php_curl_multi_close(zend_rsrc_list_entry * TSRMLS_DC); -void _php_curl_share_close(zend_rsrc_list_entry * TSRMLS_DC); +void _php_curl_multi_close(zend_resource * TSRMLS_DC); +void _php_curl_share_close(zend_resource * TSRMLS_DC); typedef struct { - zval *func_name; - zend_fcall_info_cache fci_cache; - FILE *fp; - smart_str buf; - int method; - zval *stream; + zval func_name; + zend_fcall_info_cache fci_cache; + FILE *fp; + smart_str buf; + int method; + zval stream; } php_curl_write; typedef struct { - zval *func_name; - zend_fcall_info_cache fci_cache; - FILE *fp; - long fd; - int method; - zval *stream; + zval func_name; + zend_fcall_info_cache fci_cache; + FILE *fp; + zend_resource *res; + int method; + zval stream; } php_curl_read; typedef struct { - zval *func_name; - zend_fcall_info_cache fci_cache; - int method; + zval func_name; + zend_fcall_info_cache fci_cache; + int method; } php_curl_progress, php_curl_fnmatch; typedef struct { - php_curl_write *write; - php_curl_write *write_header; - php_curl_read *read; + php_curl_write *write; + php_curl_write *write_header; + php_curl_read *read; #if CURLOPT_PASSWDFUNCTION != 0 - zval *passwd; + zval passwd; #endif - zval *std_err; - php_curl_progress *progress; + zval std_err; + php_curl_progress *progress; #if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */ - php_curl_fnmatch *fnmatch; + php_curl_fnmatch *fnmatch; #endif } php_curl_handlers; @@ -178,18 +178,18 @@ typedef struct { void ***thread_ctx; CURL *cp; php_curl_handlers *handlers; - long id; + zend_resource *res; zend_bool in_callback; - zval *clone; + zval clone; zend_bool safe_upload; } php_curl; #define CURLOPT_SAFE_UPLOAD -1 typedef struct { - int still_running; - CURLM *multi; - zend_llist easyh; + int still_running; + CURLM *multi; + zend_llist easyh; } php_curlm; typedef struct { diff --git a/ext/curl/share.c b/ext/curl/share.c index d46361e0ae..5983579f08 100644 --- a/ext/curl/share.c +++ b/ext/curl/share.c @@ -61,12 +61,12 @@ PHP_FUNCTION(curl_share_close) return; } - ZEND_FETCH_RESOURCE(sh, php_curlsh *, &z_sh, -1, le_curl_share_handle_name, le_curl_share_handle); - zend_list_delete(Z_LVAL_P(z_sh)); + ZEND_FETCH_RESOURCE(sh, php_curlsh *, z_sh, -1, le_curl_share_handle_name, le_curl_share_handle); + zend_list_close(Z_RES_P(z_sh)); } /* }}} */ -static int _php_curl_share_setopt(php_curlsh *sh, long option, zval **zvalue, zval *return_value TSRMLS_DC) /* {{{ */ +static int _php_curl_share_setopt(php_curlsh *sh, long option, zval *zvalue, zval *return_value TSRMLS_DC) /* {{{ */ { CURLSHcode error = CURLSHE_OK; @@ -74,7 +74,7 @@ static int _php_curl_share_setopt(php_curlsh *sh, long option, zval **zvalue, zv case CURLSHOPT_SHARE: case CURLSHOPT_UNSHARE: convert_to_long_ex(zvalue); - error = curl_share_setopt(sh->share, option, Z_LVAL_PP(zvalue)); + error = curl_share_setopt(sh->share, option, Z_LVAL_P(zvalue)); break; default: @@ -95,15 +95,15 @@ static int _php_curl_share_setopt(php_curlsh *sh, long option, zval **zvalue, zv Set an option for a cURL transfer */ PHP_FUNCTION(curl_share_setopt) { - zval *zid, **zvalue; + zval *zid, *zvalue; long options; php_curlsh *sh; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &zid, &options, &zvalue) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &zid, &options, &zvalue) == FAILURE) { return; } - ZEND_FETCH_RESOURCE(sh, php_curlsh *, &zid, -1, le_curl_share_handle_name, le_curl_share_handle); + ZEND_FETCH_RESOURCE(sh, php_curlsh *, zid, -1, le_curl_share_handle_name, le_curl_share_handle); if (!_php_curl_share_setopt(sh, options, zvalue, return_value TSRMLS_CC)) { RETURN_TRUE; @@ -113,9 +113,9 @@ PHP_FUNCTION(curl_share_setopt) } /* }}} */ -void _php_curl_share_close(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */ +void _php_curl_share_close(zend_resource *rsrc TSRMLS_DC) /* {{{ */ { - php_curlsh *sh = (php_curlsh *) rsrc->ptr; + php_curlsh *sh = (php_curlsh *)rsrc->ptr; if (sh) { curl_share_cleanup(sh->share); efree(sh); -- 2.40.0