1
0

Response.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@owncloud.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Thomas Tanghus <thomas@tanghus.net>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. /**
  30. * Public interface of ownCloud for apps to use.
  31. * AppFramework\HTTP\Response class
  32. */
  33. namespace OCP\AppFramework\Http;
  34. use OCP\AppFramework\Http;
  35. use OCP\AppFramework\Utility\ITimeFactory;
  36. /**
  37. * Base class for responses. Also used to just send headers.
  38. *
  39. * It handles headers, HTTP status code, last modified and ETag.
  40. * @since 6.0.0
  41. */
  42. class Response {
  43. /**
  44. * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate']
  45. * @var array
  46. */
  47. private $headers = array(
  48. 'Cache-Control' => 'no-cache, no-store, must-revalidate'
  49. );
  50. /**
  51. * Cookies that will be need to be constructed as header
  52. * @var array
  53. */
  54. private $cookies = array();
  55. /**
  56. * HTTP status code - defaults to STATUS OK
  57. * @var int
  58. */
  59. private $status = Http::STATUS_OK;
  60. /**
  61. * Last modified date
  62. * @var \DateTime
  63. */
  64. private $lastModified;
  65. /**
  66. * ETag
  67. * @var string
  68. */
  69. private $ETag;
  70. /** @var ContentSecurityPolicy|null Used Content-Security-Policy */
  71. private $contentSecurityPolicy = null;
  72. /** @var bool */
  73. private $throttled = false;
  74. /** @var array */
  75. private $throttleMetadata = [];
  76. /**
  77. * Caches the response
  78. * @param int $cacheSeconds the amount of seconds that should be cached
  79. * if 0 then caching will be disabled
  80. * @return $this
  81. * @since 6.0.0 - return value was added in 7.0.0
  82. */
  83. public function cacheFor(int $cacheSeconds) {
  84. if($cacheSeconds > 0) {
  85. $this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds . ', must-revalidate');
  86. // Old scool prama caching
  87. $this->addHeader('Pragma', 'public');
  88. // Set expires header
  89. $expires = new \DateTime();
  90. /** @var ITimeFactory $time */
  91. $time = \OC::$server->query(ITimeFactory::class);
  92. $expires->setTimestamp($time->getTime());
  93. $expires->add(new \DateInterval('PT'.$cacheSeconds.'S'));
  94. $this->addHeader('Expires', $expires->format(\DateTime::RFC2822));
  95. } else {
  96. $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
  97. unset($this->headers['Expires'], $this->headers['Pragma']);
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Adds a new cookie to the response
  103. * @param string $name The name of the cookie
  104. * @param string $value The value of the cookie
  105. * @param \DateTime|null $expireDate Date on that the cookie should expire, if set
  106. * to null cookie will be considered as session
  107. * cookie.
  108. * @return $this
  109. * @since 8.0.0
  110. */
  111. public function addCookie($name, $value, \DateTime $expireDate = null) {
  112. $this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate);
  113. return $this;
  114. }
  115. /**
  116. * Set the specified cookies
  117. * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null))
  118. * @return $this
  119. * @since 8.0.0
  120. */
  121. public function setCookies(array $cookies) {
  122. $this->cookies = $cookies;
  123. return $this;
  124. }
  125. /**
  126. * Invalidates the specified cookie
  127. * @param string $name
  128. * @return $this
  129. * @since 8.0.0
  130. */
  131. public function invalidateCookie($name) {
  132. $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00'));
  133. return $this;
  134. }
  135. /**
  136. * Invalidates the specified cookies
  137. * @param array $cookieNames array('foo', 'bar')
  138. * @return $this
  139. * @since 8.0.0
  140. */
  141. public function invalidateCookies(array $cookieNames) {
  142. foreach($cookieNames as $cookieName) {
  143. $this->invalidateCookie($cookieName);
  144. }
  145. return $this;
  146. }
  147. /**
  148. * Returns the cookies
  149. * @return array
  150. * @since 8.0.0
  151. */
  152. public function getCookies() {
  153. return $this->cookies;
  154. }
  155. /**
  156. * Adds a new header to the response that will be called before the render
  157. * function
  158. * @param string $name The name of the HTTP header
  159. * @param string $value The value, null will delete it
  160. * @return $this
  161. * @since 6.0.0 - return value was added in 7.0.0
  162. */
  163. public function addHeader($name, $value) {
  164. $name = trim($name); // always remove leading and trailing whitespace
  165. // to be able to reliably check for security
  166. // headers
  167. if(is_null($value)) {
  168. unset($this->headers[$name]);
  169. } else {
  170. $this->headers[$name] = $value;
  171. }
  172. return $this;
  173. }
  174. /**
  175. * Set the headers
  176. * @param array $headers value header pairs
  177. * @return $this
  178. * @since 8.0.0
  179. */
  180. public function setHeaders(array $headers) {
  181. $this->headers = $headers;
  182. return $this;
  183. }
  184. /**
  185. * Returns the set headers
  186. * @return array the headers
  187. * @since 6.0.0
  188. */
  189. public function getHeaders() {
  190. $mergeWith = [];
  191. if($this->lastModified) {
  192. $mergeWith['Last-Modified'] =
  193. $this->lastModified->format(\DateTime::RFC2822);
  194. }
  195. // Build Content-Security-Policy and use default if none has been specified
  196. if(is_null($this->contentSecurityPolicy)) {
  197. $this->setContentSecurityPolicy(new ContentSecurityPolicy());
  198. }
  199. $this->headers['Content-Security-Policy'] = $this->contentSecurityPolicy->buildPolicy();
  200. if($this->ETag) {
  201. $mergeWith['ETag'] = '"' . $this->ETag . '"';
  202. }
  203. return array_merge($mergeWith, $this->headers);
  204. }
  205. /**
  206. * By default renders no output
  207. * @return string
  208. * @since 6.0.0
  209. */
  210. public function render() {
  211. return '';
  212. }
  213. /**
  214. * Set response status
  215. * @param int $status a HTTP status code, see also the STATUS constants
  216. * @return Response Reference to this object
  217. * @since 6.0.0 - return value was added in 7.0.0
  218. */
  219. public function setStatus($status) {
  220. $this->status = $status;
  221. return $this;
  222. }
  223. /**
  224. * Set a Content-Security-Policy
  225. * @param EmptyContentSecurityPolicy $csp Policy to set for the response object
  226. * @return $this
  227. * @since 8.1.0
  228. */
  229. public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) {
  230. $this->contentSecurityPolicy = $csp;
  231. return $this;
  232. }
  233. /**
  234. * Get the currently used Content-Security-Policy
  235. * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if
  236. * none specified.
  237. * @since 8.1.0
  238. */
  239. public function getContentSecurityPolicy() {
  240. return $this->contentSecurityPolicy;
  241. }
  242. /**
  243. * Get response status
  244. * @since 6.0.0
  245. */
  246. public function getStatus() {
  247. return $this->status;
  248. }
  249. /**
  250. * Get the ETag
  251. * @return string the etag
  252. * @since 6.0.0
  253. */
  254. public function getETag() {
  255. return $this->ETag;
  256. }
  257. /**
  258. * Get "last modified" date
  259. * @return \DateTime RFC2822 formatted last modified date
  260. * @since 6.0.0
  261. */
  262. public function getLastModified() {
  263. return $this->lastModified;
  264. }
  265. /**
  266. * Set the ETag
  267. * @param string $ETag
  268. * @return Response Reference to this object
  269. * @since 6.0.0 - return value was added in 7.0.0
  270. */
  271. public function setETag($ETag) {
  272. $this->ETag = $ETag;
  273. return $this;
  274. }
  275. /**
  276. * Set "last modified" date
  277. * @param \DateTime $lastModified
  278. * @return Response Reference to this object
  279. * @since 6.0.0 - return value was added in 7.0.0
  280. */
  281. public function setLastModified($lastModified) {
  282. $this->lastModified = $lastModified;
  283. return $this;
  284. }
  285. /**
  286. * Marks the response as to throttle. Will be throttled when the
  287. * @BruteForceProtection annotation is added.
  288. *
  289. * @param array $metadata
  290. * @since 12.0.0
  291. */
  292. public function throttle(array $metadata = []) {
  293. $this->throttled = true;
  294. $this->throttleMetadata = $metadata;
  295. }
  296. /**
  297. * Returns the throttle metadata, defaults to empty array
  298. *
  299. * @return array
  300. * @since 13.0.0
  301. */
  302. public function getThrottleMetadata() {
  303. return $this->throttleMetadata;
  304. }
  305. /**
  306. * Whether the current response is throttled.
  307. *
  308. * @since 12.0.0
  309. */
  310. public function isThrottled() {
  311. return $this->throttled;
  312. }
  313. }