Skip to content

Commit 8f185d3

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Extract function
2 parents d5f26de + a6d385a commit 8f185d3

File tree

1 file changed

+162
-152
lines changed

1 file changed

+162
-152
lines changed

ext/curl/interface.c

Lines changed: 162 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,167 @@ static int seek_cb(void *arg, curl_off_t offset, int origin) /* {{{ */
20012001
/* }}} */
20022002
#endif
20032003

2004+
static inline int build_mime_structure_from_hash(php_curl *ch, zval *zpostfields) /* {{{ */
2005+
{
2006+
CURLcode error = CURLE_OK;
2007+
zval *current;
2008+
HashTable *postfields;
2009+
zend_string *string_key;
2010+
zend_ulong num_key;
2011+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2012+
curl_mime *mime = NULL;
2013+
curl_mimepart *part;
2014+
CURLcode form_error;
2015+
#else
2016+
struct HttpPost *first = NULL;
2017+
struct HttpPost *last = NULL;
2018+
CURLFORMcode form_error;
2019+
#endif
2020+
2021+
postfields = HASH_OF(zpostfields);
2022+
if (!postfields) {
2023+
php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2024+
return FAILURE;
2025+
}
2026+
2027+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2028+
if (zend_hash_num_elements(postfields) > 0) {
2029+
mime = curl_mime_init(ch->cp);
2030+
if (mime == NULL) {
2031+
return FAILURE;
2032+
}
2033+
}
2034+
#endif
2035+
2036+
ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
2037+
zend_string *postval, *tmp_postval;
2038+
/* Pretend we have a string_key here */
2039+
if (!string_key) {
2040+
string_key = zend_long_to_str(num_key);
2041+
} else {
2042+
zend_string_addref(string_key);
2043+
}
2044+
2045+
ZVAL_DEREF(current);
2046+
if (Z_TYPE_P(current) == IS_OBJECT &&
2047+
instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class)) {
2048+
/* new-style file upload */
2049+
zval *prop, rv;
2050+
char *type = NULL, *filename = NULL;
2051+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2052+
php_stream *stream;
2053+
#endif
2054+
2055+
prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
2056+
if (Z_TYPE_P(prop) != IS_STRING) {
2057+
php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key));
2058+
} else {
2059+
postval = Z_STR_P(prop);
2060+
2061+
if (php_check_open_basedir(ZSTR_VAL(postval))) {
2062+
return 1;
2063+
}
2064+
2065+
prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0, &rv);
2066+
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2067+
type = Z_STRVAL_P(prop);
2068+
}
2069+
prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0, &rv);
2070+
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2071+
filename = Z_STRVAL_P(prop);
2072+
}
2073+
2074+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2075+
if (!(stream = php_stream_open_wrapper(ZSTR_VAL(postval), "rb", IGNORE_PATH, NULL))) {
2076+
zend_string_release_ex(string_key, 0);
2077+
return FAILURE;
2078+
}
2079+
part = curl_mime_addpart(mime);
2080+
if (part == NULL) {
2081+
php_stream_close(stream);
2082+
zend_string_release_ex(string_key, 0);
2083+
return FAILURE;
2084+
}
2085+
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2086+
|| (form_error = curl_mime_data_cb(part, -1, read_cb, seek_cb, NULL, stream)) != CURLE_OK
2087+
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
2088+
|| (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
2089+
php_stream_close(stream);
2090+
error = form_error;
2091+
}
2092+
zend_llist_add_element(&ch->to_free->stream, &stream);
2093+
#else
2094+
form_error = curl_formadd(&first, &last,
2095+
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2096+
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2097+
CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval),
2098+
CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
2099+
CURLFORM_FILE, ZSTR_VAL(postval),
2100+
CURLFORM_END);
2101+
if (form_error != CURL_FORMADD_OK) {
2102+
/* Not nice to convert between enums but we only have place for one error type */
2103+
error = (CURLcode)form_error;
2104+
}
2105+
#endif
2106+
}
2107+
2108+
zend_string_release_ex(string_key, 0);
2109+
continue;
2110+
}
2111+
2112+
postval = zval_get_tmp_string(current, &tmp_postval);
2113+
2114+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2115+
part = curl_mime_addpart(mime);
2116+
if (part == NULL) {
2117+
zend_tmp_string_release(tmp_postval);
2118+
zend_string_release_ex(string_key, 0);
2119+
return FAILURE;
2120+
}
2121+
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2122+
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
2123+
error = form_error;
2124+
}
2125+
#else
2126+
/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2127+
* must be explicitly cast to long in curl_formadd
2128+
* use since curl needs a long not an int. */
2129+
form_error = curl_formadd(&first, &last,
2130+
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2131+
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2132+
CURLFORM_COPYCONTENTS, ZSTR_VAL(postval),
2133+
CURLFORM_CONTENTSLENGTH, ZSTR_LEN(postval),
2134+
CURLFORM_END);
2135+
2136+
if (form_error != CURL_FORMADD_OK) {
2137+
/* Not nice to convert between enums but we only have place for one error type */
2138+
error = (CURLcode)form_error;
2139+
}
2140+
#endif
2141+
zend_tmp_string_release(tmp_postval);
2142+
zend_string_release_ex(string_key, 0);
2143+
} ZEND_HASH_FOREACH_END();
2144+
2145+
SAVE_CURL_ERROR(ch, error);
2146+
if (error != CURLE_OK) {
2147+
return FAILURE;
2148+
}
2149+
2150+
if ((*ch->clone) == 1) {
2151+
zend_llist_clean(&ch->to_free->post);
2152+
}
2153+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2154+
zend_llist_add_element(&ch->to_free->post, &mime);
2155+
error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
2156+
#else
2157+
zend_llist_add_element(&ch->to_free->post, &first);
2158+
error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2159+
#endif
2160+
SAVE_CURL_ERROR(ch, error);
2161+
return error == CURLE_OK ? SUCCESS : FAILURE;
2162+
}
2163+
/* }}} */
2164+
20042165
static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{ */
20052166
{
20062167
CURLcode error = CURLE_OK;
@@ -2595,158 +2756,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
25952756

25962757
case CURLOPT_POSTFIELDS:
25972758
if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
2598-
zval *current;
2599-
HashTable *postfields;
2600-
zend_string *string_key;
2601-
zend_ulong num_key;
2602-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2603-
curl_mime *mime = NULL;
2604-
curl_mimepart *part;
2605-
CURLcode form_error;
2606-
#else
2607-
struct HttpPost *first = NULL;
2608-
struct HttpPost *last = NULL;
2609-
CURLFORMcode form_error;
2610-
#endif
2611-
postfields = HASH_OF(zvalue);
2612-
if (!postfields) {
2613-
php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2614-
return FAILURE;
2615-
}
2616-
2617-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2618-
if (zend_hash_num_elements(postfields) > 0) {
2619-
mime = curl_mime_init(ch->cp);
2620-
if (mime == NULL) {
2621-
return FAILURE;
2622-
}
2623-
}
2624-
#endif
2625-
2626-
ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
2627-
zend_string *postval, *tmp_postval;
2628-
/* Pretend we have a string_key here */
2629-
if (!string_key) {
2630-
string_key = zend_long_to_str(num_key);
2631-
} else {
2632-
zend_string_addref(string_key);
2633-
}
2634-
2635-
ZVAL_DEREF(current);
2636-
if (Z_TYPE_P(current) == IS_OBJECT &&
2637-
instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class)) {
2638-
/* new-style file upload */
2639-
zval *prop, rv;
2640-
char *type = NULL, *filename = NULL;
2641-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2642-
php_stream *stream;
2643-
#endif
2644-
2645-
prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
2646-
if (Z_TYPE_P(prop) != IS_STRING) {
2647-
php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key));
2648-
} else {
2649-
postval = Z_STR_P(prop);
2650-
2651-
if (php_check_open_basedir(ZSTR_VAL(postval))) {
2652-
return 1;
2653-
}
2654-
2655-
prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0, &rv);
2656-
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2657-
type = Z_STRVAL_P(prop);
2658-
}
2659-
prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0, &rv);
2660-
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
2661-
filename = Z_STRVAL_P(prop);
2662-
}
2663-
2664-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2665-
if (!(stream = php_stream_open_wrapper(ZSTR_VAL(postval), "rb", IGNORE_PATH, NULL))) {
2666-
zend_string_release_ex(string_key, 0);
2667-
return FAILURE;
2668-
}
2669-
part = curl_mime_addpart(mime);
2670-
if (part == NULL) {
2671-
php_stream_close(stream);
2672-
zend_string_release_ex(string_key, 0);
2673-
return FAILURE;
2674-
}
2675-
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2676-
|| (form_error = curl_mime_data_cb(part, -1, read_cb, seek_cb, NULL, stream)) != CURLE_OK
2677-
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
2678-
|| (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
2679-
php_stream_close(stream);
2680-
error = form_error;
2681-
}
2682-
zend_llist_add_element(&ch->to_free->stream, &stream);
2683-
#else
2684-
form_error = curl_formadd(&first, &last,
2685-
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2686-
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2687-
CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval),
2688-
CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
2689-
CURLFORM_FILE, ZSTR_VAL(postval),
2690-
CURLFORM_END);
2691-
if (form_error != CURL_FORMADD_OK) {
2692-
/* Not nice to convert between enums but we only have place for one error type */
2693-
error = (CURLcode)form_error;
2694-
}
2695-
#endif
2696-
}
2697-
2698-
zend_string_release_ex(string_key, 0);
2699-
continue;
2700-
}
2701-
2702-
postval = zval_get_tmp_string(current, &tmp_postval);
2703-
2704-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2705-
part = curl_mime_addpart(mime);
2706-
if (part == NULL) {
2707-
zend_tmp_string_release(tmp_postval);
2708-
zend_string_release_ex(string_key, 0);
2709-
return FAILURE;
2710-
}
2711-
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2712-
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
2713-
error = form_error;
2714-
}
2715-
#else
2716-
/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2717-
* must be explicitly cast to long in curl_formadd
2718-
* use since curl needs a long not an int. */
2719-
form_error = curl_formadd(&first, &last,
2720-
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2721-
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2722-
CURLFORM_COPYCONTENTS, ZSTR_VAL(postval),
2723-
CURLFORM_CONTENTSLENGTH, ZSTR_LEN(postval),
2724-
CURLFORM_END);
2725-
2726-
if (form_error != CURL_FORMADD_OK) {
2727-
/* Not nice to convert between enums but we only have place for one error type */
2728-
error = (CURLcode)form_error;
2729-
}
2730-
#endif
2731-
zend_tmp_string_release(tmp_postval);
2732-
zend_string_release_ex(string_key, 0);
2733-
} ZEND_HASH_FOREACH_END();
2734-
2735-
SAVE_CURL_ERROR(ch, error);
2736-
if (error != CURLE_OK) {
2737-
return FAILURE;
2738-
}
2739-
2740-
if ((*ch->clone) == 1) {
2741-
zend_llist_clean(&ch->to_free->post);
2742-
}
2743-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2744-
zend_llist_add_element(&ch->to_free->post, &mime);
2745-
error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
2746-
#else
2747-
zend_llist_add_element(&ch->to_free->post, &first);
2748-
error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2749-
#endif
2759+
return build_mime_structure_from_hash(ch, zvalue);
27502760
} else {
27512761
#if LIBCURL_VERSION_NUM >= 0x071101
27522762
zend_string *tmp_str;

0 commit comments

Comments
 (0)