@@ -45,358 +45,358 @@ |
||
| 45 | 45 | */ |
| 46 | 46 | class Response { |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
|
| 50 | - * @var array |
|
| 51 | - */ |
|
| 52 | - private $headers = [ |
|
| 53 | - 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
|
| 54 | - ]; |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Cookies that will be need to be constructed as header |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - private $cookies = []; |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * HTTP status code - defaults to STATUS OK |
|
| 66 | - * @var int |
|
| 67 | - */ |
|
| 68 | - private $status = Http::STATUS_OK; |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Last modified date |
|
| 73 | - * @var \DateTime |
|
| 74 | - */ |
|
| 75 | - private $lastModified; |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * ETag |
|
| 80 | - * @var string |
|
| 81 | - */ |
|
| 82 | - private $ETag; |
|
| 83 | - |
|
| 84 | - /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
|
| 85 | - private $contentSecurityPolicy = null; |
|
| 86 | - |
|
| 87 | - /** @var FeaturePolicy */ |
|
| 88 | - private $featurePolicy; |
|
| 89 | - |
|
| 90 | - /** @var bool */ |
|
| 91 | - private $throttled = false; |
|
| 92 | - /** @var array */ |
|
| 93 | - private $throttleMetadata = []; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @since 17.0.0 |
|
| 97 | - */ |
|
| 98 | - public function __construct() { |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Caches the response |
|
| 103 | - * @param int $cacheSeconds the amount of seconds that should be cached |
|
| 104 | - * if 0 then caching will be disabled |
|
| 105 | - * @return $this |
|
| 106 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 107 | - */ |
|
| 108 | - public function cacheFor(int $cacheSeconds, bool $public = false) { |
|
| 109 | - if ($cacheSeconds > 0) { |
|
| 110 | - $pragma = $public ? 'public' : 'private'; |
|
| 111 | - $this->addHeader('Cache-Control', $pragma . ', max-age=' . $cacheSeconds . ', must-revalidate'); |
|
| 112 | - $this->addHeader('Pragma', $pragma); |
|
| 113 | - |
|
| 114 | - // Set expires header |
|
| 115 | - $expires = new \DateTime(); |
|
| 116 | - /** @var ITimeFactory $time */ |
|
| 117 | - $time = \OC::$server->query(ITimeFactory::class); |
|
| 118 | - $expires->setTimestamp($time->getTime()); |
|
| 119 | - $expires->add(new \DateInterval('PT'.$cacheSeconds.'S')); |
|
| 120 | - $this->addHeader('Expires', $expires->format(\DateTime::RFC2822)); |
|
| 121 | - } else { |
|
| 122 | - $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); |
|
| 123 | - unset($this->headers['Expires'], $this->headers['Pragma']); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $this; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Adds a new cookie to the response |
|
| 131 | - * @param string $name The name of the cookie |
|
| 132 | - * @param string $value The value of the cookie |
|
| 133 | - * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
|
| 134 | - * to null cookie will be considered as session |
|
| 135 | - * cookie. |
|
| 136 | - * @return $this |
|
| 137 | - * @since 8.0.0 |
|
| 138 | - */ |
|
| 139 | - public function addCookie($name, $value, \DateTime $expireDate = null) { |
|
| 140 | - $this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate]; |
|
| 141 | - return $this; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Set the specified cookies |
|
| 147 | - * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
|
| 148 | - * @return $this |
|
| 149 | - * @since 8.0.0 |
|
| 150 | - */ |
|
| 151 | - public function setCookies(array $cookies) { |
|
| 152 | - $this->cookies = $cookies; |
|
| 153 | - return $this; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Invalidates the specified cookie |
|
| 159 | - * @param string $name |
|
| 160 | - * @return $this |
|
| 161 | - * @since 8.0.0 |
|
| 162 | - */ |
|
| 163 | - public function invalidateCookie($name) { |
|
| 164 | - $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); |
|
| 165 | - return $this; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Invalidates the specified cookies |
|
| 170 | - * @param array $cookieNames array('foo', 'bar') |
|
| 171 | - * @return $this |
|
| 172 | - * @since 8.0.0 |
|
| 173 | - */ |
|
| 174 | - public function invalidateCookies(array $cookieNames) { |
|
| 175 | - foreach ($cookieNames as $cookieName) { |
|
| 176 | - $this->invalidateCookie($cookieName); |
|
| 177 | - } |
|
| 178 | - return $this; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Returns the cookies |
|
| 183 | - * @return array |
|
| 184 | - * @since 8.0.0 |
|
| 185 | - */ |
|
| 186 | - public function getCookies() { |
|
| 187 | - return $this->cookies; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Adds a new header to the response that will be called before the render |
|
| 192 | - * function |
|
| 193 | - * @param string $name The name of the HTTP header |
|
| 194 | - * @param string $value The value, null will delete it |
|
| 195 | - * @return $this |
|
| 196 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 197 | - */ |
|
| 198 | - public function addHeader($name, $value) { |
|
| 199 | - $name = trim($name); // always remove leading and trailing whitespace |
|
| 200 | - // to be able to reliably check for security |
|
| 201 | - // headers |
|
| 202 | - |
|
| 203 | - if (is_null($value)) { |
|
| 204 | - unset($this->headers[$name]); |
|
| 205 | - } else { |
|
| 206 | - $this->headers[$name] = $value; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return $this; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Set the headers |
|
| 215 | - * @param array $headers value header pairs |
|
| 216 | - * @return $this |
|
| 217 | - * @since 8.0.0 |
|
| 218 | - */ |
|
| 219 | - public function setHeaders(array $headers) { |
|
| 220 | - $this->headers = $headers; |
|
| 221 | - |
|
| 222 | - return $this; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Returns the set headers |
|
| 228 | - * @return array the headers |
|
| 229 | - * @since 6.0.0 |
|
| 230 | - */ |
|
| 231 | - public function getHeaders() { |
|
| 232 | - $mergeWith = []; |
|
| 233 | - |
|
| 234 | - if ($this->lastModified) { |
|
| 235 | - $mergeWith['Last-Modified'] = |
|
| 236 | - $this->lastModified->format(\DateTime::RFC2822); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - $this->headers['Content-Security-Policy'] = $this->getContentSecurityPolicy()->buildPolicy(); |
|
| 240 | - $this->headers['Feature-Policy'] = $this->getFeaturePolicy()->buildPolicy(); |
|
| 241 | - |
|
| 242 | - if ($this->ETag) { |
|
| 243 | - $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return array_merge($mergeWith, $this->headers); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * By default renders no output |
|
| 252 | - * @return string |
|
| 253 | - * @since 6.0.0 |
|
| 254 | - */ |
|
| 255 | - public function render() { |
|
| 256 | - return ''; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Set response status |
|
| 262 | - * @param int $status a HTTP status code, see also the STATUS constants |
|
| 263 | - * @return Response Reference to this object |
|
| 264 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 265 | - */ |
|
| 266 | - public function setStatus($status) { |
|
| 267 | - $this->status = $status; |
|
| 268 | - |
|
| 269 | - return $this; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Set a Content-Security-Policy |
|
| 274 | - * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
|
| 275 | - * @return $this |
|
| 276 | - * @since 8.1.0 |
|
| 277 | - */ |
|
| 278 | - public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
|
| 279 | - $this->contentSecurityPolicy = $csp; |
|
| 280 | - return $this; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Get the currently used Content-Security-Policy |
|
| 285 | - * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
|
| 286 | - * none specified. |
|
| 287 | - * @since 8.1.0 |
|
| 288 | - */ |
|
| 289 | - public function getContentSecurityPolicy() { |
|
| 290 | - if ($this->contentSecurityPolicy === null) { |
|
| 291 | - $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy()); |
|
| 292 | - } |
|
| 293 | - return $this->contentSecurityPolicy; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * @since 17.0.0 |
|
| 299 | - */ |
|
| 300 | - public function getFeaturePolicy(): EmptyFeaturePolicy { |
|
| 301 | - if ($this->featurePolicy === null) { |
|
| 302 | - $this->setFeaturePolicy(new EmptyFeaturePolicy()); |
|
| 303 | - } |
|
| 304 | - return $this->featurePolicy; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * @since 17.0.0 |
|
| 309 | - */ |
|
| 310 | - public function setFeaturePolicy(EmptyFeaturePolicy $featurePolicy): self { |
|
| 311 | - $this->featurePolicy = $featurePolicy; |
|
| 312 | - |
|
| 313 | - return $this; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Get response status |
|
| 320 | - * @since 6.0.0 |
|
| 321 | - */ |
|
| 322 | - public function getStatus() { |
|
| 323 | - return $this->status; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Get the ETag |
|
| 329 | - * @return string the etag |
|
| 330 | - * @since 6.0.0 |
|
| 331 | - */ |
|
| 332 | - public function getETag() { |
|
| 333 | - return $this->ETag; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Get "last modified" date |
|
| 339 | - * @return \DateTime RFC2822 formatted last modified date |
|
| 340 | - * @since 6.0.0 |
|
| 341 | - */ |
|
| 342 | - public function getLastModified() { |
|
| 343 | - return $this->lastModified; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Set the ETag |
|
| 349 | - * @param string $ETag |
|
| 350 | - * @return Response Reference to this object |
|
| 351 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 352 | - */ |
|
| 353 | - public function setETag($ETag) { |
|
| 354 | - $this->ETag = $ETag; |
|
| 355 | - |
|
| 356 | - return $this; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Set "last modified" date |
|
| 362 | - * @param \DateTime $lastModified |
|
| 363 | - * @return Response Reference to this object |
|
| 364 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 365 | - */ |
|
| 366 | - public function setLastModified($lastModified) { |
|
| 367 | - $this->lastModified = $lastModified; |
|
| 368 | - |
|
| 369 | - return $this; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Marks the response as to throttle. Will be throttled when the |
|
| 374 | - * @BruteForceProtection annotation is added. |
|
| 375 | - * |
|
| 376 | - * @param array $metadata |
|
| 377 | - * @since 12.0.0 |
|
| 378 | - */ |
|
| 379 | - public function throttle(array $metadata = []) { |
|
| 380 | - $this->throttled = true; |
|
| 381 | - $this->throttleMetadata = $metadata; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Returns the throttle metadata, defaults to empty array |
|
| 386 | - * |
|
| 387 | - * @return array |
|
| 388 | - * @since 13.0.0 |
|
| 389 | - */ |
|
| 390 | - public function getThrottleMetadata() { |
|
| 391 | - return $this->throttleMetadata; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * Whether the current response is throttled. |
|
| 396 | - * |
|
| 397 | - * @since 12.0.0 |
|
| 398 | - */ |
|
| 399 | - public function isThrottled() { |
|
| 400 | - return $this->throttled; |
|
| 401 | - } |
|
| 48 | + /** |
|
| 49 | + * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
|
| 50 | + * @var array |
|
| 51 | + */ |
|
| 52 | + private $headers = [ |
|
| 53 | + 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
|
| 54 | + ]; |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Cookies that will be need to be constructed as header |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + private $cookies = []; |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * HTTP status code - defaults to STATUS OK |
|
| 66 | + * @var int |
|
| 67 | + */ |
|
| 68 | + private $status = Http::STATUS_OK; |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Last modified date |
|
| 73 | + * @var \DateTime |
|
| 74 | + */ |
|
| 75 | + private $lastModified; |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * ETag |
|
| 80 | + * @var string |
|
| 81 | + */ |
|
| 82 | + private $ETag; |
|
| 83 | + |
|
| 84 | + /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
|
| 85 | + private $contentSecurityPolicy = null; |
|
| 86 | + |
|
| 87 | + /** @var FeaturePolicy */ |
|
| 88 | + private $featurePolicy; |
|
| 89 | + |
|
| 90 | + /** @var bool */ |
|
| 91 | + private $throttled = false; |
|
| 92 | + /** @var array */ |
|
| 93 | + private $throttleMetadata = []; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @since 17.0.0 |
|
| 97 | + */ |
|
| 98 | + public function __construct() { |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Caches the response |
|
| 103 | + * @param int $cacheSeconds the amount of seconds that should be cached |
|
| 104 | + * if 0 then caching will be disabled |
|
| 105 | + * @return $this |
|
| 106 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 107 | + */ |
|
| 108 | + public function cacheFor(int $cacheSeconds, bool $public = false) { |
|
| 109 | + if ($cacheSeconds > 0) { |
|
| 110 | + $pragma = $public ? 'public' : 'private'; |
|
| 111 | + $this->addHeader('Cache-Control', $pragma . ', max-age=' . $cacheSeconds . ', must-revalidate'); |
|
| 112 | + $this->addHeader('Pragma', $pragma); |
|
| 113 | + |
|
| 114 | + // Set expires header |
|
| 115 | + $expires = new \DateTime(); |
|
| 116 | + /** @var ITimeFactory $time */ |
|
| 117 | + $time = \OC::$server->query(ITimeFactory::class); |
|
| 118 | + $expires->setTimestamp($time->getTime()); |
|
| 119 | + $expires->add(new \DateInterval('PT'.$cacheSeconds.'S')); |
|
| 120 | + $this->addHeader('Expires', $expires->format(\DateTime::RFC2822)); |
|
| 121 | + } else { |
|
| 122 | + $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); |
|
| 123 | + unset($this->headers['Expires'], $this->headers['Pragma']); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $this; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Adds a new cookie to the response |
|
| 131 | + * @param string $name The name of the cookie |
|
| 132 | + * @param string $value The value of the cookie |
|
| 133 | + * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
|
| 134 | + * to null cookie will be considered as session |
|
| 135 | + * cookie. |
|
| 136 | + * @return $this |
|
| 137 | + * @since 8.0.0 |
|
| 138 | + */ |
|
| 139 | + public function addCookie($name, $value, \DateTime $expireDate = null) { |
|
| 140 | + $this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate]; |
|
| 141 | + return $this; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Set the specified cookies |
|
| 147 | + * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
|
| 148 | + * @return $this |
|
| 149 | + * @since 8.0.0 |
|
| 150 | + */ |
|
| 151 | + public function setCookies(array $cookies) { |
|
| 152 | + $this->cookies = $cookies; |
|
| 153 | + return $this; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Invalidates the specified cookie |
|
| 159 | + * @param string $name |
|
| 160 | + * @return $this |
|
| 161 | + * @since 8.0.0 |
|
| 162 | + */ |
|
| 163 | + public function invalidateCookie($name) { |
|
| 164 | + $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); |
|
| 165 | + return $this; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Invalidates the specified cookies |
|
| 170 | + * @param array $cookieNames array('foo', 'bar') |
|
| 171 | + * @return $this |
|
| 172 | + * @since 8.0.0 |
|
| 173 | + */ |
|
| 174 | + public function invalidateCookies(array $cookieNames) { |
|
| 175 | + foreach ($cookieNames as $cookieName) { |
|
| 176 | + $this->invalidateCookie($cookieName); |
|
| 177 | + } |
|
| 178 | + return $this; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Returns the cookies |
|
| 183 | + * @return array |
|
| 184 | + * @since 8.0.0 |
|
| 185 | + */ |
|
| 186 | + public function getCookies() { |
|
| 187 | + return $this->cookies; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Adds a new header to the response that will be called before the render |
|
| 192 | + * function |
|
| 193 | + * @param string $name The name of the HTTP header |
|
| 194 | + * @param string $value The value, null will delete it |
|
| 195 | + * @return $this |
|
| 196 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 197 | + */ |
|
| 198 | + public function addHeader($name, $value) { |
|
| 199 | + $name = trim($name); // always remove leading and trailing whitespace |
|
| 200 | + // to be able to reliably check for security |
|
| 201 | + // headers |
|
| 202 | + |
|
| 203 | + if (is_null($value)) { |
|
| 204 | + unset($this->headers[$name]); |
|
| 205 | + } else { |
|
| 206 | + $this->headers[$name] = $value; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return $this; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Set the headers |
|
| 215 | + * @param array $headers value header pairs |
|
| 216 | + * @return $this |
|
| 217 | + * @since 8.0.0 |
|
| 218 | + */ |
|
| 219 | + public function setHeaders(array $headers) { |
|
| 220 | + $this->headers = $headers; |
|
| 221 | + |
|
| 222 | + return $this; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Returns the set headers |
|
| 228 | + * @return array the headers |
|
| 229 | + * @since 6.0.0 |
|
| 230 | + */ |
|
| 231 | + public function getHeaders() { |
|
| 232 | + $mergeWith = []; |
|
| 233 | + |
|
| 234 | + if ($this->lastModified) { |
|
| 235 | + $mergeWith['Last-Modified'] = |
|
| 236 | + $this->lastModified->format(\DateTime::RFC2822); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + $this->headers['Content-Security-Policy'] = $this->getContentSecurityPolicy()->buildPolicy(); |
|
| 240 | + $this->headers['Feature-Policy'] = $this->getFeaturePolicy()->buildPolicy(); |
|
| 241 | + |
|
| 242 | + if ($this->ETag) { |
|
| 243 | + $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return array_merge($mergeWith, $this->headers); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * By default renders no output |
|
| 252 | + * @return string |
|
| 253 | + * @since 6.0.0 |
|
| 254 | + */ |
|
| 255 | + public function render() { |
|
| 256 | + return ''; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Set response status |
|
| 262 | + * @param int $status a HTTP status code, see also the STATUS constants |
|
| 263 | + * @return Response Reference to this object |
|
| 264 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 265 | + */ |
|
| 266 | + public function setStatus($status) { |
|
| 267 | + $this->status = $status; |
|
| 268 | + |
|
| 269 | + return $this; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Set a Content-Security-Policy |
|
| 274 | + * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
|
| 275 | + * @return $this |
|
| 276 | + * @since 8.1.0 |
|
| 277 | + */ |
|
| 278 | + public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
|
| 279 | + $this->contentSecurityPolicy = $csp; |
|
| 280 | + return $this; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Get the currently used Content-Security-Policy |
|
| 285 | + * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
|
| 286 | + * none specified. |
|
| 287 | + * @since 8.1.0 |
|
| 288 | + */ |
|
| 289 | + public function getContentSecurityPolicy() { |
|
| 290 | + if ($this->contentSecurityPolicy === null) { |
|
| 291 | + $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy()); |
|
| 292 | + } |
|
| 293 | + return $this->contentSecurityPolicy; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @since 17.0.0 |
|
| 299 | + */ |
|
| 300 | + public function getFeaturePolicy(): EmptyFeaturePolicy { |
|
| 301 | + if ($this->featurePolicy === null) { |
|
| 302 | + $this->setFeaturePolicy(new EmptyFeaturePolicy()); |
|
| 303 | + } |
|
| 304 | + return $this->featurePolicy; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * @since 17.0.0 |
|
| 309 | + */ |
|
| 310 | + public function setFeaturePolicy(EmptyFeaturePolicy $featurePolicy): self { |
|
| 311 | + $this->featurePolicy = $featurePolicy; |
|
| 312 | + |
|
| 313 | + return $this; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Get response status |
|
| 320 | + * @since 6.0.0 |
|
| 321 | + */ |
|
| 322 | + public function getStatus() { |
|
| 323 | + return $this->status; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Get the ETag |
|
| 329 | + * @return string the etag |
|
| 330 | + * @since 6.0.0 |
|
| 331 | + */ |
|
| 332 | + public function getETag() { |
|
| 333 | + return $this->ETag; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Get "last modified" date |
|
| 339 | + * @return \DateTime RFC2822 formatted last modified date |
|
| 340 | + * @since 6.0.0 |
|
| 341 | + */ |
|
| 342 | + public function getLastModified() { |
|
| 343 | + return $this->lastModified; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Set the ETag |
|
| 349 | + * @param string $ETag |
|
| 350 | + * @return Response Reference to this object |
|
| 351 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 352 | + */ |
|
| 353 | + public function setETag($ETag) { |
|
| 354 | + $this->ETag = $ETag; |
|
| 355 | + |
|
| 356 | + return $this; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Set "last modified" date |
|
| 362 | + * @param \DateTime $lastModified |
|
| 363 | + * @return Response Reference to this object |
|
| 364 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 365 | + */ |
|
| 366 | + public function setLastModified($lastModified) { |
|
| 367 | + $this->lastModified = $lastModified; |
|
| 368 | + |
|
| 369 | + return $this; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Marks the response as to throttle. Will be throttled when the |
|
| 374 | + * @BruteForceProtection annotation is added. |
|
| 375 | + * |
|
| 376 | + * @param array $metadata |
|
| 377 | + * @since 12.0.0 |
|
| 378 | + */ |
|
| 379 | + public function throttle(array $metadata = []) { |
|
| 380 | + $this->throttled = true; |
|
| 381 | + $this->throttleMetadata = $metadata; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Returns the throttle metadata, defaults to empty array |
|
| 386 | + * |
|
| 387 | + * @return array |
|
| 388 | + * @since 13.0.0 |
|
| 389 | + */ |
|
| 390 | + public function getThrottleMetadata() { |
|
| 391 | + return $this->throttleMetadata; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * Whether the current response is throttled. |
|
| 396 | + * |
|
| 397 | + * @since 12.0.0 |
|
| 398 | + */ |
|
| 399 | + public function isThrottled() { |
|
| 400 | + return $this->throttled; |
|
| 401 | + } |
|
| 402 | 402 | } |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | public function cacheFor(int $cacheSeconds, bool $public = false) { |
| 109 | 109 | if ($cacheSeconds > 0) { |
| 110 | 110 | $pragma = $public ? 'public' : 'private'; |
| 111 | - $this->addHeader('Cache-Control', $pragma . ', max-age=' . $cacheSeconds . ', must-revalidate'); |
|
| 111 | + $this->addHeader('Cache-Control', $pragma.', max-age='.$cacheSeconds.', must-revalidate'); |
|
| 112 | 112 | $this->addHeader('Pragma', $pragma); |
| 113 | 113 | |
| 114 | 114 | // Set expires header |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | * @since 6.0.0 - return value was added in 7.0.0 |
| 197 | 197 | */ |
| 198 | 198 | public function addHeader($name, $value) { |
| 199 | - $name = trim($name); // always remove leading and trailing whitespace |
|
| 199 | + $name = trim($name); // always remove leading and trailing whitespace |
|
| 200 | 200 | // to be able to reliably check for security |
| 201 | 201 | // headers |
| 202 | 202 | |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | $this->headers['Feature-Policy'] = $this->getFeaturePolicy()->buildPolicy(); |
| 241 | 241 | |
| 242 | 242 | if ($this->ETag) { |
| 243 | - $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
| 243 | + $mergeWith['ETag'] = '"'.$this->ETag.'"'; |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | return array_merge($mergeWith, $this->headers); |