|
1
|
|
|
<?php |
|
2
|
|
|
namespace Qiniu; |
|
3
|
|
|
|
|
4
|
|
|
use Qiniu\Http\Client; |
|
5
|
|
|
use Qiniu\Http\Error; |
|
6
|
|
|
|
|
7
|
|
|
final class Zone |
|
8
|
|
|
{ |
|
9
|
18 |
|
public $ioHost; // 七牛源站Host |
|
10
|
|
|
public $upHost; |
|
11
|
18 |
|
public $upHostBackup; |
|
12
|
18 |
|
|
|
13
|
18 |
|
public $hostCache; //<scheme>:<ak>:<bucket> ==> array('deadline' => 'xxx', 'upHosts' => array(), 'ioHost' => 'xxx.com') |
|
14
|
|
|
public $scheme = 'http'; |
|
15
|
15 |
|
|
|
16
|
|
|
public function __construct($scheme = null) |
|
17
|
15 |
|
{ |
|
18
|
|
|
$this->hostCache = array(); |
|
19
|
|
|
if ($scheme != null) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->scheme = $scheme; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function getUpHostByToken($uptoken) |
|
26
|
|
|
{ |
|
27
|
|
|
list($ak, $bucket) = $this->unmarshalUpToken($uptoken); |
|
28
|
|
|
list($upHosts,) = $this->getUpHosts($ak, $bucket); |
|
29
|
|
|
return $upHosts[0]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getBackupUpHostByToken($uptoken) |
|
33
|
|
|
{ |
|
34
|
|
|
list($ak, $bucket) = $this->unmarshalUpToken($uptoken); |
|
35
|
|
|
list($upHosts,) = $this->getUpHosts($ak, $bucket); |
|
36
|
|
|
return $upHosts[1]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getIoHost($ak, $bucket) |
|
40
|
|
|
{ |
|
41
|
|
|
list($bucketHosts, ) = $this->getBucketHosts($ak, $bucket); |
|
42
|
|
|
$ioHost = $bucketHosts['ioHost']; |
|
|
|
|
|
|
43
|
|
|
return $upHost; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getUpHosts($ak, $bucket) |
|
47
|
|
|
{ |
|
48
|
|
|
list($bucketHosts, $err) = $this->getBucketHosts($ak, $bucket); |
|
49
|
|
|
if ($err !== null) |
|
50
|
|
|
{ |
|
51
|
|
|
return array(null, $err); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$upHosts = $bucketHosts['upHosts']; |
|
55
|
|
|
return array($upHosts, null); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function unmarshalUpToken($uptoken) |
|
59
|
|
|
{ |
|
60
|
|
|
$token = split(':', $uptoken); |
|
|
|
|
|
|
61
|
|
|
if (count($token) !== 3) |
|
62
|
|
|
{ |
|
63
|
|
|
throw new \Exception("Invalid Uptoken", 1); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$ak = $token[0]; |
|
67
|
|
|
$policy = base64_urlSafeDecode($token[2]); |
|
68
|
|
|
$policy = json_decode($policy, true); |
|
69
|
|
|
|
|
70
|
|
|
$bucket = $policy['scope']; |
|
71
|
|
|
if (strpos($bucket, ':')) |
|
72
|
|
|
{ |
|
73
|
|
|
$bucket = split(':', $bucket)[0]; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return array($ak, $bucket); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getBucketHosts($ak, $bucket) |
|
80
|
|
|
{ |
|
81
|
|
|
$key = $ak . $bucket; |
|
82
|
|
|
|
|
83
|
|
|
$exist = false; |
|
84
|
|
|
if (count($this->hostCache) > 0) |
|
85
|
|
|
{ |
|
86
|
|
|
$exist = array_key_exists($key, $this->hostCache) && $this->hostCache[$key]['deadline'] > time(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($exist) |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->hostCache[$key]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
list($hosts, $err) = $this->bucketHosts($ak, $bucket); |
|
95
|
|
|
if ($err !== null) |
|
96
|
|
|
{ |
|
97
|
|
|
return array(null , $err); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$schemeHosts = $hosts[$this->scheme]; |
|
101
|
|
|
$bucketHosts = array('upHosts' => $schemeHosts['up'], 'ioHost' => $schemeHosts['io'], 'deadline' => time() + $hosts['ttl']); |
|
102
|
|
|
|
|
103
|
|
|
$this->hostCache[$key] = $bucketHosts; |
|
104
|
|
|
return array($bucketHosts, null); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/* 请求包: |
|
109
|
|
|
* GET /v1/query?ak=<ak>&&bucket=<bucket> |
|
110
|
|
|
* 返回包: |
|
111
|
|
|
* |
|
112
|
|
|
* 200 OK { |
|
113
|
|
|
* "ttl": <ttl>, // 有效时间 |
|
114
|
|
|
* "http": { |
|
115
|
|
|
* "up": [], |
|
116
|
|
|
* "io": [], // 当bucket为global时,我们不需要iohost, io缺省 |
|
117
|
|
|
* }, |
|
118
|
|
|
* "https": { |
|
119
|
|
|
* "up": [], |
|
120
|
|
|
* "io": [], // 当bucket为global时,我们不需要iohost, io缺省 |
|
121
|
|
|
* } |
|
122
|
|
|
* } |
|
123
|
|
|
**/ |
|
124
|
|
|
private function bucketHosts($ak, $bucket) |
|
125
|
|
|
{ |
|
126
|
|
|
$path = '/v1/query' . "?ak=$ak&bucket=$bucket"; |
|
127
|
|
|
$ret = Client::Get(Config::UC_HOST . $path); |
|
128
|
|
|
if (!$ret->ok()) { |
|
129
|
|
|
return array(null, new Error($url, $ret)); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
|
132
|
|
|
return array($r, null); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.