Skip to content

Commit 05a5af9

Browse files
committed
Fix php-curl-class#472: Reset request content-length to fix subsequent request hanging after a PUT request
1 parent 0634f44 commit 05a5af9

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

TROUBLESHOOTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
```php
66
$curl = new Curl();
77
$curl->verbose();
8+
var_dump($curl);
89
```
910

1011
### Compare request with and without the library

src/Curl/Curl.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ public function exec($ch = null)
393393
unset($this->effectiveUrl);
394394
unset($this->totalTime);
395395

396+
// Reset content-length possibly set from a PUT or SEARCH request.
397+
$this->unsetHeader('Content-Length');
398+
399+
// Reset nobody setting possibly set from a HEAD request.
400+
$this->setOpt(CURLOPT_NOBODY, false);
401+
396402
// Allow multicurl to attempt retry as needed.
397403
if ($this->isChildOfMultiCurl) {
398404
return;
@@ -514,7 +520,6 @@ public function options($url, $data = array())
514520
$url = (string)$this->url;
515521
}
516522
$this->setUrl($url, $data);
517-
$this->removeHeader('Content-Length');
518523
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
519524
return $this->exec();
520525
}

tests/PHPCurlClass/Helper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ public function server($test, $request_method, $query_parameters = array(), $dat
3333
* previously forced method might be inherited.
3434
* Especially, POSTs must be configured to not perform post-redirect-get.
3535
*/
36-
private function chainedRequest($request_method)
36+
private function chainedRequest($request_method, $data)
3737
{
3838
if ($request_method === 'POST') {
39-
$this->server('request_method', $request_method, array(), true);
39+
$this->server('request_method', $request_method, $data, true);
4040
} else {
41-
$this->server('request_method', $request_method);
41+
$this->server('request_method', $request_method, $data);
4242
}
4343
\PHPUnit\Framework\Assert::assertEquals($request_method, $this->curl->responseHeaders['X-REQUEST-METHOD']);
4444
}
4545

46-
public function chainRequests($first, $second)
46+
public function chainRequests($first, $second, $data = array())
4747
{
48-
$this->chainedRequest($first);
49-
$this->chainedRequest($second);
48+
$this->chainedRequest($first, $data);
49+
$this->chainedRequest($second, $data);
5050
}
5151
}
5252

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,6 +2809,16 @@ public function testRequestMethodSuccessiveGetRequests()
28092809
$test->chainRequests('GET', 'DELETE');
28102810
$test->chainRequests('GET', 'HEAD');
28112811
$test->chainRequests('GET', 'OPTIONS');
2812+
$test->chainRequests('GET', 'GET');
2813+
2814+
$test = new Test();
2815+
$test->chainRequests('GET', 'POST', array('a' => '1'));
2816+
$test->chainRequests('GET', 'PUT', array('b' => '22'));
2817+
$test->chainRequests('GET', 'PATCH', array('c' => '333'));
2818+
$test->chainRequests('GET', 'DELETE', array('d' => '4444'));
2819+
$test->chainRequests('GET', 'HEAD', array('e' => '55555'));
2820+
$test->chainRequests('GET', 'OPTIONS', array('f' => '666666'));
2821+
$test->chainRequests('GET', 'GET', array('g' => '7777777'));
28122822
}
28132823

28142824
public function testRequestMethodSuccessivePostRequests()
@@ -2820,6 +2830,16 @@ public function testRequestMethodSuccessivePostRequests()
28202830
$test->chainRequests('POST', 'DELETE');
28212831
$test->chainRequests('POST', 'HEAD');
28222832
$test->chainRequests('POST', 'OPTIONS');
2833+
$test->chainRequests('POST', 'POST');
2834+
2835+
$test = new Test();
2836+
$test->chainRequests('POST', 'GET', array('a' => '1'));
2837+
$test->chainRequests('POST', 'PUT', array('b' => '22'));
2838+
$test->chainRequests('POST', 'PATCH', array('c' => '333'));
2839+
$test->chainRequests('POST', 'DELETE', array('d' => '4444'));
2840+
$test->chainRequests('POST', 'HEAD', array('e' => '55555'));
2841+
$test->chainRequests('POST', 'OPTIONS', array('f' => '666666'));
2842+
$test->chainRequests('POST', 'POST', array('g' => '7777777'));
28232843
}
28242844

28252845
public function testRequestMethodSuccessivePutRequests()
@@ -2831,6 +2851,16 @@ public function testRequestMethodSuccessivePutRequests()
28312851
$test->chainRequests('PUT', 'DELETE');
28322852
$test->chainRequests('PUT', 'HEAD');
28332853
$test->chainRequests('PUT', 'OPTIONS');
2854+
$test->chainRequests('PUT', 'PUT');
2855+
2856+
$test = new Test();
2857+
$test->chainRequests('PUT', 'GET', array('a' => '1'));
2858+
$test->chainRequests('PUT', 'POST', array('b' => '22'));
2859+
$test->chainRequests('PUT', 'PATCH', array('c' => '333'));
2860+
$test->chainRequests('PUT', 'DELETE', array('d' => '4444'));
2861+
$test->chainRequests('PUT', 'HEAD', array('e' => '55555'));
2862+
$test->chainRequests('PUT', 'OPTIONS', array('f' => '666666'));
2863+
$test->chainRequests('PUT', 'PUT', array('g' => '7777777'));
28342864
}
28352865

