Completed
Pull Request — master (#5948)
by Lukas
17:30
created

Response::cacheFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 * @author Thomas Tanghus <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
/**
29
 * Public interface of ownCloud for apps to use.
30
 * AppFramework\HTTP\Response class
31
 */
32
33
namespace OCP\AppFramework\Http;
34
35
use OCP\AppFramework\Http;
36
37
/**
38
 * Base class for responses. Also used to just send headers.
39
 *
40
 * It handles headers, HTTP status code, last modified and ETag.
41
 * @since 6.0.0
42
 */
43
class Response {
44
45
	/**
46
	 * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate']
47
	 * @var array
48
	 */
49
	private $headers = array(
50
		'Cache-Control' => 'no-cache, no-store, must-revalidate'
51
	);
52
53
54
	/**
55
	 * Cookies that will be need to be constructed as header
56
	 * @var array
57
	 */
58
	private $cookies = array();
59
60
61
	/**
62
	 * HTTP status code - defaults to STATUS OK
63
	 * @var int
64
	 */
65
	private $status = Http::STATUS_OK;
66
67
68
	/**
69
	 * Last modified date
70
	 * @var \DateTime
71
	 */
72
	private $lastModified;
73
74
75
	/**
76
	 * ETag
77
	 * @var string
78
	 */
79
	private $ETag;
80
81
	/** @var ContentSecurityPolicy|null Used Content-Security-Policy */
82
	private $contentSecurityPolicy = null;
83
84
	/** @var bool */
85
	private $throttled = false;
86
	/** @var array */
87
	private $throttleMetadata = [];
88
89
	/**
90
	 * Caches the response
91
	 * @param int $cacheSeconds the amount of seconds that should be cached
92
	 * if 0 then caching will be disabled
93
	 * @return $this
94
	 * @since 6.0.0 - return value was added in 7.0.0
95
	 */
96
	public function cacheFor($cacheSeconds) {
97
98
		if($cacheSeconds > 0) {
99
			$this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds . ', must-revalidate');
100
		} else {
101
			$this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
102
		}
103
104
		return $this;
105
	}
106
107
	/**
108
	 * Adds a new cookie to the response
109
	 * @param string $name The name of the cookie
110
	 * @param string $value The value of the cookie
111
	 * @param \DateTime|null $expireDate Date on that the cookie should expire, if set
112
	 * 									to null cookie will be considered as session
113
	 * 									cookie.
114
	 * @return $this
115
	 * @since 8.0.0
116
	 */
117
	public function addCookie($name, $value, \DateTime $expireDate = null) {
118
		$this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate);
119
		return $this;
120
	}
121
122
123
	/**
124
	 * Set the specified cookies
125
	 * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null))
126
	 * @return $this
127
	 * @since 8.0.0
128
	 */
129
	public function setCookies(array $cookies) {
130
		$this->cookies = $cookies;
131
		return $this;
132
	}
133
134
135
	/**
136
	 * Invalidates the specified cookie
137
	 * @param string $name
138
	 * @return $this
139
	 * @since 8.0.0
140
	 */
141
	public function invalidateCookie($name) {
142
		$this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00'));
143
		return $this;
144
	}
145
146
	/**
147
	 * Invalidates the specified cookies
148
	 * @param array $cookieNames array('foo', 'bar')
149
	 * @return $this
150
	 * @since 8.0.0
151
	 */
152
	public function invalidateCookies(array $cookieNames) {
153
		foreach($cookieNames as $cookieName) {
154
			$this->invalidateCookie($cookieName);
155
		}
156
		return $this;
157
	}
158
159
	/**
160
	 * Returns the cookies
161
	 * @return array
162
	 * @since 8.0.0
163
	 */
164
	public function getCookies() {
165
		return $this->cookies;
166
	}
167
168
	/**
169
	 * Adds a new header to the response that will be called before the render
170
	 * function
171
	 * @param string $name The name of the HTTP header
172
	 * @param string $value The value, null will delete it
173
	 * @return $this
174
	 * @since 6.0.0 - return value was added in 7.0.0
175
	 */
176 View Code Duplication
	public function addHeader($name, $value) {
177
		$name = trim($name);  // always remove leading and trailing whitespace
178
		                      // to be able to reliably check for security
179
		                      // headers
180
181
		if(is_null($value)) {
182
			unset($this->headers[$name]);
183
		} else {
184
			$this->headers[$name] = $value;
185
		}
186
187
		return $this;
188
	}
189
190
191
	/**
192
	 * Set the headers
193
	 * @param array $headers value header pairs
194
	 * @return $this
195
	 * @since 8.0.0
196
	 */
