Skip to content

Commit ba3cad7

Browse files
authored
Pre-release 2.4.3 (#248)
* prerelease 2.4.1
1 parent afc332f commit ba3cad7

File tree

10 files changed

+90
-25
lines changed

10 files changed

+90
-25
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
cos-php-sdk-v5 Upgrade Guide
22
====================
3+
2.4.2 to 2.4.3
4+
---------
5+
- 优化审核接口返回相关字段
6+
- 增加host开关功能
7+
- GetObject接口支持万象自定义样式
8+
- 修复ip的host bug
9+
310
2.4.1 to 2.4.2
411
---------
512
- 修复图片处理相关接口403签名不对的bug

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"config": {
4343
"platform-check": false
4444
},
45-
4645
"scripts": {
4746
"test": [
4847
"@putenv XDEBUG_MODE=coverage",

src/Client.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
* @see \Qcloud\Cos\Service::getService()
115115
*/
116116
class Client extends GuzzleClient {
117-
const VERSION = '2.4.2';
117+
const VERSION = '2.4.3';
118118

119119
public $httpClient;
120120

@@ -133,7 +133,7 @@ public function __construct(array $cosConfig) {
133133
$this->cosConfig['appId'] = isset($cosConfig['credentials']['appId']) ? $cosConfig['credentials']['appId'] : null;
134134
$this->cosConfig['secretId'] = isset($cosConfig['credentials']['secretId']) ? $cosConfig['credentials']['secretId'] : '';
135135
$this->cosConfig['secretKey'] = isset($cosConfig['credentials']['secretKey']) ? $cosConfig['credentials']['secretKey'] : '';
136-
$this->cosConfig['anonymous'] = isset($cosConfig['credentials']['anonymous']) ? $cosConfig['anonymous']['anonymous'] : false;
136+
$this->cosConfig['anonymous'] = isset($cosConfig['credentials']['anonymous']) ? $cosConfig['credentials']['anonymous'] : false;
137137
$this->cosConfig['token'] = isset($cosConfig['credentials']['token']) ? $cosConfig['credentials']['token'] : null;
138138
$this->cosConfig['timeout'] = isset($cosConfig['timeout']) ? $cosConfig['timeout'] : 3600;
139139
$this->cosConfig['connect_timeout'] = isset($cosConfig['connect_timeout']) ? $cosConfig['connect_timeout'] : 3600;
@@ -145,6 +145,7 @@ public function __construct(array $cosConfig) {
145145
$this->cosConfig['retry'] = isset($cosConfig['retry']) ? $cosConfig['retry'] : 1;
146146
$this->cosConfig['userAgent'] = isset($cosConfig['userAgent']) ? $cosConfig['userAgent'] : 'cos-php-sdk-v5.'. Client::VERSION;
147147
$this->cosConfig['pathStyle'] = isset($cosConfig['pathStyle']) ? $cosConfig['pathStyle'] : false;
148+
$this->cosConfig['signHost'] = isset($cosConfig['signHost']) ? $cosConfig['signHost'] : true;
148149
$this->cosConfig['allow_redirects'] = isset($cosConfig['allow_redirects']) ? $cosConfig['allow_redirects'] : false;
149150
$this->cosConfig['allow_accelerate'] = isset($cosConfig['allow_accelerate']) ? $cosConfig['allow_accelerate'] : false;
150151

@@ -158,15 +159,15 @@ public function __construct(array $cosConfig) {
158159
return $request->withHeader('User-Agent', $this->cosConfig['userAgent']);
159160
}));
160161
if ($this->cosConfig['anonymous'] != true) {
161-
$handler->push($this::handleSignature($this->cosConfig['secretId'], $this->cosConfig['secretKey']));
162+
$handler->push($this::handleSignature($this->cosConfig['secretId'], $this->cosConfig['secretKey'], $this->cosConfig['signHost']));
162163
}
163164
if ($this->cosConfig['token'] != null) {
164165
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
165166
return $request->withHeader('x-cos-security-token', $this->cosConfig['token']);
166167
}));
167168
}
168169
$handler->push($this::handleErrors());
169-
$this->signature = new Signature(trim($this->cosConfig['secretId']), trim($this->cosConfig['secretKey']), trim($this->cosConfig['token']));
170+
$this->signature = new Signature(trim($this->cosConfig['secretId']), trim($this->cosConfig['secretKey']), $this->cosConfig, trim($this->cosConfig['token'] ));
170171
$area = $this->cosConfig['allow_accelerate'] ? 'accelerate' : $this->cosConfig['region'];
171172
$this->httpClient = new HttpClient([
172173
'base_uri' => $this->cosConfig['schema'].'://cos.' . $area . '.myqcloud.com/',
@@ -289,10 +290,6 @@ private function createPresignedUrl(RequestInterface $request, $expires) {
289290
return $this->signature->createPresignedUrl($request, $expires);
290291
}
291292

292-
public function getPresignetUrl($method, $args, $expires = "+30 minutes") {
293-
return $this->getPresignedUrl($method, $args, $expires);
294-
}
295-
296293
public function getPresignedUrl($method, $args, $expires = "+30 minutes") {
297294
$command = $this->getCommand($method, $args);
298295
$request = $this->commandToRequestTransformer($command);
@@ -460,9 +457,9 @@ public static function explodeKey($key) {
460457
}
461458

462459

463-
public static function handleSignature($secretId, $secretKey) {
464-
return function (callable $handler) use ($secretId, $secretKey) {
465-
return new SignatureMiddleware($handler, $secretId, $secretKey);
460+
public static function handleSignature($secretId, $secretKey, $signHost) {
461+
return function (callable $handler) use ($secretId, $secretKey, $signHost) {
462+
return new SignatureMiddleware($handler, $secretId, $secretKey, $signHost);
466463
};
467464
}
468465

src/CommandToRequestTransformer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\Command\CommandInterface;
77
use GuzzleHttp\Psr7\Uri;
88
use InvalidArgumentException;
9+
use Psr\Http\Message\UriInterface;
910

1011
class CommandToRequestTransformer {
1112
private $config;
@@ -96,7 +97,7 @@ public function bucketStyleTransformer( CommandInterface $command, RequestInterf
9697
}
9798
$uri = $uri->withQuery( $query );
9899
$request = $request->withUri( $uri );
99-
// $request = $request->withHeader( 'Host', $origin_host );
100+
$request = $request->withHeader( 'Host', $origin_host );
100101
return $request;
101102
}
102103

@@ -201,6 +202,10 @@ public function specialParamTransformer( CommandInterface $command, $request ) {
201202
public function ciParamTransformer( CommandInterface $command, $request ) {
202203
$action = $command->getName();
203204
if ( $action == 'GetObject' ) {
205+
if(str_contains($uri = $request->getUri(), '%21') ) {
206+
$uri = new Uri( str_replace('%21', '!', $uri) );
207+
$request = $request->withUri( $uri );
208+
}
204209
if(isset($command['ImageHandleParam']) && $command['ImageHandleParam']){
205210
$uri = $request->getUri();
206211
$query = $uri->getQuery();

src/Signature.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
use Psr\Http\Message\RequestInterface;
66

77
class Signature {
8-
private $accessKey;
98
// string: access key.
10-
private $secretKey;
9+
private $accessKey;
10+
1111
// string: secret key.
12+
private $secretKey;
13+
14+
// bool: host trigger
15+
private $signHost;
1216

13-
public function __construct( $accessKey, $secretKey, $token = null ) {
17+
public function __construct( $accessKey, $secretKey, $signHost, $token = null ) {
1418
$this->accessKey = $accessKey;
1519
$this->secretKey = $secretKey;
20+
$this->signHost = $signHost;
1621
$this->token = $token;
1722
$this->signHeader = [
1823
'cache-control',
@@ -69,12 +74,16 @@ public function createAuthorization( RequestInterface $request, $expires = '+30
6974
foreach ( explode( '&', $request->getUri()->getQuery() ) as $query ) {
7075
if (!empty($query)) {
7176
$tmpquery = explode( '=', $query );
72-
$key = strtolower( urlencode( $tmpquery[0] ));
77+
$key = strtolower( urlencode($tmpquery[0]) );
7378
if (count($tmpquery) >= 2) {
7479
$value = $tmpquery[1];
7580
} else {
7681
$value = "";
7782
}
83+
//host开关
84+
if (!$this->signHost && $key == 'host') {
85+
continue;
86+
}
7887
$urlParamListArray[$key] = $key. '='. $value;
7988
}
8089
}
@@ -86,6 +95,9 @@ public function createAuthorization( RequestInterface $request, $expires = '+30
8695
foreach ( $request->getHeaders() as $key => $value ) {
8796
$key = strtolower( urlencode( $key ) );
8897
$value = rawurlencode( $value[0] );
98+
if ( !$this->signHost && $key == 'host' ) {
99+
continue;
100+
}
89101
if ( $this->needCheckHeader( $key ) ) {
90102
$headerListArray[$key] = $key. '='. $value;
91103
}
@@ -106,7 +118,7 @@ public function createAuthorization( RequestInterface $request, $expires = '+30
106118
}
107119

108120
public function createPresignedUrl( RequestInterface $request, $expires = '+30 minutes' ) {
109-
$authorization = $this->createAuthorization( $request, $expires );
121+
$authorization = $this->createAuthorization( $request, $expires);
110122
$uri = $request->getUri();
111123
$query = 'sign='.urlencode( $authorization ) . '&' . $uri->getQuery();
112124
if ( $this->token != null ) {

src/SignatureMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class SignatureMiddleware {
1111
/**
1212
* @param callable $nextHandler Next handler to invoke.
1313
*/
14-
public function __construct(callable $nextHandler, $accessKey, $secretKey) {
14+
public function __construct(callable $nextHandler, $accessKey, $secretKey, $signHost) {
1515
$this->nextHandler = $nextHandler;
16-
$this->signature = new Signature($accessKey, $secretKey);
16+
$this->signature = new Signature($accessKey, $secretKey, $signHost);
1717
}
1818

1919
public function __invoke(RequestInterface $request, array $options) {

tests/Common.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,47 @@ public static function getCosClient()
6969
return $cosClient;
7070
}
7171

72+
public static function getCiClient() {
73+
try {
74+
$cosClient = new Client(
75+
array(
76+
'region' => self::getRegion(),
77+
'schema' => 'https',
78+
'credentials' => array(
79+
'secretId' => getenv('CI_KEY'),
80+
'secretKey' => getenv('CI_SECRET')
81+
)
82+
)
83+
);
84+
} catch (\Exception $e) {
85+
return null;
86+
}
87+
return $cosClient;
88+
}
89+
90+
public static function getCertainRegionClient($region) {
91+
try {
92+
$cosClient = new Client(
93+
array(
94+
'region' => $region,
95+
'schema' => 'https',
96+
'credentials' => array(
97+
'secretId' => getenv('CI_KEY'),
98+
'secretKey' => getenv('CI_SECRET')
99+
)
100+
)
101+
);
102+
} catch (\Exception $e) {
103+
return null;
104+
}
105+
return $cosClient;
106+
}
107+
108+
public static function getCiBucketName()
109+
{
110+
return getenv('CI_BUCKET');
111+
}
112+
72113

73114
public static function getBucketName()
74115
{

tests/CosClientBucketTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,11 @@ protected function setUp(): void
816816
$this->bucket2 = "tmp-".$this->bucket;
817817
$this->prBucket = 'public-read' . $this->bucket2;
818818
$this->hyphenBucket = '12345-'. $this->bucket;
819-
$this->doubleHyphenBucket = '12-333-4445' . $this->bucket;
819+
$this->doubleHyphenBucket = '12-333-4445' . $this->bucket2;
820820
}
821821

822822
protected function tearDown(): void {
823-
parent::setUp();
823+
parent::tearDown();
824824
try {
825825
$this->cosClient->deleteBucket(array('Bucket' => $this->bucket2));
826826
} catch(\Exception $e) {

tests/CosClientCiTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
22

3-
43
namespace Qcloud\Cos\Tests;
54

5+
require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestCosClientBase.php';
6+
7+
use Qcloud\Cos\Exception\ServiceResponseException;
8+
9+
use Qcloud\Cos\Client;
610

7-
class CosClientCiTest
11+
class CosClientCiTest extends TestCosClientBase
812
{
913
//TODO
1014
}

tests/CosClientObjectTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ protected function setUp(): void
972972
}
973973

974974
protected function tearDown(): void {
975-
parent::setUp();
975+
parent::tearDown();
976976
try {
977977
$this->cosClient->deleteObject(array('Bucket' => $this->bucket, 'Key' => $this->appendKey));
978978
} catch(\Exception $e) {

0 commit comments

Comments
 (0)