Skip to content

Commit 83f2e8a

Browse files
authored
Merge pull request #21 from xaviered/xaviered/bug_fix_append_command
Auto-adding content-length heading to fix append-command bug
2 parents e1160af + 20298b3 commit 83f2e8a

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.buildpath
22
.project
33
.settings/
4+
.idea/
45
vendor/
56
composer.lock

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "PHP WebHDFS, forked from https://github.com/simpleenergy/php-WebHDFS",
44
"minimum-stability": "stable",
55
"license": "MIT",
6-
"version": "1.0.6",
6+
"version": "1.0.7",
77
"authors": [
88
{
99
"name": "tranch-xiao",

src/org/apache/hadoop/tools/Curl.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ public function __construct($debug = false)
1717
{
1818
$this->debug = $debug;
1919
}
20+
2021
/**
2122
*
2223
* @param $localPath string local file path to save
2324
*/
24-
public function setOption($key,$value) {
25+
public function setOption($key, $value)
26+
{
2527
$this->options[$key] = $value;
2628
}
2729

@@ -194,13 +196,36 @@ private function _exec($options, $returnInfo = false)
194196
}
195197
if (isset($this->options['local_file_handler'])) {
196198
$fp = $this->options['local_file_handler'];
197-
flock($fp,LOCK_EX);
199+
flock($fp, LOCK_EX);
198200
$options[CURLOPT_WRITEFUNCTION] = function ($ch, $string) use ($fp) {
199201
$length = fwrite($fp, $string);
202+
200203
return $length;
201204
};
202205
fflush($fp);
203206
}
207+
// auto add content-length header
208+
$has_content_length_header = false;
209+
foreach ($options[CURLOPT_HTTPHEADER] as $header) {
210+
if (stripos($header, 'content-length:') === 0) {
211+
$has_content_length_header = true;
212+
break;
213+
}
214+
}
215+
if (!$has_content_length_header && !isset($options[CURLOPT_INFILE]) && !isset($options[CURLOPT_INFILESIZE])) {
216+
if (isset($options[CURLOPT_POSTFIELDS])) {
217+
// only for string content
218+
if (is_string($options[CURLOPT_POSTFIELDS]) && strpos($options[CURLOPT_POSTFIELDS], '@') !== 0) {
219+
$options[CURLOPT_HTTPHEADER] = array_merge(
220+
$options[CURLOPT_HTTPHEADER],
221+
['Content-Length: '.strlen($options[CURLOPT_POSTFIELDS])]
222+
);
223+
}
224+
} else {
225+
$options[CURLOPT_HTTPHEADER] = array_merge($options[CURLOPT_HTTPHEADER], ['Content-Length: 0']);
226+
}
227+
}
228+
204229
curl_setopt_array($ch, $options);
205230
// force clean memory before getting more data
206231
$this->cleanLastRequest();

0 commit comments

Comments
 (0)