mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-10 05:06:36 +02:00
* HTTPS by default, HTTP only if specified * Added/Updated unit tests;Added FTPS * Added confirmation to system messages such as shutdown, reboot, and lock * Corrected Typo * Added confirmation to system messages such as shutdown, reboot, and lock * Corrected Typo * Made changes requested by @mykhailopylyp * Further changes per review by mykhailopylyp * Fixes per code review * Changes per Mykhailopylyp * Fix all schemes being replaced with HTTPS Added new tests * Merging conflicts * Add ftp to fix spell-check * Fix unit tests Co-authored-by: chrisharris333 <60838650+chrisharris333@users.noreply.github.com> Co-authored-by: Chris Harris <chris.harris@mytinycloud.com>
64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using Microsoft.Plugin.Uri.Interfaces;
|
|
|
|
namespace Microsoft.Plugin.Uri.UriHelper
|
|
{
|
|
public class ExtendedUriParser : IUriParser
|
|
{
|
|
public bool TryParse(string input, out System.Uri result)
|
|
{
|
|
if (string.IsNullOrEmpty(input))
|
|
{
|
|
result = default;
|
|
return false;
|
|
}
|
|
|
|
// Handle common cases UriBuilder does not handle
|
|
// Using CurrentCulture since this is a user typed string
|
|
if (input.EndsWith(":", StringComparison.CurrentCulture)
|
|
|| input.EndsWith(".", StringComparison.CurrentCulture)
|
|
|| input.EndsWith(":/", StringComparison.CurrentCulture)
|
|
|| input.All(char.IsDigit))
|
|
{
|
|
result = default;
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
var urlBuilder = new UriBuilder(input);
|
|
var hadDefaultPort = urlBuilder.Uri.IsDefaultPort;
|
|
urlBuilder.Port = hadDefaultPort ? -1 : urlBuilder.Port;
|
|
|
|
if (input.Contains("HTTP://", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
urlBuilder.Scheme = System.Uri.UriSchemeHttp;
|
|
}
|
|
else if (input.Contains(":", StringComparison.OrdinalIgnoreCase) &&
|
|
!input.Contains("http", StringComparison.OrdinalIgnoreCase) &&
|
|
!input.Contains("[", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
// Do nothing, leave unchanged
|
|
}
|
|
else
|
|
{
|
|
urlBuilder.Scheme = System.Uri.UriSchemeHttps;
|
|
}
|
|
|
|
result = urlBuilder.Uri;
|
|
return true;
|
|
}
|
|
catch (System.UriFormatException)
|
|
{
|
|
result = default;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|