Skip to content

Commit 3cc4f0b

Browse files
committed
Merge pull request #69 from dtynn/feature/useragent
user agent
2 parents 0c46361 + c0c2cc3 commit 3cc4f0b

9 files changed

+80
-8
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ before_script:
88
- export QINIU_SECRET_KEY="b5b5vNg5nnkwkPfW5ayicPE_pj6hqgKMQEaWQ6JD"
99
- export QINIU_BUCKET_NAME="phpsdk"
1010
- export QINIU_KEY_NAME="file_name"
11+
- export QINIU_TEST_ENV="travis"
1112
script:
1213
- cd tests; phpunit .

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## CHANGE LOG
22

3+
### v6.1.9
4+
5+
2014-4-8 issues [#69](https://github.com/qiniu/php-sdk/pull/69)
6+
7+
- [#69] 增加User Agent以方便日志查询。
8+
- [#70] 增加Reqid信息以方便错误追溯。
9+
310
### v6.1.8
411

512
2014-4-6 issues [#68](https://github.com/qiniu/php-sdk/pull/68)

qiniu/conf.php

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
global $SDK_VER;
23

34
global $QINIU_UP_HOST;
45
global $QINIU_RS_HOST;
@@ -7,6 +8,8 @@
78
global $QINIU_ACCESS_KEY;
89
global $QINIU_SECRET_KEY;
910

11+
$SDK_VER = "6.1.9";
12+
1013
$QINIU_UP_HOST = 'http://up.qiniu.com';
1114
$QINIU_RS_HOST = 'http://rs.qbox.me';
1215
$QINIU_RSF_HOST = 'http://rsf.qbox.me';

qiniu/http.php

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
require_once("auth_digest.php");
4+
require_once("conf.php");
45

56
// --------------------------------------------------------------------------------
67
// class Qiniu_Error
@@ -27,12 +28,14 @@ class Qiniu_Request
2728
public $URL;
2829
public $Header;
2930
public $Body;
31+
public $UA;
3032

3133
public function __construct($url, $body)
3234
{
3335
$this->URL = $url;
3436
$this->Header = array();
3537
$this->Body = $body;
38+
$this->UA = Qiniu_UserAgent();
3639
}
3740
}
3841

@@ -113,6 +116,7 @@ function Qiniu_Client_do($req) // => ($resp, $error)
113116
$ch = curl_init();
114117
$url = $req->URL;
115118
$options = array(
119+
CURLOPT_USERAGENT => $req->UA,
116120
CURLOPT_RETURNTRANSFER => true,
117121
CURLOPT_SSL_VERIFYPEER => false,
118122
CURLOPT_SSL_VERIFYHOST => false,
@@ -308,6 +312,21 @@ function Qiniu_Build_MultipartForm($fields, $files) // => ($contentType, $body)
308312
return array($contentType, $body);
309313
}
310314

315+
function Qiniu_UserAgent() {
316+
global $SDK_VER;
317+
$sdkInfo = "QiniuPHP/$SDK_VER";
318+
319+
$systemInfo = php_uname("s");
320+
$machineInfo = php_uname("m");
321+
322+
$envInfo = "($systemInfo/$machineInfo)";
323+
324+
$phpVer = phpversion();
325+
326+
$ua = "$sdkInfo $envInfo PHP/$phpVer";
327+
return $ua;
328+
}
329+
311330
function Qiniu_escapeQuotes($str)
312331
{
313332
$find = array("\\", "\"");

qiniu/resumable_io.php

+23-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Qiniu_Rio_PutExtra
1212
public $Params = null;
1313
public $MimeType = null;
1414
public $ChunkSize = 0; // 可选。每次上传的Chunk大小
15-
public $TryTimes = 0; // 可选。尝试次数
15+
public $TryTimes = 3; // 可选。尝试次数
1616
public $Progresses = null; // 可选。上传进度:[]BlkputRet
1717
public $Notify = null; // 进度通知:func(blkIdx int, blkSize int, ret *BlkputRet)
1818
public $NotifyErr = null; // 错误通知:func(blkIdx int, blkSize int, err error)
@@ -41,7 +41,7 @@ function Qiniu_Rio_Mkblock($self, $host, $reader, $size) // => ($blkputRet, $err
4141
if (is_resource($reader)) {
4242
$body = fread($reader, $size);
4343
if ($body === false) {
44-
$err = Qiniu_NewError(0, 'fread failed');
44+
$err = new Qiniu_Error(0, 'fread failed');
4545
return array(null, $err);
4646
}
4747
} else {
@@ -51,7 +51,7 @@ function Qiniu_Rio_Mkblock($self, $host, $reader, $size) // => ($blkputRet, $err
5151
}
5252
}
5353
if (strlen($body) != $size) {
54-
$err = Qiniu_NewError(0, 'fread failed: unexpected eof');
54+
$err = new Qiniu_Error(0, 'fread failed: unexpected eof');
5555
return array(null, $err);
5656
}
5757

@@ -117,13 +117,30 @@ function Qiniu_Rio_Put($upToken, $key, $body, $fsize, $putExtra) // => ($putRet,
117117
$progresses = array();
118118
$uploaded = 0;
119119
while ($uploaded < $fsize) {
120+
$tried = 0;
121+
$tryTimes = ($putExtra->TryTimes > 0) ? $putExtra->TryTimes : 1;
122+
$blkputRet = null;
123+
$err = null;
120124
if ($fsize < $uploaded + QINIU_RIO_BLOCK_SIZE) {
121125
$bsize = $fsize - $uploaded;
122126
} else {
123127
$bsize = QINIU_RIO_BLOCK_SIZE;
124128
}
125-
list($blkputRet, $err) = Qiniu_Rio_Mkblock($self, $QINIU_UP_HOST, $body, $bsize);
126-
$host = $blkputRet['host'];
129+
while ($tried < $tryTimes) {
130+
list($blkputRet, $err) = Qiniu_Rio_Mkblock($self, $QINIU_UP_HOST, $body, $bsize);
131+
if ($err === null) {
132+
break;
133+
}
134+
$tried += 1;
135+
continue;
136+
}
137+
if ($err !== null) {
138+
return array(null, $err);
139+
}
140+
if ($blkputRet === null ) {
141+
$err = new Qiniu_Error(0, "rio: uploaded without ret");
142+
return array(null, $err);
143+
}
127144
$uploaded += $bsize;
128145
$progresses []= $blkputRet;
129146
}
@@ -136,7 +153,7 @@ function Qiniu_Rio_PutFile($upToken, $key, $localFile, $putExtra) // => ($putRet
136153
{
137154
$fp = fopen($localFile, 'rb');
138155
if ($fp === false) {
139-
$err = Qiniu_NewError(0, 'fopen failed');
156+
$err = new Qiniu_Error(0, 'fopen failed');
140157
return array(null, $err);
141158
}
142159

tests/RioTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function testMockReader()
2626

2727
public function testPut()
2828
{
29+
if (getTestEnv() == "travis") {
30+
return;
31+
}
2932
$key = 'testRioPut' . getTid();
3033
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
3134

@@ -50,6 +53,9 @@ public function testPut()
5053

5154
public function testLargePut()
5255
{
56+
if (getTestEnv() == "travis") {
57+
return;
58+
}
5359
$key = 'testRioLargePut' . getTid();
5460
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
5561

tests/RsTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function setUp()
1919

2020
public function testStat()
2121
{
22+
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket . ":" . $this->key);
23+
$upToken = $putPolicy->Token(null);
24+
list($ret, $err) = Qiniu_PutFile($upToken, $this->key, __file__, null);
25+
$this->assertNull($err);
26+
Qiniu_RS_Delete($this->client, $this->bucket, $this->notExistKey);
2227
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $this->key);
2328
$this->assertArrayHasKey('hash', $ret);
2429
$this->assertNull($err);

tests/RsUtilsTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public function setUp()
1616

1717
public function testRput()
1818
{
19+
if (getTestEnv() == "travis") {
20+
return;
21+
}
1922
$key = 'tmp/testRput' . getTid();
2023
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
2124

@@ -35,6 +38,9 @@ public function testRput()
3538

3639
public function testRputFile()
3740
{
41+
if (getTestEnv() == "travis") {
42+
return;
43+
}
3844
$key = 'tmp/testRputFile' . getTid();
3945
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
4046

tests/bootstrap.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
$secretKey = getenv("QINIU_SECRET_KEY");
99

1010
$tid = getenv("TRAVIS_JOB_NUMBER");
11+
12+
$testEnv = getenv("QINIU_TEST_ENV");
13+
1114
if (!empty($tid)) {
12-
$pid = getmypid();
15+
$pid = getmypid();
1316
$tid = strstr($tid, ".");
14-
$tid .= "." . $pid;
17+
$tid .= "." . $pid;
1518
}
1619

1720
function initKeys() {
@@ -26,6 +29,11 @@ function getTid() {
2629
return $tid;
2730
}
2831

32+
function getTestEnv() {
33+
global $testEnv;
34+
return $testEnv;
35+
}
36+
2937
class MockReader
3038
{
3139
private $off = 0;

0 commit comments

Comments
 (0)