receive method Null safety

  1. @override
Future receive(
  1. Request req
)
override

Delivers req to this instance to be processed.

This method is the entry point of a Request into this Controller. By default, it invokes this controller's handle method within a try-catch block that guarantees an HTTP response will be sent for Request.

Implementation

@override
Future receive(Request req) async {
  Controller next;
  try {
    var requestURISegmentIterator = req.raw.uri.pathSegments.iterator;

    if (req.raw.uri.pathSegments.isEmpty) {
      requestURISegmentIterator = [""].iterator;
    }

    for (var i = 0; i < _basePathSegments.length; i++) {
      requestURISegmentIterator.moveNext();
      if (_basePathSegments[i] != requestURISegmentIterator.current) {
        await _handleUnhandledRequest(req);
        return null;
      }
    }

    final node =
        _root.node!.nodeForPathSegments(requestURISegmentIterator, req.path);
    if (node?.specification == null) {
      await _handleUnhandledRequest(req);
      return null;
    }
    req.path.setSpecification(
      node!.specification!,
      segmentOffset: _basePathSegments.length,
    );
    next = node.controller!;
  } catch (any, stack) {
    return handleError(req, any, stack);
  }

  // This line is intentionally outside of the try block
  // so that this object doesn't handle exceptions for 'next'.
  return next.receive(req);
}