@@ -2118,24 +2118,39 @@ void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
2118
2118
static size_t read_cb (char * buffer , size_t size , size_t nitems , void * arg );
2119
2119
static int seek_cb (void * arg , curl_off_t offset , int origin );
2120
2120
static void free_cb (void * arg );
2121
+ #endif
2121
2122
2122
- static int rebuild_mime_structure (php_curl * ch , zval * zpostfields )
2123
+ static int build_mime_structure_from_hash (php_curl * ch , zval * zpostfields )
2123
2124
{
2124
2125
CURLcode error = CURLE_OK ;
2125
2126
zval * current ;
2127
+ HashTable * postfields ;
2126
2128
zend_string * string_key ;
2127
2129
zend_ulong num_key ;
2130
+ #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2128
2131
curl_mime * mime = NULL ;
2129
2132
curl_mimepart * part ;
2130
2133
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
2132
2139
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 */
2133
2147
if (zend_hash_num_elements (postfields ) > 0 ) {
2134
2148
mime = curl_mime_init (ch -> cp );
2135
2149
if (mime == NULL ) {
2136
2150
return FAILURE ;
2137
2151
}
2138
2152
}
2153
+ #endif
2139
2154
2140
2155
ZEND_HASH_FOREACH_KEY_VAL (postfields , num_key , string_key , current ) {
2141
2156
zend_string * postval , * tmp_postval ;
@@ -2152,9 +2167,9 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
2152
2167
/* new-style file upload */
2153
2168
zval * prop , rv ;
2154
2169
char * type = NULL , * filename = NULL ;
2155
-
2170
+ #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2156
2171
struct mime_data_cb_arg * cb_arg ;
2157
-
2172
+ #endif
2158
2173
2159
2174
prop = zend_read_property (curl_CURLFile_class , current , "name" , sizeof ("name" )- 1 , 0 , & rv );
2160
2175
if (Z_TYPE_P (prop ) != IS_STRING ) {
@@ -2175,6 +2190,7 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
2175
2190
filename = Z_STRVAL_P (prop );
2176
2191
}
2177
2192
2193
+ #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2178
2194
cb_arg = emalloc (sizeof * cb_arg );
2179
2195
cb_arg -> ch = ch ;
2180
2196
ZVAL_COPY (& cb_arg -> postfields , zpostfields );
@@ -2184,6 +2200,7 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
2184
2200
cb_arg -> ch = NULL ;
2185
2201
cb_arg -> filename = zend_string_copy (postval );
2186
2202
cb_arg -> stream = NULL ;
2203
+
2187
2204
part = curl_mime_addpart (mime );
2188
2205
if (part == NULL ) {
2189
2206
zend_string_release_ex (string_key , 0 );
@@ -2196,6 +2213,19 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
2196
2213
error = form_error ;
2197
2214
}
2198
2215
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
2199
2229
}
2200
2230
2201
2231
zend_string_release_ex (string_key , 0 );
@@ -2204,6 +2234,7 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
2204
2234
2205
2235
postval = zval_get_tmp_string (current , & tmp_postval );
2206
2236
2237
+ #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2207
2238
part = curl_mime_addpart (mime );
2208
2239
if (part == NULL ) {
2209
2240
zend_tmp_string_release (tmp_postval );
@@ -2214,6 +2245,22 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
2214
2245
|| (form_error = curl_mime_data (part , ZSTR_VAL (postval ), ZSTR_LEN (postval ))) != CURLE_OK ) {
2215
2246
error = form_error ;
2216
2247
}
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
2217
2264
zend_tmp_string_release (tmp_postval );
2218
2265
zend_string_release_ex (string_key , 0 );
2219
2266
} ZEND_HASH_FOREACH_END ();
@@ -2226,12 +2273,19 @@ static int rebuild_mime_structure(php_curl *ch, zval *zpostfields)
2226
2273
if ((* ch -> clone ) == 1 ) {
2227
2274
zend_llist_clean (& ch -> to_free -> post );
2228
2275
}
2276
+ #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2229
2277
zend_llist_add_element (& ch -> to_free -> post , & mime );
2230
2278
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
2231
2283
2232
- return SUCCESS ;
2284
+ SAVE_CURL_ERROR (ch , error );
2285
+ return error == CURLE_OK ? SUCCESS : FAILURE ;
2233
2286
}
2234
2287
2288
+ #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2235
2289
static HashTable * get_postfields_of_handle (php_curl * ch )
2236
2290
{
2237
2291
struct mime_data_cb_arg * cb_arg , * * cb_arg_p ;
@@ -2279,7 +2333,7 @@ PHP_FUNCTION(curl_copy_handle)
2279
2333
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2280
2334
postfields = get_postfields_of_handle (ch );
2281
2335
if (postfields ) {
2282
- if (rebuild_mime_structure (dupch , postfields ) != SUCCESS ) {
2336
+ if (build_mime_structure_from_hash (dupch , postfields ) != SUCCESS ) {
2283
2337
_php_curl_close_ex (dupch );
2284
2338
php_error_docref (NULL , E_WARNING , "Cannot rebuild mime structure" );
2285
2339
RETURN_FALSE ;
@@ -2941,162 +2995,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
2941
2995
2942
2996
case CURLOPT_POSTFIELDS :
2943
2997
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 );
3100
2999
} else {
3101
3000
#if LIBCURL_VERSION_NUM >= 0x071101
3102
3001
zend_string * tmp_str ;
0 commit comments