Skip to content

Commit e120273

Browse files
committed
Use curl_mime_*() functions if available
As of curl 7.56.0, `curl_formadd()` is deprecated in favor of `curl_mime_*()`, so we use the latter if available. (cherry picked from commit a83b68b)
1 parent b5d2cbe commit e120273

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

ext/curl/interface.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,11 @@ static void curl_free_string(void **string)
18441844
*/
18451845
static void curl_free_post(void **post)
18461846
{
1847+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
1848+
curl_mime_free((curl_mime *)*post);
1849+
#else
18471850
curl_formfree((struct HttpPost *)*post);
1851+
#endif
18481852
}
18491853
/* }}} */
18501854

@@ -2764,16 +2768,28 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
27642768
HashTable *postfields;
27652769
zend_string *string_key;
27662770
zend_ulong num_key;
2771+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2772+
curl_mime *mime;
2773+
curl_mimepart *part;
2774+
CURLcode form_error;
2775+
#else
27672776
struct HttpPost *first = NULL;
27682777
struct HttpPost *last = NULL;
27692778
CURLFORMcode form_error;
2770-
2779+
#endif
27712780
postfields = HASH_OF(zvalue);
27722781
if (!postfields) {
27732782
php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
27742783
return FAILURE;
27752784
}
27762785

2786+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2787+
mime = curl_mime_init(ch->cp);
2788+
if (mime == NULL) {
2789+
return FAILURE;
2790+
}
2791+
#endif
2792+
27772793
ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
27782794
zend_string *postval, *tmp_postval;
27792795
/* Pretend we have a string_key here */
@@ -2808,6 +2824,20 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28082824
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
28092825
filename = Z_STRVAL_P(prop);
28102826
}
2827+
2828+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2829+
part = curl_mime_addpart(mime);
2830+
if (part == NULL) {
2831+
zend_string_release_ex(string_key, 0);
2832+
return FAILURE;
2833+
}
2834+
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2835+
|| (form_error = curl_mime_filedata(part, ZSTR_VAL(postval))) != CURLE_OK
2836+
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
2837+
|| (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
2838+
error = form_error;
2839+
}
2840+
#else
28112841
form_error = curl_formadd(&first, &last,
28122842
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
28132843
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
@@ -2819,6 +2849,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28192849
/* Not nice to convert between enums but we only have place for one error type */
28202850
error = (CURLcode)form_error;
28212851
}
2852+
#endif
28222853
}
28232854

28242855
zend_string_release_ex(string_key, 0);
@@ -2827,6 +2858,18 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28272858

28282859
postval = zval_get_tmp_string(current, &tmp_postval);
28292860

2861+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2862+
part = curl_mime_addpart(mime);
2863+
if (part == NULL) {
2864+
zend_tmp_string_release(tmp_postval);
2865+
zend_string_release_ex(string_key, 0);
2866+
return FAILURE;
2867+
}
2868+
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2869+
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
2870+
error = form_error;
2871+
}
2872+
#else
28302873
/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
28312874
* must be explicitly cast to long in curl_formadd
28322875
* use since curl needs a long not an int. */
@@ -2841,6 +2884,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28412884
/* Not nice to convert between enums but we only have place for one error type */
28422885
error = (CURLcode)form_error;
28432886
}
2887+
#endif
28442888
zend_tmp_string_release(tmp_postval);
28452889
zend_string_release_ex(string_key, 0);
28462890
} ZEND_HASH_FOREACH_END();
@@ -2853,8 +2897,13 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28532897
if ((*ch->clone) == 1) {
28542898
zend_llist_clean(&ch->to_free->post);
28552899
}
2900+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2901+
zend_llist_add_element(&ch->to_free->post, &mime);
2902+
error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
2903+
#else
28562904
zend_llist_add_element(&ch->to_free->post, &first);
28572905
error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2906+
#endif
28582907
} else {
28592908
#if LIBCURL_VERSION_NUM >= 0x071101
28602909
zend_string *tmp_str;

0 commit comments

Comments
 (0)