-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.php
More file actions
77 lines (77 loc) · 2.22 KB
/
Copy pathrouter.php
File metadata and controls
77 lines (77 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
//Hold the http request method
$method = $_SERVER["REQUEST_METHOD"];
//Hold the http request url
$url = $_SERVER["REQUEST_URI"];
//Hold the static directory
$static = __DIR__.'/public/';
//Hold the http request query
$query = (isset($_SERVER["QUERY_STRING"]))?$_SERVER["QUERY_STRING"]:"";
//Hold the http request query paramter
$params = [];
if(strlen($query) > 1){
parse_str($query, $params);
}
//Hold the http request Content-Type
$contentType = (isset($_SERVER["CONTENT_TYPE"]))?$_SERVER["CONTENT_TYPE"]:"";
//Hold the http request Content-Length
$contentlength = strlen($contentType);
//Hold the http request body
$body = file_get_contents('php://input');
//Enable Cross-Origin-Resource-Sharing
function cors(){
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Type");
}
cors();
//Enable http methods
if($method === "GET"){
//compare with many routes
if($url == '/'){
require $static.'index.php';
}elseif (preg_match('/\bindex.html\b/i', $url)){
header("Content-Type: text/html");
readfile($static.'index.html');
}elseif(preg_match('/\bindex.css\b/i', $url)){
header("Content-Type: text/css");
readfile($static.'index.css');
}elseif(preg_match('/\bindex.js\b/i', $url)){
header("Content-Type: text/javascript");
readfile($static.'index.js');
}elseif(preg_match('/\bfavicon.ico\b/i', $url)){
header("Content-Type: image/x-icon");
readfile($static.'favicon.ico');
}else {
header("HTTP/1.1 404 Not Found");
header("Content-Type: text/html");
readfile($static.'404.html');
}
}elseif($method === "POST"){
switch($url){
case "/":{
if(strpos($contentType,'application/json') === 0){
$data = json_decode($body, true);
header("Content-Type: application/json");
$res = array("message" => "Hello ".$data["name"]);
echo json_encode($res);
}elseif(strpos($contentType,'multipart/form-data') === 0){
$data = $_POST["name"];
header("Content-Type: text/plain");
echo "Hello ".$data;
}else{
echo $body;
}
break;
}
default:{
header("HTTP/1.1 404 Not Found");
header("Content-Type: text/html");
readfile($static.'404.html');
break;
}
}
}else{
http_response_code(405);
}
?>