Skip to content

Commit

Permalink
Manage "Expect" header with cURL transport
Browse files Browse the repository at this point in the history
Fixes #453
  • Loading branch information
carlalexander committed Mar 20, 2021
1 parent 675728b commit 011baa6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
29 changes: 29 additions & 0 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ protected function setup_handle($url, $headers, $data, $options) {
$headers['Connection'] = 'close';
}

/**
* Add "Expect" header.
*
* By default, cURL adds a "Expect: 100-Continue" to most requests. This header can
* add as much as a second to the time it takes for cURL to perform a request. To
* prevent this, we need to set an empty "Expect" header. To match the behaviour of
* Guzzle, we'll add the empty header to requests that are smaller than 1 MB and use
* HTTP/1.1.
*
* https://curl.se/mail/lib-2017-07/0013.html
*/
if (!isset($headers['Expect']) && $options['protocol_version'] === 1.1) {
$headers['Expect'] = $this->get_expect_header($data);
}

$headers = Requests::flatten($headers);

if (!empty($data)) {
Expand Down Expand Up @@ -546,4 +561,18 @@ public static function test($capabilities = array()) {

return true;
}

/**
* Get the correct "Expect" header for the given request data.
*
* @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
* @return string The "Expect" header
*/
protected function get_expect_header($data) {
if (is_array($data)) {
$data = implode('', $data);
}

return strlen($data) >= 1048576 ? '100-Continue' : '';
}
}
35 changes: 35 additions & 0 deletions tests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,39 @@ public function testRevokedHTTPS() {
public function testBadDomain() {
parent::testBadDomain();
}

public function testDoesntOverwriteExpectHeaderIfManuallySet() {
$headers = array(
'Expect' => 'foo',
);
$request = Requests::post('http://localhost:8000/post', $headers, array(), $this->getOptions());

$result = json_decode($request->body, true);

$this->assertSame($headers['Expect'], $result['headers']['Expect']);
}

public function testSetsEmptyExpectHeaderWithDefaultSettings() {
$request = Requests::post('http://localhost:8000/post', array(), array(), $this->getOptions());

$result = json_decode($request->body, true);

$this->assertFalse(isset($result['headers']['Expect']));
}

public function testSetsExpectHeaderIfBodyExactly1Mb() {
$request = Requests::post('http://localhost:8000/post', array(), str_repeat('x', 1048576), $this->getOptions());

$result = json_decode($request->body, true);

$this->assertSame('100-Continue', $result['headers']['Expect']);
}

public function testSetsEmptyExpectHeaderIfBodySmallerThan1Mb() {
$request = Requests::post('http://localhost:8000/post', array(), str_repeat('x', 1048575), $this->getOptions());

$result = json_decode($request->body, true);

$this->assertFalse(isset($result['headers']['Expect']));
}
}

0 comments on commit 011baa6

Please sign in to comment.