PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/nette/http/src/Http/Context.php

https://gitlab.com/kubinos/writeoff
PHP | 103 lines | 55 code | 23 blank | 25 comment | 13 complexity | 62f78ffc4c5a8dc7c5d410a9225d4d5a MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (http://nette.org)
  4. * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  5. */
  6. namespace Nette\Http;
  7. use Nette;
  8. /**
  9. * HTTP-specific tasks.
  10. *
  11. * @property-read bool $modified
  12. * @property-read IRequest $request
  13. * @property-read IResponse $response
  14. */
  15. class Context extends Nette\Object
  16. {
  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|\DateTime last modified time
  29. * @param string strong entity tag validator
  30. * @return bool
  31. */
  32. public function isModified($lastModified = NULL, $etag = NULL)
  33. {
  34. if ($lastModified) {
  35. $this->response->setHeader('Last-Modified', Helpers::formatDate($lastModified));
  36. }
  37. if ($etag) {
  38. $this->response->setHeader('ETag', '"' . addslashes($etag) . '"');
  39. }
  40. $ifNoneMatch = $this->request->getHeader('If-None-Match');
  41. if ($ifNoneMatch === '*') {
  42. $match = TRUE; // match, check if-modified-since
  43. } elseif ($ifNoneMatch !== NULL) {
  44. $etag = $this->response->getHeader('ETag');
  45. if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === FALSE) {
  46. return TRUE;
  47. } else {
  48. $match = TRUE; // 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_NOT_MODIFIED);
  64. return FALSE;
  65. }
  66. /**
  67. * @return IRequest
  68. */
  69. public function getRequest()
  70. {
  71. return $this->request;
  72. }
  73. /**
  74. * @return IResponse
  75. */
  76. public function getResponse()
  77. {
  78. return $this->response;
  79. }
  80. }