Ensure that the Vary header is set.


@link http://www.w3.org/TR/cors/#resource-implementation
@param HttpResponse $response
@return \Zend\Http\Headers
553421
public function ensureVaryHeader(HttpResponse $response)
    {
        $headers = $response->getHeaders();
        // If the origin is not "*", we should add the "Origin" value to the "Vary" header
        // See more: http://www.w3.org/TR/cors/#resource-implementation
        $allowedOrigins = $this->options->getAllowedOrigins();

        if (in_array('*', $allowedOrigins)) {
            return $headers;
        }
        if ($headers->has('Vary')) {
            $varyHeader = $headers->get('Vary');
            $varyValue  = $varyHeader->getFieldValue() . ', Origin';

            $headers->removeHeader($varyHeader);
            $headers->addHeaderLine('Vary', $varyValue);
        } else {
            $headers->addHeaderLine('Vary', 'Origin');
        }

        return $headers;
    }
577301
protected function setHttpCache($response = null)
    {
        $http_cache_config = Lb::app()->getHttpCacheConfig();
        if (isset($http_cache_config['cache_control']) && isset($http_cache_config['offset'])) {
            HttpHelper::setCache($http_cache_config['cache_control'], $http_cache_config['offset'], $response);
        }
    }
475985
protected function cleanupHeadersForProd(Response $response)
    {
        // remove headers that identify the content or internal digest info
        $response->headers->remove('xkey');
        $response->headers->remove('x-content-digest');

        // remove vary by X-User-Hash header
        $varyValues = [];
        $variesByUser = false;
        foreach ($response->getVary() as $value) {
            if ($value === 'X-User-Hash') {
                $variesByUser = true;
            } else {
                $varyValues[] = $value;
            }
        }

        // update resulting vary header in normalized form (comma separated)
        if (empty($varyValues)) {
            $response->headers->remove('Vary');
        } else {
            $response->setVary(implode(', ', $varyValues));
        }

        // If cache varies by user hash, then make sure other proxies don't cache this
        if ($variesByUser) {
            $response->setPrivate();
            $response->headers->removeCacheControlDirective('s-maxage');
            $response->headers->addCacheControlDirective('no-cache');
        }
    }
1115370
public function headers(array $headers): HttpRequest
    {
        foreach ($headers as $name => $value) {
            $this->header($name, $value);
        }

        return $this;
    }
871004
public function applyToResponse($response)
    {
        $headers = $this->generateHeadersFor($response);
        foreach ($headers as $name => $value) {
            if (!$response->getHeader($name)) {
                $response->addHeader($name, $value);
            }
        }
        return $this;
    }
870282
private function decorateResponse(Response $response)
    {
        if ($response instanceof Http\Response) {
            return $response;
        }

        return new Http\Response($response);
    }
6481
protected static function setContentType($headers, $response)
    {
        if (isset($headers['Content-Type'])) {
            return $headers;
        }
        if (in_array($response->statusCode(), [204, 304])) {
            return $headers;
        }

        $whitelist = [
            'application/javascript', 'application/json', 'application/xml', 'application/rss+xml'
        ];

        $type = $response->type();
        $charset = $response->charset();

        $hasCharset = false;
        if ($charset && (strpos($type, 'text/') === 0 || in_array($type, $whitelist))) {
            $hasCharset = true;
        }

        $value = $type;
        if ($hasCharset) {
            $value = "{$type}; charset={$charset}";
        }
        $headers['Content-Type'] = $value;

        return $headers;
    }
313299
public function sendHeaders(ResponseInterface $response)
    {
        foreach ($response->getHeaders() as $header => $values) {
            $first = true;
            $header = HeaderHelper::normalizeHeaderName($header);

            foreach ($values as $value) {
                $this->header(sprintf('%s: %s', $header, $value), $first);

                $first = false;
            }
        }

        return $this;
    }
