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

Breadcrumb

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

function Esi::process

Overrides SurrogateInterface::process

File

vendor/symfony/http-kernel/HttpCache/Esi.php, line 57

Class

Esi
Esi implements the ESI capabilities to Request and Response instances.

Namespace

Symfony\Component\HttpKernel\HttpCache

Code

public function process(Request $request, Response $response) : Response {
    $type = $response->headers
        ->get('Content-Type');
    if (!$type) {
        $type = 'text/html';
    }
    $parts = explode(';', $type);
    if (!\in_array($parts[0], $this->contentTypes, true)) {
        return $response;
    }
    // we don't use a proper XML parser here as we can have ESI tags in a plain text response
    $content = $response->getContent();
    $content = preg_replace('#<esi\\:remove>.*?</esi\\:remove>#s', '', $content);
    $content = preg_replace('#<esi\\:comment[^>]+>#s', '', $content);
    $boundary = self::generateBodyEvalBoundary();
    $chunks = preg_split('#<esi\\:include\\s+(.*?)\\s*(?:/|</esi\\:include)>#', $content, -1, \PREG_SPLIT_DELIM_CAPTURE);
    $i = 1;
    while (isset($chunks[$i])) {
        $options = [];
        preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, \PREG_SET_ORDER);
        foreach ($matches as $set) {
            $options[$set[1]] = $set[2];
        }
        if (!isset($options['src'])) {
            throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.');
        }
        $chunks[$i] = $boundary . $options['src'] . "\n" . ($options['alt'] ?? '') . "\n" . ('continue' === ($options['onerror'] ?? '')) . "\n";
        $i += 2;
    }
    $content = $boundary . implode('', $chunks) . $boundary;
    $response->setContent($content);
    $response->headers
        ->set('X-Body-Eval', 'ESI');
    // remove ESI/1.0 from the Surrogate-Control header
    $this->removeFromControl($response);
    return $response;
}
RSS feed
Powered by Drupal