Esempio n. 1
0
 /**
  * Attempts to cache the sent entity by its last modification date
  * @param  int    last modified time as unix timestamp
  * @param  string strong entity tag validator
  * @param  int    optional expiration time
  * @return int    date of the client's cache version, if available
  * @throws TerminateException
  */
 public function lastModified($lastModified, $etag = NULL, $expire = NULL)
 {
     if (!Environment::isProduction()) {
         return NULL;
     }
     $httpResponse = $this->getHttpResponse();
     $match = FALSE;
     if ($lastModified > 0) {
         $httpResponse->setHeader('Last-Modified', HttpResponse::date($lastModified));
     }
     if ($etag != NULL) {
         // intentionally ==
         $etag = '"' . addslashes($etag) . '"';
         $httpResponse->setHeader('ETag', $etag);
     }
     if ($expire !== NULL) {
         $httpResponse->expire($expire);
     }
     $ifNoneMatch = $this->getHttpRequest()->getHeader('if-none-match');
     $ifModifiedSince = $this->getHttpRequest()->getHeader('if-modified-since');
     if ($ifModifiedSince !== NULL) {
         $ifModifiedSince = strtotime($ifModifiedSince);
     }
     if ($ifNoneMatch !== NULL) {
         if ($ifNoneMatch === '*') {
             $match = TRUE;
             // match, check if-modified-since
         } elseif ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", '  '), ' ' . $etag) === FALSE) {
             return $ifModifiedSince;
             // no match, ignore if-modified-since
         } else {
             $match = TRUE;
             // match, check if-modified-since
         }
     }
     if ($ifModifiedSince !== NULL) {
         if ($lastModified > 0 && $lastModified <= $ifModifiedSince) {
             $match = TRUE;
         } else {
             return $ifModifiedSince;
         }
     }
     if ($match) {
         $httpResponse->setCode(IHttpResponse::S304_NOT_MODIFIED);
         $httpResponse->setHeader('Content-Length', '0');
         $this->terminate();
     } else {
         return $ifModifiedSince;
     }
     return NULL;
 }