Ejemplo n.º 1
0
    /**
     * Injects the web debug toolbar into a given HTML string.
     *
     * @param string $content The HTML content
     *
     * @return Response A Response instance
     */
    protected function injectToolbar(Request $request, Response $response)
    {
        $data = '';
        foreach ($this->profiler->getCollectors() as $name => $collector) {
            $data .= $collector->getSummary();
        }
        $position = false === strpos($request->headers->get('user-agent'), 'Mobile') ? 'fixed' : 'absolute';
        $toolbar = <<<EOF

<!-- START of Symfony 2 Web Debug Toolbar -->
<div style="clear: both; height: 40px;"></div>
<div style="position: {$position}; bottom: 0px; left:0; z-index: 6000000; width: 100%; background: #dde4eb; border-top: 1px solid #bbb; padding: 5px; margin: 0; font: 11px Verdana, Arial, sans-serif; color: #222;">
    {$data}
</div>
<!-- END of Symfony 2 Web Debug Toolbar -->

EOF;
        $toolbar = "\n" . str_replace("\n", '', $toolbar) . "\n";
        $count = 0;
        $content = str_ireplace('</body>', $toolbar . '</body>', $response->getContent(), $count);
        if (!$count) {
            $content .= $toolbar;
        }
        return $content;
    }
Ejemplo n.º 2
0
 /**
  * Persists the Response HTTP headers.
  *
  * @param Symfony\Components\HttpKernel\Response $response A Response instance
  *
  * @return array An array of HTTP headers
  */
 protected function persistResponse(Response $response)
 {
     $headers = $response->headers->all();
     $headers['X-Status'] = array($response->getStatusCode());
     return $headers;
 }
Ejemplo n.º 3
0
 /**
  * Replaces a Response ESI tags with the included resource content.
  *
  * @param Symfony\Components\HttpKernel\Request  $request  A Request instance
  * @param Symfony\Components\HttpKernel\Response $response A Response instance
  */
 public function process(Request $request, Response $response)
 {
     $this->request = $request;
     $type = $response->headers->get('Content-Type');
     if (empty($type)) {
         $type = 'text/html';
     }
     if (!in_array($type, $this->contentTypes)) {
         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_callback('#<esi\\:include\\s+(.+?)\\s*/>#', array($this, 'handleEsiIncludeTag'), $content);
     $content = preg_replace('#<esi\\:comment[^>]*/>#', '', $content);
     $content = preg_replace('#<esi\\:remove>.*?</esi\\:remove>#', '', $content);
     $response->setContent($content);
     $response->headers->set('X-Body-Eval', 'ESI');
     // remove ESI/1.0 from the Surrogate-Control header
     $value = $response->headers->get('Surrogate-Control');
     if (preg_match('#^content="ESI/1.0"$#', $value)) {
         $response->headers->delete('Surrogate-Control');
     } else {
         $response->headers->set('Surrogate-Control', preg_replace('#ESI/1.0#', '', $value));
     }
 }
Ejemplo n.º 4
0
 /**
  * Restores the Response body.
  *
  * @param Symfony\Components\HttpKernel\Response $response A Response instance
  *
  * @return Symfony\Components\HttpKernel\Response A Response instance
  */
 protected function restoreResponseBody(Response $response)
 {
     if ($response->headers->has('X-Body-Eval')) {
         ob_start();
         if ($response->headers->has('X-Body-File')) {
             include $response->headers->get('X-Body-File');
         } else {
             eval('; ?>' . $response->getContent() . '<?php ;');
         }
         $response->setContent(ob_get_clean());
         $response->headers->delete('X-Body-Eval');
     } elseif ($response->headers->has('X-Body-File')) {
         $response->setContent(file_get_contents($response->headers->get('X-Body-File')));
     } else {
         return;
     }
     $response->headers->delete('X-Body-File');
     if (!$response->headers->has('Transfer-Encoding')) {
         $response->headers->set('Content-Length', strlen($response->getContent()));
     }
 }
Ejemplo n.º 5
0
 /**
  * Validates that a cache entry is fresh.
  *
  * The original request is used as a template for a conditional
  * GET request with the backend.
  *
  * @param Symfony\Components\HttpKernel\Request  $request A Request instance
  * @param Symfony\Components\HttpKernel\Response $entry A Response instance to validate
  *
  * @return Symfony\Components\HttpKernel\Response A Response instance
  */
 protected function validate(Request $request, $entry)
 {
     $subRequest = clone $request;
     // send no head requests because we want content
     $subRequest->setMethod('get');
     // add our cached last-modified validator
     $subRequest->headers->set('if_modified_since', $entry->headers->get('Last-Modified'));
     // Add our cached etag validator to the environment.
     // We keep the etags from the client to handle the case when the client
     // has a different private valid entry which is not cached here.
     $cachedEtags = array($entry->getEtag());
     $requestEtags = $request->getEtags();
     $etags = array_unique(array_merge($cachedEtags, $requestEtags));
     $subRequest->headers->set('if_none_match', $etags ? implode(', ', $etags) : '');
     $response = $this->forward($subRequest, false, $entry);
     if (304 == $response->getStatusCode()) {
         $this->record($request, 'valid');
         // return the response and not the cache entry if the response is valid but not cached
         $etag = $response->getEtag();
         if ($etag && in_array($etag, $requestEtags) && !in_array($etag, $cachedEtags)) {
             return $response;
         }
         $entry = clone $entry;
         $entry->headers->delete('Date');
         foreach (array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified') as $name) {
             if ($response->headers->has($name)) {
                 $entry->headers->set($name, $response->headers->get($name));
             }
         }
         $response = $entry;
     } else {
         $this->record($request, 'invalid');
     }
     if ($response->isCacheable()) {
         $this->store($request, $response);
     }
     return $response;
 }
Ejemplo n.º 6
0
 public function testGetVary()
 {
     $response = new Response();
     $this->assertEquals(array(), $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
     $response = new Response();
     $response->headers->set('Vary', 'Accept-Language');
     $this->assertEquals(array('Accept-Language'), $response->getVary(), '->getVary() parses a single header name value');
     $response = new Response();
     $response->headers->set('Vary', 'Accept-Language User-Agent    X-Foo');
     $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
     $response = new Response();
     $response->headers->set('Vary', 'Accept-Language,User-Agent,    X-Foo');
     $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
 }