Move CreateQuery to URLHelpers

This commit is contained in:
Jaex 2017-09-24 10:14:27 +03:00
parent 16bfa023ef
commit 3d70b045c7
14 changed files with 32 additions and 55 deletions

View file

@ -370,9 +370,7 @@ public static string RemovePrefixes(string url)
public static string GetShortURL(string url)
{
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
{
string host = uri.Host;
@ -390,9 +388,19 @@ public static string GetShortURL(string url)
return url;
}
public static string CreateQuery(string url, Dictionary<string, string> args)
public static string CreateQuery(Dictionary<string, string> args, bool customEncoding = false)
{
string query = CreateQuery(args);
if (args != null && args.Count > 0)
{
return string.Join("&", args.Select(x => x.Key + "=" + (customEncoding ? URLEncode(x.Value) : HttpUtility.UrlEncode(x.Value))).ToArray());
}
return "";
}
public static string CreateQuery(string url, Dictionary<string, string> args, bool customEncoding = false)
{
string query = CreateQuery(args, customEncoding);
if (!string.IsNullOrEmpty(query))
{
@ -402,16 +410,6 @@ public static string CreateQuery(string url, Dictionary<string, string> args)
return url;
}
public static string CreateQuery(Dictionary<string, string> args)
{
if (args != null && args.Count > 0)
{
return string.Join("&", args.Select(x => x.Key + "=" + URLEncode(x.Value)).ToArray());
}
return "";
}
public static string RemoveQuery(string url)
{
if (!string.IsNullOrEmpty(url))

View file

@ -28,11 +28,9 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Text;
using System.Web;
namespace ShareX.UploadersLib
{
@ -154,7 +152,7 @@ protected string SendRequest(HttpMethod method, string url, string content, stri
protected string SendRequestURLEncoded(HttpMethod method, string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
ResponseType responseType = ResponseType.Text)
{
string query = CreateQuery(args);
string query = URLHelpers.CreateQuery(args);
return SendRequest(method, url, query, ContentTypeURLEncoded, args, headers, cookies, responseType);
}
@ -288,7 +286,7 @@ private HttpWebResponse GetResponse(HttpMethod method, string url, Stream data =
try
{
url = CreateQuery(url, args);
url = URLHelpers.CreateQuery(url, args);
long length = 0;
@ -510,28 +508,6 @@ private string ResponseToString(WebResponse response, ResponseType responseType
return null;
}
protected string CreateQuery(Dictionary<string, string> args)
{
if (args != null && args.Count > 0)
{
return string.Join("&", args.Select(x => x.Key + "=" + HttpUtility.UrlEncode(x.Value)).ToArray());
}
return "";
}
protected string CreateQuery(string url, Dictionary<string, string> args)
{
string query = CreateQuery(args);
if (!string.IsNullOrEmpty(query))
{
return url + "?" + query;
}
return url;
}
protected NameValueCollection CreateAuthenticationHeader(string username, string password)
{
string authInfo = username + ":" + password;

View file

@ -131,7 +131,7 @@ public override UploadResult Upload(Stream stream, string fileName)
canonicalURI = URLHelpers.AddSlash(canonicalURI, SlashType.Prefix);
canonicalURI = URLHelpers.URLPathEncode(canonicalURI);
string canonicalQueryString = URLHelpers.CreateQuery(args);
string canonicalQueryString = URLHelpers.CreateQuery(args, true);
string canonicalHeaders = CreateCanonicalHeaders(headers);
string canonicalRequest = "PUT" + "\n" +
@ -158,7 +158,7 @@ public override UploadResult Upload(Stream stream, string fileName)
headers.Remove("host");
string url = URLHelpers.CombineURL(host, canonicalURI);
url = URLHelpers.CreateQuery(url, args);
url = URLHelpers.CreateQuery(url, args, true);
url = URLHelpers.ForcePrefix(url, "https://");
NameValueCollection responseHeaders = SendRequestGetHeaders(HttpMethod.PUT, url, stream, contentType, null, headers);

View file

@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System.Collections.Generic;
using System.Collections.Specialized;
@ -82,7 +83,7 @@ public string GetAuthorizationURL()
args.Add("response_type", "code");
args.Add("client_id", AuthInfo.Client_ID);
return CreateQuery("https://www.box.com/api/oauth2/authorize", args);
return URLHelpers.CreateQuery("https://www.box.com/api/oauth2/authorize", args);
}
public bool GetAccessToken(string pin)

View file

@ -114,7 +114,7 @@ public string GetAuthorizationURL()
args.Add("response_type", "code");
args.Add("client_id", AuthInfo.Client_ID);
return CreateQuery(URLOAuth2Authorize, args);
return URLHelpers.CreateQuery(URLOAuth2Authorize, args);
}
public bool GetAccessToken(string code)

View file

@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System.Collections.Generic;
using System.Drawing;
@ -83,7 +84,7 @@ public Ge_ttShare CreateShare(string accessToken)
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("accesstoken", accessToken);
string url = CreateQuery("https://api.ge.tt/1/shares/create", args);
string url = URLHelpers.CreateQuery("https://api.ge.tt/1/shares/create", args);
string response = SendRequest(HttpMethod.POST, url);
return JsonConvert.DeserializeObject<Ge_ttShare>(response);

View file

@ -98,7 +98,7 @@ public string GetAuthorizationURL()
args.Add("response_type", "code");
args.Add("redirect_uri", Links.URL_CALLBACK);
return CreateQuery(URL_AUTHORIZE, args);
return URLHelpers.CreateQuery(URL_AUTHORIZE, args);
}
public bool GetAccessToken(string code)

View file

@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
@ -89,7 +90,7 @@ public string GetAuthorizationURL()
args.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
args.Add("scope", "https://www.googleapis.com/auth/drive");
return CreateQuery("https://accounts.google.com/o/oauth2/auth", args);
return URLHelpers.CreateQuery("https://accounts.google.com/o/oauth2/auth", args);
}
public bool GetAccessToken(string code)

View file

@ -120,7 +120,7 @@ private string SimpleUpload(Stream stream, string fileName)
args.Add("path", UploadPath);
args.Add("response_format", "json");
args.Add("signature", GetSignature("upload/simple.php", args));
string url = CreateQuery(_apiUrl + "upload/simple.php", args);
string url = URLHelpers.CreateQuery(_apiUrl + "upload/simple.php", args);
UploadResult res = SendRequestFile(url, stream, fileName, "Filedata");
if (!res.IsSuccess) throw new IOException(res.ErrorsToString());
SimpleUploadResponse resp = DeserializeResponse<SimpleUploadResponse>(res.Response);

View file

@ -82,7 +82,7 @@ public string GetAuthorizationURL()
args.Add("response_type", "code");
args.Add("redirect_uri", Links.URL_CALLBACK);
return CreateQuery("https://login.live.com/oauth20_authorize.srf", args);
return URLHelpers.CreateQuery("https://login.live.com/oauth20_authorize.srf", args);
}
public bool GetAccessToken(string code)
@ -181,7 +181,7 @@ public override UploadResult Upload(Stream stream, string fileName)
folderPath = "me/skydrive/files";
}
string url = CreateQuery(URLHelpers.CombineURL("https://apis.live.net/v5.0", folderPath), args);
string url = URLHelpers.CreateQuery(URLHelpers.CombineURL("https://apis.live.net/v5.0", folderPath), args);
UploadResult result = SendRequestFile(url, stream, fileName);

View file

@ -118,7 +118,7 @@ private string GetAutomaticName()
if (!string.IsNullOrEmpty(host))
{
if (host.StartsWith("www."))
if (host.StartsWith("www.", StringComparison.InvariantCultureIgnoreCase))
{
host = host.Substring(4);
}

View file

@ -113,7 +113,7 @@ public string GetAuthorizationURL()
args.Add("client_id", AuthInfo.Client_ID);
args.Add("response_type", "pin");
return CreateQuery("https://api.imgur.com/oauth2/authorize", args);
return URLHelpers.CreateQuery("https://api.imgur.com/oauth2/authorize", args);
}
public bool GetAccessToken(string pin)

View file

@ -89,7 +89,7 @@ public string GetAuthorizationURL()
args.Add("redirect_uri", Links.URL_CALLBACK);
args.Add("scope", "gist");
return CreateQuery("https://github.com/login/oauth/authorize", args);
return URLHelpers.CreateQuery("https://github.com/login/oauth/authorize", args);
}
public bool GetAccessToken(string code)

View file

@ -80,7 +80,7 @@ public string GetAuthorizationURL()
args.Add("client_id", AuthInfo.Client_ID);
args.Add("redirect_uri", Links.URL_CALLBACK);
return CreateQuery("https://bitly.com/oauth/authorize", args);
return URLHelpers.CreateQuery("https://bitly.com/oauth/authorize", args);
}
public bool GetAccessToken(string code)