Total Complexity | 5 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
9 | class IfNoneMatch extends Middleware |
||
10 | { |
||
11 | public string $middleware = 'ifNoneMatch'; |
||
12 | |||
13 | /** |
||
14 | * Handle an incoming request. |
||
15 | * |
||
16 | * @param \Illuminate\Http\Request $request |
||
17 | * @param \Closure $next |
||
18 | * @return mixed |
||
19 | */ |
||
20 | public function handle(Request $request, Closure $next) |
||
21 | { |
||
22 | // Handle request |
||
23 | $method = $request->getMethod(); |
||
24 | |||
25 | // Support using HEAD method for checking If-None-Match |
||
26 | if ($request->isMethod('HEAD')) { |
||
27 | $request->setMethod('GET'); |
||
28 | } |
||
29 | |||
30 | //Handle response |
||
31 | $response = $next($request); |
||
32 | |||
33 | $etag = EtagConditionals::getEtag($request, $response); |
||
34 | $noneMatch = $request->getETags(); |
||
35 | |||
36 | // Strip W/ if weak comparison algorithm can be used |
||
37 | if (config('etagconditionals.if_none_match_weak')) { |
||
38 | $noneMatch = array_map([$this, 'stripWeakTags'], $noneMatch); |
||
39 | } |
||
40 | |||
41 | if (in_array($etag, $noneMatch)) { |
||
42 | $response->setNotModified(); |
||
43 | } |
||
44 | |||
45 | $request->setMethod($method); |
||
46 | |||
47 | return $response; |
||
48 | } |
||
49 | |||
50 | private function stripWeakTags($etag) |
||
53 | } |
||
54 | } |
||
55 |