197
	public function setHeaders(array $headers) {
198
		$this->headers = $headers;
199
200
		return $this;
201
	}
202
203
204
	/**
205
	 * Returns the set headers
206
	 * @return array the headers
207
	 * @since 6.0.0
208
	 */
209
	public function getHeaders() {
210
		$mergeWith = [];
211
212
		if($this->lastModified) {
213
			$mergeWith['Last-Modified'] =
214
				$this->lastModified->format(\DateTime::RFC2822);
215
		}
216
217
		// Build Content-Security-Policy and use default if none has been specified
218
		if(is_null($this->contentSecurityPolicy)) {
219
			$this->setContentSecurityPolicy(new ContentSecurityPolicy());
220
		}
221
		$this->headers['Content-Security-Policy'] = $this->contentSecurityPolicy->buildPolicy();
222
223
		if($this->ETag) {
224
			$mergeWith['ETag'] = '"' . $this->ETag . '"';
225
		}
226
227
		return array_merge($mergeWith, $this->headers);
228
	}
229
230
231
	/**
232
	 * By default renders no output
233
	 * @return string|null
234
	 * @since 6.0.0
235
	 */
236
	public function render() {
237
		return null;
238
	}
239
240
241
	/**
242
	 * Set response status
243
	 * @param int $status a HTTP status code, see also the STATUS constants
244
	 * @return Response Reference to this object
245
	 * @since 6.0.0 - return value was added in 7.0.0
246
	 */
247
	public function setStatus($status) {
248
		$this->status = $status;
249
250
		return $this;
251
	}
252
253
	/**
254
	 * Set a Content-Security-Policy
255
	 * @param EmptyContentSecurityPolicy $csp Policy to set for the response object
256
	 * @return $this
257
	 * @since 8.1.0
258
	 */
259
	public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) {
260
		$this->contentSecurityPolicy = $csp;
0 ignored issues
show
Documentation Bug introduced by
It seems like $csp of type object<OCP\AppFramework\...yContentSecurityPolicy> is incompatible with the declared type object<OCP\AppFramework\...entSecurityPolicy>|null of property $contentSecurityPolicy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
261
		return $this;
262
	}
263
264
	/**
265
	 * Get the currently used Content-Security-Policy
266
	 * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if
267
	 *                                    none specified.
268
	 * @since 8.1.0
269
	 */
270
	public function getContentSecurityPolicy() {
271
		return $this->contentSecurityPolicy;
272
	}
273
274
275
	/**
276
	 * Get response status
277
	 * @since 6.0.0
278
	 */
279
	public function getStatus() {
280
		return $this->status;
281
	}
282
283
284
	/**
285
	 * Get the ETag
286
	 * @return string the etag
287
	 * @since 6.0.0
288
	 */
289
	public function getETag() {
290
		return $this->ETag;
291
	}
292
293
294
	/**
295
	 * Get "last modified" date
296
	 * @return \DateTime RFC2822 formatted last modified date
297
	 * @since 6.0.0
298
	 */
299
	public function getLastModified() {
300
		return $this->lastModified;
301
	}
302
303
304
	/**
305
	 * Set the ETag
306
	 * @param string $ETag
307
	 * @return Response Reference to this object
308
	 * @since 6.0.0 - return value was added in 7.0.0
309
	 */
310
	public function setETag($ETag) {
311
		$this->ETag = $ETag;
312
313
		return $this;
314
	}
315
316
317
	/**
318
	 * Set "last modified" date
319
	 * @param \DateTime $lastModified
320
	 * @return Response Reference to this object
321
	 * @since 6.0.0 - return value was added in 7.0.0
322
	 */
323
	public function setLastModified($lastModified) {
324
		$this->lastModified = $lastModified;
325
326
		return $this;
327
	}
328
329
	/**
330
	 * Marks the response as to throttle. Will be throttled when the
331
	 * @BruteForceProtection annotation is added.
332
	 *
333
	 * @param array $metadata
334
	 * @since 12.0.0
335
	 */
336
	public function throttle(array $metadata = []) {
337
		$this->throttled = true;
338
		$this->throttleMetadata = $metadata;
339
	}
340
341
	/**
342
	 * Returns the throttle metadata, defaults to empty array
343
	 *
344
	 * @return array
345
	 * @since 13.0.0
346
	 */
347
	public function getThrottleMetadata() {
348
		return $this->throttleMetadata;
349
	}
350
351
	/**
352
	 * Whether the current response is throttled.
353
	 *
354
	 * @since 12.0.0
355
	 */
356
	public function isThrottled() {
357
		return $this->throttled;
358
	}
359
}
360