Passed
Pull Request — master (#11)
by Joao
02:10
created

HttpRequestHandler::withCorsOrigins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByJG\RestServer;
4
5
use ByJG\RestServer\Exception\ClassNotFoundException;
6
use ByJG\RestServer\Exception\Error404Exception;
7
use ByJG\RestServer\Exception\Error405Exception;
8
use ByJG\RestServer\Exception\Error520Exception;
9
use ByJG\RestServer\Exception\InvalidClassException;
10
use ByJG\RestServer\OutputProcessor\BaseOutputProcessor;
11
use ByJG\RestServer\OutputProcessor\OutputProcessorInterface;
12
use ByJG\RestServer\Route\RouteDefinitionInterface;
13
use Closure;
14
use FastRoute\Dispatcher;
15
use InvalidArgumentException;
16
17
class HttpRequestHandler implements RequestHandler
18
{
19
    const OK = "OK";
20
    const METHOD_NOT_ALLOWED = "NOT_ALLOWED";
21
    const NOT_FOUND = "NOT FOUND";
22
23
    protected $useErrorHandler = true;
24
    protected $detailedErrorHandler = false;
25
    protected $corsOrigins = [];
26
    protected $corsMethods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
27
    protected $corsHeaders = [
28
        'Authorization',
29
        'Content-Type',
30
        'Accept',
31
        'Origin',
32
        'User-Agent',
33
        'Cache-Control',
34
        'Keep-Alive',
35
        'X-Requested-With',
36
        'If-Modified-Since'
37
    ];
38
39
    protected $defaultOutputProcessor = null;
40
    protected $defaultOutputProcessorArgs = [];
41
42
    /**
43
     * @param RouteDefinitionInterface $routeDefinition
44
     * @return bool
45
     * @throws ClassNotFoundException
46
     * @throws Error404Exception
47
     * @throws Error405Exception
48
     * @throws Error520Exception
49
     * @throws InvalidClassException
50
     */
51 7
    protected function process(RouteDefinitionInterface $routeDefinition)
52
    {
53
        // Initialize ErrorHandler with default error handler
54 7
        if ($this->useErrorHandler) {
55 7
            ErrorHandler::getInstance()->register();
56
        }
57
58
        // Get HttpRequest
59 7
        $request = $this->getHttpRequest();
60
61
        // Get the URL parameters
62 7
        $httpMethod = $request->server('REQUEST_METHOD');
63 7
        $uri = parse_url($request->server('REQUEST_URI'), PHP_URL_PATH);
64 7
        $query = parse_url($request->server('REQUEST_URI'), PHP_URL_QUERY);
65 7
        $queryStr = [];
66 7
        if (!empty($query)) {
67
            parse_str($query, $queryStr);
68
        }
69
70
        // Generic Dispatcher for RestServer
71 7
        $dispatcher = $routeDefinition->getDispatcher();
72
73 7
        $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
74
75
        // Processing
76 7
        switch ($routeInfo[0]) {
77 7
            case Dispatcher::NOT_FOUND:
78 3
                if ($this->tryDeliveryPhysicalFile() === false) {
79 2
                    $this->prepareToOutput();
80 2
                    throw new Error404Exception("Route '$uri' not found");
81
                }
82 1
                return true;
83
84 4
            case Dispatcher::METHOD_NOT_ALLOWED:
85 1
                $outputProcessor = $this->prepareToOutput();
86 1
                if (strtoupper($httpMethod) == "OPTIONS" && !empty($this->corsOrigins)) {
0 ignored issues
show
Bug introduced by
It seems like $httpMethod can also be of type boolean; however, parameter $string of strtoupper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
                if (strtoupper(/** @scrutinizer ignore-type */ $httpMethod) == "OPTIONS" && !empty($this->corsOrigins)) {
Loading history...
87
                    $this->executeRequest($outputProcessor, function () {}, $request);
88
                    return;
89
                }
90 1
                throw new Error405Exception('Method not allowed');
91
92 3
            case Dispatcher::FOUND:
93
                // ... 200 Process:
94 3
                $vars = array_merge($routeInfo[2], $queryStr);
95
96
                // Get the Selected Route
97 3
                $selectedRoute = $routeInfo[1];
98
99
                // Default Handler for errors and
100 3
                $outputProcessor = $this->prepareToOutput($selectedRoute["output_processor"]);
101
102
                // Class
103 3
                $class = $selectedRoute["class"];
104 3
                $request->appendVars($vars);
105
106
                // Execute the request
107 3
                $this->executeRequest($outputProcessor, $class, $request);
108
109 2
                break;
110
111
            default:
112
                throw new Error520Exception('Unknown');
113
        }
114
    }
115
116 6
    protected function prepareToOutput($class = null)
117
    {
118 6
        if (!empty($class)) {
119 3
            $outputProcessor = BaseOutputProcessor::getFromClassName($class);
120 3
        } elseif (!empty($this->defaultOutputProcessor)) {
121
            $outputProcessor = BaseOutputProcessor::getFromClassName($this->defaultOutputProcessor);
122
        } else {
123 3
            $outputProcessor = BaseOutputProcessor::getFromHttpAccept();
124
        }
125 6
        $outputProcessor->writeContentType();
126 6
        if ($this->detailedErrorHandler) {
127
            ErrorHandler::getInstance()->setHandler($outputProcessor->getDetailedErrorHandler());
128
        } else {
129 6
            ErrorHandler::getInstance()->setHandler($outputProcessor->getErrorHandler());
130
        }
131
132 6
        return $outputProcessor;
133
    }
134
135 7
    protected function getHttpRequest()
136
    {
137 7
        return new HttpRequest($_GET, $_POST, $_SERVER, isset($_SESSION) ? $_SESSION : [], $_COOKIE);
138
    }
139
140
    /**
141
     * @param OutputProcessorInterface $outputProcessor
142
     * @param $class
143
     * @param HttpRequest $request
144
     * @throws ClassNotFoundException
145
     * @throws InvalidClassException
146
     */
147 3
    protected function executeRequest(OutputProcessorInterface $outputProcessor, $class, HttpRequest $request)
148
    {
149
        // Create the Request and Response methods
150 3
        $response = new HttpResponse();
151
152 3
        if (!empty($this->corsOrigins)) {
153
            // Allow from any origin
154
            if (!empty($request->server('HTTP_ORIGIN'))) {
155
                foreach ((array)$this->corsOrigins as $origin) {
156
                    if (preg_match("~^.*//$origin$~", $request->server('HTTP_ORIGIN'))) {
0 ignored issues
show
Bug introduced by
It seems like $request->server('HTTP_ORIGIN') can also be of type boolean; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
                    if (preg_match("~^.*//$origin$~", /** @scrutinizer ignore-type */ $request->server('HTTP_ORIGIN'))) {
Loading history...
157
                        $response->addHeader("Access-Control-Allow-Origin", $request->server('HTTP_ORIGIN'));
158
                        $response->addHeader('Access-Control-Allow-Credentials', 'true');
159
                        $response->addHeader('Access-Control-Max-Age', '86400');    // cache for 1 day
160
161
                        // Access-Control headers are received during OPTIONS requests
162
                        if ($request->server('REQUEST_METHOD') == 'OPTIONS') {
163
                            $response->addHeader("Access-Control-Allow-Methods", implode(",", array_merge(['OPTIONS'], $this->corsMethods)));
164
                            $response->addHeader("Access-Control-Allow-Headers", implode(",", $this->corsHeaders));
165
                            $outputProcessor->processResponse($response);
166
                            return;
167
                        }
168
                        break;
169
                    }
170
                }
171
            }
172
        }
173
174
        // Process Closure
175 3
        if ($class instanceof Closure) {
176 2
            $class($response, $request);
177 2
            $outputProcessor->processResponse($response);
178 2
            return;
179
        }
180
181
        // Process Class::Method()
182 1
        $function = $class[1];
183 1
        $class =  $class[0];
184 1
        if (!class_exists($class)) {
185 1
            throw new ClassNotFoundException("Class '$class' defined in the route is not found");
186
        }
187
        $instance = new $class();
188
        if (!method_exists($instance, $function)) {
189
            throw new InvalidClassException("There is no method '$class::$function''");
190
        }
191
        $instance->$function($response, $request);
192
        $outputProcessor->processResponse($response);
193
    }
194
195
    /**
196
     * Handle the ROUTE (see web/app-dist.php)
197
     *
198
     * @param RouteDefinitionInterface $routeDefinition
199
     * @param bool $outputBuffer
200
     * @param bool $session
201
     * @return bool|void
202
     * @throws ClassNotFoundException
203
     * @throws Error404Exception
204
     * @throws Error405Exception
205
     * @throws Error520Exception
206
     * @throws InvalidClassException
207
     */
208 7
    public function handle(RouteDefinitionInterface $routeDefinition, $outputBuffer = true, $session = false)
209
    {
210 7
        if ($outputBuffer) {
211
            ob_start();
212
        }
213 7
        if ($session) {
214
            session_start();
215
        }
216
217
        // --------------------------------------------------------------------------
218
        // Check if script exists or if is itself
219
        // --------------------------------------------------------------------------
220 7
        return $this->process($routeDefinition);
221
    }
222
223
    /**
224
     * @return bool
225
     * @throws Error404Exception
226
     */
227 3
    protected function tryDeliveryPhysicalFile()
228
    {
229 3
        $file = $_SERVER['SCRIPT_FILENAME'];
230 3
        if (!empty($file) && file_exists($file)) {
231 3
            $mime = $this->mimeContentType($file);
232
233 3
            if ($mime === false) {
0 ignored issues
show
introduced by
The condition $mime === false is always false.
Loading history...
234 2
                return false;
235
            }
236
237 1
            if (!defined("RESTSERVER_TEST")) {
238
                header("Content-Type: $mime");
239
            }
240 1
            echo file_get_contents($file);
241 1
            return true;
242
        }
243
244
        return false;
245
    }
246
247
    /**
248
     * Get the Mime Type based on the filename
249
     *
250
     * @param string $filename
251
     * @return string
252
     * @throws Error404Exception
253
     */
254 7
    protected function mimeContentType($filename)
255
    {
256
        $prohibitedTypes = [
257
            "php",
258
            "vb",
259
            "cs",
260
            "rb",
261
            "py",
262
            "py3",
263
            "lua"
264
        ];
265
266
        $mimeTypes = [
267
            'txt' => 'text/plain',
268
            'htm' => 'text/html',
269
            'html' => 'text/html',
270
            'css' => 'text/css',
271
            'js' => 'application/javascript',
272
            'json' => 'application/json',
273
            'xml' => 'application/xml',
274
            'swf' => 'application/x-shockwave-flash',
275
            'flv' => 'video/x-flv',
276
            // images
277
            'png' => 'image/png',
278
            'jpe' => 'image/jpeg',
279
            'jpeg' => 'image/jpeg',
280
            'jpg' => 'image/jpeg',
281
            'gif' => 'image/gif',
282
            'bmp' => 'image/bmp',
283
            'ico' => 'image/vnd.microsoft.icon',
284
            'tiff' => 'image/tiff',
285
            'tif' => 'image/tiff',
286
            'svg' => 'image/svg+xml',
287
            'svgz' => 'image/svg+xml',
288
            // archives
289
            'zip' => 'application/zip',
290
            'rar' => 'application/x-rar-compressed',
291
            'exe' => 'application/x-msdownload',
292
            'msi' => 'application/x-msdownload',
293
            'cab' => 'application/vnd.ms-cab-compressed',
294
            // audio/video
295
            'mp3' => 'audio/mpeg',
296
            'qt' => 'video/quicktime',
297
            'mov' => 'video/quicktime',
298
            // adobe
299
            'pdf' => 'application/pdf',
300
            'psd' => 'image/vnd.adobe.photoshop',
301
            'ai' => 'application/postscript',
302
            'eps' => 'application/postscript',
303
            'ps' => 'application/postscript',
304
            // ms office
305
            'doc' => 'application/msword',
306
            'rtf' => 'application/rtf',
307
            'xls' => 'application/vnd.ms-excel',
308
            'ppt' => 'application/vnd.ms-powerpoint',
309
            // open office
310
            'odt' => 'application/vnd.oasis.opendocument.text',
311
            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
312
        ];
313
314 7
        if (!file_exists($filename)) {
315 1
            throw new Error404Exception();
316
        }
317
318 6
        $ext = substr(strrchr($filename, "."), 1);
319 6
        if (!in_array($ext, $prohibitedTypes)) {
320 4
            if (array_key_exists($ext, $mimeTypes)) {
321 4
                return $mimeTypes[$ext];
322
            } elseif (function_exists('finfo_open')) {
323
                $finfo = finfo_open(FILEINFO_MIME);
324
                $mimetype = finfo_file($finfo, $filename);
325
                finfo_close($finfo);
326
                return $mimetype;
327
            }
328
        }
329
330 2
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
331
    }
332
333
    public function withDoNotUseErrorHandler()
334
    {
335
        $this->useErrorHandler = false;
336
    }
337
338
    public function withDetailedErrorHandler()
339
    {
340
        $this->detailedErrorHandler = true;
341
    }
342
343
    public function withCorsOrigins($origins)
344
    {
345
        $this->corsOrigins = $origins;
346
        return $this;
347
    }
348
349
    public function withAcceptCorsHeaders($headers)
350
    {
351
        $this->corsHeaders = $headers;
352
        return $this;
353
    }
354
355
    public function withAcceptCorsMethods($methods)
356
    {
357
        $this->corsMethods = $methods;
358
        return $this;
359
    }
360
361
    public function withDefaultOutputProcessor($processor, $args = [])
362
    {
363
        if (!($processor instanceof \Closure)) {
364
            if (!is_string($processor)) {
365
                throw new InvalidArgumentException("Default processor needs to class name of an OutputProcessor");
366
            }
367
            if (!is_subclass_of($processor, BaseOutputProcessor::class)) {
368
                throw new InvalidArgumentException("Needs to be a class of " . BaseOutputProcessor::class);
369
            }
370
        }
371
372
        $this->defaultOutputProcessor = $processor;
373
374
        return $this;
375
    }
376
}
377