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