Skip to content

Commit 4d3a81f

Browse files
committed
#29: Request not using CURLOPT_POSTFIELDS have content-length set to 0
1 parent 13a895a commit 4d3a81f

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/Client.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -317,20 +317,21 @@ private function addRequestBodyOptions(RequestInterface $request, array $options
317317
private function createHeaders(RequestInterface $request, array $options)
318318
{
319319
$curlHeaders = [];
320-
$headers = array_keys($request->getHeaders());
321-
foreach ($headers as $name) {
320+
$headers = $request->getHeaders();
321+
foreach ($headers as $name => $values) {
322322
$header = strtolower($name);
323323
if ('expect' === $header) {
324324
// curl-client does not support "Expect-Continue", so dropping "expect" headers
325325
continue;
326326
}
327327
if ('content-length' === $header) {
328-
$values = [0];
329328
if (array_key_exists(CURLOPT_POSTFIELDS, $options)) {
329+
// Small body content length can be calculated here.
330330
$values = [strlen($options[CURLOPT_POSTFIELDS])];
331+
} elseif (!array_key_exists(CURLOPT_READFUNCTION, $options)) {
332+
// Else if there is no body, forcing "Content-length" to 0
333+
$values = [0];
331334
}
332-
} else {
333-
$values = $request->getHeader($name);
334335
}
335336
foreach ($values as $value) {
336337
$curlHeaders[] = $name . ': ' . $value;

tests/HttpClientTestCase.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,21 @@ public function testSendLargeFile()
7878
$request = self::$messageFactory->createRequest(
7979
'POST',
8080
PHPUnitUtility::getUri(),
81-
[],
81+
['content-length' => 1024 * 2048],
8282
$body
8383
);
8484

8585
$response = $this->httpAdapter->sendRequest($request);
8686
$this->assertResponse(
8787
$response,
8888
[
89-
'body' => 'Ok',
89+
'body' => 'Ok'
9090
]
9191
);
92+
93+
$request = $this->getRequest();
94+
self::assertArrayHasKey('CONTENT_LENGTH', $request['SERVER']);
95+
self::assertEquals($body->getSize(), $request['SERVER']['CONTENT_LENGTH']);
9296
}
9397

9498
/**

0 commit comments

Comments
 (0)