Skip to content
Open
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
22 changes: 14 additions & 8 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
_php_curl_verify_handlers(ch, /* reporterror */ true);
/* TODO Check callback returns an int or something castable to int */
length = php_curl_get_long(&retval);
} else if (EG(exception)) {
length = -1;
}

zval_ptr_dtor(&argv[0]);
Expand Down Expand Up @@ -606,7 +608,7 @@ static int curl_fnmatch(void *ctx, const char *pattern, const char *string)
static int curl_progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
{
php_curl *ch = (php_curl *)clientp;
int rval = 0;
int rval = 1;

#if PHP_CURL_DEBUG
fprintf(stderr, "curl_progress() called\n");
Expand All @@ -633,8 +635,8 @@ static int curl_progress(void *clientp, double dltotal, double dlnow, double ult
if (!Z_ISUNDEF(retval)) {
_php_curl_verify_handlers(ch, /* reporterror */ true);
/* TODO Check callback returns an int or something castable to int */
if (0 != php_curl_get_long(&retval)) {
rval = 1;
if (0 == php_curl_get_long(&retval)) {
rval = 0;
}
}

Expand All @@ -647,7 +649,7 @@ static int curl_progress(void *clientp, double dltotal, double dlnow, double ult
static int curl_xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
php_curl *ch = (php_curl *)clientp;
int rval = 0;
int rval = 1;

#if PHP_CURL_DEBUG
fprintf(stderr, "curl_xferinfo() called\n");
Expand All @@ -674,8 +676,8 @@ static int curl_xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow, cu
if (!Z_ISUNDEF(retval)) {
_php_curl_verify_handlers(ch, /* reporterror */ true);
/* TODO Check callback returns an int or something castable to int */
if (0 != php_curl_get_long(&retval)) {
rval = 1;
if (0 == php_curl_get_long(&retval)) {
rval = 0;
}
}

Expand All @@ -688,13 +690,13 @@ static int curl_xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow, cu
static int curl_prereqfunction(void *clientp, char *conn_primary_ip, char *conn_local_ip, int conn_primary_port, int conn_local_port)
{
php_curl *ch = (php_curl *)clientp;
int rval = CURL_PREREQFUNC_OK;
int rval = CURL_PREREQFUNC_ABORT;

// when CURLOPT_PREREQFUNCTION is set to null, curl_prereqfunction still
// gets called. Return CURL_PREREQFUNC_OK immediately in this case to avoid
// zend_call_known_fcc() with an uninitialized FCC.
if (!ZEND_FCC_INITIALIZED(ch->handlers.prereq)) {
return rval;
return CURL_PREREQFUNC_OK;
}

#if PHP_CURL_DEBUG
Expand Down Expand Up @@ -823,6 +825,8 @@ static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
}
// TODO Do type error if invalid type?
zval_ptr_dtor(&retval);
} else if (EG(exception)) {
length = CURL_READFUNC_ABORT;
}

zval_ptr_dtor(&argv[0]);
Expand Down Expand Up @@ -915,6 +919,8 @@ static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx
// TODO: Check for valid int type for return value
_php_curl_verify_handlers(ch, /* reporterror */ true);
length = php_curl_get_long(&retval);
} else if (EG(exception)) {
length = -1;
}
zval_ptr_dtor(&argv[0]);
zval_ptr_dtor(&argv[1]);
Expand Down
35 changes: 35 additions & 0 deletions ext/curl/tests/curl_headerfunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
CURLOPT_HEADERFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_HEADERFUNCTION')) {
die('skip CURLOPT_HEADERFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function (): int {
throw new Exception('header exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_WRITE_ERROR);

?>
--EXPECTF--
header exception
bool(true)
35 changes: 35 additions & 0 deletions ext/curl/tests/curl_prereqfunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
CURLOPT_PREREQFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_PREREQFUNCTION')) {
die('skip CURLOPT_PREREQFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

curl_setopt($ch, CURLOPT_PREREQFUNCTION,
function (): int {
throw new Exception('prereq exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_ABORTED_BY_CALLBACK);

?>
--EXPECTF--
prereq exception
bool(true)
36 changes: 36 additions & 0 deletions ext/curl/tests/curl_progressfunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
CURLOPT_PROGRESSFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_PROGRESSFUNCTION')) {
die('skip CURLOPT_PROGRESSFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,
function (): int {
throw new Exception('info exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_ABORTED_BY_CALLBACK);

?>
--EXPECTF--
info exception
bool(true)
38 changes: 38 additions & 0 deletions ext/curl/tests/curl_readfunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
CURLOPT_READFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_READFUNCTION')) {
die('skip CURLOPT_READFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

$file = new CURLFile(__DIR__ . '/curl_testdata1.txt');
curl_setopt($ch, CURLOPT_POST, ['file' => $file]);

curl_setopt($ch, CURLOPT_READFUNCTION,
function (): int {
throw new Exception('read exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_ABORTED_BY_CALLBACK);

?>
--EXPECTF--
read exception
bool(true)
35 changes: 35 additions & 0 deletions ext/curl/tests/curl_writefunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
CURLOPT_WRITEFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_WRITEFUNCTION')) {
die('skip CURLOPT_WRITEFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

curl_setopt($ch, CURLOPT_WRITEFUNCTION,
function (): int {
throw new Exception('write exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_WRITE_ERROR);

?>
--EXPECTF--
write exception
bool(true)
36 changes: 36 additions & 0 deletions ext/curl/tests/curl_xferinfofunction_throws_abort.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
CURLOPT_XFERINFOFUNCTION aborts transfer when callback throws
--EXTENSIONS--
curl
--SKIPIF--
<?php
if (!defined('CURLOPT_XFERINFOFUNCTION')) {
die('skip CURLOPT_XFERINFOFUNCTION not available');
}
?>
--FILE--
<?php

include 'server.inc';
$host = curl_cli_server_start();
$ch = curl_init("{$host}/get.inc");

curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
curl_setopt($ch, CURLOPT_XFERINFOFUNCTION,
function (): int {
throw new Exception('info exception');
}
);

try {
curl_exec($ch);
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump(curl_errno($ch) === CURLE_ABORTED_BY_CALLBACK);

?>
--EXPECTF--
info exception
bool(true)
Loading