﻿// MIT License. Copyright (c) 2016 Maxim Kuzmin. Contacts: https://github.com/maxim-kuzmin

// Author: Maxim Kuzmin

using System;

namespace Makc2016.Core.Extensions.Networking.OfUrl
{
    /// <summary>
    /// Ядро. Расширения. Работа с сетью. Расширение URL.
    /// </summary>
    public static class CoreExtensionNetworkingOfUrl
    {
        #region Public methods

        /// <summary>
        /// Заменить хост в URL на новый.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="host">Новый хост.</param>
        /// <returns>URL с новым хостом.</returns>
        public static string ReplaceUrlHost(this string url, string host)
        {
            if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(host)) return url;

            var uri = new Uri(url);

            var port = uri.IsDefaultPort ? string.Empty : ":" + uri.Port;

            url = string.Concat(uri.Scheme, Uri.SchemeDelimiter, host, port, uri.PathAndQuery);

            return url;
        }

        #endregion Public methods
    }
}