28362866
public function testRequestMethodSuccessivePatchRequests()
@@ -2842,6 +2872,16 @@ public function testRequestMethodSuccessivePatchRequests()
28422872
$test->chainRequests('PATCH', 'DELETE');
28432873
$test->chainRequests('PATCH', 'HEAD');
28442874
$test->chainRequests('PATCH', 'OPTIONS');
2875+
$test->chainRequests('PATCH', 'PATCH');
2876+
2877+
$test = new Test();
2878+
$test->chainRequests('PATCH', 'GET', array('a' => '1'));
2879+
$test->chainRequests('PATCH', 'POST', array('b' => '22'));
2880+
$test->chainRequests('PATCH', 'PUT', array('c' => '333'));
2881+
$test->chainRequests('PATCH', 'DELETE', array('d' => '4444'));
2882+
$test->chainRequests('PATCH', 'HEAD', array('e' => '55555'));
2883+
$test->chainRequests('PATCH', 'OPTIONS', array('f' => '666666'));
2884+
$test->chainRequests('PATCH', 'PATCH', array('g' => '7777777'));
28452885
}
28462886

28472887
public function testRequestMethodSuccessiveDeleteRequests()
@@ -2853,6 +2893,16 @@ public function testRequestMethodSuccessiveDeleteRequests()
28532893
$test->chainRequests('DELETE', 'PATCH');
28542894
$test->chainRequests('DELETE', 'HEAD');
28552895
$test->chainRequests('DELETE', 'OPTIONS');
2896+
$test->chainRequests('DELETE', 'DELETE');
2897+
2898+
$test = new Test();
2899+
$test->chainRequests('DELETE', 'GET', array('a' => '1'));
2900+
$test->chainRequests('DELETE', 'POST', array('b' => '22'));
2901+
$test->chainRequests('DELETE', 'PUT', array('c' => '333'));
2902+
$test->chainRequests('DELETE', 'PATCH', array('d' => '4444'));
2903+
$test->chainRequests('DELETE', 'HEAD', array('e' => '55555'));
2904+
$test->chainRequests('DELETE', 'OPTIONS', array('f' => '666666'));
2905+
$test->chainRequests('DELETE', 'DELETE', array('g' => '7777777'));
28562906
}
28572907

28582908
public function testRequestMethodSuccessiveHeadRequests()
@@ -2864,6 +2914,16 @@ public function testRequestMethodSuccessiveHeadRequests()
28642914
$test->chainRequests('HEAD', 'PATCH');
28652915
$test->chainRequests('HEAD', 'DELETE');
28662916
$test->chainRequests('HEAD', 'OPTIONS');
2917+
$test->chainRequests('HEAD', 'HEAD');
2918+
2919+
$test = new Test();
2920+
$test->chainRequests('HEAD', 'GET', array('a' => '1'));
2921+
$test->chainRequests('HEAD', 'POST', array('b' => '22'));
2922+
$test->chainRequests('HEAD', 'PUT', array('c' => '333'));
2923+
$test->chainRequests('HEAD', 'PATCH', array('d' => '4444'));
2924+
$test->chainRequests('HEAD', 'DELETE', array('e' => '55555'));
2925+
$test->chainRequests('HEAD', 'OPTIONS', array('f' => '666666'));
2926+
$test->chainRequests('HEAD', 'HEAD', array('g' => '7777777'));
28672927
}
28682928

28692929
public function testRequestMethodSuccessiveOptionsRequests()
@@ -2875,6 +2935,16 @@ public function testRequestMethodSuccessiveOptionsRequests()
28752935
$test->chainRequests('OPTIONS', 'PATCH');
28762936
$test->chainRequests('OPTIONS', 'DELETE');
28772937
$test->chainRequests('OPTIONS', 'HEAD');
2938+
$test->chainRequests('OPTIONS', 'OPTIONS');
2939+
2940+
$test = new Test();
2941+
$test->chainRequests('OPTIONS', 'GET', array('a' => '1'));
2942+
$test->chainRequests('OPTIONS', 'POST', array('b' => '22'));
2943+
$test->chainRequests('OPTIONS', 'PUT', array('c' => '333'));
2944+
$test->chainRequests('OPTIONS', 'PATCH', array('d' => '4444'));
2945+
$test->chainRequests('OPTIONS', 'DELETE', array('e' => '55555'));
2946+
$test->chainRequests('OPTIONS', 'HEAD', array('f' => '666666'));
2947+
$test->chainRequests('OPTIONS', 'OPTIONS', array('g' => '7777777'));
28782948
}
28792949

28802950
public function testMemoryLeak()

0 commit comments

Comments
 (0)