Skip to content

Commit 532b207

Browse files
Merge branch '4.4' into 5.4
* 4.4: [HttpClient] fix sending PUT requests with curl
2 parents f9bd075 + ea41b93 commit 532b207

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

AmpHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function request(string $method, string $url, array $options = []): Respo
9292
}
9393
}
9494

95-
if ('' !== $options['body'] && 'POST' === $method && !isset($options['normalized_headers']['content-type'])) {
95+
if (\is_string($options['body']) && '' !== $options['body'] && !isset($options['normalized_headers']['content-type'])) {
9696
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
9797
}
9898

CurlHttpClient.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,12 @@ public function request(string $method, string $url, array $options = []): Respo
246246
if ('POST' !== $method) {
247247
$curlopts[\CURLOPT_UPLOAD] = true;
248248
}
249-
} elseif ('' !== $body || 'POST' === $method) {
249+
} elseif ('' !== $body || 'POST' === $method || $hasContentLength) {
250250
$curlopts[\CURLOPT_POSTFIELDS] = $body;
251+
252+
if ('' === $body && !isset($options['normalized_headers']['content-type'])) {
253+
$curlopts[\CURLOPT_HTTPHEADER][] = 'Content-Type:';
254+
}
251255
}
252256

253257
if ($options['peer_fingerprint']) {

NativeHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function request(string $method, string $url, array $options = []): Respo
8383

8484
$options['body'] = self::getBodyAsString($options['body']);
8585

86-
if ('' !== $options['body'] && 'POST' === $method && !isset($options['normalized_headers']['content-type'])) {
86+
if ('' !== $options['body'] && !isset($options['normalized_headers']['content-type'])) {
8787
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
8888
}
8989

Tests/HttpClientTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,18 @@ public function testRedirectAfterPost()
408408
$this->assertSame(200, $response->getStatusCode());
409409
}
410410

411+
public function testEmptyPut()
412+
{
413+
$client = $this->getHttpClient(__FUNCTION__);
414+
415+
$response = $client->request('PUT', 'http://localhost:8057/post', [
416+
'headers' => ['Content-Length' => '0'],
417+
]);
418+
419+
$this->assertSame(200, $response->getStatusCode());
420+
$this->assertStringContainsString("\r\nContent-Length: ", $response->getInfo('debug'));
421+
}
422+
411423
public function testNullBody()
412424
{
413425
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)