Skip to content

Commit 8d10b8b

Browse files
committed
use qiniu authorization
1 parent 4db7bfe commit 8d10b8b

File tree

3 files changed

+59
-110
lines changed

3 files changed

+59
-110
lines changed

src/Qiniu/Auth.php

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -216,48 +216,16 @@ public function authorization($url, $body = null, $contentType = null)
216216

217217
public function authorizationV2($url, $method, $body = null, $contentType = null)
218218
{
219-
$urlItems = parse_url($url);
220-
$host = $urlItems['host'];
221-
222-
if (isset($urlItems['port'])) {
223-
$port = $urlItems['port'];
224-
} else {
225-
$port = '';
226-
}
227-
228-
$path = $urlItems['path'];
229-
if (isset($urlItems['query'])) {
230-
$query = $urlItems['query'];
231-
} else {
232-
$query = '';
233-
}
234-
235-
//write request uri
236-
$toSignStr = $method . ' ' . $path;
237-
if (!empty($query)) {
238-
$toSignStr .= '?' . $query;
239-
}
240-
241-
//write host and port
242-
$toSignStr .= "\nHost: " . $host;
243-
if (!empty($port)) {
244-
$toSignStr .= ":" . $port;
245-
}
246-
247-
//write content type
248-
if (!empty($contentType)) {
249-
$toSignStr .= "\nContent-Type: " . $contentType;
250-
}
251-
252-
$toSignStr .= "\n\n";
253-
254-
//write body
255-
if (!empty($body)) {
256-
$toSignStr .= $body;
257-
}
258-
259-
$sign = $this->sign($toSignStr);
260-
$auth = 'Qiniu ' . $sign;
261-
return array('Authorization' => $auth);
219+
$headers = null;
220+
$result = array();
221+
if ($contentType != null) {
222+
$headers = new Header(array(
223+
'Content-Type' => array($contentType),
224+
));
225+
$result['Content-Type'] = $contentType;
226+
}
227+
list($sign) = $this->signQiniuAuthorization($url, $method, $body, $headers);
228+
$result['Authorization'] = 'Qiniu ' . $sign;
229+
return $result;
262230
}
263231
}

src/Qiniu/Storage/BucketManager.php

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function listFiles(
150150
\Qiniu\setWithoutEmpty($query, 'limit', $limit);
151151
\Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
152152
$url = $this->getRsfHost() . '/list?' . http_build_query($query);
153-
return $this->get($url);
153+
return $this->getV2($url);
154154
}
155155

156156
/**
@@ -182,7 +182,7 @@ public function listFilesv2(
182182
\Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm);
183183
$path = '/v2/list?' . http_build_query($query);
184184
$url = $this->getRsfHost() . $path;
185-
$headers = $this->auth->authorization($url, null, 'application/x-www-form-urlencoded');
185+
$headers = $this->auth->authorizationV2($url, 'POST', null, 'application/x-www-form-urlencoded');
186186
$ret = Client::post($url, null, $headers);
187187
if (!$ret->ok()) {
188188
return array(null, new Error($url, $ret));
@@ -729,7 +729,7 @@ public function fetch($url, $bucket, $key = null)
729729
}
730730

731731
$url = $ioHost . $path;
732-
return $this->post($url, null);
732+
return $this->postV2($url, null);
733733
}
734734

735735
/**
@@ -809,13 +809,12 @@ public function asynchFetchStatus($zone, $id)
809809

810810
$url = $scheme . "api-" . $zone . ".qiniu.com/sisyphus/fetch?id=" . $id;
811811

812-
$response = $this->getV2($url);
812+
list($ret, $err) = $this->getV2($url);
813813

814-
if (!$response->ok()) {
815-
print("statusCode: " . $response->statusCode);
816-
return array(null, new Error($url, $response));
814+
if ($err != null) {
815+
return array(null, $err);
817816
}
818-
return array($response->json(), null);
817+
return array($ret, null);
819818
}
820819

821820

@@ -841,7 +840,7 @@ public function prefetch($bucket, $key)
841840
}
842841

843842
$url = $ioHost . $path;
844-
return $this->post($url, null);
843+
return $this->postV2($url, null);
845844
}
846845

847846
/**
@@ -922,70 +921,52 @@ private function getUcHost()
922921
private function rsPost($path, $body = null)
923922
{
924923
$url = $this->getRsHost() . $path;
925-
return $this->post($url, $body);
924+
return $this->postV2($url, $body);
926925
}
927926

928927
private function apiPost($path, $body = null)
929928
{
930929
$url = $this->getApiHost() . $path;
931-
return $this->post($url, $body);
930+
return $this->postV2($url, $body);
932931
}
933932

934933
private function ucPost($path, $body = null)
935934
{
936935
$url = $this->getUcHost() . $path;
937-
return $this->post($url, $body);
936+
return $this->postV2($url, $body);
938937
}
939938

940939
private function ucGet($path)
941940
{
942941
$url = $this->getUcHost() . $path;
943-
return $this->get($url);
942+
return $this->getV2($url);
944943
}
945944

946945
private function apiGet($path)
947946
{
948947
$url = $this->getApiHost() . $path;
949-
return $this->get($url);
948+
return $this->getV2($url);
950949
}
951950

952951
private function rsGet($path)
953952
{
954953
$url = $this->getRsHost() . $path;
955-
return $this->get($url);
954+
return $this->getV2($url);
956955
}
957956

958-
private function get($url)
957+
private function getV2($url)
959958
{
960-
$headers = $this->auth->authorization($url);
959+
$headers = $this->auth->authorizationV2($url, 'GET', null, 'application/x-www-form-urlencoded');
961960
$ret = Client::get($url, $headers);
962961
if (!$ret->ok()) {
963962
return array(null, new Error($url, $ret));
964963
}
965964
return array($ret->json(), null);
966965
}
967966

968-
private function getV2($url)
969-
{
970-
$headers = $this->auth->authorizationV2($url, 'GET');
971-
return Client::get($url, $headers);
972-
}
973-
974-
private function post($url, $body)
975-
{
976-
$headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
977-
$ret = Client::post($url, $body, $headers);
978-
if (!$ret->ok()) {
979-
return array(null, new Error($url, $ret));
980-
}
981-
$r = ($ret->body === null) ? array() : $ret->json();
982-
return array($r, null);
983-
}
984-
985967
private function postV2($url, $body)
986968
{
987-
$headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/json');
988-
$headers["Content-Type"] = 'application/json';
969+
$headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/x-www-form-urlencoded');
989970
$ret = Client::post($url, $body, $headers);
990971
if (!$ret->ok()) {
991972
return array(null, new Error($url, $ret));

0 commit comments

Comments
 (0)