mirror of https://bitbucket.org/anguist/ntpa
Fix bug in URIs with mono 2.6
This commit is contained in:
parent
aa5ad73b7e
commit
e28c0b6e85
|
@ -396,10 +396,10 @@ protected Uri CheckLink(string link, string keyword)
|
|||
return null;
|
||||
|
||||
Uri uri;
|
||||
if (Uri.TryCreate(link, UriKind.Absolute, out uri))
|
||||
if (Uri.TryCreate("/" + link.TrimStart('/'), UriKind.Relative, out uri))
|
||||
return uri;
|
||||
|
||||
if (Uri.TryCreate("/" + link.TrimStart('/'), UriKind.Relative, out uri))
|
||||
if (Uri.TryCreate(link, UriKind.Absolute, out uri))
|
||||
return uri;
|
||||
|
||||
var name = Keyword.Find(Symbol).Name;
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Ntp.Common.Web
|
||||
{
|
||||
|
@ -29,41 +30,98 @@ public static class UriExtensions
|
|||
{
|
||||
public static Uri Append(this Uri uri, params string[] paths)
|
||||
{
|
||||
string basePath = uri.ToString();
|
||||
var uriKind = Uri.IsWellFormedUriString(basePath, UriKind.Absolute)
|
||||
? UriKind.Absolute
|
||||
: UriKind.Relative;
|
||||
string basePath;
|
||||
UriKind uriKind;
|
||||
|
||||
GetBase(uri, out basePath, out uriKind);
|
||||
string resPath = paths.Aggregate(
|
||||
basePath,
|
||||
(current, path) => $"{current.TrimEnd('/')}/{path.TrimStart('/')}"
|
||||
);
|
||||
(current, path) => $"{current.TrimEnd('/')}/{path.TrimStart('/')}");
|
||||
Uri newUri = new Uri(resPath, uriKind);
|
||||
|
||||
return new Uri(resPath, uriKind);
|
||||
return newUri;
|
||||
}
|
||||
|
||||
public static Uri Append(this Uri uri, Uri relativeUri, params string[] paths)
|
||||
{
|
||||
var list = new List<string> {relativeUri.ToString()};
|
||||
string relativeUriText = relativeUri.IsAbsoluteUri
|
||||
? relativeUri.AbsolutePath
|
||||
: relativeUri.ToString();
|
||||
var list = new List<string> { relativeUriText };
|
||||
list.AddRange(paths);
|
||||
return uri.Append(list.ToArray());
|
||||
}
|
||||
|
||||
public static Uri Append(this Uri uri, Uri relativeUri, string path)
|
||||
{
|
||||
var list = new List<string> {relativeUri.ToString(), path};
|
||||
string relativeUriText = relativeUri.IsAbsoluteUri
|
||||
? relativeUri.AbsolutePath
|
||||
: relativeUri.ToString();
|
||||
var list = new List<string> { relativeUriText, path};
|
||||
return uri.Append(list.ToArray());
|
||||
}
|
||||
|
||||
public static Uri AppendExtension(this Uri uri, string extension)
|
||||
{
|
||||
string basePath = uri.ToString().TrimEnd('/') + extension;
|
||||
return new Uri(basePath, uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative);
|
||||
string basePath;
|
||||
UriKind uriKind;
|
||||
|
||||
GetBase(uri, out basePath, out uriKind);
|
||||
basePath = basePath.TrimEnd('/') + extension;
|
||||
Uri newUri = new Uri(basePath, uriKind);
|
||||
|
||||
return newUri;
|
||||
}
|
||||
|
||||
public static string ToHtmlString(this Uri uri)
|
||||
{
|
||||
return Uri.EscapeUriString(uri.ToString());
|
||||
}
|
||||
|
||||
private static void GetBase(Uri uri, out string basePath, out UriKind uriKind)
|
||||
{
|
||||
string orig = uri.OriginalString.TrimStart();
|
||||
if (orig.StartsWith("/") && !orig.StartsWith("//")) {
|
||||
basePath = uri.IsAbsoluteUri
|
||||
? uri.AbsolutePath
|
||||
: uri.ToString();
|
||||
uriKind = UriKind.Relative;
|
||||
} else {
|
||||
basePath = uri.ToString();
|
||||
uriKind = UriKind.Absolute;
|
||||
}
|
||||
}
|
||||
|
||||
public static string DebugInfo(this Uri uri)
|
||||
{
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.AppendLine($"ToString(): {uri.ToString()}");
|
||||
b.AppendLine($"IsAbsoluteUri: {uri.IsAbsoluteUri}");
|
||||
b.AppendLine($"OriginalString: {uri.OriginalString}");
|
||||
|
||||
if (uri.IsAbsoluteUri) {
|
||||
b.AppendLine($"AbsolutePath: {uri.AbsolutePath}");
|
||||
b.AppendLine($"AbsoluteUri: {uri.AbsoluteUri}");
|
||||
b.AppendLine($"Authority: {uri.Authority}");
|
||||
b.AppendLine($"DnsSafeHost: {uri.DnsSafeHost}");
|
||||
b.AppendLine($"Fragment: {uri.Fragment}");
|
||||
b.AppendLine($"Host: {uri.Host}");
|
||||
b.AppendLine($"HostNameType: {uri.HostNameType}");
|
||||
b.AppendLine($"IsDefaultPort {uri.IsDefaultPort}");
|
||||
b.AppendLine($"IsFile: {uri.IsFile}");
|
||||
b.AppendLine($"IsLoopback: {uri.IsLoopback}");
|
||||
b.AppendLine($"IsUnc: {uri.IsUnc}");
|
||||
b.AppendLine($"LocalPath: {uri.LocalPath}");
|
||||
b.AppendLine($"PathAndQuery: {uri.PathAndQuery}");
|
||||
b.AppendLine($"Port: {uri.Port}");
|
||||
b.AppendLine($"Query: {uri.Query}");
|
||||
b.AppendLine($"Scheme: {uri.Scheme}");
|
||||
b.AppendLine($"Segments: {uri.Segments}");
|
||||
b.AppendLine($"UserEscaped: {uri.UserEscaped}");
|
||||
b.AppendLine($"UserInfo: {uri.UserInfo}");
|
||||
}
|
||||
|
||||
return b.ToString();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue