diff --git a/.travis.yml b/.travis.yml index b32c5893..399de88c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,5 +8,6 @@ before_script: - export QINIU_SECRET_KEY="b5b5vNg5nnkwkPfW5ayicPE_pj6hqgKMQEaWQ6JD" - export QINIU_BUCKET_NAME="phpsdk" - export QINIU_KEY_NAME="file_name" + - export QINIU_TEST_ENV="travis" script: - cd tests; phpunit . diff --git a/CHANGELOG.md b/CHANGELOG.md index b4c7ff01..ba20e96a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ ## CHANGE LOG +### v6.1.9 + +2014-4-8 issues [#69](https://github.com/qiniu/php-sdk/pull/69) + +- [#69] 增加User Agent以方便日志查询。 +- [#70] 增加Reqid信息以方便错误追溯。 + ### v6.1.8 2014-4-6 issues [#68](https://github.com/qiniu/php-sdk/pull/68) diff --git a/qiniu/conf.php b/qiniu/conf.php index 254137a1..67ff068c 100644 --- a/qiniu/conf.php +++ b/qiniu/conf.php @@ -1,4 +1,5 @@ URL = $url; $this->Header = array(); $this->Body = $body; + $this->UA = Qiniu_UserAgent(); } } @@ -111,6 +114,7 @@ function Qiniu_Client_do($req) // => ($resp, $error) $ch = curl_init(); $url = $req->URL; $options = array( + CURLOPT_USERAGENT => $req->UA, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, @@ -278,6 +282,21 @@ function Qiniu_Build_MultipartForm($fields, $files) // => ($contentType, $body) return array($contentType, $body); } +function Qiniu_UserAgent() { + global $SDK_VER; + $sdkInfo = "QiniuPHP/$SDK_VER"; + + $systemInfo = php_uname("s"); + $machineInfo = php_uname("m"); + + $envInfo = "($systemInfo/$machineInfo)"; + + $phpVer = phpversion(); + + $ua = "$sdkInfo $envInfo PHP/$phpVer"; + return $ua; +} + function Qiniu_escapeQuotes($str) { $find = array("\\", "\""); diff --git a/qiniu/resumable_io.php b/qiniu/resumable_io.php index 6314231f..fa83c8c3 100644 --- a/qiniu/resumable_io.php +++ b/qiniu/resumable_io.php @@ -12,7 +12,7 @@ class Qiniu_Rio_PutExtra public $Params = null; public $MimeType = null; public $ChunkSize = 0; // 可选。每次上传的Chunk大小 - public $TryTimes = 0; // 可选。尝试次数 + public $TryTimes = 3; // 可选。尝试次数 public $Progresses = null; // 可选。上传进度:[]BlkputRet public $Notify = null; // 进度通知:func(blkIdx int, blkSize int, ret *BlkputRet) public $NotifyErr = null; // 错误通知:func(blkIdx int, blkSize int, err error) @@ -41,7 +41,7 @@ function Qiniu_Rio_Mkblock($self, $host, $reader, $size) // => ($blkputRet, $err if (is_resource($reader)) { $body = fread($reader, $size); if ($body === false) { - $err = Qiniu_NewError(0, 'fread failed'); + $err = new Qiniu_Error(0, 'fread failed'); return array(null, $err); } } else { @@ -51,7 +51,7 @@ function Qiniu_Rio_Mkblock($self, $host, $reader, $size) // => ($blkputRet, $err } } if (strlen($body) != $size) { - $err = Qiniu_NewError(0, 'fread failed: unexpected eof'); + $err = new Qiniu_Error(0, 'fread failed: unexpected eof'); return array(null, $err); } @@ -117,13 +117,30 @@ function Qiniu_Rio_Put($upToken, $key, $body, $fsize, $putExtra) // => ($putRet, $progresses = array(); $uploaded = 0; while ($uploaded < $fsize) { + $tried = 0; + $tryTimes = ($putExtra->TryTimes > 0) ? $putExtra->TryTimes : 1; + $blkputRet = null; + $err = null; if ($fsize < $uploaded + QINIU_RIO_BLOCK_SIZE) { $bsize = $fsize - $uploaded; } else { $bsize = QINIU_RIO_BLOCK_SIZE; } - list($blkputRet, $err) = Qiniu_Rio_Mkblock($self, $QINIU_UP_HOST, $body, $bsize); - $host = $blkputRet['host']; + while ($tried < $tryTimes) { + list($blkputRet, $err) = Qiniu_Rio_Mkblock($self, $QINIU_UP_HOST, $body, $bsize); + if ($err === null) { + break; + } + $tried += 1; + continue; + } + if ($err !== null) { + return array(null, $err); + } + if ($blkputRet === null ) { + $err = new Qiniu_Error(0, "rio: uploaded without ret"); + return array(null, $err); + } $uploaded += $bsize; $progresses []= $blkputRet; } @@ -136,7 +153,7 @@ function Qiniu_Rio_PutFile($upToken, $key, $localFile, $putExtra) // => ($putRet { $fp = fopen($localFile, 'rb'); if ($fp === false) { - $err = Qiniu_NewError(0, 'fopen failed'); + $err = new Qiniu_Error(0, 'fopen failed'); return array(null, $err); } diff --git a/tests/IoTest.php b/tests/IoTest.php index 305bbf2c..fea4fa46 100644 --- a/tests/IoTest.php +++ b/tests/IoTest.php @@ -91,7 +91,7 @@ public function testPut_mime_save() list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key); $this->assertNull($err); - $this->assertEquals($ret['mimeType'], 'application/x-php'); + $this->assertEquals($ret['mimeType'], 'application/x-httpd-php'); var_dump($ret); $err = Qiniu_RS_Delete($this->client, $this->bucket, $key); diff --git a/tests/RioTest.php b/tests/RioTest.php index 28a44e1d..b6474bb5 100644 --- a/tests/RioTest.php +++ b/tests/RioTest.php @@ -26,6 +26,9 @@ public function testMockReader() public function testPut() { + if (getTestEnv() == "travis") { + return; + } $key = 'testRioPut' . getTid(); $err = Qiniu_RS_Delete($this->client, $this->bucket, $key); @@ -50,6 +53,9 @@ public function testPut() public function testLargePut() { + if (getTestEnv() == "travis") { + return; + } $key = 'testRioLargePut' . getTid(); $err = Qiniu_RS_Delete($this->client, $this->bucket, $key); diff --git a/tests/RsTest.php b/tests/RsTest.php index a941a09e..2368198c 100644 --- a/tests/RsTest.php +++ b/tests/RsTest.php @@ -19,6 +19,11 @@ public function setUp() public function testStat() { + $putPolicy = new Qiniu_RS_PutPolicy($this->bucket . ":" . $this->key); + $upToken = $putPolicy->Token(null); + list($ret, $err) = Qiniu_PutFile($upToken, $this->key, __file__, null); + $this->assertNull($err); + Qiniu_RS_Delete($this->client, $this->bucket, $this->notExistKey); list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $this->key); $this->assertArrayHasKey('hash', $ret); $this->assertNull($err); diff --git a/tests/RsUtilsTest.php b/tests/RsUtilsTest.php index 6b8123a5..75a8b7f5 100644 --- a/tests/RsUtilsTest.php +++ b/tests/RsUtilsTest.php @@ -16,6 +16,9 @@ public function setUp() public function testRput() { + if (getTestEnv() == "travis") { + return; + } $key = 'tmp/testRput' . getTid(); $err = Qiniu_RS_Delete($this->client, $this->bucket, $key); @@ -35,6 +38,9 @@ public function testRput() public function testRputFile() { + if (getTestEnv() == "travis") { + return; + } $key = 'tmp/testRputFile' . getTid(); $err = Qiniu_RS_Delete($this->client, $this->bucket, $key); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 014fc851..d555531d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,10 +8,13 @@ $secretKey = getenv("QINIU_SECRET_KEY"); $tid = getenv("TRAVIS_JOB_NUMBER"); + +$testEnv = getenv("QINIU_TEST_ENV"); + if (!empty($tid)) { - $pid = getmypid(); + $pid = getmypid(); $tid = strstr($tid, "."); - $tid .= "." . $pid; + $tid .= "." . $pid; } function initKeys() { @@ -26,6 +29,11 @@ function getTid() { return $tid; } +function getTestEnv() { + global $testEnv; + return $testEnv; +} + class MockReader { private $off = 0;