toLocalUrl method

String toLocalUrl()

Converts the current string (assumed to be a URL) to a local HTTP address.

  • If the string does not start with 'http', returns itself.
  • If the URL already points to the local IP and port, returns itself.
  • Otherwise, replaces the host and port with local config values, and adds an 'origin' query parameter (base64-encoded original origin).

Implementation

String toLocalUrl() {
  if (!startsWith('http')) return this;
  Uri uri = this.toSafeUri();
  if (uri.host == Config.ip && uri.port == Config.port) return this;
  Map<String, String> queryParameters = {...uri.queryParameters};
  queryParameters.putIfAbsent(
      'origin', () => base64Url.encode(utf8.encode(uri.origin)));
  uri = uri.replace(
    scheme: 'http',
    host: Config.ip,
    port: Config.port,
    queryParameters: queryParameters,
  );
  return uri.toString();
}