-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Extend CURLFile to support streams #4034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--TEST-- | ||
FR #77711 (CURLFile should support UNICODE filenames) | ||
--SKIPIF-- | ||
<?php include 'skipif.inc'; ?> | ||
--FILE-- | ||
<?php | ||
include 'server.inc'; | ||
$host = curl_cli_server_start(); | ||
|
||
$ch = curl_init(); | ||
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1); | ||
curl_setopt($ch, CURLOPT_URL, "{$host}/get.php?test=file"); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
|
||
$filename = __DIR__ . '/АБВ.txt'; | ||
file_put_contents($filename, "Test."); | ||
$file = curl_file_create($filename); | ||
$params = array('file' => $file); | ||
var_dump(curl_setopt($ch, CURLOPT_POSTFIELDS, $params)); | ||
|
||
var_dump(curl_exec($ch)); | ||
curl_close($ch); | ||
?> | ||
===DONE=== | ||
--EXPECTF-- | ||
bool(true) | ||
string(%d) "АБВ.txt|application/octet-stream" | ||
===DONE=== | ||
--CLEAN-- | ||
<?php | ||
@unlink(__DIR__ . '/АБВ.txt'); | ||
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--TEST-- | ||
curl_copy_handle() allows to post CURLFile multiple times | ||
--SKIPIF-- | ||
<?php include 'skipif.inc'; ?> | ||
--FILE-- | ||
<?php | ||
include 'server.inc'; | ||
$host = curl_cli_server_start(); | ||
|
||
$ch1 = curl_init(); | ||
curl_setopt($ch1, CURLOPT_SAFE_UPLOAD, 1); | ||
curl_setopt($ch1, CURLOPT_URL, "{$host}/get.php?test=file"); | ||
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); | ||
|
||
$filename = __DIR__ . '/АБВ.txt'; | ||
file_put_contents($filename, "Test."); | ||
$file = curl_file_create($filename); | ||
$params = array('file' => $file); | ||
var_dump(curl_setopt($ch1, CURLOPT_POSTFIELDS, $params)); | ||
|
||
$ch2 = curl_copy_handle($ch1); | ||
|
||
var_dump(curl_exec($ch1)); | ||
curl_close($ch1); | ||
|
||
var_dump(curl_exec($ch2)); | ||
curl_close($ch2); | ||
?> | ||
===DONE=== | ||
--EXPECTF-- | ||
bool(true) | ||
string(%d) "АБВ.txt|application/octet-stream" | ||
string(%d) "АБВ.txt|application/octet-stream" | ||
===DONE=== | ||
--CLEAN-- | ||
<?php | ||
@unlink(__DIR__ . '/АБВ.txt'); | ||
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
CURL file uploading from stream | ||
--SKIPIF-- | ||
<?php include 'skipif.inc'; ?> | ||
<?php | ||
if (curl_version()['version_number'] < 0x73800) die('skip requires curl >= 7.56.0'); | ||
--FILE-- | ||
<?php | ||
include 'server.inc'; | ||
$host = curl_cli_server_start(); | ||
|
||
$ch = curl_init(); | ||
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1); | ||
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=file"); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
|
||
$file = curl_file_create('data://text/plain;base64,SSBsb3ZlIFBIUAo=', 'text/plain', 'i-love-php'); | ||
$params = array('file' => $file); | ||
var_dump(curl_setopt($ch, CURLOPT_POSTFIELDS, $params)); | ||
|
||
var_dump(curl_exec($ch)); | ||
curl_close($ch); | ||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
bool(true) | ||
string(21) "i-love-php|text/plain" | ||
===DONE=== |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth to support non seekable stream, like URL, which is a completely legit scenario.
Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That may not always work (see https://curl.haxx.se/libcurl/c/curl_mime_data_cb.html), but we still could fail in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing -1 for data length should be ok in general and would spare the stat call. Not requiring stream to be seekable and setting the seek function to NULL might be still a fallback. Knowing the stream size ahead is good, but doesn't guarantee we still don't end up with a partial data as it's stream dependent.
Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The curl docs are not particularly clear regarding the
datasize
, but it seems passing-1
works.Instead of passing
NULL
asseekfunc
I'd suggest to have a callback which tries to seek, and returnsCURL_SEEKFUNC_CANTSEEK
otherwise. which indicates that "libcurl is free to work around the problem if possible".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, could be probably an option, too. Thanks.