PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/google-analytics-dashboard-for-wp/tools/src/Google/Http/CacheParser.php

https://gitlab.com/bhargavi_dcw/dflocal
PHP | 182 lines | 93 code | 7 blank | 82 comment | 30 complexity | 6f05a1df0747b486aa7417e76b5818f0 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2012 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
  18. /**
  19. * Implement the caching directives specified in rfc2616.
  20. * This
  21. * implementation is guided by the guidance offered in rfc2616-sec13.
  22. *
  23. * @author Chirag Shah <chirags@google.com>
  24. */
  25. class Google_Http_CacheParser
  26. {
  27. public static $CACHEABLE_HTTP_METHODS = array(
  28. 'GET',
  29. 'HEAD'
  30. );
  31. public static $CACHEABLE_STATUS_CODES = array(
  32. '200',
  33. '203',
  34. '300',
  35. '301'
  36. );
  37. /**
  38. * Check if an HTTP request can be cached by a private local cache.
  39. *
  40. * @static
  41. *
  42. * @param Google_Http_Request $resp
  43. * @return bool True if the request is cacheable.
  44. * False if the request is uncacheable.
  45. */
  46. public static function isRequestCacheable(Google_Http_Request $resp)
  47. {
  48. $method = $resp->getRequestMethod();
  49. if (! in_array($method, self::$CACHEABLE_HTTP_METHODS)) {
  50. return false;
  51. }
  52. // Don't cache authorized requests/responses.
  53. // [rfc2616-14.8] When a shared cache receives a request containing an
  54. // Authorization field, it MUST NOT return the corresponding response
  55. // as a reply to any other request...
  56. if ($resp->getRequestHeader("authorization")) {
  57. return false;
  58. }
  59. return true;
  60. }
  61. /**
  62. * Check if an HTTP response can be cached by a private local cache.
  63. *
  64. * @static
  65. *
  66. * @param Google_Http_Request $resp
  67. * @return bool True if the response is cacheable.
  68. * False if the response is un-cacheable.
  69. */
  70. public static function isResponseCacheable(Google_Http_Request $resp)
  71. {
  72. // First, check if the HTTP request was cacheable before inspecting the
  73. // HTTP response.
  74. if (false == self::isRequestCacheable($resp)) {
  75. return false;
  76. }
  77. $code = $resp->getResponseHttpCode();
  78. if (! in_array($code, self::$CACHEABLE_STATUS_CODES)) {
  79. return false;
  80. }
  81. // The resource is uncacheable if the resource is already expired and
  82. // the resource doesn't have an ETag for revalidation.
  83. $etag = $resp->getResponseHeader("etag");
  84. if (self::isExpired($resp) && $etag == false) {
  85. return false;
  86. }
  87. // [rfc2616-14.9.2] If [no-store is] sent in a response, a cache MUST NOT
  88. // store any part of either this response or the request that elicited it.
  89. $cacheControl = $resp->getParsedCacheControl();
  90. if (isset($cacheControl['no-store'])) {
  91. return false;
  92. }
  93. // Pragma: no-cache is an http request directive, but is occasionally
  94. // used as a response header incorrectly.
  95. $pragma = $resp->getResponseHeader('pragma');
  96. if ($pragma == 'no-cache' || strpos($pragma, 'no-cache') !== false) {
  97. return false;
  98. }
  99. // [rfc2616-14.44] Vary: * is extremely difficult to cache. "It implies that
  100. // a cache cannot determine from the request headers of a subsequent request
  101. // whether this response is the appropriate representation."
  102. // Given this, we deem responses with the Vary header as uncacheable.
  103. $vary = $resp->getResponseHeader('vary');
  104. if ($vary) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. /**
  110. *
  111. * @static
  112. *
  113. * @param Google_Http_Request $resp
  114. * @return bool True if the HTTP response is considered to be expired.
  115. * False if it is considered to be fresh.
  116. */
  117. public static function isExpired(Google_Http_Request $resp)
  118. {
  119. // HTTP/1.1 clients and caches MUST treat other invalid date formats,
  120. // especially including the value “0”, as in the past.
  121. $parsedExpires = false;
  122. $responseHeaders = $resp->getResponseHeaders();
  123. if (isset($responseHeaders['expires'])) {
  124. $rawExpires = $responseHeaders['expires'];
  125. // Check for a malformed expires header first.
  126. if (empty($rawExpires) || (is_numeric($rawExpires) && $rawExpires <= 0)) {
  127. return true;
  128. }
  129. // See if we can parse the expires header.
  130. $parsedExpires = strtotime($rawExpires);
  131. if (false == $parsedExpires || $parsedExpires <= 0) {
  132. return true;
  133. }
  134. }
  135. // Calculate the freshness of an http response.
  136. $freshnessLifetime = false;
  137. $cacheControl = $resp->getParsedCacheControl();
  138. if (isset($cacheControl['max-age'])) {
  139. $freshnessLifetime = $cacheControl['max-age'];
  140. }
  141. $rawDate = $resp->getResponseHeader('date');
  142. $parsedDate = strtotime($rawDate);
  143. if (empty($rawDate) || false == $parsedDate) {
  144. // We can't default this to now, as that means future cache reads
  145. // will always pass with the logic below, so we will require a
  146. // date be injected if not supplied.
  147. throw new Google_Exception("All cacheable requests must have creation dates.");
  148. }
  149. if (false == $freshnessLifetime && isset($responseHeaders['expires'])) {
  150. $freshnessLifetime = $parsedExpires - $parsedDate;
  151. }
  152. if (false == $freshnessLifetime) {
  153. return true;
  154. }
  155. // Calculate the age of an http response.
  156. $age = max(0, time() - $parsedDate);
  157. if (isset($responseHeaders['age'])) {
  158. $age = max($age, strtotime($responseHeaders['age']));
  159. }
  160. return $freshnessLifetime <= $age;
  161. }
  162. /**
  163. * Determine if a cache entry should be revalidated with by the origin.
  164. *
  165. * @param Google_Http_Request $response
  166. * @return bool True if the entry is expired, else return false.
  167. */
  168. public static function mustRevalidate(Google_Http_Request $response)
  169. {
  170. // [13.3] When a cache has a stale entry that it would like to use as a
  171. // response to a client's request, it first has to check with the origin
  172. // server to see if its cached entry is still usable.
  173. return self::isExpired($response);
  174. }
  175. }