/**
  * Get the values of trusted proxy header.
  *
  * @param string $type One of the HEADER_* constants
  * @param Request $request The request to get the trusted proxy header from
  * @return \Iterator An array of the values for this header type or NULL if this header type should not be trusted
  */
 protected function getTrustedProxyHeaderValues($type, Request $request)
 {
     $trustedHeaders = isset($this->settings['headers'][$type]) ? $this->settings['headers'][$type] : '';
     if ($trustedHeaders === '' || !$request->getAttribute(Request::ATTRIBUTE_TRUSTED_PROXY)) {
         (yield null);
         return;
     }
     $trustedHeaders = array_map('trim', explode(',', $trustedHeaders));
     foreach ($trustedHeaders as $trustedHeader) {
         if ($request->hasHeader($trustedHeader)) {
             (yield array_map('trim', explode(',', $request->getHeader($trustedHeader))));
         }
     }
     (yield null);
 }
Exemplo n.º 2
0
 /**
  * Analyzes this response, considering the given request and makes additions
  * or removes certain headers in order to make the response compliant to
  * RFC 2616 and related standards.
  *
  * It is recommended to call this method before the response is sent and Flow
  * does so by default in its built-in HTTP request handler.
  *
  * @param Request $request The corresponding request
  * @return void
  * @api
  */
 public function makeStandardsCompliant(Request $request)
 {
     if ($request->hasHeader('If-Modified-Since') && $this->headers->has('Last-Modified') && $this->statusCode === 200) {
         $ifModifiedSinceDate = $request->getHeader('If-Modified-Since');
         $lastModifiedDate = $this->headers->get('Last-Modified');
         if ($lastModifiedDate <= $ifModifiedSinceDate) {
             $this->setStatus(304);
             $this->content = '';
         }
     } elseif ($request->hasHeader('If-Unmodified-Since') && $this->headers->has('Last-Modified') && ($this->statusCode >= 200 && $this->statusCode <= 299 || $this->statusCode === 412)) {
         $unmodifiedSinceDate = $request->getHeader('If-Unmodified-Since');
         $lastModifiedDate = $this->headers->get('Last-Modified');
         if ($lastModifiedDate > $unmodifiedSinceDate) {
             $this->setStatus(412);
         }
     }
     if (in_array($this->statusCode, [100, 101, 204, 304])) {
         $this->content = '';
     }
     if ($this->headers->getCacheControlDirective('no-cache') !== null || $this->headers->has('Expires')) {
         $this->headers->removeCacheControlDirective('max-age');
     }
     if ($request->getMethod() === 'HEAD') {
         if (!$this->headers->has('Content-Length')) {
             $this->headers->set('Content-Length', strlen($this->content));
         }
         $this->content = '';
     }
     if (!$this->headers->has('Content-Length')) {
         $this->headers->set('Content-Length', strlen($this->content));
     }
     if ($this->headers->has('Transfer-Encoding')) {
         $this->headers->remove('Content-Length');
     }
 }