Example #1
0
 /**
  * @static
  * @param apiHttpRequest $resp
  * @return bool True if the HTTP response is considered to be expired.
  * False if it is considered to be fresh.
  */
 public static function isExpired(apiHttpRequest $resp)
 {
     // HTTP/1.1 clients and caches MUST treat other invalid date formats,
     // especially including the value “0”, as in the past.
     $parsedExpires = false;
     $responseHeaders = $resp->getResponseHeaders();
     if (isset($responseHeaders['expires'])) {
         $rawExpires = $responseHeaders['expires'];
         // Check for a malformed expires header first.
         if (empty($rawExpires) || is_numeric($rawExpires) && $rawExpires <= 0) {
             return true;
         }
         // See if we can parse the expires header.
         $parsedExpires = strtotime($rawExpires);
         if (false == $parsedExpires || $parsedExpires <= 0) {
             return true;
         }
     }
     // Calculate the freshness of an http response.
     $freshnessLifetime = false;
     $cacheControl = $resp->getParsedCacheControl();
     if (in_array('max-age', $cacheControl)) {
         $freshnessLifetime = $cacheControl['max-age'];
     }
     $rawDate = $resp->getResponseHeader('date');
     $parsedDate = strtotime($rawDate);
     if (empty($rawDate) || false == $parsedDate) {
         $parsedDate = time();
     }
     if (false == $freshnessLifetime && isset($responseHeaders['expires'])) {
         $freshnessLifetime = $parsedExpires - $parsedDate;
     }
     if (false == $freshnessLifetime) {
         return true;
     }
     // Calculate the age of an http response.
     $age = max(0, time() - $parsedDate);
     if (isset($responseHeaders['age'])) {
         $age = max($age, strtotime($responseHeaders['age']));
     }
     return $freshnessLifetime <= $age;
 }
Example #2
0
 /**
  * Normalize all HTTP headers.
  * @param apiHttpRequest $request
  * @return array
  */
 private function getNormalizedHeaders(apiHttpRequest $request)
 {
     if (!is_array($request->getResponseHeaders())) {
         return array();
     }
     $headers = $request->getResponseHeaders();
     $newHeaders = array();
     foreach ($headers as $key => $val) {
         $newHeaders[strtolower($key)] = $val;
     }
     return $newHeaders;
 }