Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user agent #69

Merged
merged 9 commits into from
Apr 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 3 additions & 0 deletions qiniu/conf.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
global $SDK_VER;

global $QINIU_UP_HOST;
global $QINIU_RS_HOST;
Expand All @@ -7,6 +8,8 @@
global $QINIU_ACCESS_KEY;
global $QINIU_SECRET_KEY;

$SDK_VER = "6.1.9";

$QINIU_UP_HOST = 'http://up.qiniu.com';
$QINIU_RS_HOST = 'http://rs.qbox.me';
$QINIU_RSF_HOST = 'http://rsf.qbox.me';
Expand Down
19 changes: 19 additions & 0 deletions qiniu/http.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once("auth_digest.php");
require_once("conf.php");

// --------------------------------------------------------------------------------
// class Qiniu_Error
Expand All @@ -27,12 +28,14 @@ class Qiniu_Request
public $URL;
public $Header;
public $Body;
public $UA;

public function __construct($url, $body)
{
$this->URL = $url;
$this->Header = array();
$this->Body = $body;
$this->UA = Qiniu_UserAgent();
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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("\\", "\"");
Expand Down
29 changes: 23 additions & 6 deletions qiniu/resumable_io.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/IoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tests/RioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions tests/RsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tests/RsUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
12 changes: 10 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -26,6 +29,11 @@ function getTid() {
return $tid;
}

function getTestEnv() {
global $testEnv;
return $testEnv;
}

class MockReader
{
private $off = 0;
Expand Down