response.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. use Nyholm\Psr7\Stream;
  3. use Psr\Http\Message\MessageInterface;
  4. use Psr\Http\Message\{ResponseInterface, StreamInterface};
  5. /**
  6. * Roadrunner的response类。
  7. * Response class for RoadRunner.
  8. *
  9. * @package zand
  10. */
  11. class zandResponse implements ResponseInterface
  12. {
  13. /**
  14. * @var mixed[]
  15. */
  16. private $headers = array();
  17. /**
  18. * @var mixed[]
  19. */
  20. private $headerNames = array();
  21. /**
  22. * @var bool
  23. */
  24. private $sent = false;
  25. /**
  26. * @var string
  27. */
  28. public $cookieDomain = '';
  29. /**
  30. * @var string
  31. */
  32. public $cookiePath = '/';
  33. /**
  34. * @var int
  35. */
  36. public $statusCode = 200;
  37. /**
  38. * @var string
  39. */
  40. public $reasonPhrase = '';
  41. private $protocol = '1.1';
  42. /**
  43. * @var \Nyholm\Psr7\Stream
  44. */
  45. public $stream;
  46. /**
  47. * @var bool
  48. */
  49. public $cookieSecure = false;
  50. private const PHRASES = array(
  51. 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing',
  52. 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-status', 208 => 'Already Reported',
  53. 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => 'Switch Proxy', 307 => 'Temporary Redirect',
  54. 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', 418 => 'I\'m a teapot', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 425 => 'Unordered Collection', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 451 => 'Unavailable For Legal Reasons',
  55. 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 508 => 'Loop Detected', 511 => 'Network Authentication Required',
  56. );
  57. public function __construct()
  58. {
  59. $this->stream = Stream::create('');
  60. }
  61. public function getProtocolVersion()
  62. {
  63. return $this->protocol;
  64. }
  65. public function withProtocolVersion($version)
  66. {
  67. if (!is_scalar($version)) {
  68. throw new InvalidArgumentException('Protocol version must be a string');
  69. }
  70. if ($this->protocol === $version) {
  71. return $this;
  72. }
  73. $new = clone $this;
  74. $new->protocol = (string) $version;
  75. return $new;
  76. }
  77. public function cleanup()
  78. {
  79. $this->headers = array();
  80. $this->statusCode = 200;
  81. $this->sent = false;
  82. $this->reasonPhrase = '';
  83. }
  84. public function getHeaders()
  85. {
  86. return $this->headers;
  87. }
  88. public function getHeaderLine($header)
  89. {
  90. return implode(', ', $this->getHeader($header));
  91. }
  92. /**
  93. * @return $this
  94. * @param string $name
  95. * @param string|null $value
  96. */
  97. public function setHeader($name, $value = null)
  98. {
  99. $this->headers[$name] = [$value];
  100. return $this;
  101. }
  102. /**
  103. * @return $this
  104. * @param string $name
  105. * @param string $value
  106. */
  107. public function addHeader($name, $value)
  108. {
  109. $this->headers[$name][] = $value;
  110. return $this;
  111. }
  112. public function hasHeader($header)
  113. {
  114. return isset($this->headerNames[strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')]);
  115. }
  116. public function withHeader($header, $value)
  117. {
  118. $normalized = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
  119. $new = clone $this;
  120. if (isset($new->headerNames[$normalized])) {
  121. unset($new->headers[$new->headerNames[$normalized]]);
  122. }
  123. $new->headerNames[$normalized] = $header;
  124. $new->headers[$header] = $value;
  125. return $new;
  126. }
  127. public function withAddedHeader($header, $value)
  128. {
  129. if (!is_string($header) || '' === $header) {
  130. throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string');
  131. }
  132. $new = clone $this;
  133. $new->setHeaders(array($header => $value));
  134. return $new;
  135. }
  136. public function withoutHeader($header)
  137. {
  138. if (!is_string($header)) {
  139. throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string');
  140. }
  141. $normalized = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
  142. if (!isset($this->headerNames[$normalized])) {
  143. return $this;
  144. }
  145. $header = $this->headerNames[$normalized];
  146. $new = clone $this;
  147. unset($new->headers[$header], $new->headerNames[$normalized]);
  148. return $new;
  149. }
  150. public function getHeader($header)
  151. {
  152. if (!is_string($header)) {
  153. throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string');
  154. }
  155. $header = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
  156. if (!isset($this->headerNames[$header])) {
  157. return [];
  158. }
  159. $header = $this->headerNames[$header];
  160. return $this->headers[$header];
  161. }
  162. /**
  163. * @return $this
  164. * @param string $name
  165. */
  166. public function deleteHeader($name)
  167. {
  168. unset($this->headers[$name]);
  169. return $this;
  170. }
  171. /**
  172. * @return $this
  173. * @param string $type
  174. * @param string|null $charset
  175. */
  176. public function setContentType($type, $charset = null)
  177. {
  178. $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
  179. return $this;
  180. }
  181. /**
  182. * @param string $url
  183. * @param int $code
  184. */
  185. public function redirect($url, $code = 302)
  186. {
  187. $this->setCode($code);
  188. $this->setHeader('Location', $url);
  189. }
  190. public function isSent()
  191. {
  192. return $this->sent;
  193. }
  194. /**
  195. * @return $this
  196. * @param bool $sent
  197. */
  198. public function setSent($sent)
  199. {
  200. $this->sent = true;
  201. return $this;
  202. }
  203. public function getStatusCode()
  204. {
  205. return $this->statusCode;
  206. }
  207. public function setStatus($code, $reasonPhrase = '')
  208. {
  209. $this->statusCode = $code;
  210. if ((null === $reasonPhrase || '' === $reasonPhrase) && isset(self::PHRASES[$this->statusCode])) {
  211. $reasonPhrase = self::PHRASES[$this->statusCode];
  212. }
  213. $this->reasonPhrase = $reasonPhrase;
  214. }
  215. public function withStatus($code, $reasonPhrase = '')
  216. {
  217. if (!is_int($code) && !is_string($code)) {
  218. throw new InvalidArgumentException('Status code has to be an integer');
  219. }
  220. $code = (int) $code;
  221. if ($code < 100 || $code > 599) {
  222. throw new InvalidArgumentException(\sprintf('Status code has to be an integer between 100 and 599. A status code of %d was given', $code));
  223. }
  224. $new = clone $this;
  225. $new->statusCode = $code;
  226. if ((null === $reasonPhrase || '' === $reasonPhrase) && isset(self::PHRASES[$new->statusCode])) {
  227. $reasonPhrase = self::PHRASES[$new->statusCode];
  228. }
  229. $new->reasonPhrase = $reasonPhrase;
  230. return $new;
  231. }
  232. public function getReasonPhrase()
  233. {
  234. return $this->reasonPhrase;
  235. }
  236. public function getBody()
  237. {
  238. if (null === $this->stream) {
  239. $this->stream = Stream::create('');
  240. }
  241. return $this->stream;
  242. }
  243. /**
  244. * @param \Psr\Http\Message\StreamInterface $body
  245. */
  246. public function withBody($body)
  247. {
  248. if ($body === $this->stream) {
  249. return $this;
  250. }
  251. $new = clone $this;
  252. $new->stream = $body;
  253. return $new;
  254. }
  255. /**
  256. * @param string $body
  257. */
  258. public function setBody($body)
  259. {
  260. $this->stream = Stream::create($body);
  261. }
  262. /**
  263. * @return $this
  264. * @param string $name
  265. * @param string $value
  266. * @param int|null $expire
  267. * @param string|null $path
  268. * @param string|null $domain
  269. * @param bool|null $secure
  270. * @param bool|null $httpOnly
  271. * @param string|null $sameSite
  272. */
  273. public function setCookie($name, $value, $expire, $path = null, $domain = null, $secure = null, $httpOnly = null, $sameSite = null)
  274. {
  275. $headerValue = sprintf('%s=%s; path=%s; SameSite=%s', $name, urlencode($value), $path ?? ($domain ? '/' : $this->cookiePath), $sameSite ?? 'Lax');
  276. if($expire)
  277. {
  278. $headerValue .= '; Expires='.(date('D, d M Y H:i:s T', $expire));
  279. }
  280. $cookieDomain = $domain ?? $this->cookieDomain;
  281. if($cookieDomain && !$path) $headerValue .= '; domain='.$cookieDomain;
  282. if($secure ?? $this->cookieSecure) $headerValue .= '; secure';
  283. if($httpOnly || $httpOnly === null) $headerValue .= '; HttpOnly';
  284. $this->addHeader('Set-Cookie', $headerValue);
  285. return $this;
  286. }
  287. /**
  288. * @return $this
  289. * @param string $name
  290. * @param string|null $path
  291. * @param string|null $domain
  292. * @param bool|null $secure
  293. */
  294. public function deleteCookie($name, $path = null, $domain = null, $secure = null)
  295. {
  296. $this->setCookie($name, '', 0, $path, $domain, $secure);
  297. return $this;
  298. }
  299. }