2013-08-17 09:16:48 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2013-08-17 09:16:48 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public interface of ownCloud for apps to use.
|
|
|
|
* AppFramework\HTTP\Response class
|
|
|
|
*/
|
|
|
|
|
2013-08-20 23:00:26 +00:00
|
|
|
namespace OCP\AppFramework\Http;
|
2013-08-17 09:16:48 +00:00
|
|
|
|
2013-10-23 03:57:34 +00:00
|
|
|
use OCP\AppFramework\Http;
|
2018-05-29 09:10:07 +00:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
/**
|
2013-10-16 22:07:29 +00:00
|
|
|
* Base class for responses. Also used to just send headers.
|
|
|
|
*
|
|
|
|
* It handles headers, HTTP status code, last modified and ETag.
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
class Response {
|
|
|
|
|
|
|
|
/**
|
2017-01-09 20:29:59 +00:00
|
|
|
* Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate']
|
2013-10-16 22:07:29 +00:00
|
|
|
* @var array
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
private $headers = array(
|
2017-01-09 20:29:59 +00:00
|
|
|
'Cache-Control' => 'no-cache, no-store, must-revalidate'
|
2013-08-17 09:16:48 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
2014-11-27 13:19:00 +00:00
|
|
|
/**
|
|
|
|
* Cookies that will be need to be constructed as header
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $cookies = array();
|
|
|
|
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
/**
|
2013-10-16 22:07:29 +00:00
|
|
|
* HTTP status code - defaults to STATUS OK
|
2014-11-27 13:19:00 +00:00
|
|
|
* @var int
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
private $status = Http::STATUS_OK;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-16 22:07:29 +00:00
|
|
|
* Last modified date
|
2013-08-17 09:16:48 +00:00
|
|
|
* @var \DateTime
|
|
|
|
*/
|
|
|
|
private $lastModified;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-16 22:07:29 +00:00
|
|
|
* ETag
|
2013-08-17 09:16:48 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $ETag;
|
|
|
|
|
2015-02-09 15:30:01 +00:00
|
|
|
/** @var ContentSecurityPolicy|null Used Content-Security-Policy */
|
|
|
|
private $contentSecurityPolicy = null;
|
|
|
|
|
2017-04-13 20:50:44 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $throttled = false;
|
2017-07-27 12:14:20 +00:00
|
|
|
/** @var array */
|
|
|
|
private $throttleMetadata = [];
|
2013-08-17 09:16:48 +00:00
|
|
|
|
2019-04-03 16:42:34 +00:00
|
|
|
/**
|
|
|
|
* Response constructor.
|
|
|
|
*
|
|
|
|
* @since 17.0.0
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
|
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
/**
|
|
|
|
* Caches the response
|
|
|
|
* @param int $cacheSeconds the amount of seconds that should be cached
|
|
|
|
* if 0 then caching will be disabled
|
2014-11-27 13:19:00 +00:00
|
|
|
* @return $this
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0 - return value was added in 7.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
2018-05-29 09:10:07 +00:00
|
|
|
public function cacheFor(int $cacheSeconds) {
|
2013-08-17 09:16:48 +00:00
|
|
|
if($cacheSeconds > 0) {
|
2016-08-29 12:55:23 +00:00
|
|
|
$this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds . ', must-revalidate');
|
2018-05-29 09:10:07 +00:00
|
|
|
|
|
|
|
// Old scool prama caching
|
|
|
|
$this->addHeader('Pragma', 'public');
|
|
|
|
|
|
|
|
// Set expires header
|
|
|
|
$expires = new \DateTime();
|
|
|
|
/** @var ITimeFactory $time */
|
|
|
|
$time = \OC::$server->query(ITimeFactory::class);
|
|
|
|
$expires->setTimestamp($time->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT'.$cacheSeconds.'S'));
|
|
|
|
$this->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
2013-08-17 09:16:48 +00:00
|
|
|
} else {
|
2015-06-15 16:35:41 +00:00
|
|
|
$this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
|
2018-05-29 09:10:07 +00:00
|
|
|
unset($this->headers['Expires'], $this->headers['Pragma']);
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 22:01:16 +00:00
|
|
|
return $this;
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 13:19:00 +00:00
|
|
|
/**
|
|
|
|
* Adds a new cookie to the response
|
|
|
|
* @param string $name The name of the cookie
|
|
|
|
* @param string $value The value of the cookie
|
|
|
|
* @param \DateTime|null $expireDate Date on that the cookie should expire, if set
|
|
|
|
* to null cookie will be considered as session
|
|
|
|
* cookie.
|
|
|
|
* @return $this
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 8.0.0
|
2014-11-27 13:19:00 +00:00
|
|
|
*/
|
|
|
|
public function addCookie($name, $value, \DateTime $expireDate = null) {
|
|
|
|
$this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the specified cookies
|
|
|
|
* @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null))
|
|
|
|
* @return $this
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 8.0.0
|
2014-11-27 13:19:00 +00:00
|
|
|
*/
|
|
|
|
public function setCookies(array $cookies) {
|
|
|
|
$this->cookies = $cookies;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates the specified cookie
|
|
|
|
* @param string $name
|
|
|
|
* @return $this
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 8.0.0
|
2014-11-27 13:19:00 +00:00
|
|
|
*/
|
|
|
|
public function invalidateCookie($name) {
|
|
|
|
$this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00'));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates the specified cookies
|
|
|
|
* @param array $cookieNames array('foo', 'bar')
|
|
|
|
* @return $this
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 8.0.0
|
2014-11-27 13:19:00 +00:00
|
|
|
*/
|
|
|
|
public function invalidateCookies(array $cookieNames) {
|
|
|
|
foreach($cookieNames as $cookieName) {
|
|
|
|
$this->invalidateCookie($cookieName);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the cookies
|
|
|
|
* @return array
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 8.0.0
|
2014-11-27 13:19:00 +00:00
|
|
|
*/
|
|
|
|
public function getCookies() {
|
|
|
|
return $this->cookies;
|
|
|
|
}
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new header to the response that will be called before the render
|
|
|
|
* function
|
|
|
|
* @param string $name The name of the HTTP header
|
|
|
|
* @param string $value The value, null will delete it
|
2014-11-27 13:19:00 +00:00
|
|
|
* @return $this
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0 - return value was added in 7.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function addHeader($name, $value) {
|
2014-05-08 09:47:18 +00:00
|
|
|
$name = trim($name); // always remove leading and trailing whitespace
|
2014-10-28 15:34:04 +00:00
|
|
|
// to be able to reliably check for security
|
2014-05-08 09:47:18 +00:00
|
|
|
// headers
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
if(is_null($value)) {
|
|
|
|
unset($this->headers[$name]);
|
|
|
|
} else {
|
|
|
|
$this->headers[$name] = $value;
|
|
|
|
}
|
2014-03-09 22:01:16 +00:00
|
|
|
|
|
|
|
return $this;
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-28 15:34:04 +00:00
|
|
|
/**
|
|
|
|
* Set the headers
|
2014-11-27 13:19:00 +00:00
|
|
|
* @param array $headers value header pairs
|
|
|
|
* @return $this
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 8.0.0
|
2014-10-28 15:34:04 +00:00
|
|
|
*/
|
2014-11-27 13:19:00 +00:00
|
|
|
public function setHeaders(array $headers) {
|
2014-10-28 15:34:04 +00:00
|
|
|
$this->headers = $headers;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
/**
|
|
|
|
* Returns the set headers
|
|
|
|
* @return array the headers
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function getHeaders() {
|
2015-02-09 15:30:01 +00:00
|
|
|
$mergeWith = [];
|
2013-11-03 12:51:39 +00:00
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
if($this->lastModified) {
|
2013-11-03 12:51:39 +00:00
|
|
|
$mergeWith['Last-Modified'] =
|
2013-08-17 09:16:48 +00:00
|
|
|
$this->lastModified->format(\DateTime::RFC2822);
|
|
|
|
}
|
|
|
|
|
2015-02-09 15:30:01 +00:00
|
|
|
// Build Content-Security-Policy and use default if none has been specified
|
|
|
|
if(is_null($this->contentSecurityPolicy)) {
|
|
|
|
$this->setContentSecurityPolicy(new ContentSecurityPolicy());
|
|
|
|
}
|
|
|
|
$this->headers['Content-Security-Policy'] = $this->contentSecurityPolicy->buildPolicy();
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
if($this->ETag) {
|
|
|
|
$mergeWith['ETag'] = '"' . $this->ETag . '"';
|
|
|
|
}
|
2013-11-03 12:51:39 +00:00
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
return array_merge($mergeWith, $this->headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* By default renders no output
|
2018-02-21 12:28:40 +00:00
|
|
|
* @return string
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function render() {
|
2018-02-21 12:28:40 +00:00
|
|
|
return '';
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-16 15:00:08 +00:00
|
|
|
* Set response status
|
|
|
|
* @param int $status a HTTP status code, see also the STATUS constants
|
|
|
|
* @return Response Reference to this object
|
|
|
|
* @since 6.0.0 - return value was added in 7.0.0
|
|
|
|
*/
|
2013-08-17 09:16:48 +00:00
|
|
|
public function setStatus($status) {
|
|
|
|
$this->status = $status;
|
2014-03-09 22:01:16 +00:00
|
|
|
|
|
|
|
return $this;
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 15:30:01 +00:00
|
|
|
/**
|
|
|
|
* Set a Content-Security-Policy
|
2016-09-15 09:26:24 +00:00
|
|
|
* @param EmptyContentSecurityPolicy $csp Policy to set for the response object
|
2015-02-09 15:30:01 +00:00
|
|
|
* @return $this
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 8.1.0
|
2015-02-09 15:30:01 +00:00
|
|
|
*/
|
2016-09-15 09:26:24 +00:00
|
|
|
public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) {
|
2015-02-09 15:30:01 +00:00
|
|
|
$this->contentSecurityPolicy = $csp;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the currently used Content-Security-Policy
|
2016-09-15 09:26:24 +00:00
|
|
|
* @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if
|
2015-02-09 15:30:01 +00:00
|
|
|
* none specified.
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 8.1.0
|
2015-02-09 15:30:01 +00:00
|
|
|
*/
|
|
|
|
public function getContentSecurityPolicy() {
|
|
|
|
return $this->contentSecurityPolicy;
|
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get response status
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function getStatus() {
|
|
|
|
return $this->status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-16 22:07:29 +00:00
|
|
|
* Get the ETag
|
2013-08-17 09:16:48 +00:00
|
|
|
* @return string the etag
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function getETag() {
|
|
|
|
return $this->ETag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-16 22:07:29 +00:00
|
|
|
* Get "last modified" date
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return \DateTime RFC2822 formatted last modified date
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function getLastModified() {
|
|
|
|
return $this->lastModified;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-16 22:07:29 +00:00
|
|
|
* Set the ETag
|
2013-08-17 09:16:48 +00:00
|
|
|
* @param string $ETag
|
2014-03-10 08:31:30 +00:00
|
|
|
* @return Response Reference to this object
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0 - return value was added in 7.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function setETag($ETag) {
|
|
|
|
$this->ETag = $ETag;
|
2014-03-09 22:01:16 +00:00
|
|
|
|
|
|
|
return $this;
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-16 22:07:29 +00:00
|
|
|
* Set "last modified" date
|
2013-08-17 09:16:48 +00:00
|
|
|
* @param \DateTime $lastModified
|
2014-03-10 08:31:30 +00:00
|
|
|
* @return Response Reference to this object
|
2015-04-16 15:00:08 +00:00
|
|
|
* @since 6.0.0 - return value was added in 7.0.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
|
|
|
public function setLastModified($lastModified) {
|
|
|
|
$this->lastModified = $lastModified;
|
2014-03-09 22:01:16 +00:00
|
|
|
|
|
|
|
return $this;
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 20:50:44 +00:00
|
|
|
/**
|
|
|
|
* Marks the response as to throttle. Will be throttled when the
|
|
|
|
* @BruteForceProtection annotation is added.
|
|
|
|
*
|
2017-07-27 12:14:20 +00:00
|
|
|
* @param array $metadata
|
2017-04-13 20:50:44 +00:00
|
|
|
* @since 12.0.0
|
|
|
|
*/
|
2017-07-27 12:14:20 +00:00
|
|
|
public function throttle(array $metadata = []) {
|
2017-04-13 20:50:44 +00:00
|
|
|
$this->throttled = true;
|
2017-07-27 12:14:20 +00:00
|
|
|
$this->throttleMetadata = $metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the throttle metadata, defaults to empty array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @since 13.0.0
|
|
|
|
*/
|
|
|
|
public function getThrottleMetadata() {
|
|
|
|
return $this->throttleMetadata;
|
2017-04-13 20:50:44 +00:00
|
|
|
}
|
2013-08-17 09:16:48 +00:00
|
|
|
|
2017-04-13 20:50:44 +00:00
|
|
|
/**
|
|
|
|
* Whether the current response is throttled.
|
|
|
|
*
|
|
|
|
* @since 12.0.0
|
|
|
|
*/
|
|
|
|
public function isThrottled() {
|
|
|
|
return $this->throttled;
|
|
|
|
}
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|