function ContentLength::handle

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

File

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

Class

ContentLength
Adds a Content-Length HTTP header to responses.

Namespace

Drupal\Core\StackMiddleware

Code

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;
}

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