Skip to content

Commit 8676e40

Browse files
committed
fix urlencode bug
- utf-8 encode wrong before: `测试 +.txt` -> `%E6%B5_%E8%AF_%20+.txt` now: `测试 +.txt` -> `%E6%B5%8B%E8%AF%95%20+.txt` - add debug mode in config, default is false
1 parent c8a547d commit 8676e40

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

Diff for: src/Upyun/Api/Rest.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\Psr7;
77
use Upyun\Config;
88
use Upyun\Signature;
9+
use Upyun\Util;
910

1011
class Rest {
1112
/**
@@ -61,13 +62,22 @@ public function send() {
6162
if($this->file && $this->method === 'PUT') {
6263
$body = $this->file;
6364
}
64-
// TODO urlencode path
65-
$path = '/' . $this->config->bucketName . '/' . ltrim($this->storagePath, '/');
6665

67-
$authHeader = Signature::getHeaderSign($this->config, $this->method, $path);
68-
$response = $client->request($this->method, $url, [
69-
'headers' => array_merge($authHeader, $this->headers),
70-
'body' => $body
66+
$request = new Psr7\Request(
67+
$this->method,
68+
Util::encodeURI($url),
69+
$this->headers,
70+
$body
71+
);
72+
$authHeader = Signature::getHeaderSign($this->config,
73+
$this->method,
74+
$request->getUri()->getPath()
75+
);
76+
foreach($authHeader as $head => $value) {
77+
$request = $request->withHeader($head, $value);
78+
}
79+
$response = $client->send($request, [
80+
'debug' => $this->config->debug
7181
]);
7282

7383
return $response;

Diff for: src/Upyun/Config.php

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class Config {
5656
*/
5757
public $processNotifyUrl;
5858

59+
/**
60+
* @var boolean curl debug
61+
*/
62+
public $debug = false;
63+
5964
private $version = '3.0.0';
6065

6166

Diff for: src/Upyun/Util.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,24 @@ public static function md5Hash($resource) {
5656
$md5 = hash_final($ctx);
5757
return $md5;
5858
}
59-
}
59+
60+
/**
61+
* GuzzleHttp\Psr\Uri use `parse_url`,not good for utf-8,
62+
* So should `encodeURI` first, before `new Psr7\Request`
63+
* @see http://stackoverflow.com/questions/4929584/encodeuri-in-php
64+
*/
65+
public static function encodeURI($url) {
66+
$unescaped = array(
67+
'%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
68+
'%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
69+
);
70+
$reserved = array(
71+
'%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
72+
'%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
73+
);
74+
$score = array(
75+
'%23'=>'#'
76+
);
77+
return strtr(rawurlencode($url), array_merge($reserved,$unescaped,$score));
78+
}
79+
}

Diff for: tests/UpyunTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function tearDownAfterClass() {
2929
}
3030

3131
public function testWriteString() {
32-
$filename = 'test.txt';
32+
$filename = '/中文/测试 +.txt';
3333
$content = 'test file content';
3434
self::$upyun->write($filename, $content);
3535
$size = getUpyunFileSize($filename);
@@ -215,4 +215,4 @@ public function testQueryProcessResult() {
215215
$this->assertTrue($result[self::$taskId]['path'][0] === '/video/result.mp4');
216216
$this->assertTrue($result[self::$taskId]['status_code'] === 200);
217217
}
218-
}
218+
}

0 commit comments

Comments
 (0)