redirect static method

FutureOr<Message> redirect({
  1. required Request request,
  2. required String path,
  3. required BuildContext context,
})

Implementation

static FutureOr<Message> redirect(
    {required Request request,
    required String path,
    required BuildContext context}) async {
  var uri = Uri.parse(path);

  if (uri.hasScheme) {
    if (uri.scheme.startsWith("http")) {
      if (request is HttpStyleRequest) {
        var uriString = uri.toString();
        var regex = RegExp(r"%7B([^}]*)%7D");
        if (regex.hasMatch(uriString)) {
          uriString = uriString.replaceAllMapped(regex, (match) {
            var matched = uriString.substring(match.start, match.end);
            matched = matched.substring(3, matched.length - 3);
            return request.path.arguments[matched] ?? "null";
          });
        }

        request.baseRequest.response
          ..statusCode = 301
          ..headers.add("Location", uriString)
          ..close();
        return NoResponseRequired(request: request);
      } else {
        //var req = await HttpClient().getUrl(uri);
        //var res = await req.close();
        //var resBodyList = await res.toList();
        //var resBodyBinary = mergeList(resBodyList as List<Uint8List>);
        //var resBody = utf8.decode(resBodyBinary);
        throw "un";
      }
    } else {
      throw "un";
    }
  } else {
    var segments = List<String>.from(uri.pathSegments);

    if (segments.first != "..") {
      var service = context.findAncestorServiceByName(segments.first);
      if (service == null) {
        throw "Service Not Found";
      }
      segments.removeAt(0);
      request.path.notProcessedValues.addAll(segments);
      request.path.next = segments.first;
      return service.calling(request);
    }

    var nBinding = context;
    while (segments.first == "..") {
      var n = nBinding.findAncestorBindingOfType<GatewayBinding>();
      var s = nBinding.findAncestorBindingOfType<ServerBinding>();
      if (n == null && s == null) {
        throw Exception("Path No Found"
            " : ${request.path.next} in ${nBinding.component}");
      }
      nBinding = n as GatewayBinding;
      segments.removeAt(0);
    }
    request.path.notProcessedValues.addAll(segments);
    request.path.next = segments.first;

    return ((nBinding).findAncestorBindingOfType<RouteBinding>() ??
            nBinding.findAncestorBindingOfType<ServerBinding>()!)
        .calling(request);
  }
}