Skip to content

Commit 17a9f14

Browse files
committed
Extend CURLFile to support streams
Due to former restrictions of the libcurl API, curl multipart/formdata file uploads supported only proper files. However, as of curl 7.56.0 the new `curl_mime_*()` API is available (and already supported by PHP[1]), which allows us to support arbitrary *seekable* streams, which is generally desirable, and particularly resolves issues with the transparent Unicode and long part support on Windows (see bug #77711). Note that older curl versions are still supported, but CURLFile is still restricted to proper files in this case. [1] <http://git.php.net/?p=php-src.git;a=commit;h=a83b68ba56714bfa06737a61af795460caa4a105> (cherry picked from commit c68dc6b)
1 parent e120273 commit 17a9f14

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 7.3.14
44

5+
- CURL:
6+
. Implemented FR #77711 (CURLFile should support UNICODE filenames). (cmb)
7+
58
- GD:
69
. Fixed bug #78923 (Artifacts when convoluting image with transparency).
710
(wilson chen)

ext/curl/interface.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,14 @@ static void curl_free_post(void **post)
18521852
}
18531853
/* }}} */
18541854

1855+
/* {{{ curl_free_stream
1856+
*/
1857+
static void curl_free_stream(void **post)
1858+
{
1859+
php_stream_close((php_stream *)*post);
1860+
}
1861+
/* }}} */
1862+
18551863
/* {{{ curl_free_slist
18561864
*/
18571865
static void curl_free_slist(zval *el)
@@ -1943,6 +1951,7 @@ php_curl *alloc_curl_handle()
19431951

19441952
zend_llist_init(&ch->to_free->str, sizeof(char *), (llist_dtor_func_t)curl_free_string, 0);
19451953
zend_llist_init(&ch->to_free->post, sizeof(struct HttpPost *), (llist_dtor_func_t)curl_free_post, 0);
1954+
zend_llist_init(&ch->to_free->stream, sizeof(php_stream *), (llist_dtor_func_t)curl_free_stream, 0);
19461955

19471956
ch->to_free->slist = emalloc(sizeof(HashTable));
19481957
zend_hash_init(ch->to_free->slist, 4, NULL, curl_free_slist, 0);
@@ -2170,6 +2179,32 @@ PHP_FUNCTION(curl_copy_handle)
21702179
}
21712180
/* }}} */
21722181

