[FEATURE] Introduce Request/Response based on PSR-7
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Http / Response.php
1 <?php
2 namespace TYPO3\CMS\Core\Http;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\StreamInterface;
19
20 /**
21 * Default implementation for the ResponseInterface of the PSR-7 standard.
22 *
23 * Highly inspired by https://github.com/phly/http/
24 *
25 * @internal Note that this is not public API yet.
26 */
27 class Response extends Message implements ResponseInterface {
28
29 /**
30 * The HTTP status code of the response
31 * @var int $statusCode
32 */
33 protected $statusCode;
34
35 /**
36 * The reason phrase of the response
37 * @var string $reasonPhrase
38 */
39 protected $reasonPhrase = '';
40
41 /**
42 * The standardized and other important HTTP Status Codes
43 * @var array
44 */
45 protected $availableStatusCodes = array(
46 // INFORMATIONAL CODES
47 100 => 'Continue',
48 101 => 'Switching Protocols',
49 102 => 'Processing',
50 // SUCCESS CODES
51 200 => 'OK',
52 201 => 'Created',
53 202 => 'Accepted',
54 203 => 'Non-Authoritative Information',
55 204 => 'No Content',
56 205 => 'Reset Content',
57 206 => 'Partial Content',
58 207 => 'Multi-status',
59 208 => 'Already Reported',
60 // REDIRECTION CODES
61 300 => 'Multiple Choices',
62 301 => 'Moved Permanently',
63 302 => 'Found',
64 303 => 'See Other',
65 304 => 'Not Modified',
66 305 => 'Use Proxy',
67 306 => 'Switch Proxy', // Deprecated
68 307 => 'Temporary Redirect',
69 // CLIENT ERROR
70 400 => 'Bad Request',
71 401 => 'Unauthorized',
72 402 => 'Payment Required',
73 403 => 'Forbidden',
74 404 => 'Not Found',
75 405 => 'Method Not Allowed',
76 406 => 'Not Acceptable',
77 407 => 'Proxy Authentication Required',
78 408 => 'Request Time-out',
79 409 => 'Conflict',
80 410 => 'Gone',
81 411 => 'Length Required',
82 412 => 'Precondition Failed',
83 413 => 'Request Entity Too Large',
84 414 => 'Request-URI Too Large',
85 415 => 'Unsupported Media Type',
86 416 => 'Requested range not satisfiable',
87 417 => 'Expectation Failed',
88 418 => 'I\'m a teapot',
89 422 => 'Unprocessable Entity',
90 423 => 'Locked',
91 424 => 'Failed Dependency',
92 425 => 'Unordered Collection',
93 426 => 'Upgrade Required',
94 428 => 'Precondition Required',
95 429 => 'Too Many Requests',
96 431 => 'Request Header Fields Too Large',
97 // SERVER ERROR
98 500 => 'Internal Server Error',
99 501 => 'Not Implemented',
100 502 => 'Bad Gateway',
101 503 => 'Service Unavailable',
102 504 => 'Gateway Time-out',
103 505 => 'HTTP Version not supported',
104 506 => 'Variant Also Negotiates',
105 507 => 'Insufficient Storage',
106 508 => 'Loop Detected',
107 509 => 'Bandwidth Limit Exceeded',
108 511 => 'Network Authentication Required'
109 );
110
111 /**
112 * Constructor for generating new responses
113 *
114 * @param StreamInterface|string $body
115 * @param int $statusCode
116 * @param array $headers
117 * @throws \InvalidArgumentException if any of the given arguments are given
118 */
119 public function __construct($body = 'php://temp', $statusCode = 200, $headers = array()) {
120 // Build a streamable object for the body
121 if (!is_string($body) && !is_resource($body) && !$body instanceof StreamInterface) {
122 throw new \InvalidArgumentException('Body must be a string stream resource identifier, a stream resource, or a StreamInterface instance', 1436717277);
123 }
124
125 if (!$body instanceof StreamInterface) {
126 $body = new Stream($body, 'rw');
127 }
128 $this->body = $body;
129
130 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($statusCode) === FALSE || !array_key_exists((int)$statusCode, $this->availableStatusCodes)) {
131 throw new \InvalidArgumentException('The given status code is not a valid HTTP status code.', 1436717278);
132 }
133 $this->statusCode = (int)$statusCode;
134
135 $this->reasonPhrase = $this->availableStatusCodes[$this->statusCode];
136 list($this->headerNames, $headers) = $this->filterHeaders($headers);
137 $this->assertHeaders($headers);
138 $this->headers = $headers;
139 }
140
141 /**
142 * Gets the response status code.
143 *
144 * The status code is a 3-digit integer result code of the server's attempt
145 * to understand and satisfy the request.
146 *
147 * @return int Status code.
148 */
149 public function getStatusCode() {
150 return $this->statusCode;
151 }
152
153 /**
154 * Return an instance with the specified status code and, optionally, reason phrase.
155 *
156 * If no reason phrase is specified, implementations MAY choose to default
157 * to the RFC 7231 or IANA recommended reason phrase for the response's
158 * status code.
159 *
160 * This method MUST be implemented in such a way as to retain the
161 * immutability of the message, and MUST return an instance that has the
162 * updated status and reason phrase.
163 *
164 * @link http://tools.ietf.org/html/rfc7231#section-6
165 * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
166 *
167 * @param int $code The 3-digit integer result code to set.
168 * @param string $reasonPhrase The reason phrase to use with the
169 * provided status code; if none is provided, implementations MAY
170 * use the defaults as suggested in the HTTP specification.
171 * @return Response
172 * @throws \InvalidArgumentException For invalid status code arguments.
173 */
174 public function withStatus($code, $reasonPhrase = '') {
175 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($code) === FALSE || !array_key_exists((int)$code, $this->availableStatusCodes)) {
176 throw new \InvalidArgumentException('The given status code is not a valid HTTP status code', 1436717279);
177 }
178 $clonedObject = clone $this;
179 $clonedObject->statusCode = $code;
180 $clonedObject->reasonPhrase = $reasonPhrase !== '' ? $reasonPhrase : $this->availableStatusCodes[$code];
181 return $clonedObject;
182 }
183
184 /**
185 * Gets the response reason phrase associated with the status code.
186 *
187 * Because a reason phrase is not a required element in a response
188 * status line, the reason phrase value MAY be null. Implementations MAY
189 * choose to return the default RFC 7231 recommended reason phrase (or those
190 * listed in the IANA HTTP Status Code Registry) for the response's
191 * status code.
192 *
193 * @link http://tools.ietf.org/html/rfc7231#section-6
194 * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
195 * @return string Reason phrase; must return an empty string if none present.
196 */
197 public function getReasonPhrase() {
198 return $this->reasonPhrase;
199 }
200
201 }