Пример #1
0
 /**
  * Evaluate preconditions found in this request based on the given values and create an appropriate
  * HTTP response if an HTTP/1.1 304 Not Modified response should be sent.
  * 
  * You can pass an ETag, a modification time or bot of these to the method, every precondition
  * that is not NULL will be checked.
  * 
  * @param EntityTag $etag The ETag of the requested resource.
  * @param \DateTimeInterface $lastModified Date of the last modification of the requested resource.
  * @return HttpResponse An HTTP 304 response or NULL if the client cache is invalid.
  */
 public function evaluatePreconditions(EntityTag $etag = NULL, \DateTimeInterface $lastModified = NULL)
 {
     $response = new HttpResponse();
     if ($etag !== NULL) {
         if ($this->hasHeader('If-None-Match')) {
             $valid = $this->firstHeader(function (IfNoneMatchHeader $match) use($etag) {
                 return $match->isWildcard() || $match->getEntityTag() == $etag;
             });
             if ($valid) {
                 $response->setStatus(Http::CODE_NOT_MODIFIED);
                 $response->setReason(Http::getReason(Http::CODE_NOT_MODIFIED));
                 $response->setHeader(new ETagHeader($etag));
             }
         }
     }
     if ($lastModified !== NULL) {
         $unmodified = false;
         if ($this->hasHeader('If-Modified-Since')) {
             $unmodified = $this->firstHeader(function (IfModifiedSinceHeader $since) use($lastModified) {
                 return $since->getDate() >= $lastModified;
             });
         }
         if ($unmodified) {
             $response->setStatus(Http::CODE_NOT_MODIFIED);
             $response->setReason(Http::getReason(Http::CODE_NOT_MODIFIED));
             $response->setHeader(new LastModifiedHeader($lastModified));
         }
     }
     return $response->isRedirect() ? $response : NULL;
 }