Skip to content

Commit 38ad37a

Browse files
committed
Fix #80595: Resetting POSTFIELDS to empty array breaks request
This is mainly to work around curl/curl#6455, but not building the mime structure for empty hashtables is a general performance optimization, so we do not restrict it to affected cURL versions (7.56.0 to 7.75.0). The minor change to bug79033.phpt is unexpected, but should not matter in practice. Closes GH-6606.
1 parent 25103c3 commit 38ad37a

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ PHP NEWS
77
. Fixed bug #80384 (filter buffers entire read until file closed). (Adam
88
Seitz, cmb)
99

10+
- Curl:
11+
. Fixed bug #80595 (Resetting POSTFIELDS to empty array breaks request). (cmb)
12+
1013
- Date:
1114
. Fixed bug #80376 (last day of the month causes runway cpu usage. (Derick)
1215

ext/curl/interface.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2969,7 +2969,14 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue) /* {{{
29692969

29702970
case CURLOPT_POSTFIELDS:
29712971
if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
2972-
return build_mime_structure_from_hash(ch, zvalue);
2972+
if (zend_hash_num_elements(HASH_OF(zvalue)) == 0) {
2973+
/* no need to build the mime structure for empty hashtables;
2974+
also works around https://github.com/curl/curl/issues/6455 */
2975+
curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDS, "");
2976+
error = curl_easy_setopt(ch->cp, CURLOPT_POSTFIELDSIZE, 0);
2977+
} else {
2978+
return build_mime_structure_from_hash(ch, zvalue);
2979+
}
29732980
} else {
29742981
#if LIBCURL_VERSION_NUM >= 0x071101
29752982
zend_string *tmp_str;

ext/curl/tests/bug79033.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ var_dump(curl_getinfo($ch)["request_header"]);
2121
string(%d) "array(0) {
2222
}
2323
"
24-
string(90) "POST /get.inc?test=post HTTP/1.1
24+
string(%d) "POST /get.inc?test=post HTTP/1.1
2525
Host: localhost:%d
2626
Accept: */*
2727
Content-Length: 0
28+
Content-Type: application/x-www-form-urlencoded
2829

2930
"

ext/curl/tests/bug80595.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #80595 (Resetting POSTFIELDS to empty array breaks request)
3+
--SKIPIF--
4+
<?php include 'skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
include 'server.inc';
8+
$host = curl_cli_server_start();
9+
$ch = curl_init();
10+
curl_setopt_array($ch, [
11+
CURLOPT_RETURNTRANSFER => true,
12+
CURLOPT_POST => true,
13+
CURLOPT_URL => "{$host}/get.inc?test=post",
14+
]);
15+
16+
curl_setopt($ch, CURLOPT_POSTFIELDS, ['foo' => 'bar']);
17+
var_dump(curl_exec($ch));
18+
19+
curl_setopt($ch, CURLOPT_POSTFIELDS, []);
20+
var_dump(curl_exec($ch));
21+
?>
22+
--EXPECT--
23+
string(43) "array(1) {
24+
["foo"]=>
25+
string(3) "bar"
26+
}
27+
"
28+
string(13) "array(0) {
29+
}
30+
"

0 commit comments

Comments
 (0)