httpRouteHandler function

RouteMatch? httpRouteHandler(
  1. HttpRequest req, {
  2. String? method,
  3. RequestBody? body,
})

method is the method to match, when it is not the request method (a _method form field, see MethodOverride).

body is a body that is already read. A fallback uses it, so it does not read the request body a second time.

Implementation

RouteMatch? httpRouteHandler(
  HttpRequest req, {
  String? method,
  RequestBody? body,
}) {
  /// read Host once. only domain routes need it.
  _routeIndex();
  String? host = _hasDomain ? req.headers.value(HttpHeaders.hostHeader) : null;
  String path = requestPath(req);
  RouteMatch? match = _getMatchRoute(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(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(
        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')) {
      _staticOrNotFound(req, host, method, body);
    } else {
      _notFound(req, host, method, body);
    }
  }
  return match;
}