tryServe method
Attempts to serve a static file for the given HttpRequest.
Returns true if file was served or 304 sent.
Returns false to continue to next middleware/router.
Security:
- Prevents path traversal (
../) - Resolves symlinks to prevent escaping root
- Only allows
GETandHEAD
Implementation
Future<bool> tryServe(HttpRequest req) async {
final method = req.method.toUpperCase();
if (method != 'GET' && method != 'HEAD') return false;
final rel = _sanitize(req.uri.path);
final candidate = rel.endsWith('/') ? '${rel}index.html' : rel;
// Try exact file
final file = File(_join(root.path, candidate));
if (await _serveIfExists(req, file)) return true;
// SPA fallback
if (spaFallback) {
final index = File(_join(root.path, 'index.html'));
if (await _serveIfExists(req, index)) return true;
}
return false;
}