Skip to content

Commit 517eeb8

Browse files
author
sabakugaara
committed
add x-request-id
1 parent 49f0e93 commit 517eeb8

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

tests/upyunTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,12 @@ public function testDelete()
8080
$this->assertTrue($rsp);
8181
}
8282

83+
/**
84+
* 获取错误请求的 X-Request-Id
85+
*/
86+
public function testXRequestId(){
87+
$rsp = $this->upyun->getList('/demo/');
88+
$x_id = $this->upyun->getXRequestId();
89+
$this->assertEquals(strlen($x_id), 32);
90+
}
8391
}

upyun.class.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class UpYun {
8383

8484
protected $endpoint;
8585

86+
private $x_request_id;
87+
8688
/**
8789
* 初始化 UpYun 存储接口
8890
* @param $bucketname 空间名称
@@ -269,14 +271,12 @@ protected function _do_request($method, $path, $headers = NULL, $body= NULL, $fi
269271
array_push($_headers, "Content-Length: {$length}");
270272
curl_setopt($ch, CURLOPT_INFILE, $body);
271273
curl_setopt($ch, CURLOPT_INFILESIZE, $length);
272-
}
273-
else {
274+
} else {
274275
$length = @strlen($body);
275276
array_push($_headers, "Content-Length: {$length}");
276277
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
277278
}
278-
}
279-
else {
279+
} else {
280280
array_push($_headers, "Content-Length: {$length}");
281281
}
282282

@@ -293,8 +293,7 @@ protected function _do_request($method, $path, $headers = NULL, $body= NULL, $fi
293293

294294
if ($method == 'PUT' || $method == 'POST') {
295295
curl_setopt($ch, CURLOPT_POST, 1);
296-
}
297-
else {
296+
} else {
298297
curl_setopt($ch, CURLOPT_POST, 0);
299298
}
300299

@@ -320,25 +319,18 @@ protected function _do_request($method, $path, $headers = NULL, $body= NULL, $fi
320319
if ($method == 'GET' && is_resource($file_handle)) {
321320
$header_string = '';
322321
$body = $response;
323-
}
324-
else {
322+
} else {
325323
list($header_string, $body) = explode("\r\n\r\n", $response, 2);
326324
}
325+
$this->setXRequestId($header_string);
327326
if ($http_code == 200) {
328327
if ($method == 'GET' && is_null($file_handle)) {
329328
return $body;
330-
}
331-
else {
329+
} else {
332330
$data = $this->_getHeadersData($header_string);
333331
return count($data) > 0 ? $data : true;
334332
}
335-
//elseif ($method == 'HEAD') {
336-
// //return $this->_get_headers_data(substr($response, 0 , $header_size));
337-
// return $this->_getHeadersData($header_string);
338-
//}
339-
//return True;
340-
}
341-
else {
333+
} else {
342334
$message = $this->_getErrorMessage($header_string);
343335
if (is_null($message) && $method == 'GET' && is_resource($file_handle)) {
344336
$message = 'File Not Found';
@@ -395,7 +387,16 @@ private function _getHeadersData($text) {/*{{{*/
395387
private function _getErrorMessage($header_string) {
396388
list($status, $stash) = explode("\r\n", $header_string, 2);
397389
list($v, $code, $message) = explode(" ", $status, 3);
398-
return $message;
390+
return $message . " X-Request-Id: " . $this->getXRequestId();
391+
}
392+
393+
private function setXRequestId($header_string) {
394+
preg_match('~^X-Request-Id: ([0-9a-zA-Z]{32})~ism', $header_string, $result);
395+
$this->x_request_id = isset($result[1]) ? $result[1] : '';
396+
}
397+
398+
public function getXRequestId() {
399+
return $this->x_request_id;
399400
}
400401

401402
/**

0 commit comments

Comments
 (0)