Response.php 6.2 KB

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