Skip to content

curl_seek bug #47204 #581

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ PHP_MINIT_FUNCTION(curl)
REGISTER_CURL_CONSTANT(CURLPAUSE_SEND_CONT);
REGISTER_CURL_CONSTANT(CURL_READFUNC_PAUSE);
REGISTER_CURL_CONSTANT(CURL_WRITEFUNC_PAUSE);
REGISTER_CURL_CONSTANT(CURLOPT_SEEKFUNCTION);
#endif

#if LIBCURL_VERSION_NUM >= 0x071202 /* Available since 7.18.2 */
Expand Down Expand Up @@ -1558,6 +1559,87 @@ static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
}
/* }}} */

/* {{{ curl_seek
*/
static int curl_seek(void *ctx, curl_off_t offset, int origin)
{
php_curl *ch = (php_curl *) ctx;
php_curl_seek *t = ch->handlers->seek;
int status = CURL_SEEKFUNC_CANTSEEK;

switch (t->method) {
case PHP_CURL_DIRECT:
if (t->fp) {
if (fseek(t->fp, offset, origin) == 0)
{
return CURL_SEEKFUNC_OK;
}
}
break;
case PHP_CURL_USER: {
zval **argv[4];
zval *handle = NULL;
zval *zfd = NULL;
zval *zoffset = NULL;
zval *zorigin = NULL;
zval *retval_ptr;
int error;
zend_fcall_info fci;
TSRMLS_FETCH_FROM_CTX(ch->thread_ctx);

MAKE_STD_ZVAL(handle);
MAKE_STD_ZVAL(zfd);
MAKE_STD_ZVAL(zoffset);
MAKE_STD_ZVAL(zorigin);

ZVAL_RESOURCE(handle, ch->id);
zend_list_addref(ch->id);
ZVAL_RESOURCE(zfd, t->fd);
zend_list_addref(t->fd);
ZVAL_LONG(zoffset, (int) offset);
ZVAL_LONG(zorigin, (int) origin);

argv[0] = &handle;
argv[1] = &zfd;
argv[2] = &zoffset;
argv[3] = &zorigin;

fci.size = sizeof(fci);
fci.function_table = EG(function_table);
fci.function_name = t->func_name;
fci.object_ptr = NULL;
fci.retval_ptr_ptr = &retval_ptr;
fci.param_count = 4;
fci.params = argv;
fci.no_separation = 0;
fci.symbol_table = NULL;

ch->in_callback = 1;
error = zend_call_function(&fci, &t->fci_cache TSRMLS_CC);
ch->in_callback = 0;
if (error == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot call the CURLOPT_SEEKFUNCTION");
} else if (retval_ptr) {
if (Z_TYPE_P(retval_ptr) != IS_BOOL) {
convert_to_boolean_ex(&retval_ptr);
}
if (Z_LVAL_P(retval_ptr) == 1)
{
status = CURL_SEEKFUNC_OK;
}
zval_ptr_dtor(&retval_ptr);
}
zval_ptr_dtor(argv[0]);
zval_ptr_dtor(argv[1]);
zval_ptr_dtor(argv[2]);
zval_ptr_dtor(argv[3]);
}
break;
}
return status;
}
/* }}} */

/* {{{ curl_write_header
*/
static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
Expand Down Expand Up @@ -1776,6 +1858,7 @@ static void alloc_curl_handle(php_curl **ch)
(*ch)->handlers->write = ecalloc(1, sizeof(php_curl_write));
(*ch)->handlers->write_header = ecalloc(1, sizeof(php_curl_write));
(*ch)->handlers->read = ecalloc(1, sizeof(php_curl_read));
(*ch)->handlers->seek = ecalloc(1, sizeof(php_curl_seek));
(*ch)->handlers->progress = NULL;
#if LIBCURL_VERSION_NUM >= 0x071500 /* Available since 7.21.0 */
(*ch)->handlers->fnmatch = NULL;
Expand Down Expand Up @@ -1888,6 +1971,8 @@ static void _php_curl_set_default_options(php_curl *ch)
curl_easy_setopt(ch->cp, CURLOPT_WRITEFUNCTION, curl_write);
curl_easy_setopt(ch->cp, CURLOPT_FILE, (void *) ch);
curl_easy_setopt(ch->cp, CURLOPT_READFUNCTION, curl_read);
curl_easy_setopt(ch->cp, CURLOPT_SEEKFUNCTION, curl_seek);
curl_easy_setopt(ch->cp, CURLOPT_SEEKDATA, ch);
curl_easy_setopt(ch->cp, CURLOPT_INFILE, (void *) ch);
curl_easy_setopt(ch->cp, CURLOPT_HEADERFUNCTION, curl_write_header);
curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
Expand Down Expand Up @@ -2707,6 +2792,16 @@ static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *retu
ch->handlers->read->method = PHP_CURL_USER;
break;

