<commit_before><?php
/**
 * This file is part of the Everon framework.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Everon\Rest;

use Everon\Dependency;
use Everon\Interfaces;
use Everon\Exception;
use Everon\RequestIdentifier;
use Everon\Http;
use Everon\Rest;

/**
 * @method \Everon\Http\Interfaces\Response getResponse
 * @method \Everon\Rest\Interfaces\Request getRequest
 */
class Server extends \Everon\Core implements Rest\Interfaces\Server
{
    /**
     * @inheritdoc
     */
    public function run(RequestIdentifier $RequestIdentifier)
    {
        try {
            $response_headers = $this->getConfigManager()->getConfigValue('rest.response_headers', null);
            if ($response_headers !== null) {
                foreach ($response_headers as $name => $values) {
                    $origin = $this->getRequest()->getHeader('HTTP_ORIGIN', null);
                    if (strcasecmp($values['Access-Control-Allow-Origin'], $origin) === 0) {
                        foreach ($values as $header_name => $header_value) {
                            $this->getResponse()->setHeader($header_name, $header_value);
                        }
                    }
                }
            }
            
            if ($this->getRequest()->getMethod() === \Everon\Request::METHOD_OPTIONS) {
                echo $this->getResponse()->toJson(); //xxx
            }
            else {
                parent::run($RequestIdentifier);
            }
        }
        catch (Exception\Pdo $Exception) {
            $PdoException = new Http\Exception((new Http\Message\BadRequest($Exception->getMessage())));
            $this->showException($PdoException, $this->Controller);
        }
        catch (Exception\RouteNotDefined $Exception) {
            $RouteNotDefined = new Http\Exception((new Http\Message\NotFound('Invalid resource name, request method or version')));
            $this->showException($RouteNotDefined, $this->Controller);
        }
        catch (Exception\InvalidRoute $Exception) {
            $BadRequest = new Http\Exception((new Http\Message\BadRequest($Exception->getMessage())));
            $this->showException($BadRequest, $this->Controller);
        }
        catch (Http\Exception $Exception) {
            $this->showException($Exception, $this->Controller);
        }
        catch (Rest\Exception\Resource $Exception) {
            $RestException = new Http\Exception((new Http\Message\BadRequest($Exception->getMessage())));
            $this->showException($RestException, $this->Controller);
        }
        catch (\Exception $Exception) {
            $InternalServerError = new Http\Exception((new Http\Message\InternalServerError($Exception->getMessage())));
            $this->showException($InternalServerError, $this->Controller);
        }
        finally {
            $url = $this->getConfigManager()->getConfigValue('rest.server.url');
            $this->getLogger()->rest(
                sprintf(
                    '[%d] %s %s (%s)',
                    $this->getResponse()->getStatusCode(),
                    $this->getRequest()->getMethod(), 
                    $url.$this->getRequest()->getFullPath(), 
                    $this->getResponse()->getStatusMessage()
                )
            );
        }
    }
    
 
    public function showException(Http\Exception $Exception, $Controller)
    {
        $message = $Exception->getMessage();
        $code = $Exception->getCode();
        if ($Exception instanceof Http\Exception) {
            $message = $Exception->getHttpMessage()->getInfo();
            $code = $Exception->getHttpMessage()->getCode();
        }

        $this->getResponse()->setData(['error' => $message]); //xxx
        
        $this->getResponse()->setStatusCode($code);
        $this->getResponse()->setStatusMessage($message);
        echo $this->getResponse()->toJson();
    }

    public function shutdown()
    {
    }
}
<commit_msg>fixed OPTIONS header<commit_after><?php
/**
 * This file is part of the Everon framework.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Everon\Rest;

use Everon\Dependency;
use Everon\Interfaces;
use Everon\Exception;
use Everon\RequestIdentifier;
use Everon\Http;
use Everon\Rest;

/**
 * @method \Everon\Http\Interfaces\Response getResponse
 * @method \Everon\Rest\Interfaces\Request getRequest
 */
class Server extends \Everon\Core implements Rest\Interfaces\Server
{
    
    /**
     * @inheritdoc
     */
    public function run(RequestIdentifier $RequestIdentifier)
    {
        try {
            $response_headers = $this->getConfigManager()->getConfigValue('rest.response_headers', []);
            foreach ($response_headers as $name => $values) {
                $origin = $this->getRequest()->getHeader('HTTP_ORIGIN', null);
                if (strcasecmp($values['Access-Control-Allow-Origin'], $origin) === 0) {
                    foreach ($values as $header_name => $header_value) {
                        $this->getResponse()->setHeader($header_name, $header_value);
                    }
                }
            }
            
            if ($this->getRequest()->getMethod() === \Everon\Request::METHOD_OPTIONS) {
                $Ok = new Http\Message\Ok();//DRY
                $this->getResponse()->setStatusCode($Ok->getCode());
                $this->getResponse()->setStatusMessage($Ok->getMessage());
                echo $this->getResponse()->toJson(); //xxx
            }
            else {
                parent::run($RequestIdentifier);
            }
        }
        catch (Exception\Pdo $Exception) {
            $PdoException = new Http\Exception((new Http\Message\BadRequest($Exception->getMessage())));
            $this->showException($PdoException, $this->Controller);
        }
        catch (Exception\RouteNotDefined $Exception) {
            $RouteNotDefined = new Http\Exception((new Http\Message\NotFound('Invalid resource name, request method or version')));
            $this->showException($RouteNotDefined, $this->Controller);
        }
        catch (Exception\InvalidRoute $Exception) {
            $BadRequest = new Http\Exception((new Http\Message\BadRequest($Exception->getMessage())));
            $this->showException($BadRequest, $this->Controller);
        }
        catch (Http\Exception $Exception) {
            $this->showException($Exception, $this->Controller);
        }
        catch (Rest\Exception\Resource $Exception) {
            $RestException = new Http\Exception((new Http\Message\BadRequest($Exception->getMessage())));
            $this->showException($RestException, $this->Controller);
        }
        catch (\Exception $Exception) {
            $InternalServerError = new Http\Exception((new Http\Message\InternalServerError($Exception->getMessage())));
            $this->showException($InternalServerError, $this->Controller);
        }
    }
 
    public function showException(Http\Exception $Exception, $Controller)
    {
        $message = $Exception->getMessage();
        $code = $Exception->getCode();
        if ($Exception instanceof Http\Exception) {
            $message = $Exception->getHttpMessage()->getInfo();
            $code = $Exception->getHttpMessage()->getCode();
        }

        $this->getResponse()->setData(['error' => $message]); //xxx
        
        $this->getResponse()->setStatusCode($code);
        $this->getResponse()->setStatusMessage($message);
        echo $this->getResponse()->toJson();
    }

    public function shutdown()
    {
        $s = parent::shutdown();

        $this->getLogger()->response(
            sprintf(
                '[%s] (%d) %s : %s %s',
                $s,
                $this->getResponse()->getStatusCode(),
                $this->getResponse()->getStatusMessage(),
                $this->getRequest()->getMethod(),
                $this->getRequest()->getPath()
            )
        );

        return $s;
    }
}

{'fraction_non_alphanumeric': 0.14603718199608612, 'fraction_numerical': 0.0002446183953033268, 'mean_word_length': 2.2422680412371134, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 123, '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': '784833', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2071, 'n_words': 390}