Passed
Push — master ( 4fbea3...7c15c6 )
by Roeland
12:56 queued 10s
created

NotModifiedMiddleware   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B afterController() 0 14 7
A __construct() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2020, Roeland Jago Douma <[email protected]>
6
 *
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OC\AppFramework\Middleware;
27
28
use OCP\AppFramework\Http;
29
use OCP\AppFramework\Http\Response;
30
use OCP\AppFramework\Middleware;
31
use OCP\IRequest;
32
33
class NotModifiedMiddleware extends Middleware {
34
	/** @var IRequest */
35
	private $request;
36
37
	public function __construct(IRequest $request) {
38
		$this->request = $request;
39
	}
40
41
	public function afterController($controller, $methodName, Response $response) {
42
		$etagHeader = $this->request->getHeader('IF_NONE_MATCH');
43
		if ($etagHeader !== '' && $response->getETag() !== null && trim($etagHeader) === '"' . $response->getETag() . '"') {
44
			$response->setStatus(Http::STATUS_NOT_MODIFIED);
45
			return $response;
46
		}
47
48
		$modifiedSinceHeader = $this->request->getHeader('IF_MODIFIED_SINCE');
49
		if ($modifiedSinceHeader !== '' && $response->getLastModified() !== null && trim($modifiedSinceHeader) === $response->getLastModified()->format(\DateTime::RFC2822)) {
50
			$response->setStatus(Http::STATUS_NOT_MODIFIED);
51
			return $response;
52
		}
53
54
		return $response;
55
	}
56
}
57