2182+
#if LIBCURL_VERSION_NUM >= 0x073800
2183+
static size_t read_cb(char *buffer, size_t size, size_t nitems, void *arg) /* {{{ */
2184+
{
2185+
php_stream *stream = (php_stream *) arg;
2186+
size_t numread = php_stream_read(stream, buffer, nitems * size);
2187+
2188+
if (numread == (size_t)-1) {
2189+
return CURL_READFUNC_ABORT;
2190+
}
2191+
return numread;
2192+
}
2193+
/* }}} */
2194+
2195+
static int seek_cb(void *arg, curl_off_t offset, int origin) /* {{{ */
2196+
{
2197+
php_stream *stream = (php_stream *) arg;
2198+
int res = php_stream_seek(stream, offset, origin);
2199+
2200+
if (res) {
2201+
return CURL_SEEKFUNC_CANTSEEK;
2202+
}
2203+
return CURL_SEEKFUNC_OK;
2204+
}
2205+
/* }}} */
2206+
#endif
2207+
21732208
static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{ */
21742209
{
21752210
CURLcode error = CURLE_OK;
@@ -2805,6 +2840,9 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28052840
/* new-style file upload */
28062841
zval *prop, rv;
28072842
char *type = NULL, *filename = NULL;
2843+
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2844+
php_stream *stream;
2845+
#endif
28082846

28092847
prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
28102848
if (Z_TYPE_P(prop) != IS_STRING) {
@@ -2826,17 +2864,24 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
28262864
}
28272865

28282866
#if LIBCURL_VERSION_NUM >= 0x073800 /* 7.56.0 */
2867+
if (!(stream = php_stream_open_wrapper(ZSTR_VAL(postval), "rb", IGNORE_PATH, NULL))) {
2868+
zend_string_release_ex(string_key, 0);
2869+
return FAILURE;
2870+
}
28292871
part = curl_mime_addpart(mime);
28302872
if (part == NULL) {
2873+
php_stream_close(stream);
28312874
zend_string_release_ex(string_key, 0);
28322875
return FAILURE;
28332876
}
28342877
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
2878+
|| (form_error = curl_mime_data_cb(part, -1, read_cb, seek_cb, NULL, stream)) != CURLE_OK
28362879
|| (form_error = curl_mime_filename(part, filename ? filename : ZSTR_VAL(postval))) != CURLE_OK
28372880
|| (form_error = curl_mime_type(part, type ? type : "application/octet-stream")) != CURLE_OK) {
2881+
php_stream_close(stream);
28382882
error = form_error;
28392883
}
2884+
zend_llist_add_element(&ch->to_free->stream, &stream);
28402885
#else
28412886
form_error = curl_formadd(&first, &last,
28422887
CURLFORM_COPYNAME, ZSTR_VAL(string_key),
@@ -3566,6 +3611,7 @@ static void _php_curl_close_ex(php_curl *ch)
35663611
if (--(*ch->clone) == 0) {
35673612
zend_llist_clean(&ch->to_free->str);
35683613
zend_llist_clean(&ch->to_free->post);
3614+
zend_llist_clean(&ch->to_free->stream);
35693615
zend_hash_destroy(ch->to_free->slist);
35703616
efree(ch->to_free->slist);
35713617
efree(ch->to_free);

ext/curl/php_curl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ struct _php_curl_send_headers {
169169
struct _php_curl_free {
170170
zend_llist str;
171171
zend_llist post;
172+
zend_llist stream;
172173
HashTable *slist;
173174
};
174175

ext/curl/tests/bug77711.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
FR #77711 (CURLFile should support UNICODE filenames)
3+
--SKIPIF--
4+
<?php include 'skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
include 'server.inc';
8+
$host = curl_cli_server_start();
9+
10+
$ch = curl_init();
11+
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
12+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.php?test=file");
13+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
14+
15+
$filename = __DIR__ . '/АБВ.txt';
16+
file_put_contents($filename, "Test.");
17+
$file = curl_file_create($filename);
18+
$params = array('file' => $file);
19+
var_dump(curl_setopt($ch, CURLOPT_POSTFIELDS, $params));
20+
21+
var_dump(curl_exec($ch));
22+
curl_close($ch);
23+
?>
24+
===DONE===
25+
--EXPECTF--
26+
bool(true)
27+
string(%d) "АБВ.txt|application/octet-stream"
28+
===DONE===
29+
--CLEAN--
30+
<?php
31+
@unlink(__DIR__ . '/АБВ.txt');
32+
?>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
curl_copy_handle() allows to post CURLFile multiple times
3+
--SKIPIF--
4+
<?php include 'skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
include 'server.inc';
8+
$host = curl_cli_server_start();
9+
10+
$ch1 = curl_init();
11+
curl_setopt($ch1, CURLOPT_SAFE_UPLOAD, 1);
12+
curl_setopt($ch1, CURLOPT_URL, "{$host}/get.php?test=file");
13+
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
14+
15+
$filename = __DIR__ . '/АБВ.txt';
16+
file_put_contents($filename, "Test.");
17+
$file = curl_file_create($filename);
18+
$params = array('file' => $file);
19+
var_dump(curl_setopt($ch1, CURLOPT_POSTFIELDS, $params));
20+
21+
$ch2 = curl_copy_handle($ch1);
22+
23+
var_dump(curl_exec($ch1));
24+
curl_close($ch1);
25+
26+
var_dump(curl_exec($ch2));
27+
curl_close($ch2);
28+
?>
29+
===DONE===
30+
--EXPECTF--
31+
bool(true)
32+
string(%d) "АБВ.txt|application/octet-stream"
33+
string(%d) "АБВ.txt|application/octet-stream"
34+
===DONE===
35+
--CLEAN--
36+
<?php
37+
@unlink(__DIR__ . '/АБВ.txt');
38+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
CURL file uploading from stream
3+
--SKIPIF--
4+
<?php include 'skipif.inc'; ?>
5+
<?php
6+
if (curl_version()['version_number'] < 0x73800) die('skip requires curl >= 7.56.0');
7+
--FILE--
8+
<?php
9+
include 'server.inc';
10+
$host = curl_cli_server_start();
11+
12+
$ch = curl_init();
13+
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
14+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=file");
15+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
16+
17+
$file = curl_file_create('data://text/plain;base64,SSBsb3ZlIFBIUAo=', 'text/plain', 'i-love-php');
18+
$params = array('file' => $file);
19+
var_dump(curl_setopt($ch, CURLOPT_POSTFIELDS, $params));
20+
21+
var_dump(curl_exec($ch));
22+
curl_close($ch);
23+
?>
24+
===DONE===
25+
--EXPECT--
26+
bool(true)
27+
string(21) "i-love-php|text/plain"
28+
===DONE===

0 commit comments

Comments
 (0)