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