Skip to content

Commit 3529867

Browse files
committed
Extract function
1 parent 28afd62 commit 3529867

File tree

1 file changed

+61
-162
lines changed

1 file changed

+61
-162
lines changed

ext/curl/interface.c

Lines changed: 61 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,24 +2118,39 @@ void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
21182118
static size_t read_cb(char *buffer, size_t size, size_t nitems, void *arg);
21192119
static int seek_cb(void *arg, curl_off_t offset, int origin);
21202120
static void free_cb(void *arg);
2121+
#endif
21212122

2122-
static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
2123+
static int build_mime_structure_from_hash(php_curl *ch, zval *zpostfields)
21232124
{
21242125
CURLcode error = CURLE_OK;
21252126
zval *current;
2127+
HashTable *postfields;
21262128
zend_string *string_key;
21272129
zend_ulong num_key;
2130+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
21282131
curl_mime *mime = NULL;
21292132
curl_mimepart *part;
21302133
CURLcode form_error;
2131-
HashTable *postfields = Z_ARRVAL_P(zpostfields);
2134+
#else
2135+
struct HttpPost *first = NULL;
2136+
struct HttpPost *last = NULL;
2137+
CURLFORMcode form_error;
2138+
#endif
21322139

2140+
postfields = HASH_OF(zpostfields);
2141+
if (!postfields) {
2142+
php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2143+
return FAILURE;
2144+
}
2145+
2146+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
21332147
if (zend_hash_num_elements(postfields) > 0) {
21342148
mime = curl_mime_init(ch->cp);
21352149
if (mime == NULL) {
21362150
return FAILURE;
21372151
}
21382152
}
2153+
#endif
21392154

21402155
ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
21412156
zend_string *postval, *tmp_postval;
@@ -2152,9 +2167,9 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
21522167
/* new-style file upload */
21532168
zval *prop, rv;
21542169
char *type = NULL, *filename = NULL;
2155-
2170+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
21562171
struct mime_data_cb_arg *cb_arg;
2157-
2172+
#endif
21582173

21592174
prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
21602175
if (Z_TYPE_P(prop) != IS_STRING) {
@@ -2175,6 +2190,7 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
21752190
filename = Z_STRVAL_P(prop);
21762191
}
21772192

2193+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
21782194
cb_arg = emalloc(sizeof *cb_arg);
21792195
cb_arg->ch = ch;
21802196
ZVAL_COPY(&cb_arg->postfields, zpostfields);
@@ -2184,6 +2200,7 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
21842200
cb_arg->ch = NULL;
21852201
cb_arg->filename = zend_string_copy(postval);
21862202
cb_arg->stream = NULL;
2203+
21872204
part = curl_mime_addpart(mime);
21882205
if (part == NULL) {
21892206
zend_string_release_ex(string_key, 0);
@@ -2196,6 +2213,19 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
21962213
error = form_error;
21972214
}
21982215
zend_llist_add_element(&ch->to_free->stream, &cb_arg);
2216+
#else
2217+
form_error = curl_formadd(&first, &last,
2218+
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2219+
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2220+
CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval),
2221+
CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
2222+
CURLFORM_FILE, ZSTR_VAL(postval),
2223+
CURLFORM_END);
2224+
if (form_error != CURL_FORMADD_OK) {
2225+
/* Not nice to convert between enums but we only have place for one error type */
2226+
error = (CURLcode)form_error;
2227+
}
2228+
#endif
21992229
}
22002230

22012231
zend_string_release_ex(string_key, 0);
@@ -2204,6 +2234,7 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
22042234

22052235
postval = zval_get_tmp_string(current, &tmp_postval);
22062236

2237+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
22072238
part = curl_mime_addpart(mime);
22082239
if (part == NULL) {
22092240
zend_tmp_string_release(tmp_postval);
@@ -2214,6 +2245,22 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
22142245
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
22152246
error = form_error;
22162247
}
2248+
#else
2249+
/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
2250+
* must be explicitly cast to long in curl_formadd
2251+
* use since curl needs a long not an int. */
2252+
form_error = curl_formadd(&first, &last,
2253+
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
2254+
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
2255+
CURLFORM_COPYCONTENTS, ZSTR_VAL(postval),
2256+
CURLFORM_CONTENTSLENGTH, ZSTR_LEN(postval),
2257+
CURLFORM_END);
2258+
2259+
if (form_error != CURL_FORMADD_OK) {
2260+
/* Not nice to convert between enums but we only have place for one error type */
2261+
error = (CURLcode)form_error;
2262+
}
2263+
#endif
22172264
zend_tmp_string_release(tmp_postval);
22182265
zend_string_release_ex(string_key, 0);
22192266
} ZEND_HASH_FOREACH_END();
@@ -2226,12 +2273,19 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
22262273
if ((*ch->clone) == 1) {
22272274
zend_llist_clean(&ch->to_free->post);
22282275
}
2276+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
22292277
zend_llist_add_element(&ch->to_free->post, &mime);
22302278
error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
2279+
#else
2280+
zend_llist_add_element(&ch->to_free->post, &first);
2281+
error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
2282+
#endif
22312283

