Skip to content

Commit 970bfe2

Browse files
alexkartsamdark
authored andcommitted
Fixes #189: Fixed Content-Length header when using CURLOPT_INFILE option
1 parent d0c7818 commit 970bfe2

5 files changed

+35
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Yii Framework 2 HTTP client extension Change Log
44
2.0.11 under development
55
------------------------
66

7-
- no changes in this release.
7+
- Bug #189: Fixed Content-Length header when using `CURLOPT_INFILE` option (alexkart)
88

99

1010
2.0.10 April 30, 2019

src/CurlTransport.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ private function prepare($request)
144144

145145
if ($method === 'HEAD') {
146146
$curlOptions[CURLOPT_NOBODY] = true;
147-
} else {
147+
}
148+
149+
if ($content !== null) {
148150
$curlOptions[CURLOPT_POSTFIELDS] = $content;
149151
}
150152

src/UrlEncodedFormatter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public function format(Request $request)
6060

6161
if (isset($content)) {
6262
$request->setContent($content);
63-
} else {
63+
}
64+
65+
if (!isset($content) && !isset($request->getOptions()[CURLOPT_INFILE])) {
6466
$request->getHeaders()->set('Content-Length', '0');
6567
}
6668

tests/CurlTransportTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public function testPreparePostRequestWithEmptyBody()
7878

7979
$expectedCurlOptions = [
8080
CURLOPT_POST => true,
81-
CURLOPT_POSTFIELDS => null,
8281
CURLOPT_RETURNTRANSFER => true,
8382
CURLOPT_URL => 'http://app.test/full/url',
8483
CURLOPT_HTTPHEADER => [
@@ -107,7 +106,6 @@ public function testPrepareRequestWithOptions()
107106
$curlOptions = $this->invoke($transport, 'prepare', [$request]);
108107

109108
$expectedCurlOptions = [
110-
CURLOPT_POSTFIELDS => null,
111109
CURLOPT_RETURNTRANSFER => true,
112110
CURLOPT_URL => 'http://app.test/full/url',
113111
CURLOPT_HTTPHEADER => [],
@@ -163,7 +161,6 @@ public function testPrepareGetRequestWithOutputFile()
163161
CURLOPT_URL => 'http://app.test/full/url',
164162
CURLOPT_HTTPHEADER => [],
165163
CURLOPT_CUSTOMREQUEST => 'GET',
166-
CURLOPT_POSTFIELDS => null,
167164
CURLOPT_FILE => 'file_handle'
168165
];
169166

tests/UrlEncodedFormatterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,32 @@ public function testFormatPostRequestWithEmptyBody()
7575

7676
$this->assertEquals('0', $headers['content-length'][0]);
7777
}
78+
79+
public function testFormatPutRequestWithInfileOption()
80+
{
81+
$fh = fopen(__DIR__ . '/test_file.txt', 'r');
82+
83+
$request = new Request();
84+
$request->setMethod('PUT');
85+
$request->setOptions([
86+
CURLOPT_INFILE => $fh,
87+
CURLOPT_INFILESIZE => filesize(__DIR__ . '/test_file.txt'),
88+
CURLOPT_BINARYTRANSFER => true,
89+
CURLOPT_PUT => 1,
90+
]);
91+
92+
$formatter = new UrlEncodedFormatter();
93+
$formatter->format($request);
94+
95+
$headers = $request->getHeaders()->toArray();
96+
97+
$expectedHeaders = [
98+
'content-type' =>
99+
[
100+
0 => 'application/x-www-form-urlencoded; charset=UTF-8',
101+
],
102+
];
103+
104+
$this->assertEquals($expectedHeaders, $headers);
105+
}
78106
}

0 commit comments

Comments
 (0)