public function 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

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/BinaryFileResponse.php, line 155

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());
  $this->headers
    ->set('Accept-Ranges', 'bytes');
  $this->headers
    ->set('Content-Transfer-Encoding', 'binary');
  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.
      foreach (explode(',', $request->headers
        ->get('X-Accel-Mapping', '')) as $mapping) {
        $mapping = explode('=', $mapping, 2);
        if (2 == count($mapping)) {
          $location = trim($mapping[0]);
          $pathPrefix = 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;
      }
      $start = max($start, 0);
      $end = min($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));
    }
  }
  return $this;
}