class ContentLength

Same name in this branch
  1. 10 core/modules/big_pipe/src/StackMiddleware/ContentLength.php \Drupal\big_pipe\StackMiddleware\ContentLength
Same name and namespace in other branches
  1. 11.x core/modules/big_pipe/src/StackMiddleware/ContentLength.php \Drupal\big_pipe\StackMiddleware\ContentLength
  2. 11.x core/lib/Drupal/Core/StackMiddleware/ContentLength.php \Drupal\Core\StackMiddleware\ContentLength

Adds a Content-Length HTTP header to responses.

Hierarchy

  • class \Drupal\Core\StackMiddleware\ContentLength extends \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of ContentLength

1 file declares its use of ContentLength
ContentLengthTest.php in core/tests/Drupal/Tests/Core/StackMiddleware/ContentLengthTest.php
1 string reference to 'ContentLength'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ContentLength
http_middleware.content_length in core/core.services.yml
Drupal\Core\StackMiddleware\ContentLength

File

core/lib/Drupal/Core/StackMiddleware/ContentLength.php, line 12

Namespace

Drupal\Core\StackMiddleware
View source
class ContentLength implements HttpKernelInterface {
  
  /**
   * Constructs a new ContentLength instance.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
   *   The wrapped HTTP kernel.
   */
  public function __construct(protected readonly HttpKernelInterface $httpKernel) {
  }
  
  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) : Response {
    $response = $this->httpKernel
      ->handle($request, $type, $catch);
    if ($response->isInformational() || $response->isEmpty()) {
      return $response;
    }
    if ($response->headers
      ->has('Transfer-Encoding')) {
      return $response;
    }
    // Drupal cannot set the correct content length header when there is a
    // server error.
    if ($response->isServerError()) {
      return $response;
    }
    $content = $response->getContent();
    if ($content === FALSE) {
      return $response;
    }
    $response->headers
      ->set('Content-Length', strlen($content), TRUE);
    return $response;
  }

}

Members

Title Sort descending Modifiers Object type Summary
ContentLength::handle public function
ContentLength::__construct public function Constructs a new ContentLength instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.