private string ParseConnectionUri()

in MySQL.Data/src/X/XDevAPI/BaseSession.cs [518:596]


    private string ParseConnectionUri(string connectionUri)
    {
      Uri uri = null;
      string updatedUri = null;
      bool parseServerAsUnixSocket = false;
      string hierPart = null;
      try
      {
        uri = new Uri(connectionUri);
      }
      catch (UriFormatException ex)
      {
        if (ex.Message != "Invalid URI: The hostname could not be parsed.")
          throw;

        // Identify if multiple hosts were specified.
        string[] splitUri = connectionUri.Split('@', '?');
        if (splitUri.Length == 1) throw;

        hierPart = splitUri[1];
        var schema = string.Empty;
        parseServerAsUnixSocket = IsUnixSocket(hierPart);
        bool isArray = hierPart.StartsWith("[") && hierPart.Contains("]");

        // Remove schema.
        if ((!parseServerAsUnixSocket && hierPart.Contains("/")) && !isArray ||
          (parseServerAsUnixSocket && hierPart.Contains(")/")) ||
          (hierPart.StartsWith("[") && hierPart.Contains("]/") && isArray))
        {
          schema = hierPart.Substring(hierPart.LastIndexOf('/') + 1);
          hierPart = hierPart.Substring(0, hierPart.Length - schema.Length - 1);
        }

        if (parseServerAsUnixSocket)
        {
          updatedUri = splitUri[0] + "@localhost" +
            (schema != string.Empty ? "/" + schema : string.Empty) +
            (splitUri.Length > 2 ? "?" + splitUri[2] : string.Empty);
        }
        else if (isArray)
        {
          hierPart = hierPart.Substring(1, hierPart.Length - 2);
          int hostCount = FailoverManager.ParseHostList(hierPart, true, true);
          if (FailoverManager.FailoverGroup != null)
          {
            hierPart = FailoverManager.FailoverGroup.ActiveHost.Host;
            parseServerAsUnixSocket = IsUnixSocket(FailoverManager.FailoverGroup.ActiveHost.Host);
            updatedUri = splitUri[0] + "@" +
              (parseServerAsUnixSocket ? "localhost" : hierPart) +
              (FailoverManager.FailoverGroup.ActiveHost.Port != -1 ? ":" + FailoverManager.FailoverGroup.ActiveHost.Port : string.Empty) +
              (schema != string.Empty ? "/" + schema : string.Empty) +
              (splitUri.Length == 3 ? "?" + splitUri[2] : string.Empty);
          }
          else if (hostCount == 1)
            updatedUri = splitUri[0] + "@" + hierPart +
              (schema != string.Empty ? "/" + schema : string.Empty) +
              (splitUri.Length == 3 ? "?" + splitUri[2] : string.Empty);
          else
            throw;
        }
      }

      if (uri == null)
        uri = updatedUri == null ? new Uri(connectionUri) : new Uri(updatedUri);

      if (uri.Scheme == DNS_SRV_URI_SCHEME)
      {
        if (FailoverManager.FailoverGroup != null && FailoverManager.FailoverGroup.Hosts?.Count > 1)
          throw new ArgumentException(Resources.DnsSrvInvalidConnOptionMultihost);
        if (!uri.IsDefaultPort)
          throw new ArgumentException(Resources.DnsSrvInvalidConnOptionPort);
        if (parseServerAsUnixSocket)
          throw new ArgumentException(Resources.DnsSrvInvalidConnOptionUnixSocket);
      }
      else if (uri.Scheme != MYSQLX_URI_SCHEME)
        throw new ArgumentException(string.Format(ResourcesX.DnsSrvInvalidScheme, uri.Scheme));

      return ConvertToConnectionString(uri, hierPart, parseServerAsUnixSocket, uri.Scheme == DNS_SRV_URI_SCHEME);
    }