httpRouteHandler function
Implementation
RouteData? httpRouteHandler(HttpRequest req) {
/// read Host once. only domain routes need it.
_routeIndex();
String? host = _hasDomain ? req.headers.value(HttpHeaders.hostHeader) : null;
RouteData? route = _getMatchRoute(req.uri.path, 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 (route == null && req.method == 'HEAD') {
route = _getMatchRoute(req.uri.path, 'GET', host);
}
if (route == 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,
);
httpCorsHandler(target?.corsEnabled, req);
req.response.statusCode = HttpStatus.noContent;
responseDataHandler(null, req);
} else {
/// same path as a thrown 404, so the app ExceptionHandler and
/// responseHandler can format it. the default body is JSON.
httpErrorHandler(
req,
NotFoundHttpException(
message: '${req.method} ${req.uri.path} not found',
),
StackTrace.empty,
);
}
}
return route;
}