httpRouteHandler function
method is the method to match, when it is not the request method
(a _method form field, see MethodOverride).
Implementation
RouteMatch? httpRouteHandler(HttpRequest req, {String? method}) {
/// read Host once. only domain routes need it.
_routeIndex();
String? host = _hasDomain ? req.headers.value(HttpHeaders.hostHeader) : null;
RouteMatch? match = _getMatchRoute(req.uri.path, method ?? req.method, host);
/// HEAD runs the GET route when there is no HEAD route.
/// the server sends the status and headers, with no body.
if (match == null && req.method == 'HEAD') {
match = _getMatchRoute(req.uri.path, 'GET', host);
}
if (match == null) {
/// answer preflight OPTIONS with 204 and cors headers.
/// use cors setting of the route the browser is asking about.
if (req.method == HttpRequestMethod.OPTIONS.name) {
RouteData? target = _getMatchRoute(
req.uri.path,
req.headers.value('access-control-request-method') ?? '',
host,
)?.route;
httpCorsHandler(target?.corsEnabled, req);
req.response.statusCode = HttpStatus.noContent;
responseDataHandler(null, req);
} else if (Route().statics.isNotEmpty &&
(req.method == 'GET' || req.method == 'HEAD')) {
/// a file of `Route.staticFiles`, else 404
httpStaticHandler(req, Route().statics).then(
(bool sent) {
if (!sent) _notFound(req, host, method);
},
onError: (Object error, StackTrace stackTrace) {
httpErrorHandler(req, error, stackTrace);
},
);
} else {
_notFound(req, host, method);
}
}
return match;
}