2232-
return SUCCESS;
2284+
SAVE_CURL_ERROR(ch, error);
2285+
return error == CURLE_OK ? SUCCESS : FAILURE;
22332286
}
22342287

2288+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
22352289
static HashTable *get_postfields_of_handle(php_curl *ch)
22362290
{
22372291
struct mime_data_cb_arg *cb_arg, **cb_arg_p;
@@ -2279,7 +2333,7 @@ PHP_FUNCTION(curl_copy_handle)
22792333
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
22802334
postfields = get_postfields_of_handle(ch);
22812335
if (postfields) {
2282-
if (rebuild_mime_structure(dupch, postfields) != SUCCESS) {
2336+
if (build_mime_structure_from_hash(dupch, postfields) != SUCCESS) {
22832337
_php_curl_close_ex(dupch);
22842338
php_error_docref(NULL, E_WARNING, "Cannot rebuild mime structure");
22852339
RETURN_FALSE;
@@ -2941,162 +2995,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
29412995

29422996
case CURLOPT_POSTFIELDS:
29432997
if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
2944-
zval *current;
2945-
HashTable *postfields;
2946-
zend_string *string_key;
2947-
zend_ulong num_key;
2948-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2949-
curl_mime *mime = NULL;
2950-
curl_mimepart *part;
2951-
CURLcode form_error;
2952-
#else
2953-
struct HttpPost *first = NULL;
2954-
struct HttpPost *last = NULL;
2955-
CURLFORMcode form_error;
2956-
#endif
2957-
postfields = HASH_OF(zvalue);
2958-
if (!postfields) {
2959-
php_error_docref(NULL, E_WARNING, "Couldn't get HashTable in CURLOPT_POSTFIELDS");
2960-
return FAILURE;
2961-
}
2962-
2963-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2964-
if (zend_hash_num_elements(postfields) > 0) {
2965-
mime = curl_mime_init(ch->cp);
2966-
if (mime == NULL) {
2967-
return FAILURE;
2968-
}
2969-
}
2970-
#endif
2971-
2972-
ZEND_HASH_FOREACH_KEY_VAL(postfields, num_key, string_key, current) {
2973-
zend_string *postval, *tmp_postval;
2974-
/* Pretend we have a string_key here */
2975-
if (!string_key) {
2976-
string_key = zend_long_to_str(num_key);
2977-
} else {
2978-
zend_string_addref(string_key);
2979-
}
2980-
2981-
ZVAL_DEREF(current);
2982-
if (Z_TYPE_P(current) == IS_OBJECT &&
2983-
instanceof_function(Z_OBJCE_P(current), curl_CURLFile_class)) {
2984-
/* new-style file upload */
2985-
zval *prop, rv;
2986-
char *type = NULL, *filename = NULL;
2987-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2988-
struct mime_data_cb_arg *cb_arg;
2989-
#endif
2990-
2991-
prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
2992-
if (Z_TYPE_P(prop) != IS_STRING) {
2993-
php_error_docref(NULL, E_WARNING, "Invalid filename for key %s", ZSTR_VAL(string_key));
2994-
} else {
2995-
postval = Z_STR_P(prop);
2996-
2997-
if (php_check_open_basedir(ZSTR_VAL(postval))) {
2998-
return 1;
2999-
}
3000-
3001-
prop = zend_read_property(curl_CURLFile_class, current, "mime", sizeof("mime")-1, 0, &rv);
3002-
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
3003-
type = Z_STRVAL_P(prop);
3004-
}
3005-
prop = zend_read_property(curl_CURLFile_class, current, "postname", sizeof("postname")-1, 0, &rv);
3006-
if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
3007-
filename = Z_STRVAL_P(prop);
3008-
}
3009-
3010-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
3011-
cb_arg = emalloc(sizeof *cb_arg);
3012-
cb_arg->ch = ch;
3013-
ZVAL_COPY(&cb_arg->postfields, zvalue);
3014-
zend_llist_add_element(&ch->to_free->stream, &cb_arg);
3015-
3016-
cb_arg = emalloc(sizeof *cb_arg);
3017-
cb_arg->ch = NULL;
3018-
cb_arg->filename = zend_string_copy(postval);
3019-
cb_arg->stream = NULL;
3020-
3021-
part = curl_mime_addpart(mime);
3022-
if (part == NULL) {
3023-
zend_string_release_ex(string_key, 0);
3024-
return FAILURE;
3025-
}
3026-
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
3027-
|| (form_error = curl_mime_data_cb(part, -1, read_cb, seek_cb, free_cb, cb_arg)) != CURLE_OK
3028-
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
3029-
|| (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
3030-
error = form_error;
3031-
}
3032-
zend_llist_add_element(&ch->to_free->stream, &cb_arg);
3033-
#else
3034-
form_error = curl_formadd(&first, &last,
3035-
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
3036-
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
3037-
CURLFORM_FILENAME, filename ? filename : ZSTR_VAL(postval),
3038-
CURLFORM_CONTENTTYPE, type ? type : "application/octet-stream",
3039-
CURLFORM_FILE, ZSTR_VAL(postval),
3040-
CURLFORM_END);
3041-
if (form_error != CURL_FORMADD_OK) {
3042-
/* Not nice to convert between enums but we only have place for one error type */
3043-
error = (CURLcode)form_error;
3044-
}
3045-
#endif
3046-
}
3047-
3048-
zend_string_release_ex(string_key, 0);
3049-
continue;
3050-
}
3051-
3052-
postval = zval_get_tmp_string(current, &tmp_postval);
3053-
3054-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
3055-
part = curl_mime_addpart(mime);
3056-
if (part == NULL) {
3057-
zend_tmp_string_release(tmp_postval);
3058-
zend_string_release_ex(string_key, 0);
3059-
return FAILURE;
3060-
}
3061-
if ((form_error = curl_mime_name(part, ZSTR_VAL(string_key))) != CURLE_OK
3062-
|| (form_error = curl_mime_data(part, ZSTR_VAL(postval), ZSTR_LEN(postval))) != CURLE_OK) {
3063-
error = form_error;
3064-
}
3065-
#else
3066-
/* The arguments after _NAMELENGTH and _CONTENTSLENGTH
3067-
* must be explicitly cast to long in curl_formadd
3068-
* use since curl needs a long not an int. */
3069-
form_error = curl_formadd(&first, &last,
3070-
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
3071-
CURLFORM_NAMELENGTH, ZSTR_LEN(string_key),
3072-
CURLFORM_COPYCONTENTS, ZSTR_VAL(postval),
3073-
CURLFORM_CONTENTSLENGTH, ZSTR_LEN(postval),
3074-
CURLFORM_END);
3075-
3076-
if (form_error != CURL_FORMADD_OK) {
3077-
/* Not nice to convert between enums but we only have place for one error type */
3078-
error = (CURLcode)form_error;
3079-
}
3080-
#endif
3081-
zend_tmp_string_release(tmp_postval);
3082-
zend_string_release_ex(string_key, 0);
3083-
} ZEND_HASH_FOREACH_END();
3084-
3085-
SAVE_CURL_ERROR(ch, error);
3086-
if (error != CURLE_OK) {
3087-
return FAILURE;
3088-
}
3089-
3090-
if ((*ch->clone) == 1) {
3091-
zend_llist_clean(&ch->to_free->post);
3092-
}
3093-
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
3094-
zend_llist_add_element(&ch->to_free->post, &mime);
3095-
error = curl_easy_setopt(ch->cp, CURLOPT_MIMEPOST, mime);
3096-
#else
3097-
zend_llist_add_element(&ch->to_free->post, &first);
3098-
error = curl_easy_setopt(ch->cp, CURLOPT_HTTPPOST, first);
3099-
#endif
2998+
return build_mime_structure_from_hash(ch, zvalue);
31002999
} else {
31013000
#if LIBCURL_VERSION_NUM >= 0x071101
31023001
zend_string *tmp_str;

0 commit comments

Comments
 (0)