<commit_before><?php

namespace ForceHttpsModule\Listener;

use ForceHttpsModule\HttpsTrait;
use Zend\Console\Console;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\MvcEvent;

class ForceHttps extends AbstractListenerAggregate
{
    use HttpsTrait;

    /**
     * @var array
     */
    private $config;

    /**
     * @param  array       $config
     */
    public function __construct(array $config)
    {
        $this->config = $config;
    }

    /**
     * @param EventManagerInterface $events
     * @param int                   $priority
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        if (Console::isConsole()) {
            return;
        }

        $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'forceHttpsScheme']);
    }

    /**
     * Set The HTTP Strict Transport Security.
     *
     * @param string   $uriScheme
     * @param mixed    $match     didn't typed hinted in code in favor of zf-mvc ^2.5 RouteMatch compat
     * @param Response $response
     */
    private function setHttpStrictTransportSecurity($uriScheme, $match, Response $response)
    {
        if (
            ! $this->isSchemeHttps($uriScheme) ||
            ! $this->isGoingToBeForcedToHttps($match) ||
            ! isset(
                $this->config['strict_transport_security']['enable'],
                $this->config['strict_transport_security']['value']
            )
        ) {
            return;
        }

        if ($this->config['strict_transport_security']['enable'] === true) {
            $response->getHeaders()
                        ->addHeaderLine('Strict-Transport-Security: ' . $this->config['strict_transport_security']['value']);
            return;
        }

        // set max-age = 0 to strictly expire it,
        $response->getHeaders()
                    ->addHeaderLine('Strict-Transport-Security: max-age=0');
        return;
    }

    /**
     * Force Https Scheme handle.
     *
     * @param  MvcEvent         $e
     */
    public function forceHttpsScheme(MvcEvent $e)
    {
        if (! $this->config['enable']) {
           return;
        }

        /** @var $request \Zend\Http\PhpEnvironment\Request */
        $request   = $e->getRequest();
        /** @var $response \Zend\Http\PhpEnvironment\Response */
        $response  = $e->getResponse();

        $uri       = $request->getUri();
        $uriScheme = $uri->getScheme();

        $routeMatch = $e->getRouteMatch();
        $this->setHttpStrictTransportSecurity($uriScheme, $routeMatch, $response);
        if (! $this->validateSchemeAndToBeForcedHttpsConfig($uriScheme, $routeMatch)) {
            return;
        }

        /** @var $response \Zend\Http\PhpEnvironment\Response */
        $response        = $e->getResponse();
        $httpsRequestUri = $this->withWwwPrefixWhenRequired($uri->setScheme('https')->toString());

        // 307 keeps headers, request method, and request body
        // \Zend\Http\PhpEnvironment\Response doesn't support 308 yet
        $response->setStatusCode(307);
        $response->getHeaders()
                 ->addHeaderLine('Location', $httpsRequestUri);
        $response->send();
    }
}<commit_msg>uriScheme var<commit_after><?php

namespace ForceHttpsModule\Listener;

use ForceHttpsModule\HttpsTrait;
use Zend\Console\Console;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\MvcEvent;

class ForceHttps extends AbstractListenerAggregate
{
    use HttpsTrait;

    /**
     * @var array
     */
    private $config;

    /**
     * @param  array       $config
     */
    public function __construct(array $config)
    {
        $this->config = $config;
    }

    /**
     * @param EventManagerInterface $events
     * @param int                   $priority
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        if (Console::isConsole()) {
            return;
        }

        $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'forceHttpsScheme']);
    }

    /**
     * Set The HTTP Strict Transport Security.
     *
     * @param string   $uriScheme
     * @param mixed    $match     didn't typed hinted in code in favor of zf-mvc ^2.5 RouteMatch compat
     * @param Response $response
     */
    private function setHttpStrictTransportSecurity($uriScheme, $match, Response $response)
    {
        if (
            ! $this->isSchemeHttps($uriScheme) ||
            ! $this->isGoingToBeForcedToHttps($match) ||
            ! isset(
                $this->config['strict_transport_security']['enable'],
                $this->config['strict_transport_security']['value']
            )
        ) {
            return;
        }

        if ($this->config['strict_transport_security']['enable'] === true) {
            $response->getHeaders()
                     ->addHeaderLine('Strict-Transport-Security: ' . $this->config['strict_transport_security']['value']);
            return;
        }

        // set max-age = 0 to strictly expire it,
        $response->getHeaders()
                 ->addHeaderLine('Strict-Transport-Security: max-age=0');
        return;
    }

    /**
     * Force Https Scheme handle.
     *
     * @param  MvcEvent         $e
     */
    public function forceHttpsScheme(MvcEvent $e)
    {
        if (! $this->config['enable']) {
           return;
        }

        /** @var $request \Zend\Http\PhpEnvironment\Request */
        $request   = $e->getRequest();
        /** @var $response \Zend\Http\PhpEnvironment\Response */
        $response  = $e->getResponse();

        $uri       = $request->getUri();
        /** @var $uriScheme string */
        $uriScheme = $uri->getScheme();

        $routeMatch = $e->getRouteMatch();
        $this->setHttpStrictTransportSecurity($uriScheme, $routeMatch, $response);
        if (! $this->validateSchemeAndToBeForcedHttpsConfig($uriScheme, $routeMatch)) {
            return;
        }

        /** @var $response \Zend\Http\PhpEnvironment\Response */
        $response        = $e->getResponse();
        $httpsRequestUri = $this->withWwwPrefixWhenRequired($uri->setScheme('https')->toString());

        // 307 keeps headers, request method, and request body
        // \Zend\Http\PhpEnvironment\Response doesn't support 308 yet
        $response->setStatusCode(307);
        $response->getHeaders()
                 ->addHeaderLine('Location', $httpsRequestUri);
        $response->send();
    }
}
{'fraction_non_alphanumeric': 0.13442475217783117, 'fraction_numerical': 0.004205467107239411, 'mean_word_length': 2.1785202863961812, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '746261', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1869, 'n_words': 350}