Skip to content

Commit

Permalink
fix urlencode bug
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
sabakugaara committed Feb 16, 2017
1 parent c8a547d commit 8676e40
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/Upyun/Api/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Psr7;
use Upyun\Config;
use Upyun\Signature;
use Upyun\Util;

class Rest {
/**
Expand Down Expand Up @@ -61,13 +62,22 @@ public function send() {
if($this->file && $this->method === 'PUT') {
$body = $this->file;
}
// TODO urlencode path
$path = '/' . $this->config->bucketName . '/' . ltrim($this->storagePath, '/');

$authHeader = Signature::getHeaderSign($this->config, $this->method, $path);
$response = $client->request($this->method, $url, [
'headers' => array_merge($authHeader, $this->headers),
'body' => $body
$request = new Psr7\Request(
$this->method,
Util::encodeURI($url),
$this->headers,
$body
);
$authHeader = Signature::getHeaderSign($this->config,
$this->method,
$request->getUri()->getPath()
);
foreach($authHeader as $head => $value) {
$request = $request->withHeader($head, $value);
}
$response = $client->send($request, [
'debug' => $this->config->debug
]);

return $response;
Expand Down
5 changes: 5 additions & 0 deletions src/Upyun/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class Config {
*/
public $processNotifyUrl;

/**
* @var boolean curl debug
*/
public $debug = false;

private $version = '3.0.0';


Expand Down
22 changes: 21 additions & 1 deletion src/Upyun/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,24 @@ public static function md5Hash($resource) {
$md5 = hash_final($ctx);
return $md5;
}
}

/**
* GuzzleHttp\Psr\Uri use `parse_url`,not good for utf-8,
* So should `encodeURI` first, before `new Psr7\Request`
* @see http://stackoverflow.com/questions/4929584/encodeuri-in-php
*/
public static function encodeURI($url) {
$unescaped = array(
'%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
'%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
);
$reserved = array(
'%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
'%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
);
$score = array(
'%23'=>'#'
);
return strtr(rawurlencode($url), array_merge($reserved,$unescaped,$score));
}
}
4 changes: 2 additions & 2 deletions tests/UpyunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function tearDownAfterClass() {
}

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

0 comments on commit 8676e40

Please sign in to comment.