123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- <?php
- namespace OCP\AppFramework\Http;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\IConfig;
- use OCP\IRequest;
- use Psr\Log\LoggerInterface;
- class Response {
-
- private $headers;
-
- private $cookies = [];
-
- private $status;
-
- private $lastModified;
-
- private $ETag;
-
- private $contentSecurityPolicy = null;
-
- private $featurePolicy;
-
- private $throttled = false;
-
- private $throttleMetadata = [];
-
- public function __construct(int $status = Http::STATUS_OK, array $headers = []) {
- $this->setStatus($status);
- $this->setHeaders($headers);
- }
-
- public function cacheFor(int $cacheSeconds, bool $public = false, bool $immutable = false) {
- if ($cacheSeconds > 0) {
- $cacheStore = $public ? 'public' : 'private';
- $this->addHeader('Cache-Control', sprintf('%s, max-age=%s, %s', $cacheStore, $cacheSeconds, ($immutable ? 'immutable' : 'must-revalidate')));
-
- $expires = new \DateTime();
-
- $time = \OCP\Server::get(ITimeFactory::class);
- $expires->setTimestamp($time->getTime());
- $expires->add(new \DateInterval('PT' . $cacheSeconds . 'S'));
- $this->addHeader('Expires', $expires->format(\DateTimeInterface::RFC2822));
- } else {
- $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
- unset($this->headers['Expires']);
- }
- return $this;
- }
-
- public function addCookie($name, $value, ?\DateTime $expireDate = null, $sameSite = 'Lax') {
- $this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate, 'sameSite' => $sameSite];
- return $this;
- }
-
- public function setCookies(array $cookies) {
- $this->cookies = $cookies;
- return $this;
- }
-
- public function invalidateCookie($name) {
- $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00'));
- return $this;
- }
-
- public function invalidateCookies(array $cookieNames) {
- foreach ($cookieNames as $cookieName) {
- $this->invalidateCookie($cookieName);
- }
- return $this;
- }
-
- public function getCookies() {
- return $this->cookies;
- }
-
- public function addHeader($name, $value) {
- $name = trim($name);
-
-
- if ($this->status === Http::STATUS_NOT_MODIFIED
- && stripos($name, 'x-') === 0) {
-
- $config = \OC::$server->get(IConfig::class);
- if ($config->getSystemValueBool('debug', false)) {
- \OC::$server->get(LoggerInterface::class)->error('Setting custom header on a 304 is not supported (Header: {header})', [
- 'header' => $name,
- ]);
- }
- }
- if (is_null($value)) {
- unset($this->headers[$name]);
- } else {
- $this->headers[$name] = $value;
- }
- return $this;
- }
-
- public function setHeaders(array $headers): static {
-
- $this->headers = $headers;
- return $this;
- }
-
- public function getHeaders() {
-
-
- $request = \OC::$server->get(IRequest::class);
- $mergeWith = [
- 'X-Request-Id' => $request->getId(),
- 'Cache-Control' => 'no-cache, no-store, must-revalidate',
- 'Content-Security-Policy' => $this->getContentSecurityPolicy()->buildPolicy(),
- 'Feature-Policy' => $this->getFeaturePolicy()->buildPolicy(),
- 'X-Robots-Tag' => 'noindex, nofollow',
- ];
- if ($this->lastModified) {
- $mergeWith['Last-Modified'] = $this->lastModified->format(\DateTimeInterface::RFC2822);
- }
- if ($this->ETag) {
- $mergeWith['ETag'] = '"' . $this->ETag . '"';
- }
- return array_merge($mergeWith, $this->headers);
- }
-
- public function render() {
- return '';
- }
-
- public function setStatus($status): static {
-
- $this->status = $status;
- return $this;
- }
-
- public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) {
- $this->contentSecurityPolicy = $csp;
- return $this;
- }
-
- public function getContentSecurityPolicy() {
- if ($this->contentSecurityPolicy === null) {
- $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
- }
- return $this->contentSecurityPolicy;
- }
-
- public function getFeaturePolicy(): EmptyFeaturePolicy {
- if ($this->featurePolicy === null) {
- $this->setFeaturePolicy(new EmptyFeaturePolicy());
- }
- return $this->featurePolicy;
- }
-
- public function setFeaturePolicy(EmptyFeaturePolicy $featurePolicy): self {
- $this->featurePolicy = $featurePolicy;
- return $this;
- }
-
- public function getStatus() {
- return $this->status;
- }
-
- public function getETag() {
- return $this->ETag;
- }
-
- public function getLastModified() {
- return $this->lastModified;
- }
-
- public function setETag($ETag) {
- $this->ETag = $ETag;
- return $this;
- }
-
- public function setLastModified($lastModified) {
- $this->lastModified = $lastModified;
- return $this;
- }
-
- public function throttle(array $metadata = []) {
- $this->throttled = true;
- $this->throttleMetadata = $metadata;
- }
-
- public function getThrottleMetadata() {
- return $this->throttleMetadata;
- }
-
- public function isThrottled() {
- return $this->throttled;
- }
- }
|