Skip to content

Commit f05a32f

Browse files
committed
A rework of the phpserver router
- handles versioned static assets - our own mime handling for those, since php's built in web server's mime table isn't accessible from userland - works with a magento instance set up with use-rewrites=1
1 parent 8bbddb0 commit f05a32f

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

phpserver/router.php

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,74 +28,93 @@
2828
$val = json_encode($val);
2929
}
3030

31-
echo 'debug: ' . $val . PHP_EOL . '<br/>' . PHP_EOL;
31+
error_log('debug: '.$val);
3232
};
3333

3434
/**
35-
* Note: the code below is experimental and not intended to be used outside development environment.
36-
* The code is protected against running outside of PHP built-in web server.
35+
* Caution, this is very experimental stuff
36+
* no guarantee for working result
37+
* has tons of potential big security holes
3738
*/
3839

3940
if (php_sapi_name() === 'cli-server') {
40-
$debug($_SERVER["REQUEST_URI"]);
41+
42+
$debug("URI: {$_SERVER["REQUEST_URI"]}");
4143
if (preg_match('/^\/(index|get|static)\.php(\/)?/', $_SERVER["REQUEST_URI"])) {
4244
return false; // serve the requested resource as-is.
4345
}
4446

4547
$path = pathinfo($_SERVER["SCRIPT_FILENAME"]);
4648
$url = pathinfo(substr($_SERVER["REQUEST_URI"], 1));
4749
$route = parse_url(substr($_SERVER["REQUEST_URI"], 1))["path"];
48-
49-
$debug($path);
50-
$debug($route);
50+
$ext = @pathinfo($route)['extension'];
5151

5252
if ($path["basename"] == 'favicon.ico') {
5353
return false;
5454
}
5555

56-
$debug($route);
57-
$debug(strpos($route, 'errors/default/css/'));
56+
$debug("route: $route");
5857

5958
if (strpos($route, 'pub/errors/default/') === 0) {
6059
$route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1);
6160
}
6261

63-
$debug($route);
64-
65-
if (strpos($route, 'static/version') === 0) {
66-
$redirectRoute = preg_replace("/version\d+\//", "", $route, 1);
67-
$redirectDebugInfo = "redirect static version string to: " . $redirectRoute;
68-
$debug($redirectDebugInfo);
69-
header('Location: /' . $redirectRoute);
70-
exit;
71-
}
62+
$magentoPackagePubDir = __DIR__."/../pub";
7263

73-
if (strpos($route, 'media/') === 0 ||
64+
if (
65+
strpos($route, 'media/') === 0 ||
7466
strpos($route, 'opt/') === 0 ||
7567
strpos($route, 'static/') === 0 ||
7668
strpos($route, 'errors/default/css/') === 0 ||
7769
strpos($route, 'errors/default/images/') === 0
7870
) {
79-
$magentoPackagePubDir = __DIR__ . "/../pub";
8071

81-
$file = $magentoPackagePubDir . '/' . $route;
82-
$debug($file);
83-
if (file_exists($file)) {
72+
$origFile = $magentoPackagePubDir.'/'.$route;
73+
74+
if (strpos($route, 'static/version') === 0) {
75+
$route = preg_replace('#static/(version\d+/)?#', 'static/', $route, 1);
76+
}
77+
$file = $magentoPackagePubDir.'/'.$route;
78+
79+
$debug("file: $file");
80+
81+
if (file_exists($origFile)) {
8482
$debug('file exists');
8583
return false;
84+
} else if (file_exists($file)) {
85+
$mimeTypes = [
86+
'css' => 'text/css',
87+
'js' => 'application/javascript',
88+
'jpg' => 'image/jpg',
89+
'png' => 'image/png',
90+
'gif' => 'image/gif',
91+
'svg' => 'image/svg+xml',
92+
'map' => 'application/json',
93+
'woff' => 'application/x-woff',
94+
'woff2' => 'application/font-woff2',
95+
'html' => 'text/html',
96+
];
97+
98+
$type = @$mimeTypes[$ext];
99+
if ($type) {
100+
header("Content-Type: $type");
101+
}
102+
return readfile($file);
86103
} else {
87104
$debug('file does not exist');
88105
if (strpos($route, 'static/') === 0) {
89-
$route = preg_replace('#static/#', '', $route, 1);
90106
$_GET['resource'] = $route;
91-
include $magentoPackagePubDir . '/static.php';
107+
$debug("static: $route");
108+
include($magentoPackagePubDir.'/static.php');
92109
exit;
93110
} elseif (strpos($route, 'media/') === 0) {
94-
include $magentoPackagePubDir . '/get.php';
111+
$debug("media: $route");
112+
include($magentoPackagePubDir.'/get.php');
95113
exit;
96114
}
97115
}
116+
} else {
117+
$debug("thunk to index in $route");
118+
include($magentoPackagePubDir.'/index.php');
98119
}
99-
100-
header('HTTP/1.0 404 Not Found');
101120
}

0 commit comments

Comments
 (0)