php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80518 CURLOPT_READFUNCTION stream can't rewind
Submitted: 2020-12-15 09:19 UTC Modified: 2020-12-15 12:48 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: malacca42 at gmail dot com Assigned:
Status: Open Package: cURL related
PHP Version: 7.4 OS: windows
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-12-15 09:19 UTC] malacca42 at gmail dot com
Description:
------------
CURLOPT_READFUNCTION  stream  can't rewind

Test script:
---------------
$file = __DIR__.'/payload';
$fp = fopen($file, 'r');

$ch = curl_init('http://localhost/server.php');
curl_setopt_array($ch, [
    CURLOPT_VERBOSE => true,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_FOLLOWLOCATION => true,  // follow location
    CURLOPT_UPLOAD => true,
    CURLOPT_INFILESIZE => filesize($file),
    CURLOPT_READFUNCTION => function ($ch, $fd, $length) use ($fp) {
        return fread($fp, $length);
    },
]);

curl_exec($ch);
curl_close($ch);


----------

the "server.php"  is very simple

if (isset($_GET['n'])) {
    echo file_get_contents('php://input');
} else {
    Header("Location: sever.php?n=1");
    http_response_code(307);
}



Expected result:
----------------
server can read payload after redirect

as RFC, entity body will resend after response 307 redirect
https://tools.ietf.org/html/rfc7231#section-6.4

but CURLOPT_READFUNCTION function only read once
maybe  php lost CURLOPT_SEEKFUNCTION option
https://curl.se/libcurl/c/CURLOPT_SEEKFUNCTION.html

One more thing:
this is very very slow using php8,  php7 is fast



Actual result:
--------------
got issue

"necessary data rewind wasn't possible"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-15 09:21 UTC] nikic@php.net
-Summary: win7 +Summary: CURLOPT_READFUNCTION stream can't rewind
 [2020-12-15 10:37 UTC] malacca42 at gmail dot com
replace CURLOPT_READFUNCTION  to  CURLOPT_INFILE  still has same issue


Test script:
---------------
$file = __DIR__.'/payload';
$fp = fopen($file, 'r');

$ch = curl_init('http://localhost/server.php');
curl_setopt_array($ch, [
    CURLOPT_VERBOSE => true,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_FOLLOWLOCATION => true,  // follow location
    CURLOPT_UPLOAD => true,
    CURLOPT_INFILESIZE => filesize($file),
    CURLOPT_INFILE => $fp,
]);

curl_exec($ch);
curl_close($ch);


result:
------------
also got "necessary data rewind wasn't possible"
 [2020-12-15 12:48 UTC] cmb@php.net
-PHP Version: 8.0.0 +PHP Version: 7.4
 [2020-12-15 12:48 UTC] cmb@php.net
This is not particularly related to PHP 8.0, but likely did never
work (at least it does not with PHP 7.3 and 7.4).  It seems to me
to make it work, we'd have to introduce CURLOPT_SEEKFUNCTION[1].

> this is very very slow using php8,  php7 is fast

Please file a separate ticket for this, because it is obviously
not directly related.

[1] <https://curl.se/libcurl/c/CURLOPT_SEEKFUNCTION.html>
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 13 11:01:24 2021 UTC