normalizeVmServiceUriString function

String normalizeVmServiceUriString(
  1. String raw
)

Rewrites a scraped URI into the canonical ws://host:port/<token>/ws the VM Service client dials.

Implementation

String normalizeVmServiceUriString(String raw) {
  String uri = raw;
  if (uri.startsWith('http://')) {
    uri = 'ws://${uri.substring('http://'.length)}';
  } else if (uri.startsWith('https://')) {
    uri = 'wss://${uri.substring('https://'.length)}';
  }
  if (uri.endsWith('/ws')) return uri;
  // A trailing slash after the suffix is the same URI, not a path segment to
  // append to: `.../ws/` must not become `.../ws/ws`.
  if (uri.endsWith('/ws/')) return uri.substring(0, uri.length - 1);
  return uri.endsWith('/') ? '${uri}ws' : '$uri/ws';
}