From ac511652a7cf4b6d5a1384e9347118a833679c23 Mon Sep 17 00:00:00 2001 From: Kevin Amorim Date: Wed, 23 Sep 2020 16:32:54 +0100 Subject: [PATCH 1/4] Convert all ampersands to UTF-8 hex for request body params. --- RestAPI.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/RestAPI.cs b/RestAPI.cs index 6d04d19..350ea35 100644 --- a/RestAPI.cs +++ b/RestAPI.cs @@ -9,6 +9,7 @@ using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using WooCommerceNET.Base; @@ -164,7 +165,10 @@ public virtual async Task SendHttpClientRequest(string endpoint, Requ if (JWTRequestFilter != null) JWTRequestFilter.Invoke(request); - var buffer = Encoding.UTF8.GetBytes($"username={wc_key}&password={wc_secret}"); + var convKey = ConvertAmpersandsToUTF8Hex(wc_key); + var convSecret = ConvertAmpersandsToUTF8Hex(wc_secret); + + var buffer = Encoding.UTF8.GetBytes($"username={convKey}&password={convSecret}"); Stream dataStream = await request.GetRequestStreamAsync().ConfigureAwait(false); dataStream.Write(buffer, 0, buffer.Length); WebResponse response = await request.GetResponseAsync().ConfigureAwait(false); @@ -451,6 +455,12 @@ public string DateTimeFormat return IsLegacy ? "yyyy-MM-ddTHH:mm:ssZ" : "yyyy-MM-ddTHH:mm:ss"; } } + + private string ConvertAmpersandsToUTF8Hex(string original) + { + var pattern = new Regex("[&]"); + return pattern.Replace(original, "%26"); + } } public class WP_JWT_Object From 593fa73ce16143b9e8352c4fa943e46c35b78576 Mon Sep 17 00:00:00 2001 From: Kevin Amorim Date: Wed, 23 Sep 2020 16:33:48 +0100 Subject: [PATCH 2/4] Close DataStream after writing. Set ContentLength header. --- RestAPI.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/RestAPI.cs b/RestAPI.cs index 350ea35..c9e94d3 100644 --- a/RestAPI.cs +++ b/RestAPI.cs @@ -171,6 +171,10 @@ public virtual async Task SendHttpClientRequest(string endpoint, Requ var buffer = Encoding.UTF8.GetBytes($"username={convKey}&password={convSecret}"); Stream dataStream = await request.GetRequestStreamAsync().ConfigureAwait(false); dataStream.Write(buffer, 0, buffer.Length); + dataStream.Close(); + + request.ContentLength = buffer.Length; + WebResponse response = await request.GetResponseAsync().ConfigureAwait(false); Stream resStream = response.GetResponseStream(); string result = await GetStreamContent(resStream, "UTF-8").ConfigureAwait(false); From 895b187808021b414cb10401737ab5c9feba8d73 Mon Sep 17 00:00:00 2001 From: Kevin Amorim Date: Thu, 24 Sep 2020 10:47:44 +0100 Subject: [PATCH 3/4] Added flag to allow loading a stream from URL's. --- RestAPI.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/RestAPI.cs b/RestAPI.cs index c9e94d3..285abad 100644 --- a/RestAPI.cs +++ b/RestAPI.cs @@ -241,7 +241,12 @@ public virtual async Task SendHttpClientRequest(string endpoint, Requ httpWebRequest.ContentType = "application/x-www-form-urlencoded"; Stream dataStream = await httpWebRequest.GetRequestStreamAsync().ConfigureAwait(false); - FileStream fileStream = new FileStream(parms["path"], FileMode.Open, FileAccess.Read); + + // If the given path is a physical path, open file. If not, read from URL. + var fileStream = parms["source"] == "local" ? + new FileStream(parms["path"], FileMode.Open, FileAccess.Read) : + (new WebClient()).OpenRead(parms["path"]); + byte[] buffer = new byte[4096]; int bytesRead = 0; From 2499ee699d0e7f38a938cbe364ad582e68c5019c Mon Sep 17 00:00:00 2001 From: Kevin Amorim Date: Tue, 17 Nov 2020 16:39:52 +0000 Subject: [PATCH 4/4] Parameter to set add path as URL. --- WordPress/v2/WPObject.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPress/v2/WPObject.cs b/WordPress/v2/WPObject.cs index e2d1caa..0e0dbe8 100644 --- a/WordPress/v2/WPObject.cs +++ b/WordPress/v2/WPObject.cs @@ -73,11 +73,12 @@ public WPMediaItem(RestAPI api) : base(api) API = api; } - public async Task Add(string fileName, string filePath) + public async Task Add(string fileName, string filePath, bool fromUrl = false) { Dictionary ps = new Dictionary(); ps.Add("name", fileName); ps.Add("path", filePath); + ps.Add("source", fromUrl ? "url" : "local"); return API.DeserializeJSon(await API.PostRestful(APIEndpoint, "fileupload", ps).ConfigureAwait(false)); }