Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Response.php

function Response::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.

Return value

$this

2 calls to Response::prepare()
BinaryFileResponse::prepare in vendor/symfony/http-foundation/BinaryFileResponse.php
Prepares the Response before it is sent to the client.
BinaryFileResponse::prepare in vendor/symfony/http-foundation/BinaryFileResponse.php
Prepares the Response before it is sent to the client.
1 method overrides Response::prepare()
BinaryFileResponse::prepare in vendor/symfony/http-foundation/BinaryFileResponse.php
Prepares the Response before it is sent to the client.

File

vendor/symfony/http-foundation/Response.php, line 242

Class

Response
Response represents an HTTP response.

Namespace

Symfony\Component\HttpFoundation

Code

public function prepare(Request $request) : static {
    $headers = $this->headers;
    if ($this->isInformational() || $this->isEmpty()) {
        $this->setContent(null);
        $headers->remove('Content-Type');
        $headers->remove('Content-Length');
        // prevent PHP from sending the Content-Type header based on default_mimetype
        ini_set('default_mimetype', '');
    }
    else {
        // Content-type based on the Request
        if (!$headers->has('Content-Type')) {
            $format = $request->getRequestFormat(null);
            if (null !== $format && ($mimeType = $request->getMimeType($format))) {
                $headers->set('Content-Type', $mimeType);
            }
        }
        // Fix Content-Type
        $charset = $this->charset ?: 'UTF-8';
        if (!$headers->has('Content-Type')) {
            $headers->set('Content-Type', 'text/html; charset=' . $charset);
        }
        elseif (0 === stripos($headers->get('Content-Type') ?? '', 'text/') && false === stripos($headers->get('Content-Type') ?? '', 'charset')) {
            // add the charset
            $headers->set('Content-Type', $headers->get('Content-Type') . '; charset=' . $charset);
        }
        // Fix Content-Length
        if ($headers->has('Transfer-Encoding')) {
            $headers->remove('Content-Length');
        }
        if ($request->isMethod('HEAD')) {
            // cf. RFC2616 14.13
            $length = $headers->get('Content-Length');
            $this->setContent(null);
            if ($length) {
                $headers->set('Content-Length', $length);
            }
        }
    }
    // Fix protocol
    if ('HTTP/1.0' != $request->server
        ->get('SERVER_PROTOCOL')) {
        $this->setProtocolVersion('1.1');
    }
    // Check if we need to send extra expire info headers
    if ('1.0' == $this->getProtocolVersion() && str_contains($headers->get('Cache-Control', ''), 'no-cache')) {
        $headers->set('pragma', 'no-cache');
        $headers->set('expires', -1);
    }
    $this->ensureIEOverSSLCompatibility($request);
    if ($request->isSecure()) {
        foreach ($headers->getCookies() as $cookie) {
            $cookie->setSecureDefault(true);
        }
    }
    return $this;
}
RSS feed
Powered by Drupal