CarotDav_decompile/Rei.Fs/UriUtil.cs

452 lines
15 KiB
C#

// Decompiled with JetBrains decompiler
// Type: Rei.Fs.UriUtil
// Assembly: Rei.Fs, Version=1.13.2.8796, Culture=neutral, PublicKeyToken=null
// MVID: D8B08A8B-697C-4439-9CFF-1BE4EE46F7B0
// Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\Rei.Fs.dll
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Rei.Fs
{
public class UriUtil
{
private const string unescapestringforsegment = "!$&'()*+,-.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
private const string unescapestringforquery = "!$&'()*+,-.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~/?";
private const string FormUrlEncodeReserved = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_.!*'(),\",";
private const string OAuthUnreserved = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~";
public static Uri AddLastSlash(Uri target)
{
string leftPart = target.GetLeftPart(UriPartial.Path);
if (leftPart.EndsWith("/"))
return target;
return new Uri(leftPart + "/" + target.Query + target.Fragment);
}
public static Uri RemoveLastSlash(Uri target)
{
if (target.Segments.Length == 1)
return target;
string leftPart = target.GetLeftPart(UriPartial.Path);
if (!leftPart.EndsWith("/"))
return target;
return new Uri(leftPart.Substring(0, checked (leftPart.Length - 1)) + target.Query + target.Fragment);
}
public static bool EndWithSlash(Uri target)
{
return target.GetLeftPart(UriPartial.Path).EndsWith("/");
}
public static Uri GetParent(Uri target)
{
string str = target.AbsolutePath;
int index = checked (str.Length - 1);
while (index >= 0 && Operators.CompareString(Conversions.ToString(str[index]), "/", false) == 0)
checked { --index; }
while (index >= 0 && Operators.CompareString(Conversions.ToString(str[index]), "/", false) != 0)
checked { --index; }
while (index >= 0 && Operators.CompareString(Conversions.ToString(str[index]), "/", false) == 0)
checked { --index; }
int num = checked (index + 1);
if (num == 0)
{
str = "/";
num = 0;
}
if (Operators.CompareString(target.Authority, "", false) == 0)
return new Uri(target.Scheme + ":" + str.Substring(0, checked (num + 1)));
return new Uri(target.Scheme + "://" + target.Authority + str.Substring(0, checked (num + 1)));
}
public static Uri GetRoot(Uri target)
{
if (Operators.CompareString(target.Authority, "", false) == 0)
return new Uri(target.Scheme + ":/");
return new Uri(target.Scheme + "://" + target.Authority + "/");
}
public static string GetLastSegment(Uri target)
{
string absolutePath = target.AbsolutePath;
int length = absolutePath.Length;
int num = length;
while (num == length)
{
checked { --length; }
if (length < 0)
{
num = -1;
break;
}
num = absolutePath.LastIndexOf('/', length);
if (num < 0)
{
num = -1;
break;
}
}
return absolutePath.Substring(checked (num + 1), checked (length - num));
}
public static string GetLastName(Uri target)
{
string stringToUnescape = UriUtil.GetLastSegment(target);
try
{
stringToUnescape = Uri.UnescapeDataString(stringToUnescape);
}
catch (UriFormatException ex)
{
ProjectData.SetProjectError((Exception) ex);
ProjectData.ClearProjectError();
}
return stringToUnescape;
}
public static string GetAuthority(Uri target)
{
return target.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.UriEscaped) + "/";
}
public static Uri CombineSegment(Uri baseuri, string segment)
{
if (baseuri.AbsoluteUri.EndsWith("/"))
return new Uri(baseuri.AbsoluteUri + segment);
return new Uri(baseuri.AbsoluteUri + "/" + segment);
}
public static Uri CombineName(Uri baseuri, string name)
{
return UriUtil.CombineSegment(baseuri, Uri.EscapeDataString(name));
}
public static bool UriEquals(Uri u1, Uri u2, bool casesensitive)
{
if ((object) u1 == null)
return (object) u2 == null;
if ((object) u2 == null)
return false;
if (casesensitive)
return Uri.Compare(UriUtil.RemoveLastSlash(u1), UriUtil.RemoveLastSlash(u2), UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.InvariantCulture) == 0;
return Uri.Compare(UriUtil.RemoveLastSlash(u1), UriUtil.RemoveLastSlash(u2), UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.InvariantCultureIgnoreCase) == 0;
}
private static string UriEncodeConvertHelper(string str, Encoding sourceencoding, Encoding destinationencoding, bool queryorfragment)
{
if (str.Contains("%u") || str.Contains("%U"))
throw new ArgumentException("cannot use %u encoding uri");
List<byte> byteList = new List<byte>();
int index1;
while (index1 < str.Length)
{
if ((int) str[index1] != 37)
{
byteList.Add(Convert.ToByte(str[index1]));
checked { ++index1; }
}
else
{
byteList.Add(Convert.ToByte(str.Substring(checked (index1 + 1), 2), 16));
checked { index1 += 3; }
}
}
byte[] numArray = new byte[checked (byteList.Count - 1 + 1)];
byteList.CopyTo(numArray);
str = sourceencoding.GetString(numArray);
StringBuilder stringBuilder = new StringBuilder(checked (str.Length * 3));
string str1 = !queryorfragment ? "!$&'()*+,-.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~" : "!$&'()*+,-.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~/?";
int num1 = 0;
int num2 = checked (str.Length - 1);
int index2 = num1;
while (index2 <= num2)
{
if (str1.Contains(Conversions.ToString(str[index2])))
{
stringBuilder.Append(str[index2]);
}
else
{
byte[] bytes = destinationencoding.GetBytes(Conversions.ToString(str[index2]));
int index3 = 0;
while (index3 < bytes.Length)
{
byte num3 = bytes[index3];
char ch = Convert.ToChar(num3);
if (str1.Contains(Conversions.ToString(ch)))
{
stringBuilder.Append(ch);
}
else
{
stringBuilder.Append("%");
stringBuilder.Append(num3.ToString("X2"));
}
checked { ++index3; }
}
}
checked { ++index2; }
}
str = stringBuilder.ToString();
return str;
}
public static Uri UriEncodeConvert(Uri target, Encoding sourceencoding, Encoding destinationencoding)
{
if (sourceencoding == null)
sourceencoding = Encoding.UTF8;
if (destinationencoding == null)
destinationencoding = Encoding.UTF8;
if (sourceencoding == destinationencoding)
return target;
string components = target.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.UriEscaped);
string[] strArray = target.GetComponents(UriComponents.Path, UriFormat.UriEscaped).Split(new char[1]
{
'/'
}, StringSplitOptions.None);
int num1 = 0;
int num2 = checked (strArray.Length - 1);
int index = num1;
while (index <= num2)
{
strArray[index] = UriUtil.UriEncodeConvertHelper(strArray[index], sourceencoding, destinationencoding, false);
checked { ++index; }
}
string str1 = target.GetComponents(UriComponents.Query, UriFormat.UriEscaped);
if (str1.Length > 0)
str1 = UriUtil.UriEncodeConvertHelper(str1, sourceencoding, destinationencoding, true);
string str2 = target.GetComponents(UriComponents.Fragment, UriFormat.UriEscaped);
if (str2.Length > 0)
str2 = UriUtil.UriEncodeConvertHelper(str2, sourceencoding, destinationencoding, true);
string uriString = components + "/" + string.Join("/", strArray);
if (str1.Length > 0)
uriString = uriString + "?" + str1;
if (str2.Length > 0)
uriString = uriString + "#" + str2;
return new Uri(uriString);
}
public static bool IsChild(Uri baseuri, Uri target)
{
if (checked (baseuri.Segments.Length + 1) != target.Segments.Length)
return false;
return UriUtil.IsDescendant(baseuri, target);
}
public static bool IsDescendant(Uri baseuri, Uri target)
{
if (checked (baseuri.Segments.Length + 1) > target.Segments.Length || Operators.CompareString(baseuri.Scheme, target.Scheme, false) != 0 || (Operators.CompareString(baseuri.Host, target.Host, false) != 0 || baseuri.Port != target.Port) || Operators.CompareString(baseuri.UserInfo, target.UserInfo, false) != 0)
return false;
int num1 = 0;
int num2 = checked (baseuri.Segments.Length - 1);
int index = num1;
while (index <= num2)
{
if (!UriUtil.IsSameSegmentString(baseuri.Segments[index], target.Segments[index]))
return false;
checked { ++index; }
}
return true;
}
public static bool IsSameSegmentString(string seg1, string seg2)
{
while (seg1.EndsWith("/"))
seg1 = seg1.Substring(0, checked (seg1.Length - 1));
while (seg2.EndsWith("/"))
seg2 = seg2.Substring(0, checked (seg2.Length - 1));
return Operators.CompareString(Uri.UnescapeDataString(seg1), Uri.UnescapeDataString(seg2), false) == 0;
}
public static bool IsRoot(Uri u)
{
return u.Segments.Length < 2;
}
public static string FormUrlEncode(string str)
{
StringBuilder stringBuilder = new StringBuilder();
int num1 = 0;
int num2 = checked (str.Length - 1);
int index1 = num1;
while (index1 <= num2)
{
if ((int) str[index1] == 32)
stringBuilder.Append("+");
else if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_.!*'(),\",".Contains(Conversions.ToString(str[index1])))
{
stringBuilder.Append(str[index1]);
}
else
{
byte[] bytes = Encoding.UTF8.GetBytes(Conversions.ToString(str[index1]));
int index2 = 0;
while (index2 < bytes.Length)
{
byte num3 = bytes[index2];
stringBuilder.Append("%");
stringBuilder.Append(num3.ToString("X2"));
checked { ++index2; }
}
}
checked { ++index1; }
}
return stringBuilder.ToString();
}
public static string OAuthEncode(string str)
{
StringBuilder stringBuilder = new StringBuilder();
int num1 = 0;
int num2 = checked (str.Length - 1);
int index1 = num1;
while (index1 <= num2)
{
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~".Contains(Conversions.ToString(str[index1])))
{
stringBuilder.Append(str[index1]);
}
else
{
byte[] bytes = Encoding.UTF8.GetBytes(Conversions.ToString(str[index1]));
int index2 = 0;
while (index2 < bytes.Length)
{
byte num3 = bytes[index2];
stringBuilder.Append("%");
stringBuilder.Append(num3.ToString("X2"));
checked { ++index2; }
}
}
checked { ++index1; }
}
return stringBuilder.ToString();
}
public static string OAuthDecode(string str)
{
List<byte> byteList = new List<byte>();
int index = 0;
while (index < str.Length)
{
byte result;
if (Operators.CompareString(Conversions.ToString(str[index]), "%", false) == 0 && index < checked (str.Length - 3) && byte.TryParse(str.Substring(checked (index + 1), 2), NumberStyles.HexNumber, (IFormatProvider) null, out result))
{
byteList.Add(result);
checked { index += 3; }
}
else
{
byteList.AddRange((IEnumerable<byte>) Encoding.UTF8.GetBytes(Conversions.ToString(str[index])));
checked { ++index; }
}
}
return Encoding.UTF8.GetString(byteList.ToArray());
}
public static Uri CreateNextCopyName(Uri target)
{
int result = 2;
string lastSegment = UriUtil.GetLastSegment(target);
string str = UriUtil.RemoveExtension(lastSegment);
if (str.EndsWith(")"))
{
int length = str.LastIndexOf("(");
if (int.TryParse(str.Substring(checked (length + 1), checked (str.Length - length - 2)), out result))
{
str = str.Substring(0, length).TrimEnd();
checked { ++result; }
}
}
return new Uri(UriUtil.GetParent(target).AbsoluteUri + str + " (" + result.ToString() + ")" + UriUtil.GetExtension(lastSegment));
}
public static string CreateNextCopyName(string name)
{
int result = 1;
string str = UriUtil.RemoveExtension(name);
if (str.EndsWith(")"))
{
int num = str.LastIndexOf("(");
if (int.TryParse(str.Substring(checked (num + 1), checked (str.Length - num - 2)), out result))
str = str.Substring(0, checked (num - 1));
}
int num1 = checked (result + 1);
return str + " (" + num1.ToString() + ")" + UriUtil.GetExtension(name);
}
public static string CreateFormUrlencodedString(Dictionary<string, string> values)
{
List<string> stringList = new List<string>();
Dictionary<string, string>.KeyCollection.Enumerator enumerator;
try
{
enumerator = values.Keys.GetEnumerator();
while (enumerator.MoveNext())
{
string current = enumerator.Current;
stringList.Add(Uri.EscapeDataString(current).Replace("%20", "+") + "=" + Uri.EscapeDataString(values[current]).Replace("%20", "+"));
}
}
finally
{
enumerator.Dispose();
}
return string.Join("&", stringList.ToArray());
}
public static Uri CreateUriFromSchemeAndPath(string scheme, string path)
{
string[] strArray = path.Split(new char[1]{ '/' }, StringSplitOptions.RemoveEmptyEntries);
string str = "";
if (strArray.Length == 0)
{
str = "/";
}
else
{
int num1 = 0;
int num2 = checked (strArray.Length - 1);
int index = num1;
while (index <= num2)
{
str = str + "/" + Uri.EscapeDataString(strArray[index]);
checked { ++index; }
}
}
return new Uri(scheme + ":" + str);
}
private static string GetFilename(string path)
{
while (path.EndsWith("\\"))
path = path.Substring(0, checked (path.Length - 1));
int num = path.LastIndexOf("\\");
if (num < 0)
return path;
return path.Substring(checked (num + 1));
}
private static string RemoveExtension(string filename)
{
int length = filename.LastIndexOf(".");
if (length < 0)
return filename;
return filename.Substring(0, length);
}
private static string GetExtension(string path)
{
string filename = UriUtil.GetFilename(path);
int num = filename.LastIndexOf(".");
if (num < 0 || checked (num + 1) >= filename.Length)
return "";
return filename.Substring(checked (num + 1));
}
}
}