2323use Ratchet \Http \HttpServerInterface ;
2424use Throwable ;
2525
26+ use function is_array ;
2627use function str_replace ;
2728use function strlen ;
2829use function trim ;
@@ -33,9 +34,10 @@ final class HttpHandler implements HttpServerInterface
3334
3435 private ExtensionMimeTypeDetector $ detector ;
3536
37+ /** @param string|string[] $indexFile */
3638 public function __construct (
3739 private FlySystemAdapter $ files ,
38- private string $ indexFile = 'index.html ' ,
40+ private string | array $ indexFile = 'index.html ' ,
3941 ) {
4042 $ this ->detector = new ExtensionMimeTypeDetector ();
4143 }
@@ -53,13 +55,17 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request
5355 // Remove leading slash and any route parameters
5456 $ requestPath = trim ($ path , '/ ' );
5557
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+ }
6369 }
6470
6571 if ($ this ->files ->has ($ requestPath )) {
@@ -81,7 +87,9 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request
8187 return ;
8288 }
8389
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> ' ;
8593 $ headers = [
8694 'Content-Type ' => 'text/html ' ,
8795 'Content-Length ' => strlen ($ content ),
@@ -95,7 +103,7 @@ private function injectWebSocketClient(string $html): string
95103 {
96104 //Read html and inject script before closing body tag
97105 $ injection = <<<'EOT'
98- <script>
106+ <script>
99107 const socket = new WebSocket('ws://' + window.location.host + '/ws');
100108 socket.addEventListener('message', function (event) {
101109 if (event.data === 'update') {
0 commit comments