case CURLOPT_SEEKFUNCTION:
if (ch->handlers->seek->func_name) {
zval_ptr_dtor(&ch->handlers->seek->func_name);
ch->handlers->seek->fci_cache = empty_fcall_info_cache;
}
zval_add_ref(zvalue);
ch->handlers->seek->func_name = *zvalue;
ch->handlers->seek->method = PHP_CURL_USER;
break;

case CURLOPT_RETURNTRANSFER:
convert_to_long_ex(zvalue);
if (Z_LVAL_PP(zvalue)) {
Expand Down
10 changes: 10 additions & 0 deletions ext/curl/php_curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ typedef struct {
zval *stream;
} php_curl_read;

typedef struct {
zval *func_name;
zend_fcall_info_cache fci_cache;
FILE *fp;
long fd;
int method;
zval *stream;
} php_curl_seek;

typedef struct {
zval *func_name;
zend_fcall_info_cache fci_cache;
Expand All @@ -145,6 +154,7 @@ typedef struct {
php_curl_write *write;
php_curl_write *write_header;
php_curl_read *read;
php_curl_seek *seek;
#if CURLOPT_PASSWDFUNCTION != 0
zval *passwd;
#endif
Expand Down
65 changes: 65 additions & 0 deletions ext/curl/tests/curl_upload_redirect.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
Test curl_exec() function with basic functionality
--CREDITS--
Sebastian Deutsch <sebastian.deutsch@9elements.com>
TestFest 2009 - AFUP - Jean-Marc Fontaine <jmf@durcommefaire.net>
--SKIPIF--
<?php
if (!extension_loaded("curl")) exit("skip curl extension not loaded");
if (false === getenv('PHP_CURL_HTTP_REMOTE_SERVER')) exit("skip PHP_CURL_HTTP_REMOTE_SERVER env variable is not defined");
?>
--FILE--
<?php
/* Prototype : bool curl_exec(resource ch)
* Description: Perform a cURL session
* Source code: ext/curl/interface.c
* Alias to functions:
*/

$host = getenv('PHP_CURL_HTTP_REMOTE_SERVER');

// start testing
echo "*** Testing curl_exec() : basic functionality ***\n";

$fp = fopen(__FILE__, 'r');
/**
* @param resource $ch curl handle
* @param resource $fd file descriptor - do not use
* @param int $offset move to position in file
* @param int $whence start to seek from ...
*
* @return string
*/
function seek_callback($ch, $fd, $offset, $whence)
{
global $fp;
return fseek($fp, $offset, $whence) == 0;
}

$url = "{$host}/get.php?test=redirect_upload";
$ch = curl_init();

ob_start(); // start output buffering
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_URL, $url); //set the url we want to use
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SEEKFUNCTION, 'seek_callback');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about a closure callback?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bhoehl he means something like that

curl_setopt($ch, CURLOPT_SEEKFUNCTION, function() {
  // my seek function code here
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, is that supported?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laruence
as closures for sure are handled differently than a string callback, I assume not.
I need to lookup and add, if wanted

$ok = curl_exec($ch);
curl_close($ch);
fclose($fp);
$curl_content = ob_get_contents();
ob_end_clean();

if($ok) {
var_dump( $curl_content );
} else {
echo "curl_exec returned false";
}
?>
===DONE===
--EXPECTF--
*** Testing curl_exec() : basic functionality ***
string(25) "Hello World!
Hello World!"
===DONE===
3 changes: 3 additions & 0 deletions ext/curl/tests/responder/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
echo $_FILES['file']['name'] . '|' . $_FILES['file']['type'];
}
break;
case 'redirect_upload':
header('Location: /get.php?test=upload');
break;
default:
echo "Hello World!\n";
echo "Hello World!";
Expand Down