You are here

public function BinaryFileResponse::prepare in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/http-foundation/BinaryFileResponse.php \Symfony\Component\HttpFoundation\BinaryFileResponse::prepare()

Prepares the Response before it is sent to the client.

This method tweaks the Response to ensure that it is compliant with RFC 2616. Most of the changes are based on the Request that is "associated" with this Response.

Parameters

Request $request A Request instance:

Return value

Response The current response.

Overrides Response::prepare

File

vendor/symfony/http-foundation/BinaryFileResponse.php, line 170

Class

BinaryFileResponse
BinaryFileResponse represents an HTTP response delivering a file.

Namespace

Symfony\Component\HttpFoundation

Code

public function prepare(Request $request) {
  $this->headers
    ->set('Content-Length', $this->file
    ->getSize());
  if (!$this->headers
    ->has('Accept-Ranges')) {

    // Only accept ranges on safe HTTP methods
    $this->headers
      ->set('Accept-Ranges', $request
      ->isMethodSafe() ? 'bytes' : 'none');
  }
  if (!$this->headers
    ->has('Content-Type')) {
    $this->headers
      ->set('Content-Type', $this->file
      ->getMimeType() ?: 'application/octet-stream');
  }
  if ('HTTP/1.0' != $request->server
    ->get('SERVER_PROTOCOL')) {
    $this
      ->setProtocolVersion('1.1');
  }
  $this
    ->ensureIEOverSSLCompatibility($request);
  $this->offset = 0;
  $this->maxlen = -1;
  if (self::$trustXSendfileTypeHeader && $request->headers
    ->has('X-Sendfile-Type')) {

    // Use X-Sendfile, do not send any content.
    $type = $request->headers
      ->get('X-Sendfile-Type');
    $path = $this->file
      ->getRealPath();
    if (strtolower($type) == 'x-accel-redirect') {

      // Do X-Accel-Mapping substitutions.
      // @link http://wiki.nginx.org/X-accel#X-Accel-Redirect
      foreach (explode(',', $request->headers
        ->get('X-Accel-Mapping', '')) as $mapping) {
        $mapping = explode('=', $mapping, 2);
        if (2 == count($mapping)) {
          $pathPrefix = trim($mapping[0]);
          $location = trim($mapping[1]);
          if (substr($path, 0, strlen($pathPrefix)) == $pathPrefix) {
            $path = $location . substr($path, strlen($pathPrefix));
            break;
          }
        }
      }
    }
    $this->headers
      ->set($type, $path);
    $this->maxlen = 0;
  }
  elseif ($request->headers
    ->has('Range')) {

    // Process the range headers.
    if (!$request->headers
      ->has('If-Range') || $this
      ->getEtag() == $request->headers
      ->get('If-Range')) {
      $range = $request->headers
        ->get('Range');
      $fileSize = $this->file
        ->getSize();
      list($start, $end) = explode('-', substr($range, 6), 2) + array(
        0,
      );
      $end = '' === $end ? $fileSize - 1 : (int) $end;
      if ('' === $start) {
        $start = $fileSize - $end;
        $end = $fileSize - 1;
      }
      else {
        $start = (int) $start;
      }
      if ($start <= $end) {
        if ($start < 0 || $end > $fileSize - 1) {
          $this
            ->setStatusCode(416);
        }
        elseif ($start !== 0 || $end !== $fileSize - 1) {
          $this->maxlen = $end < $fileSize ? $end - $start + 1 : -1;
          $this->offset = $start;
          $this
            ->setStatusCode(206);
          $this->headers
            ->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize));
          $this->headers
            ->set('Content-Length', $end - $start + 1);
        }
      }
    }
  }
  return $this;
}