1200940
public function modifyResponse($response){
        $config  = $this->config;

        if (!$this->isEnabled() ) {
            return $response;
        }

        if ($this->shouldCollect('config', false) && $this->di->has('config')) {
            try {
                $config_data = $this->di['config']->toArray();
                $protect = $config->options->config->get('protect');
                $configCollector = new ConfigCollector($config_data);
                $configCollector->setProtect($protect);
                $this->addCollector($configCollector);
            } catch (\Exception $e) {
                $this->addException(
                    new Exception(
                        'Cannot add ConfigCollector to Phalcon Debugbar: ' . $e->getMessage(),
                        $e->getCode(),
                        $e
                    )
                );
            }
        }

        if ($this->shouldCollect('session')   && $this->di->has('session') ) {
            try {
                $this->addCollector(new SessionCollector($this->di['session']));
            } catch (\Exception $e) {
                $this->addException(
                    new Exception(
                        'Cannot add SessionCollector to Phalcon Debugbar: ' . $e->getMessage(),
                        $e->getCode(),
                        $e
                    )
                );
            }
        }

        if ($this->shouldCollect('phalcon_request', true) and !$this->hasCollector('request')) {
            try {
                $this->addCollector(new PhalconRequestCollector($this->di['request'],$response,$this->di));
            } catch (\Exception $e) {
                $this->addException(
                    new Exception(
                        'Cannot add PhalconRequestCollector to Phalcon Debugbar: ' . $e->getMessage(),
                        $e->getCode(),
                        $e
                    )
                );
            }
        }

        if( $this->hasCollector('pdo') ){
            /** @var Profiler $profiler */
            $profiler = $this->getCollector('pdo')->getProfiler();
            $profiler->handleFailed();
        }

        if ( $this->isDebugbarRequest ) {
            // Notice: All Collectors must be added before check if is debugbar request.
            return $response;
        }

        try {
            if ($this->isRedirection($response)) {
                $this->stackData();
            }
            elseif ( $this->isJsonRequest() && $config->get('capture_ajax', true) )
            {
                $this->sendDataInHeaders(true);
            } elseif (
                ($content_type = $response->getHeaders()->get('Content-Type'))
                &&
                strpos($response->getHeaders()->get('Content-Type'), 'html') !== false
                && $config->get('inject', true)
            ) {
                $response->setHeader('Phalcon-Debugbar','on');
                $this->injectDebugbar($response);
            } elseif (
                ($content_type = $response->getHeaders()->get('Content-Type'))
                &&
                strpos($response->getHeaders()->get('Content-Type'), 'html') === false
            ) {
                $this->collect();
            } elseif($config->get('inject', true)) {
                $response->setHeader('Phalcon-Debugbar','on');
                $this->injectDebugbar($response);
            }else{
                $this->collect();
            }
        } catch (\Exception $e) {
            $this->addException($e);
        }

        // Stop further rendering (on subrequests etc)
        $this->disable();

        return $response;
    }
684511
protected function setErrors($response)
    {
        libxml_use_internal_errors(true);

        $doc = new \DOMDocument('1.0', 'utf-8');
        $doc->loadXML($response);

        $errors = libxml_get_errors();
        if (!empty($errors)) {
            $this->errors[] = 'Xml response is invalid';
        }

        return $this;
    }
1324628
protected function applyDefaultResponseHeaders(ResponseInterface $response) : ResponseInterface
    {
        foreach ($this->defaultResponseHeaders as $name => $value) {
            if (! $response->hasHeader($name)) {
                $response = $response->withHeader($name, $value);
            }
        }

        return $response;
    }
880174
private static function getHeaderAndBody($response) {
    $header = self::http_parse_headers(substr($response, 0, strrpos($response,"\r\n\r\n")));
    $body = substr($response, strrpos($response,"\r\n\r\n")+4);

    return array($header,$body);
  }
1346628
public function setNoCacheHeaders(Zend_Controller_Response_Http $response) {
        $this->getResponse()
            ->setHeader('Cache-Control', 'no-cache', true)
            ->setHeader('Pragma', 'no-cache', true)
            ->setHeader('Expires', date(DATE_RFC1123, strtotime('-1 year')), true);
    }
916281
public function prepareForResponse(\n2n\web\http\Response $response) {
		$response->setStatus($this->statusCode);
		foreach ($this->headers as $header) {
			$response->setHeader($header);
		}

		$response->setHttpCacheControl($this->httpCacheControl);

	}
847398
protected function sendHeaders(Response $response)
    {
        // Send HTTP status & reason-phrase
        header(
            sprintf(
                '%s %s %s',
                $this->getProtocolLine(),
                $response->getStatusCode(),
                $response->getReasonPhrase()
            ),
            true,
            $response->getStatusCode()
        );

        // Send headers
        $headers = $response->getHeaders();
        foreach ($headers as $name => $valueCollection) {
            foreach ($valueCollection as $value) {
                header(sprintf('%s: %s', $name, $value), false);
            }
        }
    }
1069472
private function handleResponse($response)
    {
        if ($this->client->newFormatModeEnabled === true) {
            return $response;
        }

        return $this->client->handleHttpResponse($response);
    }
449999
protected function parseResponseHeaders(Response $response, string $headerString)
    {
        foreach (array_filter(explode("\r\n\r\n", $headerString)) as $responseHeader) {
            $header = new Header();
            foreach (explode("\r\n", $responseHeader) as $headerLine) {
                $parts = explode(':', $headerLine, 2);
                if (count($parts) === 2) {
                    $header->set(trim($parts[0]), trim($parts[1]));
                }
            }
            $response->addHeader($header);
        }
        return $this;
    }