1 | <?php |
||||
2 | |||||
3 | namespace Qiniu\Http; |
||||
4 | |||||
5 | /** |
||||
6 | * HTTP response Object |
||||
7 | */ |
||||
8 | final class Response |
||||
9 | { |
||||
10 | public $statusCode; |
||||
11 | /** |
||||
12 | * deprecated because of field names case-sensitive. |
||||
13 | * use $normalizedHeaders instead which field names are case-insensitive. |
||||
14 | * but be careful not to use $normalizedHeaders with `array_*` functions, |
||||
15 | * such as `array_key_exists`, `array_keys`, `array_values`. |
||||
16 | * |
||||
17 | * use `isset` instead of `array_key_exists`, |
||||
18 | * and should never use `array_key_exists` at http header. |
||||
19 | * |
||||
20 | * use `foreach` instead of `array_keys`, `array_values`. |
||||
21 | * |
||||
22 | * @deprecated |
||||
23 | */ |
||||
24 | public $headers; |
||||
25 | public $normalizedHeaders; |
||||
26 | public $body; |
||||
27 | public $error; |
||||
28 | private $jsonData; |
||||
29 | public $duration; |
||||
30 | |||||
31 | /** @var array Mapping of status codes to reason phrases */ |
||||
32 | private static $statusTexts = array( |
||||
33 | 100 => 'Continue', |
||||
34 | 101 => 'Switching Protocols', |
||||
35 | 102 => 'Processing', |
||||
36 | 200 => 'OK', |
||||
37 | 201 => 'Created', |
||||
38 | 202 => 'Accepted', |
||||
39 | 203 => 'Non-Authoritative Information', |
||||
40 | 204 => 'No Content', |
||||
41 | 205 => 'Reset Content', |
||||
42 | 206 => 'Partial Content', |
||||
43 | 207 => 'Multi-Status', |
||||
44 | 208 => 'Already Reported', |
||||
45 | 226 => 'IM Used', |
||||
46 | 300 => 'Multiple Choices', |
||||
47 | 301 => 'Moved Permanently', |
||||
48 | 302 => 'Found', |
||||
49 | 303 => 'See Other', |
||||
50 | 304 => 'Not Modified', |
||||
51 | 305 => 'Use Proxy', |
||||
52 | 307 => 'Temporary Redirect', |
||||
53 | 308 => 'Permanent Redirect', |
||||
54 | 400 => 'Bad Request', |
||||
55 | 401 => 'Unauthorized', |
||||
56 | 402 => 'Payment Required', |
||||
57 | 403 => 'Forbidden', |
||||
58 | 404 => 'Not Found', |
||||
59 | 405 => 'Method Not Allowed', |
||||
60 | 406 => 'Not Acceptable', |
||||
61 | 407 => 'Proxy Authentication Required', |
||||
62 | 408 => 'Request Timeout', |
||||
63 | 409 => 'Conflict', |
||||
64 | 410 => 'Gone', |
||||
65 | 411 => 'Length Required', |
||||
66 | 412 => 'Precondition Failed', |
||||
67 | 413 => 'Request Entity Too Large', |
||||
68 | 414 => 'Request-URI Too Long', |
||||
69 | 415 => 'Unsupported Media Type', |
||||
70 | 416 => 'Requested Range Not Satisfiable', |
||||
71 | 417 => 'Expectation Failed', |
||||
72 | 422 => 'Unprocessable Entity', |
||||
73 | 423 => 'Locked', |
||||
74 | 424 => 'Failed Dependency', |
||||
75 | 425 => 'Reserved for WebDAV advanced collections expired proposal', |
||||
76 | 426 => 'Upgrade required', |
||||
77 | 428 => 'Precondition Required', |
||||
78 | 429 => 'Too Many Requests', |
||||
79 | 431 => 'Request Header Fields Too Large', |
||||
80 | 500 => 'Internal Server Error', |
||||
81 | 501 => 'Not Implemented', |
||||
82 | 502 => 'Bad Gateway', |
||||
83 | 503 => 'Service Unavailable', |
||||
84 | 504 => 'Gateway Timeout', |
||||
85 | 505 => 'HTTP Version Not Supported', |
||||
86 | 102 | 506 => 'Variant Also Negotiates (Experimental)', |
|||
87 | 507 => 'Insufficient Storage', |
||||
88 | 102 | 508 => 'Loop Detected', |
|||
89 | 102 | 510 => 'Not Extended', |
|||
90 | 102 | 511 => 'Network Authentication Required', |
|||
91 | 102 | ); |
|||
92 | 102 | ||||
93 | 102 | /** |
|||
94 | 102 | * @param int $code 状态码 |
|||
95 | 3 | * @param double $duration 请求时长 |
|||
96 | * @param array $headers 响应头部 |
||||
97 | * @param string $body 响应内容 |
||||
98 | 102 | * @param string $error 错误描述 |
|||
99 | */ |
||||
100 | public function __construct($code, $duration, array $headers = array(), $body = null, $error = null) |
||||
101 | { |
||||
102 | $this->statusCode = $code; |
||||
103 | $this->duration = $duration; |
||||
104 | 102 | $this->headers = array(); |
|||
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() |
|||||
105 | $this->body = $body; |
||||
106 | 90 | $this->error = $error; |
|||
107 | 90 | $this->jsonData = null; |
|||
108 | 24 | ||||
109 | 24 | if ($error !== null) { |
|||
110 | 24 | return; |
|||
111 | 24 | } |
|||
112 | 24 | ||||
113 | 90 | foreach ($headers as $k => $vs) { |
|||
114 | 90 | if (is_array($vs)) { |
|||
115 | $this->headers[$k] = $vs[count($vs) - 1]; |
||||
0 ignored issues
–
show
The property
Qiniu\Http\Response::$headers has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
116 | } else { |
||||
117 | $this->headers[$k] = $vs; |
||||
0 ignored issues
–
show
The property
Qiniu\Http\Response::$headers has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
118 | } |
||||
119 | } |
||||
120 | 102 | $this->normalizedHeaders = new Header($headers); |
|||
121 | |||||
122 | if ($body === null) { |
||||
123 | 102 | if ($code >= 400) { |
|||
124 | $this->error = self::$statusTexts[$code]; |
||||
125 | } |
||||
126 | 75 | return; |
|||
127 | } |
||||
128 | 75 | if (self::isJson($this->normalizedHeaders)) { |
|||
129 | try { |
||||
130 | $jsonData = self::bodyJson($body); |
||||
131 | 90 | if ($code >= 400) { |
|||
132 | $this->error = $body; |
||||
133 | 90 | if ($jsonData['error'] !== null) { |
|||
134 | $this->error = $jsonData['error']; |
||||
135 | } |
||||
136 | } |
||||
137 | $this->jsonData = $jsonData; |
||||
138 | } catch (\InvalidArgumentException $e) { |
||||
139 | $this->error = $body; |
||||
140 | if ($code >= 200 && $code < 300) { |
||||
141 | $this->error = $e->getMessage(); |
||||
142 | } |
||||
143 | } |
||||
144 | } elseif ($code >= 400) { |
||||
145 | $this->error = $body; |
||||
146 | } |
||||
147 | return; |
||||
148 | 6 | } |
|||
149 | |||||
150 | 6 | public function json() |
|||
151 | { |
||||
152 | return $this->jsonData; |
||||
153 | 6 | } |
|||
154 | |||||
155 | 6 | public function headers($normalized = false) |
|||
156 | { |
||||
157 | if ($normalized) { |
||||
158 | 81 | return $this->normalizedHeaders; |
|||
159 | } |
||||
160 | 81 | return $this->headers; |
|||
0 ignored issues
–
show
The property
Qiniu\Http\Response::$headers has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
161 | } |
||||
162 | |||||
163 | 6 | public function body() |
|||
164 | { |
||||
165 | 6 | return $this->body; |
|||
166 | 6 | } |
|||
167 | 3 | ||||
168 | private static function bodyJson($body) |
||||
169 | 6 | { |
|||
170 | return \Qiniu\json_decode((string) $body, true, 512); |
||||
171 | 102 | } |
|||
172 | |||||
173 | 102 | public function xVia() |
|||
174 | 102 | { |
|||
175 | $via = $this->normalizedHeaders['X-Via']; |
||||
176 | if ($via === null) { |
||||
177 | $via = $this->normalizedHeaders['X-Px']; |
||||
178 | } |
||||
179 | if ($via === null) { |
||||
180 | $via = $this->normalizedHeaders['Fw-Via']; |
||||
181 | } |
||||
182 | return $via; |
||||
183 | } |
||||
184 | |||||
185 | public function xLog() |
||||
186 | { |
||||
187 | return $this->normalizedHeaders['X-Log']; |
||||
188 | } |
||||
189 | |||||
190 | public function xReqId() |
||||
191 | { |
||||
192 | return $this->normalizedHeaders['X-Reqid']; |
||||
193 | } |
||||
194 | |||||
195 | public function ok() |
||||
196 | { |
||||
197 | return $this->statusCode >= 200 && $this->statusCode < 300 && $this->error === null; |
||||
198 | } |
||||
199 | |||||
200 | public function needRetry() |
||||
201 | { |
||||
202 | if ($this->statusCode > 0 && $this->statusCode < 500) { |
||||
203 | return false; |
||||
204 | } |
||||
205 | |||||
206 | // https://developer.qiniu.com/fusion/kb/1352/the-http-request-return-a-status-code |
||||
207 | if (in_array($this->statusCode, array( |
||||
208 | 501, 509, 573, 579, 608, 612, 614, 616, 618, 630, 631, 632, 640, 701 |
||||
209 | ))) { |
||||
210 | return false; |
||||
211 | } |
||||
212 | |||||
213 | return true; |
||||
214 | } |
||||
215 | |||||
216 | private static function isJson($headers) |
||||
217 | { |
||||
218 | return isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'application/json') === 0; |
||||
219 | } |
||||
220 | } |
||||
221 |