23
23
use Ratchet \Http \HttpServerInterface ;
24
24
use Throwable ;
25
25
26
+ use function is_array ;
26
27
use function str_replace ;
27
28
use function strlen ;
28
29
use function trim ;
@@ -33,9 +34,10 @@ final class HttpHandler implements HttpServerInterface
33
34
34
35
private ExtensionMimeTypeDetector $ detector ;
35
36
37
+ /** @param string|string[] $indexFile */
36
38
public function __construct (
37
39
private FlySystemAdapter $ files ,
38
- private string $ indexFile = 'index.html ' ,
40
+ private string | array $ indexFile = 'index.html ' ,
39
41
) {
40
42
$ this ->detector = new ExtensionMimeTypeDetector ();
41
43
}
@@ -53,13 +55,17 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request
53
55
// Remove leading slash and any route parameters
54
56
$ requestPath = trim ($ path , '/ ' );
55
57
56
- // For empty path (root) serve index.html
57
- if ($ requestPath === '' ) {
58
- $ requestPath = $ this ->indexFile ;
59
- }
60
-
61
- if ($ this ->files ->isDirectory ($ requestPath )) {
62
- $ requestPath .= '/ ' . $ this ->indexFile ;
58
+ if ($ requestPath === '' || $ this ->files ->isDirectory ($ requestPath )) {
59
+ if (is_array ($ this ->indexFile )) {
60
+ foreach ($ this ->indexFile as $ indexFile ) {
61
+ if ($ this ->files ->has (trim ($ requestPath . '/ ' . $ indexFile , '/ ' ))) {
62
+ $ requestPath = trim ($ requestPath . '/ ' . $ indexFile , '/ ' );
63
+ break ;
64
+ }
65
+ }
66
+ } else {
67
+ $ requestPath .= '/ ' . $ this ->indexFile ;
68
+ }
63
69
}
64
70
65
71
if ($ this ->files ->has ($ requestPath )) {
@@ -81,7 +87,9 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request
81
87
return ;
82
88
}
83
89
84
- $ content = '<!DOCTYPE html><html><body><h1>404 - Page Not Found</h1></body></html> ' ;
90
+ $ content = '<!DOCTYPE html><html><body><h1>404 - Page Not Found</h1>
91
+ <p>Path ' . $ requestPath . ' does not exist</p>
92
+ </body></html> ' ;
85
93
$ headers = [
86
94
'Content-Type ' => 'text/html ' ,
87
95
'Content-Length ' => strlen ($ content ),
@@ -95,7 +103,7 @@ private function injectWebSocketClient(string $html): string
95
103
{
96
104
//Read html and inject script before closing body tag
97
105
$ injection = <<<'EOT'
98
- <script>
106
+ <script>
99
107
const socket = new WebSocket('ws://' + window.location.host + '/ws');
100
108
socket.addEventListener('message', function (event) {
101
109
if (event.data === 'update') {
0 commit comments