Skip to content

Commit

Permalink
Merge pull request #201 from rwifeng/error_handle
Browse files Browse the repository at this point in the history
host query add error param
  • Loading branch information
longbai authored Nov 13, 2016
2 parents 8100792 + d46b2d2 commit 4da4eb0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/Qiniu/Storage/FormUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public static function put(
}
}

$upHost = $config->zone->getUpHostByToken($upToken);
list($upHost, $err) = $config->zone->getUpHostByToken($upToken);
if ($err != null) {
return array(null, $err);
}

$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
if (!$response->ok()) {
return array(null, new Error($upHost, $response));
Expand Down Expand Up @@ -99,7 +103,11 @@ public static function putFile(
$fields['key'] = $key;
$headers =array('Content-Type' => 'multipart/form-data');

$upHost = $config->zone->getUpHostByToken($upToken);
list($upHost, $err) = $config->zone->getUpHostByToken($upToken);
if ($err != null) {
return array(null, $err);
}

$response = client::post($upHost, $fields, $headers);
if (!$response->ok()) {
return array(null, new Error($upHost, $response));
Expand Down
13 changes: 11 additions & 2 deletions src/Qiniu/Storage/ResumeUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public function __construct(
$this->mime = $mime;
$this->contexts = array();
$this->config = $config;
$this->host = $config->zone->getUpHostByToken($upToken);

list($upHost, $err) = $config->zone->getUpHostByToken($upToken);
if ($err != null) {
throw new \Exception($err, 1);
}
$this->host = $upHost;
}

/**
Expand All @@ -76,7 +81,11 @@ public function upload()
$ret = $response->json();
}
if ($response->statusCode < 0) {
$this->host = $this->config->zone->getBackupUpHostByToken($this->upToken);
list($bakHost, $err) = $this->config->zone->getBackupUpHostByToken($this->upToken);
if ($err != null) {
return array(null, $err);
}
$this->host = $bakHost;
}
if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
$response = $this->makeBlock($data, $blockSize);
Expand Down
8 changes: 4 additions & 4 deletions src/Qiniu/Zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public function __construct($scheme = null)
public function getUpHostByToken($uptoken)
{
list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
list($upHosts,) = $this->getUpHosts($ak, $bucket);
return $upHosts[0];
list($upHosts, $err) = $this->getUpHosts($ak, $bucket);
return array($upHosts[0], $err);
}

public function getBackupUpHostByToken($uptoken)
{
list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
list($upHosts,) = $this->getUpHosts($ak, $bucket);
list($upHosts, $err) = $this->getUpHosts($ak, $bucket);

$upHost = isset($upHosts[1]) ? $upHosts[1] : $upHosts[0];
return $upHost;
return array($upHost, $err);
}

public function getIoHost($ak, $bucket)
Expand Down
42 changes: 38 additions & 4 deletions tests/Qiniu/Tests/ZoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ZoneTest extends \PHPUnit_Framework_TestCase

protected $bucketName;
protected $bucketNameBC;
protected $bucketNameNA;


protected function setUp()
Expand All @@ -22,6 +23,9 @@ protected function setUp()
global $bucketNameBC;
$this->bucketNameBC = $bucketNameBC;

global $bucketNameNA;
$this->bucketNameNA = $bucketNameNA;

global $accessKey;
$this->ak = $accessKey;

Expand All @@ -32,23 +36,37 @@ protected function setUp()
public function testUpHosts()
{

// test nb http
list($upHosts, $err) = $this->zone->getUpHosts($this->ak, $this->bucketName);
$this->assertNull($err);
$this->assertEquals('http://up.qiniu.com', $upHosts[0]);
$this->assertEquals('http://upload.qiniu.com', $upHosts[1]);

// test bc http
list($upHosts, $err) = $this->zone->getUpHosts($this->ak, $this->bucketNameBC);
$this->assertNull($err);
$this->assertEquals('http://up-z1.qiniu.com', $upHosts[0]);
$this->assertEquals('http://upload-z1.qiniu.com', $upHosts[1]);

// test na http
list($upHosts, $err) = $this->zone->getUpHosts($this->ak, $this->bucketNameNA);
$this->assertNull($err);
$this->assertEquals('http://up-na0.qiniu.com', $upHosts[0]);

// test nb https
list($upHosts, $err) = $this->zoneHttps->getUpHosts($this->ak, $this->bucketName);
$this->assertNull($err);
$this->assertEquals('https://up.qbox.me', $upHosts[0]);

// test bc https
list($upHosts, $err) = $this->zoneHttps->getUpHosts($this->ak, $this->bucketNameBC);
$this->assertNull($err);
$this->assertEquals('https://up-z1.qbox.me', $upHosts[0]);

// test na https
list($upHosts, $err) = $this->zoneHttps->getUpHosts($this->ak, $this->bucketNameNA);
$this->assertNull($err);
$this->assertEquals('https://up-na0.qbox.me', $upHosts[0]);
}

public function testUpHostByToken()
Expand All @@ -58,36 +76,52 @@ public function testUpHostByToken()
3RzIjpbImh0dHA6XC9cL3VwLXoxLnFpbml1LmNvbSIsImh0dHA6XC9cL3VwbG9hZC16MS5xaW5p
dS5jb20iLCItSCB1cC16MS5xaW5pdS5jb20gaHR0cDpcL1wvMTA2LjM4LjIyNy4yNyJdfQ==';

$upHost = $this->zone->getUpHostByToken($uptoken_bc);
list($upHost, $err) = $this->zone->getUpHostByToken($uptoken_bc);
$this->assertEquals('http://up-z1.qiniu.com', $upHost);
$this->assertEquals(null, $err);

$upHostBackup = $this->zone->getBackupUpHostByToken($uptoken_bc);
list($upHostBackup, $err) = $this->zone->getBackupUpHostByToken($uptoken_bc);
$this->assertEquals('http://upload-z1.qiniu.com', $upHostBackup);
$this->assertEquals(null, $err);


$uptoken_bc_https = 'QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm:7I47O-vFcN5TKO
6D7cobHPVkyIA=:eyJzY29wZSI6InBocHNkay1iYyIsImRlYWRsaW5lIjoxNDcwNzIyNzQ1LCJ1c
Ehvc3RzIjpbImh0dHBzOlwvXC91cC16MS5xYm94Lm1lIl19';
$upHost = $this->zoneHttps->getUpHostByToken($uptoken_bc_https);
list($upHost, $err) = $this->zoneHttps->getUpHostByToken($uptoken_bc_https);
$this->assertEquals('https://up-z1.qbox.me', $upHost);
$this->assertEquals(null, $err);

$upHostBackup = $this->zoneHttps->getBackupUpHostByToken($uptoken_bc_https);
list($upHostBackup, $err) = $this->zoneHttps->getBackupUpHostByToken($uptoken_bc_https);
$this->assertEquals('https://up-z1.qbox.me', $upHostBackup);
$this->assertEquals(null, $err);
}

public function testIoHosts()
{

// test nb http
$ioHost = $this->zone->getIoHost($this->ak, $this->bucketName);
$this->assertEquals('http://iovip.qbox.me', $ioHost);

// test bc http
$ioHost = $this->zone->getIoHost($this->ak, $this->bucketNameBC);
$this->assertEquals('http://iovip-z1.qbox.me', $ioHost);

// test na http
$ioHost = $this->zone->getIoHost($this->ak, $this->bucketNameNA);
$this->assertEquals('http://iovip-na0.qbox.me', $ioHost);

// test nb https
$ioHost = $this->zoneHttps->getIoHost($this->ak, $this->bucketName);
$this->assertEquals('https://iovip.qbox.me', $ioHost);

// test bc https
$ioHost = $this->zoneHttps->getIoHost($this->ak, $this->bucketNameBC);
$this->assertEquals('https://iovip-z1.qbox.me', $ioHost);

// test na https
$ioHost = $this->zoneHttps->getIoHost($this->ak, $this->bucketNameNA);
$this->assertEquals('https://iovip-na0.qbox.me', $ioHost);
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
$bucketName = 'phpsdk';
$key = 'php-logo.png';
$bucketNameBC = 'phpsdk-bc';
$bucketNameNA = 'phpsdk-na';

$dummyAccessKey = 'abcdefghklmnopq';
$dummySecretKey = '1234567890';
Expand Down

0 comments on commit 4da4eb0

Please sign in to comment.