1111from typing import Any
1212
1313import orjson
14- from asgi_tools import ResponseWebSocket
14+ from asgi_tools import ResponseText , ResponseWebSocket
1515from asgiref import typing as asgi_types
1616from asgiref .compatibility import guarantee_single_callable
1717from servestatic import ServeStaticASGI
@@ -81,8 +81,6 @@ def __init__(
8181 self .dispatcher_pattern = re .compile (
8282 f"^{ self .dispatcher_path } (?P<dotted_path>[a-zA-Z0-9_.]+)/$"
8383 )
84- self .js_modules_pattern = re .compile (f"^{ self .web_modules_path } .*" )
85- self .static_pattern = re .compile (f"^{ self .static_path } .*" )
8684
8785 # User defined ASGI apps
8886 self .extra_http_routes : dict [str , AsgiHttpApp ] = {}
@@ -134,13 +132,13 @@ def match_dispatch_path(self, scope: asgi_types.WebSocketScope) -> bool:
134132 return bool (re .match (self .dispatcher_pattern , scope ["path" ]))
135133
136134 def match_static_path (self , scope : asgi_types .HTTPScope ) -> bool :
137- return bool ( re . match ( self . static_pattern , scope ["path" ]) )
135+ return scope ["path" ]. startswith ( self . static_path )
138136
139137 def match_web_modules_path (self , scope : asgi_types .HTTPScope ) -> bool :
140- return bool ( re . match ( self . js_modules_pattern , scope ["path" ]) )
138+ return scope ["path" ]. startswith ( self . web_modules_path )
141139
142140 def match_extra_paths (self , scope : asgi_types .Scope ) -> AsgiApp | None :
143- # Custom defined routes are unused within middleware to encourage users to handle
141+ # Custom defined routes are unused by default to encourage users to handle
144142 # routing within their root ASGI application.
145143 return None
146144
@@ -263,7 +261,7 @@ async def __call__(
263261 """ASGI app for ReactPy static files."""
264262 if not self ._static_file_server :
265263 self ._static_file_server = ServeStaticASGI (
266- self . parent . asgi_app ,
264+ Error404App () ,
267265 root = self .parent .static_dir ,
268266 prefix = self .parent .static_path ,
269267 )
@@ -285,10 +283,16 @@ async def __call__(
285283 """ASGI app for ReactPy web modules."""
286284 if not self ._static_file_server :
287285 self ._static_file_server = ServeStaticASGI (
288- self . parent . asgi_app ,
286+ Error404App () ,
289287 root = self .parent .web_modules_dir ,
290288 prefix = self .parent .web_modules_path ,
291289 autorefresh = True ,
292290 )
293291
294292 await self ._static_file_server (scope , receive , send )
293+
294+
295+ class Error404App :
296+ async def __call__ (self , scope , receive , send ):
297+ response = ResponseText ("Resource not found on this server." , status_code = 404 )
298+ await response (scope , receive , send )
0 commit comments