Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 24a51fb

Browse files
author
Fosco Marotto
committed
Merge pull request #116 from stepanselyuk/master
Additional way to calculate the request body size.
2 parents 5c986ef + 746d8fe commit 24a51fb

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/Facebook/HttpClients/FacebookCurlHttpClient.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,11 @@ private function getHeaderSize()
295295
// This corrects a Curl bug where header size does not account
296296
// for additional Proxy headers.
297297
if ( self::needsCurlProxyFix() ) {
298-
if ( stripos($this->rawResponse, self::CONNECTION_ESTABLISHED) !== false ) {
299-
$headerSize += mb_strlen(self::CONNECTION_ESTABLISHED);
298+
// Additional way to calculate the request body size.
299+
if (preg_match('/Content-Length: (\d+)/', $this->rawResponse, $m)) {
300+
$headerSize = mb_strlen($this->rawResponse) - $m[1];
301+
} elseif (stripos($this->rawResponse, self::CONNECTION_ESTABLISHED) !== false) {
302+
$headerSize += mb_strlen(self::CONNECTION_ESTABLISHED);
300303
}
301304
}
302305

tests/HttpClients/AbstractTestHttpClient.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ abstract class AbstractTestHttpClient extends PHPUnit_Framework_TestCase
77
Content-Type: text/html; charset=utf-8
88
Location: https://foobar.com/\r\n\r\n";
99
protected $fakeRawProxyHeader = "HTTP/1.0 200 Connection established\r\n\r\n";
10+
protected $fakeRawProxyHeader2 = "HTTP/1.0 200 Connection established
11+
Proxy-agent: Kerio Control/7.1.1 build 1971\r\n\r\n";
1012
protected $fakeRawHeader = "HTTP/1.1 200 OK
1113
Etag: \"9d86b21aa74d74e574bbb35ba13524a52deb96e3\"
1214
Content-Type: text/javascript; charset=UTF-8

tests/HttpClients/FacebookCurlHttpClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,34 @@ public function testProperlyHandlesProxyHeadersWithCurlBug()
298298
$this->assertEquals($headers, $this->fakeHeadersAsArray);
299299
}
300300

301+
public function testProperlyHandlesProxyHeadersWithCurlBug2()
302+
{
303+
$rawHeader = $this->fakeRawProxyHeader2 . $this->fakeRawHeader;
304+
$this->curlMock
305+
->shouldReceive('getinfo')
306+
->with(CURLINFO_HEADER_SIZE)
307+
->once()
308+
->andReturn(mb_strlen($this->fakeRawHeader)); // Mimic bug that doesn't count proxy header
309+
$this->curlMock
310+
->shouldReceive('version')
311+
->once()
312+
->andReturn(array('version_number' => self::CURL_VERSION_BUGGY));
313+
$this->curlMock
314+
->shouldReceive('exec')
315+
->once()
316+
->andReturn($rawHeader . $this->fakeRawBody);
317+
318+
$this->curlClient->sendRequest();
319+
list($rawHeaders, $rawBody) = $this->curlClient->extractResponseHeadersAndBody();
320+
321+
$this->assertEquals($rawHeaders, trim($rawHeader));
322+
$this->assertEquals($rawBody, $this->fakeRawBody);
323+
324+
$headers = FacebookCurlHttpClient::headersToArray($rawHeaders);
325+
326+
$this->assertEquals($headers, $this->fakeHeadersAsArray);
327+
}
328+
301329
public function testProperlyHandlesRedirectHeaders()
302330
{
303331
$rawHeader = $this->fakeRawRedirectHeader . $this->fakeRawHeader;

0 commit comments

Comments
 (0)