URLEncode refactoring

This commit is contained in:
Jaex 2018-06-21 19:25:11 +03:00
parent 0546a31630
commit bf44d356eb
6 changed files with 23 additions and 21 deletions

View file

@ -70,6 +70,7 @@ public static class Helpers
public static readonly Version OSVersion = Environment.OSVersion.Version;
private static Cursor[] _cursorList;
public static Cursor[] CursorList
{
get

View file

@ -63,15 +63,26 @@ public static void OpenURL(string url)
}
}
private static string Encode(string text, string unreservedCharacters)
public static string URLEncode(string text, bool isPath = false)
{
StringBuilder result = new StringBuilder();
if (!string.IsNullOrEmpty(text))
{
string unreservedCharacters;
if (isPath)
{
unreservedCharacters = Helpers.URLPathCharacters;
}
else
{
unreservedCharacters = Helpers.URLCharacters;
}
foreach (char c in Encoding.UTF8.GetBytes(text))
{
if (unreservedCharacters.Contains(c))
if (unreservedCharacters.IndexOf(c) != -1)
{
result.Append(c);
}
@ -85,16 +96,6 @@ private static string Encode(string text, string unreservedCharacters)
return result.ToString();
}
public static string URLEncode(string text)
{
return Encode(text, Helpers.URLCharacters);
}
public static string URLPathEncode(string text)
{
return Encode(text, Helpers.URLPathCharacters);
}
public static string HtmlEncode(string text)
{
char[] chars = HttpUtility.HtmlEncode(text).ToCharArray();

View file

@ -144,7 +144,7 @@ public override UploadResult Upload(Stream stream, string fileName)
string canonicalURI = uploadPath;
if (isPathStyleRequest) canonicalURI = URLHelpers.CombineURL(Settings.Bucket, canonicalURI);
canonicalURI = URLHelpers.AddSlash(canonicalURI, SlashType.Prefix);
canonicalURI = URLHelpers.URLPathEncode(canonicalURI);
canonicalURI = URLHelpers.URLEncode(canonicalURI, true);
string canonicalQueryString = "";
string canonicalHeaders = CreateCanonicalHeaders(headers);
string signedHeaders = GetSignedHeaders(headers);
@ -251,7 +251,7 @@ public string GenerateURL(string uploadPath)
{
if (!string.IsNullOrEmpty(Settings.Endpoint) && !string.IsNullOrEmpty(Settings.Bucket))
{
uploadPath = URLHelpers.URLPathEncode(uploadPath);
uploadPath = URLHelpers.URLEncode(uploadPath, true);
string url;

View file

@ -118,7 +118,7 @@ public bool DownloadFile(string path, Stream downloadStream)
{
if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo))
{
string url = URLHelpers.CombineURL(URLFiles, URLHelpers.URLPathEncode(path));
string url = URLHelpers.CombineURL(URLFiles, URLHelpers.URLEncode(path, true));
string query = OAuthManager.GenerateQuery(url, null, HttpMethod.GET, AuthInfo);
return SendRequestDownload(HttpMethod.GET, query, downloadStream);
}
@ -136,7 +136,7 @@ public UploadResult UploadFile(Stream stream, string path, string fileName)
return null;
}
string url = URLHelpers.CombineURL(URLFiles, URLHelpers.URLPathEncode(path));
string url = URLHelpers.CombineURL(URLFiles, URLHelpers.URLEncode(path, true));
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("overwrite", "true");
@ -168,7 +168,7 @@ public CopyContentInfo GetMetadata(string path)
if (OAuthInfo.CheckOAuth(AuthInfo))
{
string url = URLHelpers.CombineURL(URLMetaData, URLHelpers.URLPathEncode(path));
string url = URLHelpers.CombineURL(URLMetaData, URLHelpers.URLEncode(path, true));
string query = OAuthManager.GenerateQuery(url, null, HttpMethod.GET, AuthInfo);
@ -210,7 +210,7 @@ public string CreatePublicURL(string path, CopyURLType urlType = CopyURLType.Def
{
path = path.Trim('/');
string url = URLHelpers.CombineURL(URLLinks, URLHelpers.URLPathEncode(path));
string url = URLHelpers.CombineURL(URLLinks, URLHelpers.URLEncode(path, true));
string query = OAuthManager.GenerateQuery(url, null, HttpMethod.POST, AuthInfo);

View file

@ -150,7 +150,7 @@ public string GenerateURL(string uploadPath)
Domain = URLHelpers.CombineURL("storage.googleapis.com", Bucket);
}
uploadPath = URLHelpers.URLPathEncode(uploadPath);
uploadPath = URLHelpers.URLEncode(uploadPath, true);
string url = URLHelpers.CombineURL(Domain, uploadPath);

View file

@ -144,7 +144,7 @@ public string GetUriPath(string filename)
filename = URLHelpers.URLEncode(filename);
string subFolderPath = GetSubFolderPath();
subFolderPath = URLHelpers.URLPathEncode(subFolderPath);
subFolderPath = URLHelpers.URLEncode(subFolderPath, true);
string httpHomePath = GetHttpHomePath();
@ -157,7 +157,7 @@ public string GetUriPath(string filename)
}
else
{
path = URLHelpers.URLPathEncode(httpHomePath);
path = URLHelpers.URLEncode(httpHomePath, true);
}
if (Port != 80)