public static bool TryGetUserInfo(this Uri uri, out string userName, out string password) { EnsureArgument.NotNull(uri, nameof(uri)); userName = null; password = null; if (string.IsNullOrWhiteSpace(uri.UserInfo)) { return(false); } /* According to RFC 3986 section 3.2.1 (https://tools.ietf.org/html/rfc3986#section-3.2.1) * the user information component of a URI should look like: * * url-encode(username):url-encode(password) */ string[] split = uri.UserInfo.Split(new[] { ':' }, count: 2); if (split.Length > 0) { userName = WebUtility.UrlDecode(split[0]); } if (split.Length > 1) { password = WebUtility.UrlDecode(split[1]); } return(split.Length > 0); }
public HttpClientFactory(ITrace trace, ISettings settings, IStandardStreams streams) { EnsureArgument.NotNull(trace, nameof(trace)); EnsureArgument.NotNull(settings, nameof(settings)); EnsureArgument.NotNull(streams, nameof(streams)); _trace = trace; _settings = settings; _streams = streams; }
/// <summary> /// Truncate the string from the first index of the given character, also removing the indexed character. /// </summary> /// <param name="str">String to truncate.</param> /// <param name="c">Character to locate the index of.</param> /// <returns>Truncated string.</returns> public static string TruncateFromIndexOf(this string str, char c) { EnsureArgument.NotNull(str, nameof(str)); int last = str.IndexOf(c); if (last > -1) { return(str.Substring(0, last)); } return(str); }
/// <summary> /// Trim all characters at the start of the string until the last index of the given string, /// also removing the indexed character. /// </summary> /// <param name="str">String to trim.</param> /// <param name="value">String to locate the index of.</param> /// <param name="comparisonType">Comparison rule for locating the string.</param> /// <returns>Trimmed string.</returns> public static string TrimUntilLastIndexOf(this string str, string value, StringComparison comparisonType = StringComparison.Ordinal) { EnsureArgument.NotNull(str, nameof(str)); int first = str.LastIndexOf(value, comparisonType); if (first > -1) { return(str.Substring(first + value.Length, str.Length - first - value.Length)); } return(str); }
/// <summary> /// Trim all characters at the start of the string until the last index of the given character, /// also removing the indexed character. /// </summary> /// <param name="str">String to trim.</param> /// <param name="c">Character to locate the index of.</param> /// <returns>Trimmed string.</returns> public static string TrimUntilLastIndexOf(this string str, char c) { EnsureArgument.NotNull(str, nameof(str)); int first = str.LastIndexOf(c); if (first > -1) { return(str.Substring(first + 1, str.Length - first - 1)); } return(str); }
private void Init(Uri remoteUri, Uri proxyUri, bool isCertificateVerificationEnabled, string oAuthConsumerKey, string oAuthConsumerSecret, string traceConfig) { EnsureArgument.NotNull(remoteUri, nameof(remoteUri)); RemoteUri = remoteUri; ProxyUri = proxyUri; IsCertificateVerificationEnabled = isCertificateVerificationEnabled; OAuthConsumerKey = oAuthConsumerKey; OAuthConsumerSecret = oAuthConsumerSecret; Trace = traceConfig; }
public static IEnumerable <string> GetGitConfigurationScopes(this Uri uri) { EnsureArgument.NotNull(uri, nameof(uri)); string schemeAndDelim = $"{uri.Scheme}{Uri.SchemeDelimiter}"; string host = uri.Host.TrimEnd('/'); string path = uri.AbsolutePath.Trim('/'); string port = uri.AbsoluteUri.Contains($":{uri.Port}") ? $":{uri.Port}" : string.Empty; // Unfold the path by component, right-to-left while (!string.IsNullOrWhiteSpace(path)) { yield return($"{schemeAndDelim}{host}{port}/{path}"); // Trim off the last path component if (!TryTrimString(path, StringExtensions.TruncateFromLastIndexOf, '/', out path)) { break; } } // Unfold the host by sub-domain, left-to-right while (!string.IsNullOrWhiteSpace(host)) { if (host.Contains(".") || host.Equals("localhost", StringComparison.InvariantCultureIgnoreCase)) // Do not emit just the TLD { yield return($"{schemeAndDelim}{host}{port}"); } // Trim off the left-most sub-domain if (!TryTrimString(host, StringExtensions.TrimUntilIndexOf, '.', out host)) { break; } } }
private static string FormatText(string message, string filePath, int lineNumber, string memberName) { const int sourceColumnMaxWidth = 23; EnsureArgument.NotNull(message, nameof(message)); EnsureArgument.NotNull(filePath, nameof(filePath)); EnsureArgument.PositiveOrZero(lineNumber, nameof(lineNumber)); EnsureArgument.NotNull(memberName, nameof(memberName)); // Source column format is file:line string source = $"{filePath}:{lineNumber}"; if (source.Length > sourceColumnMaxWidth) { int idx = 0; int maxlen = sourceColumnMaxWidth - 3; int srclen = source.Length; while (idx >= 0 && (srclen - idx) > maxlen) { idx = source.IndexOf('\\', idx + 1); } // If we cannot find a path separator which allows the path to be long enough, just truncate the file name if (idx < 0) { idx = srclen - maxlen; } source = "..." + source.Substring(idx); } // Git's trace format is "{timestamp,-15} {source,-23} trace: {details}" string text = $"{DateTime.Now:HH:mm:ss.ffffff} {source,-23} trace: [{memberName}] {message}"; return(text); }