|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Qiniu\Http; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* HTTP response Object |
|
7
|
|
|
*/ |
|
8
|
|
|
final class Response |
|
9
|
|
|
{ |
|
10
|
|
|
public $statusCode; |
|
11
|
|
|
public $headers; |
|
12
|
|
|
public $body; |
|
13
|
|
|
public $error; |
|
14
|
|
|
private $jsonData; |
|
15
|
|
|
public $duration; |
|
16
|
|
|
|
|
17
|
|
|
/** @var array Mapping of status codes to reason phrases */ |
|
18
|
|
|
private static $statusTexts = array( |
|
19
|
|
|
100 => 'Continue', |
|
20
|
|
|
101 => 'Switching Protocols', |
|
21
|
|
|
102 => 'Processing', |
|
22
|
|
|
200 => 'OK', |
|
23
|
|
|
201 => 'Created', |
|
24
|
|
|
202 => 'Accepted', |
|
25
|
|
|
203 => 'Non-Authoritative Information', |
|
26
|
|
|
204 => 'No Content', |
|
27
|
|
|
205 => 'Reset Content', |
|
28
|
|
|
206 => 'Partial Content', |
|
29
|
|
|
207 => 'Multi-Status', |
|
30
|
|
|
208 => 'Already Reported', |
|
31
|
|
|
226 => 'IM Used', |
|
32
|
|
|
300 => 'Multiple Choices', |
|
33
|
|
|
301 => 'Moved Permanently', |
|
34
|
|
|
302 => 'Found', |
|
35
|
|
|
303 => 'See Other', |
|
36
|
|
|
304 => 'Not Modified', |
|
37
|
|
|
305 => 'Use Proxy', |
|
38
|
|
|
307 => 'Temporary Redirect', |
|
39
|
|
|
308 => 'Permanent Redirect', |
|
40
|
|
|
400 => 'Bad Request', |
|
41
|
|
|
401 => 'Unauthorized', |
|
42
|
|
|
402 => 'Payment Required', |
|
43
|
|
|
403 => 'Forbidden', |
|
44
|
|
|
404 => 'Not Found', |
|
45
|
|
|
405 => 'Method Not Allowed', |
|
46
|
|
|
406 => 'Not Acceptable', |
|
47
|
|
|
407 => 'Proxy Authentication Required', |
|
48
|
|
|
408 => 'Request Timeout', |
|
49
|
|
|
409 => 'Conflict', |
|
50
|
|
|
410 => 'Gone', |
|
51
|
|
|
411 => 'Length Required', |
|
52
|
|
|
412 => 'Precondition Failed', |
|
53
|
|
|
413 => 'Request Entity Too Large', |
|
54
|
|
|
414 => 'Request-URI Too Long', |
|
55
|
|
|
415 => 'Unsupported Media Type', |
|
56
|
|
|
416 => 'Requested Range Not Satisfiable', |
|
57
|
|
|
417 => 'Expectation Failed', |
|
58
|
|
|
422 => 'Unprocessable Entity', |
|
59
|
|
|
423 => 'Locked', |
|
60
|
|
|
424 => 'Failed Dependency', |
|
61
|
|
|
425 => 'Reserved for WebDAV advanced collections expired proposal', |
|
62
|
|
|
426 => 'Upgrade required', |
|
63
|
|
|
428 => 'Precondition Required', |
|
64
|
|
|
429 => 'Too Many Requests', |
|
65
|
|
|
431 => 'Request Header Fields Too Large', |
|
66
|
|
|
500 => 'Internal Server Error', |
|
67
|
|
|
501 => 'Not Implemented', |
|
68
|
|
|
502 => 'Bad Gateway', |
|
69
|
|
|
503 => 'Service Unavailable', |
|
70
|
|
|
504 => 'Gateway Timeout', |
|
71
|
|
|
505 => 'HTTP Version Not Supported', |
|
72
|
|
|
506 => 'Variant Also Negotiates (Experimental)', |
|
73
|
|
|
507 => 'Insufficient Storage', |
|
74
|
|
|
508 => 'Loop Detected', |
|
75
|
|
|
510 => 'Not Extended', |
|
76
|
|
|
511 => 'Network Authentication Required', |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param int $code 状态码 |
|
81
|
|
|
* @param double $duration 请求时长 |
|
82
|
|
|
* @param array $headers 响应头部 |
|
83
|
|
|
* @param string $body 响应内容 |
|
84
|
|
|
* @param string $error 错误描述 |
|
85
|
|
|
*/ |
|
86
|
99 |
|
public function __construct($code, $duration, array $headers = array(), $body = null, $error = null) |
|
87
|
|
|
{ |
|
88
|
99 |
|
$this->statusCode = $code; |
|
89
|
99 |
|
$this->duration = $duration; |
|
90
|
99 |
|
$this->headers = $headers; |
|
91
|
99 |
|
$this->body = $body; |
|
92
|
99 |
|
$this->error = $error; |
|
93
|
99 |
|
$this->jsonData = null; |
|
94
|
99 |
|
if ($error !== null) { |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
99 |
|
if ($body === null) { |
|
99
|
|
|
if ($code >= 400) { |
|
100
|
|
|
$this->error = self::$statusTexts[$code]; |
|
101
|
|
|
} |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
99 |
|
if (self::isJson($headers)) { |
|
105
|
|
|
try { |
|
106
|
90 |
|
$jsonData = self::bodyJson($body); |
|
107
|
90 |
|
if ($code >=400) { |
|
108
|
27 |
|
$this->error = $body; |
|
109
|
27 |
|
if ($jsonData['error'] !== null) { |
|
110
|
27 |
|
$this->error = $jsonData['error']; |
|
111
|
18 |
|
} |
|
112
|
18 |
|
} |
|
113
|
90 |
|
$this->jsonData = $jsonData; |
|
114
|
60 |
|
} catch (\InvalidArgumentException $e) { |
|
115
|
|
|
$this->error = $body; |
|
116
|
|
|
if ($code >= 200 && $code < 300) { |
|
117
|
30 |
|
$this->error = $e->getMessage(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
69 |
|
} elseif ($code >=400) { |
|
121
|
|
|
$this->error = $body; |
|
122
|
|
|
} |
|
123
|
99 |
|
return; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
72 |
|
public function json() |
|
127
|
|
|
{ |
|
128
|
72 |
|
return $this->jsonData; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
90 |
|
private static function bodyJson($body) |
|
132
|
|
|
{ |
|
133
|
90 |
|
return \Qiniu\json_decode((string) $body, true, 512); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function xVia() |
|
137
|
|
|
{ |
|
138
|
|
|
$via = $this->headers['X-Via']; |
|
139
|
|
|
if ($via === null) { |
|
140
|
|
|
$via = $this->headers['X-Px']; |
|
141
|
|
|
} |
|
142
|
|
|
if ($via === null) { |
|
143
|
|
|
$via = $this->headers['Fw-Via']; |
|
144
|
|
|
} |
|
145
|
|
|
return $via; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
6 |
|
public function xLog() |
|
149
|
|
|
{ |
|
150
|
6 |
|
return $this->headers['X-Log']; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
6 |
|
public function xReqId() |
|
154
|
|
|
{ |
|
155
|
6 |
|
return $this->headers['X-Reqid']; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
81 |
|
public function ok() |
|
159
|
|
|
{ |
|
160
|
81 |
|
return $this->statusCode >= 200 && $this->statusCode < 300 && $this->error === null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
6 |
|
public function needRetry() |
|
164
|
|
|
{ |
|
165
|
6 |
|
$code = $this->statusCode; |
|
166
|
6 |
|
if ($code< 0 || ($code / 100 === 5 and $code !== 579) || $code === 996) { |
|
167
|
|
|
return true; |
|
168
|
|
|
} |
|
169
|
6 |
|
} |
|
170
|
|
|
|
|
171
|
99 |
|
private static function isJson($headers) |
|
172
|
|
|
{ |
|
173
|
99 |
|
return array_key_exists('Content-Type', $headers) && |
|
174
|
99 |
|
strpos($headers['Content-Type'], 'application/json') === 0; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|