Skip to content

Commit a83b68b

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.
1 parent 10aeed5 commit a83b68b

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
@@ -1795,7 +1795,11 @@ static void curl_free_string(void **string)
17951795
*/
17961796
static void curl_free_post(void **post)
17971797
{
1798+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
1799+
curl_mime_free((curl_mime *)*post);
1800+
#else
17981801
curl_formfree((struct HttpPost *)*post);
1802+
#endif
17991803
}
18001804
/* }}} */
18011805

@@ -2715,16 +2719,28 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
27152719
HashTable *postfields;
27162720
zend_string *string_key;
27172721
zend_ulong num_key;
2722+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2723+
curl_mime *mime;
2724+
curl_mimepart *part;
2725+
CURLcode form_error;
2726+
#else
27182727
struct HttpPost *first = NULL;
27192728
struct HttpPost *last = NULL;
27202729
CURLFORMcode form_error;
2721-
2730+
#endif
27222731
postfields = HASH_OF(zvalue);
27232732
if (!postfields) {
27242733
php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
27252734
return FAILURE;
27262735
}
27272736

2737+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2738+
mime = curl_mime_init(ch->cp);
2739+
if (mime == NULL) {
2740+
return FAILURE;
2741+
}
2742+
#endif
2743+
27282744
ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
27292745
zend_string *postval, *tmp_postval;
27302746
/* Pretend we have a string_key here */
@@ -2759,6 +2775,20 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
27592775
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
27602776
filename = Z_STRVAL_P(prop);
27612777
}
2778+
2779+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2780+
part = curl_mime_addpart(mime);
2781+
if (part == NULL) {
2782+
zend_string_release_ex(string_key, 0);
2783+
return FAILURE;
2784+
}
2785+
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2786+
|| (form_error = curl_mime_filedata(part, ZSTR_VAL(postval))) != CURLE_OK
2787+
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
2788+
|| (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
2789+
error = form_error;
2790+
}
2791+
#else
27622792
form_error = curl_formadd(&first, &last,
27632793
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
27642794
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
@@ -2770,6 +2800,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
27702800
/* Not nice to convert between enums but we only have place for one error type */
27712801
error = (CURLcode)form_error;
27722802
}
2803+
#endif
27732804
}
27742805

27752806
zend_string_release_ex(string_key, 0);
@@ -2778,6 +2809,18 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
27782809

27792810
postval = zval_get_tmp_string(current, &tmp_postval);
27802811

2812+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2813+
part = curl_mime_addpart(mime);
2814+
if (part == NULL) {
2815+
zend_tmp_string_release(tmp_postval);
2816+
zend_string_release_ex(string_key, 0);
2817+
return FAILURE;
2818+
}
2819+
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
2820+
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
2821+
error = form_error;
2822+
}
2823+
#else
27812824
/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
27822825
* must be explicitly cast to long in curl_formadd
27832826
* use since curl needs a long not an int. */
@@ -2792,6 +2835,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
27922835
/* Not nice to convert between enums but we only have place for one error type */
27932836
error = (CURLcode)form_error;
27942837
}
2838+
#endif
27952839
zend_tmp_string_release(tmp_postval);
27962840
zend_string_release_ex(string_key, 0);
27972841
} ZEND_HASH_FOREACH_END();
@@ -2804,8 +2848,13 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28042848
if ((*ch->clone) == 1) {
28052849
zend_llist_clean(&ch->to_free->post);
28062850
}
2851+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2852+
zend_llist_add_element(&ch->to_free->post, &mime);
2853+
error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
2854+
#else
28072855
zend_llist_add_element(&ch->to_free->post, &first);
28082856
error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2857+
#endif
28092858
} else {
28102859
#if LIBCURL_VERSION_NUM >= 0x071101
28112860
zend_string *tmp_str;

0 commit comments

Comments
 (0)