@@ -1796,11 +1796,20 @@ static void curl_free_post(void **post)
1796
1796
}
1797
1797
/* }}} */
1798
1798
1799
+ struct mime_file {
1800
+ zend_string * name ;
1801
+ php_stream * stream ;
1802
+ };
1803
+
1799
1804
/* {{{ curl_free_stream
1800
1805
*/
1801
1806
static void curl_free_stream (void * * post )
1802
1807
{
1803
- php_stream_close ((php_stream * )* post );
1808
+ struct mime_file * mime_file = (struct mime_file * ) * post ;
1809
+
1810
+ ZEND_ASSERT (mime_file -> stream == NULL );
1811
+ zend_string_release (mime_file -> name );
1812
+ efree (mime_file );
1804
1813
}
1805
1814
/* }}} */
1806
1815
@@ -1901,7 +1910,7 @@ php_curl *alloc_curl_handle()
1901
1910
1902
1911
zend_llist_init (& ch -> to_free -> str , sizeof (char * ), (llist_dtor_func_t )curl_free_string , 0 );
1903
1912
zend_llist_init (& ch -> to_free -> post , sizeof (struct HttpPost * ), (llist_dtor_func_t )curl_free_post , 0 );
1904
- zend_llist_init (& ch -> to_free -> stream , sizeof (php_stream * ), (llist_dtor_func_t )curl_free_stream , 0 );
1913
+ zend_llist_init (& ch -> to_free -> stream , sizeof (struct mime_file * ), (llist_dtor_func_t )curl_free_stream , 0 );
1905
1914
1906
1915
ch -> to_free -> slist = emalloc (sizeof (HashTable ));
1907
1916
zend_hash_init (ch -> to_free -> slist , 4 , NULL , curl_free_slist , 0 );
@@ -2129,28 +2138,42 @@ PHP_FUNCTION(curl_copy_handle)
2129
2138
}
2130
2139
/* }}} */
2131
2140
2132
- #if LIBCURL_VERSION_NUM >= 0x073800
2141
+ #if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2133
2142
static size_t read_cb (char * buffer , size_t size , size_t nitems , void * arg ) /* {{{ */
2134
2143
{
2135
- php_stream * stream = (php_stream * ) arg ;
2136
- ssize_t numread = php_stream_read ( stream , buffer , nitems * size ) ;
2144
+ struct mime_file * mime_file = (struct mime_file * ) arg ;
2145
+ ssize_t numread ;
2137
2146
2138
- if (numread < 0 ) {
2139
- return CURL_READFUNC_ABORT ;
2147
+ if (mime_file -> stream == NULL ) {
2148
+ if (!(mime_file -> stream = php_stream_open_wrapper (ZSTR_VAL (mime_file -> name ), "rb" , IGNORE_PATH , NULL ))) {
2149
+ return CURL_READFUNC_ABORT ;
2150
+ }
2140
2151
}
2141
- return numread ;
2152
+ numread = php_stream_read (mime_file -> stream , buffer , nitems * size );
2153
+ if (numread <= 0 ) {
2154
+ php_stream_close (mime_file -> stream );
2155
+ mime_file -> stream = NULL ;
2156
+ }
2157
+ return (numread >= 0 ) ? numread : CURL_READFUNC_ABORT ;
2142
2158
}
2143
2159
/* }}} */
2144
2160
2145
2161
static int seek_cb (void * arg , curl_off_t offset , int origin ) /* {{{ */
2146
2162
{
2147
- php_stream * stream = (php_stream * ) arg ;
2148
- int res = php_stream_seek ( stream , offset , origin ) ;
2163
+ struct mime_file * mime_file = (struct mime_file * ) arg ;
2164
+ int res ;
2149
2165
2166
+ if (mime_file -> stream == NULL ) {
2167
+ if (!(mime_file -> stream = php_stream_open_wrapper (ZSTR_VAL (mime_file -> name ), "rb" , IGNORE_PATH , NULL ))) {
2168
+ return CURL_SEEKFUNC_CANTSEEK ;
2169
+ }
2170
+ }
2171
+ res = php_stream_seek (mime_file -> stream , offset , origin );
2150
2172
if (res ) {
2151
- return CURL_SEEKFUNC_CANTSEEK ;
2173
+ php_stream_close (mime_file -> stream );
2174
+ mime_file -> stream = NULL ;
2152
2175
}
2153
- return CURL_SEEKFUNC_OK ;
2176
+ return ! res ? CURL_SEEKFUNC_OK : CURL_SEEKFUNC_CANTSEEK ;
2154
2177
}
2155
2178
/* }}} */
2156
2179
#endif
@@ -2793,7 +2816,7 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
2793
2816
zval * prop , rv ;
2794
2817
char * type = NULL , * filename = NULL ;
2795
2818
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2796
- php_stream * stream ;
2819
+ struct mime_file * mime_file ;
2797
2820
#endif
2798
2821
2799
2822
prop = zend_read_property (curl_CURLFile_class , current , "name" , sizeof ("name" )- 1 , 0 , & rv );
@@ -2816,24 +2839,21 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
2816
2839
}
2817
2840
2818
2841
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2819
- if (!(stream = php_stream_open_wrapper (ZSTR_VAL (postval ), "rb" , IGNORE_PATH , NULL ))) {
2820
- zend_string_release_ex (string_key , 0 );
2821
- return FAILURE ;
2822
- }
2842
+ mime_file = emalloc (sizeof * mime_file );
2843
+ mime_file -> name = zend_string_copy (postval );
2844
+ mime_file -> stream = NULL ;
2823
2845
part = curl_mime_addpart (mime );
2824
2846
if (part == NULL ) {
2825
- php_stream_close (stream );
2826
2847
zend_string_release_ex (string_key , 0 );
2827
2848
return FAILURE ;
2828
2849
}
2829
2850
if ((form_error = curl_mime_name (part , ZSTR_VAL (string_key ))) != CURLE_OK
2830
- || (form_error = curl_mime_data_cb (part , -1 , read_cb , seek_cb , NULL , stream )) != CURLE_OK
2851
+ || (form_error = curl_mime_data_cb (part , -1 , read_cb , seek_cb , NULL , mime_file )) != CURLE_OK
2831
2852
|| (form_error = curl_mime_filename (part , filename ? filename : ZSTR_VAL (postval ))) != CURLE_OK
2832
2853
|| (form_error = curl_mime_type (part , type ? type : "application/octet-stream" )) != CURLE_OK ) {
2833
- php_stream_close (stream );
2834
2854
error = form_error ;
2835
2855
}
2836
- zend_llist_add_element (& ch -> to_free -> stream , & stream );
2856
+ zend_llist_add_element (& ch -> to_free -> stream , & mime_file );
2837
2857
#else
2838
2858
form_error = curl_formadd (& first , & last ,
2839
2859
CURLFORM_COPYNAME , ZSTR_VAL (string_key ),
0 commit comments