<commit_before><?php
namespace LunixREST\Server;

use LunixREST\Endpoint\Exceptions\UnknownEndpointException;
use LunixREST\Exceptions\AccessDeniedException;
use LunixREST\Exceptions\InvalidAPIKeyException;
use LunixREST\Exceptions\ThrottleLimitExceededException;
use LunixREST\Request\BodyParser\Exceptions\InvalidRequestDataException;
use LunixREST\Request\BodyParser\Exceptions\UnknownContentTypeException;
use LunixREST\Request\RequestFactory\RequestFactory;
use LunixREST\Request\URLParser\Exceptions\InvalidRequestURLException;
use LunixREST\Response\Exceptions\NotAcceptableResponseTypeException;
use LunixREST\Server\Exceptions\MethodNotFoundException;

//TODO: Unit test? Might be impossible (this is a weird global class, and hopefully the ONLY weird global class)
class HTTPServer {
    /**
     * @var Server
     */
    protected $server;
    /**
     * @var RequestFactory
     */
    private $requestFactory;

    /**
     * HTTPServer constructor.
     * @param Server $server
     * @param RequestFactory $requestFactory
     */
    //TODO: Add RequestLogger that we can pass through to log requests
    //TODO: Add ErrorLogger to log errors (500s)
    public function __construct(Server $server, RequestFactory $requestFactory) {
        $this->server = $server;
        $this->requestFactory = $requestFactory;
    }

    public function handleSAPIRequest() {
        try {
            $request = $this->requestFactory->create($_SERVER['REQUEST_METHOD'], getallheaders(),
                file_get_contents("php://input"), $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']);

            try {
                $response = $this->server->handleRequest($request);
                header("Content-Type: " . $response->getMIMEType());
                //TODO: Find a way for the Response to specify additional headers (to enable auth, as well as pagination)
                echo $response->getAsString();
            } catch(InvalidAPIKeyException $e){
                header('400 Bad Request', true, 400);
            } catch(UnknownEndpointException $e){
                header('404 Not Found', true, 404);
            } catch(NotAcceptableResponseTypeException $e){
                header('406 Not Acceptable', true, 406);
            } catch(AccessDeniedException $e){
                header('403 Access Denied', true, 403);
            } catch(ThrottleLimitExceededException $e){
                header('429 Too Many Requests', true, 429);
            } catch (MethodNotFoundException $e) {
                header('500 Internal Server Error', true, 500);
            } catch (\Exception $e) {
                header('500 Internal Server Error', true, 500);
            }
        } catch(InvalidRequestURLException $e){
            header('400 Bad Request', true, 400);
        } catch(UnknownContentTypeException $e){
            header('400 Bad Request', true, 400);
        } catch(InvalidRequestDataException $e){
            header('400 Bad Request', true, 400);
        } catch(\Exception $e){
            header('500 Internal Server Error', true, 500);
        }
    }
}
<commit_msg>fix broken catch in httpserver dur to moved exception<commit_after><?php
namespace LunixREST\Server;

use LunixREST\Endpoint\Exceptions\UnknownEndpointException;
use LunixREST\Exceptions\AccessDeniedException;
use LunixREST\Exceptions\InvalidAPIKeyException;
use LunixREST\Exceptions\ThrottleLimitExceededException;
use LunixREST\Request\BodyParser\BodyParserFactory\Exceptions\UnknownContentTypeException;
use LunixREST\Request\BodyParser\Exceptions\InvalidRequestDataException;
use LunixREST\Request\RequestFactory\RequestFactory;
use LunixREST\Request\URLParser\Exceptions\InvalidRequestURLException;
use LunixREST\Response\Exceptions\NotAcceptableResponseTypeException;
use LunixREST\Server\Exceptions\MethodNotFoundException;

//TODO: Unit test? Might be impossible (this is a weird global class, and hopefully the ONLY weird global class)
class HTTPServer {
    /**
     * @var Server
     */
    protected $server;
    /**
     * @var RequestFactory
     */
    private $requestFactory;

    /**
     * HTTPServer constructor.
     * @param Server $server
     * @param RequestFactory $requestFactory
     */
    //TODO: Add RequestLogger that we can pass through to log requests
    //TODO: Add ErrorLogger to log errors (500s)
    public function __construct(Server $server, RequestFactory $requestFactory) {
        $this->server = $server;
        $this->requestFactory = $requestFactory;
    }

    public function handleSAPIRequest() {
        try {
            $request = $this->requestFactory->create($_SERVER['REQUEST_METHOD'], getallheaders(),
                file_get_contents("php://input"), $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']);

            try {
                $response = $this->server->handleRequest($request);
                header("Content-Type: " . $response->getMIMEType());
                //TODO: Find a way for the Response to specify additional headers (to enable auth, as well as pagination)
                echo $response->getAsString();
            } catch(InvalidAPIKeyException $e){
                header('400 Bad Request', true, 400);
            } catch(UnknownEndpointException $e){
                header('404 Not Found', true, 404);
            } catch(NotAcceptableResponseTypeException $e){
                header('406 Not Acceptable', true, 406);
            } catch(AccessDeniedException $e){
                header('403 Access Denied', true, 403);
            } catch(ThrottleLimitExceededException $e){
                header('429 Too Many Requests', true, 429);
            } catch (MethodNotFoundException $e) {
                header('500 Internal Server Error', true, 500);
            } catch (\Exception $e) {
                header('500 Internal Server Error', true, 500);
            }
        } catch(InvalidRequestURLException $e){
            header('400 Bad Request', true, 400);
        } catch(UnknownContentTypeException $e){
            header('400 Bad Request', true, 400);
        } catch(InvalidRequestDataException $e){
            header('400 Bad Request', true, 400);
        } catch(\Exception $e){
            header('500 Internal Server Error', true, 500);
        }
    }
}

{'fraction_non_alphanumeric': 0.10542264752791068, 'fraction_numerical': 0.02200956937799043, 'mean_word_length': 2.8052184466019416, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '5077921', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1585, 'n_words': 420}