/** * Attempts to cache the sent entity by its last modification date. * @param string|int|DateTime last modified time * @param string strong entity tag validator * @return bool */ public function isModified($lastModified = NULL, $etag = NULL) { if ($lastModified) { $this->response->setHeader('Last-Modified', $this->response->date($lastModified)); } if ($etag) { $this->response->setHeader('ETag', '"' . addslashes($etag) . '"'); } $ifNoneMatch = $this->request->getHeader('If-None-Match'); if ($ifNoneMatch === '*') { $match = TRUE; // match, check if-modified-since } elseif ($ifNoneMatch !== NULL) { $etag = $this->response->getHeader('ETag'); if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === FALSE) { return TRUE; } else { $match = TRUE; // match, check if-modified-since } } $ifModifiedSince = $this->request->getHeader('If-Modified-Since'); if ($ifModifiedSince !== NULL) { $lastModified = $this->response->getHeader('Last-Modified'); if ($lastModified != NULL && strtotime($lastModified) <= strtotime($ifModifiedSince)) { $match = TRUE; } else { return TRUE; } } if (empty($match)) { return TRUE; } $this->response->setCode(IHttpResponse::S304_NOT_MODIFIED); return FALSE; }
/** * Sends response to output. * @return void */ public function send(IHttpRequest $httpRequest, IHttpResponse $httpResponse) { $httpResponse->setContentType($this->contentType); $httpResponse->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"'); $filesize = $length = filesize($this->file); $handle = fopen($this->file, 'r'); if ($this->resuming) { $httpResponse->setHeader('Accept-Ranges', 'bytes'); if (preg_match('#^bytes=(\\d*)-(\\d*)$#', $httpRequest->getHeader('Range'), $matches)) { list(, $start, $end) = $matches; if ($start === '') { $start = max(0, $filesize - $end); $end = $filesize - 1; } elseif ($end === '' || $end > $filesize - 1) { $end = $filesize - 1; } if ($end < $start) { $httpResponse->setCode(416); // requested range not satisfiable return; } $httpResponse->setCode(206); $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize); $length = $end - $start + 1; fseek($handle, $start); } else { $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize); } } $httpResponse->setHeader('Content-Length', $length); while (!feof($handle) && $length > 0) { echo $s = fread($handle, min(4000000.0, $length)); $length -= strlen($s); } fclose($handle); }
/** * Sends response to output. * @return void */ public function send(IHttpRequest $httpRequest, IHttpResponse $httpResponse) { $httpResponse->setContentType($this->contentType); $httpResponse->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '"'); $filesize = $length = filesize($this->file); $handle = fopen($this->file, 'r'); if ($this->resuming) { $httpResponse->setHeader('Accept-Ranges', 'bytes'); $range = $httpRequest->getHeader('Range'); if ($range !== NULL) { $range = substr($range, 6); // 6 == strlen('bytes=') list($start, $end) = explode('-', $range); if ($start == NULL) { $start = 0; } if ($end == NULL) { $end = $filesize - 1; } if ($start < 0 || $end <= $start || $end > $filesize -1) { $httpResponse->setCode(416); // requested range not satisfiable return; } $httpResponse->setCode(206); $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize); $length = $end - $start + 1; fseek($handle, $start); } else { $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize); } } $httpResponse->setHeader('Content-Length', $length); while (!feof($handle)) { echo fread($handle, 4e6); } fclose($handle); }