Source for file HttpContext.php

Documentation is available at HttpContext.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Web
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * HTTP-specific tasks.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Web
  20. 20:  */
  21. 21: class HttpContext extends Object
  22. 22: {
  23. 23:  
  24. 24:  
  25. 25:     /**
  26. 26:      * Attempts to cache the sent entity by its last modification date
  27. 27:      * @param  string|int|DateTime last modified time
  28. 28:      * @param  string  strong entity tag validator
  29. 29:      * @return bool 
  30. 30:      */
  31. 31:     public function isModified($lastModified NULL$etag NULL)
  32. 32:     {
  33. 33:         $response $this->getResponse();
  34. 34:         $request $this->getRequest();
  35. 35:  
  36. 36:         if ($lastModified{
  37. 37:             $response->setHeader('Last-Modified'$response->date($lastModified));
  38. 38:         }
  39. 39:         if ($etag{
  40. 40:             $response->setHeader('ETag''"' addslashes($etag'"');
  41. 41:         }
  42. 42:  
  43. 43:         $ifNoneMatch $request->getHeader('If-None-Match');
  44. 44:         if ($ifNoneMatch === '*'{
  45. 45:             $match TRUE// match, check if-modified-since
  46. 46:  
  47. 47:         elseif ($ifNoneMatch !== NULL{
  48. 48:             $etag $response->getHeader('ETag');
  49. 49:  
  50. 50:             if ($etag == NULL || strpos(' ' strtr($ifNoneMatch",\t"'  ')' ' $etag=== FALSE{
  51. 51:                 return TRUE;
  52. 52:  
  53. 53:             else {
  54. 54:                 $match TRUE// match, check if-modified-since
  55. 55:             }
  56. 56:         }
  57. 57:  
  58. 58:         $ifModifiedSince $request->getHeader('If-Modified-Since');
  59. 59:         if ($ifModifiedSince !== NULL{
  60. 60:             $lastModified $response->getHeader('Last-Modified');
  61. 61:             if ($lastModified != NULL && strtotime($lastModified<= strtotime($ifModifiedSince)) {
  62. 62:                 $match TRUE;
  63. 63:  
  64. 64:             else {
  65. 65:                 return TRUE;
  66. 66:             }
  67. 67:         }
  68. 68:  
  69. 69:         if (empty($match)) {
  70. 70:             return TRUE;
  71. 71:         }
  72. 72:  
  73. 73:         $response->setCode(IHttpResponse::S304_NOT_MODIFIED);
  74. 74:         return FALSE;
  75. 75:     }
  76. 76:  
  77. 77:  
  78. 78:  
  79. 79:     /********************* backend ****************d*g**/
  80. 80:  
  81. 81:  
  82. 82:  
  83. 83:     /**
  84. 84:      * @return IHttpRequest 
  85. 85:      */
  86. 86:     public function getRequest()
  87. 87:     {
  88. 88:         return Environment::getHttpRequest();
  89. 89:     }
  90. 90:  
  91. 91:  
  92. 92:  
  93. 93:     /**
  94. 94:      * @return IHttpResponse 
  95. 95:      */
  96. 96:     public function getResponse()
  97. 97:     {
  98. 98:         return Environment::getHttpResponse();
  99. 99:     }
  100. 100: