Skip to content

Commit fc115b5

Browse files
committed
make uc host configurable
1 parent aa83e48 commit fc115b5

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

src/Qiniu/Config.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class Config
3030
public $zone;
3131
// Zone Cache
3232
private $regionCache;
33+
// UC Host
34+
private $ucHost;
3335

3436
// 构造函数
3537
public function __construct(Region $z = null)
@@ -38,6 +40,23 @@ public function __construct(Region $z = null)
3840
$this->useHTTPS = false;
3941
$this->useCdnDomains = false;
4042
$this->regionCache = array();
43+
$this->ucHost = Config::UC_HOST;
44+
}
45+
46+
public function setUcHost($ucHost)
47+
{
48+
$this->ucHost = $ucHost;
49+
}
50+
51+
public function getUcHost()
52+
{
53+
if ($this->useHTTPS === true) {
54+
$scheme = "https://";
55+
} else {
56+
$scheme = "http://";
57+
}
58+
59+
return $scheme . $this->ucHost;
4160
}
4261

4362
public function getUpHost($accessKey, $bucket)
@@ -289,7 +308,7 @@ private function getRegion($accessKey, $bucket)
289308
return $regionCache;
290309
}
291310

292-
$region = Zone::queryZone($accessKey, $bucket);
311+
$region = Zone::queryZone($accessKey, $bucket, $this->getUcHost());
293312
if (is_array($region)) {
294313
list($region, $err) = $region;
295314
if ($err != null) {
@@ -313,7 +332,7 @@ private function getRegionV2($accessKey, $bucket)
313332
return array($regionCache, null);
314333
}
315334

316-
$region = Zone::queryZone($accessKey, $bucket);
335+
$region = Zone::queryZone($accessKey, $bucket, $this->getUcHost());
317336
if (is_array($region)) {
318337
list($region, $err) = $region;
319338
return array($region, $err);

src/Qiniu/Region.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,15 @@ public static function regionSeoul()
169169
}
170170

171171
/*
172-
* GET /v2/query?ak=<ak>&&bucket=<bucket>
172+
* GET /v2/query?ak=<ak>&bucket=<bucket>
173173
**/
174-
public static function queryRegion($ak, $bucket)
174+
public static function queryRegion($ak, $bucket, $ucHost = null)
175175
{
176176
$Region = new Region();
177-
$url = 'https://' . Config::UC_HOST . '/v2/query' . "?ak=$ak&bucket=$bucket";
177+
if (!$ucHost) {
178+
$ucHost = "https://" . Config::UC_HOST;
179+
}
180+
$url = $ucHost . '/v2/query' . "?ak=$ak&bucket=$bucket";
178181
$ret = Client::Get($url);
179182
if (!$ret->ok()) {
180183
return array(null, new Error($url, $ret));
@@ -200,18 +203,18 @@ public static function queryRegion($ak, $bucket)
200203
}
201204

202205
//set specific hosts
203-
if (isset($r['rs']['acc']['main'])) {
204-
$Region->rsHost = $r['rs']['acc']['main'];
206+
if (isset($r['rs']['acc']['main']) && count($r['rs']['acc']['main']) > 0) {
207+
$Region->rsHost = $r['rs']['acc']['main'][0];
205208
} else {
206209
$Region->rsHost = Config::RS_HOST;
207210
}
208-
if (isset($r['rs']['rsf']['main'])) {
209-
$Region->rsfHost = $r['rs']['rsf']['main'];
211+
if (isset($r['rsf']['acc']['main']) && count($r['rsf']['acc']['main']) > 0) {
212+
$Region->rsfHost = $r['rsf']['acc']['main'][0];
210213
} else {
211214
$Region->rsfHost = Config::RSF_HOST;
212215
}
213-
if (isset($r['rs']['api']['main'])) {
214-
$Region->apiHost = $r['rs']['api']['main'];
216+
if (isset($r['api']['acc']['main']) && count($r['api']['acc']['main']) > 0) {
217+
$Region->apiHost = $r['api']['acc']['main'][0];
215218
} else {
216219
$Region->apiHost = Config::API_HOST;
217220
}

src/Qiniu/Storage/BucketManager.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function buckets($shared = true)
3939
if ($shared === true) {
4040
$includeShared = "true";
4141
}
42-
return $this->getV2($this->getUcHost(). '/buckets?shared=' . $includeShared);
42+
return $this->getV2($this->config->getUcHost(). '/buckets?shared=' . $includeShared);
4343
}
4444

4545
/**
@@ -71,7 +71,7 @@ public function listbuckets(
7171
public function createBucket($name, $region = 'z0')
7272
{
7373
$path = '/mkbucketv3/' . $name . '/region/' . $region;
74-
return $this->postV2($this->getUcHost() . $path, null);
74+
return $this->postV2($this->config->getUcHost() . $path, null);
7575
}
7676

7777
/**
@@ -85,7 +85,7 @@ public function createBucket($name, $region = 'z0')
8585
public function deleteBucket($name)
8686
{
8787
$path = '/drop/' . $name;
88-
return $this->postV2($this->getUcHost() . $path, null);
88+
return $this->postV2($this->config->getUcHost() . $path, null);
8989
}
9090

9191
/**
@@ -994,15 +994,6 @@ public function setObjectLifecycleWithCond(
994994
return $this->rsPost($bucket, $path);
995995
}
996996

997-
private function getUcHost()
998-
{
999-
$scheme = "http://";
1000-
if ($this->config->useHTTPS === true) {
1001-
$scheme = "https://";
1002-
}
1003-
return $scheme . Config::UC_HOST;
1004-
}
1005-
1006997
private function rsfGet($bucket, $path)
1007998
{
1008999
list($host, $err) = $this->config->getRsfHostV2($this->auth->getAccessKey(), $bucket);
@@ -1061,13 +1052,13 @@ private function apiPost($bucket, $path, $body = null)
10611052

10621053
private function ucGet($path)
10631054
{
1064-
$url = $this->getUcHost() . $path;
1055+
$url = $this->config->getUcHost() . $path;
10651056
return $this->getV2($url);
10661057
}
10671058

10681059
private function ucPost($path, $body = null)
10691060
{
1070-
$url = $this->getUcHost() . $path;
1061+
$url = $this->config->getUcHost() . $path;
10711062
return $this->postV2($url, $body);
10721063
}
10731064

src/Qiniu/Zone.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public static function qvmZonez1()
5050
return parent::qvmRegionHuabei();
5151
}
5252

53-
public static function queryZone($ak, $bucket)
53+
public static function queryZone($ak, $bucket, $ucHost = null)
5454
{
55-
return parent::queryRegion($ak, $bucket);
55+
return parent::queryRegion($ak, $bucket, $ucHost);
5656
}
5757
}

tests/Qiniu/Tests/ConfigTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,19 @@ public function testGetApiHostV2Errored()
5858
$this->assertEquals(631, $err->code());
5959
$this->assertNull($apiHost);
6060
}
61+
62+
public function testSetUcHost()
63+
{
64+
$conf = new Config();
65+
$this->assertEquals("http://uc.qbox.me", $conf->getUcHost());
66+
$conf->setUcHost("uc.example.com");
67+
$this->assertEquals("http://uc.example.com", $conf->getUcHost());
68+
69+
$conf = new Config();
70+
$conf->useHTTPS = true;
71+
$this->assertEquals("https://uc.qbox.me", $conf->getUcHost());
72+
$conf->setUcHost("uc.example.com");
73+
$this->assertEquals("https://uc.example.com", $conf->getUcHost());
74+
}
6175
}
6276
}

0 commit comments

Comments
 (0)