internal static string InsertPortIfNotSpecified(string publishUrl)
{
string[] colonParts = publishUrl.Split(new char[] { ':' });
if (colonParts.Length == 1)
{
// No port was specified so we need to add it in
int slashIndex = publishUrl.IndexOf('/');
if (slashIndex > -1)
{
publishUrl = publishUrl.Insert(slashIndex, DefaultPort);
}
else
{
publishUrl = publishUrl + DefaultPort;
}
}
if (colonParts.Length > 1)
{
// It's possible that a port was specified, but we're not sure. Apps like Monaco do weird
// things like put colon characters in the path and who knows what might happen in the future.
// We're being extra careful here to make sure that we only look for ports after the hostname.
// This means right after a colon, but never following ANY '/' characters.
// Examples of colonParts[0] might be
// test.com
// foo.com/bar
int slashIndex = colonParts[0].IndexOf('/');
if (slashIndex > -1)
{
// Since a slash was found before the first colon, we know that the first colon was
// not used for the port. Therefore we need to inject the default port before the first slash
colonParts[0] = colonParts[0].Insert(slashIndex, DefaultPort);
publishUrl = string.Join(":", colonParts);
}
}
return publishUrl;
}