PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / http / src / Http / Context.php

Context.php in Packeta 2.0.9, at deps/nette/http/src/Http/Context.php

75 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace Packetery\Nette\Http;
9
10 use Packetery\Nette;
11 /**
12 * HTTP-specific tasks.
13 */
14 class Context
15 {
16 use \Packetery\Nette\SmartObject;
17 /** @var IRequest */
18 private $request;
19 /** @var IResponse */
20 private $response;
21 public function __construct(IRequest $request, IResponse $response)
22 {
23 $this->request = $request;
24 $this->response = $response;
25 }
26 /**
27 * Attempts to cache the sent entity by its last modification date.
28 * @param string|int|\DateTimeInterface $lastModified
29 */
30 public function isModified($lastModified = null, ?string $etag = null) : bool
31 {
32 if ($lastModified) {
33 $this->response->setHeader('Last-Modified', Helpers::formatDate($lastModified));
34 }
35 if ($etag) {
36 $this->response->setHeader('ETag', '"' . \addslashes($etag) . '"');
37 }
38 $ifNoneMatch = $this->request->getHeader('If-None-Match');
39 if ($ifNoneMatch === '*') {
40 $match = \true;
41 // match, check if-modified-since
42 } elseif ($ifNoneMatch !== null) {
43 $etag = $this->response->getHeader('ETag');
44 if ($etag === null || \strpos(' ' . \strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === \false) {
45 return \true;
46 } else {
47 $match = \true;
48 // match, check if-modified-since
49 }
50 }
51 $ifModifiedSince = $this->request->getHeader('If-Modified-Since');
52 if ($ifModifiedSince !== null) {
53 $lastModified = $this->response->getHeader('Last-Modified');
54 if ($lastModified !== null && \strtotime($lastModified) <= \strtotime($ifModifiedSince)) {
55 $match = \true;
56 } else {
57 return \true;
58 }
59 }
60 if (empty($match)) {
61 return \true;
62 }
63 $this->response->setCode(IResponse::S304_NotModified);
64 return \false;
65 }
66 public function getRequest() : IRequest
67 {
68 return $this->request;
69 }
70 public function getResponse() : IResponse
71 {
72 return $this->response;
73 }
74 }
75