summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/LogParser
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-12-02 18:49:49 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-12-02 18:49:49 -0500
commit5cc5f089b9645a60385ff293b5a7202f260bfc0f (patch)
tree68f8bee734d164277f711b8ac54bd1064c0757d6 /src/SMAPI.Web/Framework/LogParser
parente0b72374cd14298aacc6f71dc391fdc9814be37c (diff)
parentdc4f89acb6cd8f838934b60e8f5645c6145706f8 (diff)
downloadSMAPI-5cc5f089b9645a60385ff293b5a7202f260bfc0f.tar.gz
SMAPI-5cc5f089b9645a60385ff293b5a7202f260bfc0f.tar.bz2
SMAPI-5cc5f089b9645a60385ff293b5a7202f260bfc0f.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Web/Framework/LogParser')
-rw-r--r--src/SMAPI.Web/Framework/LogParser/PastebinClient.cs41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs b/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs
index 738330d3..1cfaed17 100644
--- a/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs
+++ b/src/SMAPI.Web/Framework/LogParser/PastebinClient.cs
@@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
+using System.Text;
using System.Threading.Tasks;
+using System.Web;
using Pathoschild.Http.Client;
namespace StardewModdingAPI.Web.Framework.LogParser
@@ -67,6 +69,8 @@ namespace StardewModdingAPI.Web.Framework.LogParser
}
}
+ /// <summary>Save a paste to Pastebin.</summary>
+ /// <param name="content">The paste content.</param>
public async Task<SavePasteResponse> PostAsync(string content)
{
try
@@ -77,18 +81,18 @@ namespace StardewModdingAPI.Web.Framework.LogParser
// post to API
string response = await this.Client
- .PostAsync("api/api_post.php")
- .WithBodyContent(new FormUrlEncodedContent(new Dictionary<string, string>
- {
- ["api_option"] = "paste",
- ["api_user_key"] = this.UserKey,
- ["api_dev_key"] = this.DevKey,
- ["api_paste_private"] = "1", // unlisted
- ["api_paste_name"] = $"SMAPI log {DateTime.UtcNow:s}",
- ["api_paste_expire_date"] = "1W", // one week
- ["api_paste_code"] = content
- }))
- .AsString();
+ .PostAsync("api/api_post.php")
+ .WithBodyContent(this.GetFormUrlEncodedContent(new Dictionary<string, string>
+ {
+ ["api_option"] = "paste",
+ ["api_user_key"] = this.UserKey,
+ ["api_dev_key"] = this.DevKey,
+ ["api_paste_private"] = "1", // unlisted
+ ["api_paste_name"] = $"SMAPI log {DateTime.UtcNow:s}",
+ ["api_paste_expire_date"] = "N", // never expire
+ ["api_paste_code"] = content
+ }))
+ .AsString();
// handle Pastebin errors
if (string.IsNullOrWhiteSpace(response))
@@ -113,5 +117,18 @@ namespace StardewModdingAPI.Web.Framework.LogParser
{
this.Client.Dispose();
}
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Build an HTTP content body with form-url-encoded content.</summary>
+ /// <param name="data">The content to encode.</param>
+ /// <remarks>This bypasses an issue where <see cref="FormUrlEncodedContent"/> restricts the body length to the maximum size of a URL, which isn't applicable here.</remarks>
+ private HttpContent GetFormUrlEncodedContent(IDictionary<string, string> data)
+ {
+ string body = string.Join("&", from arg in data select $"{HttpUtility.UrlEncode(arg.Key)}={HttpUtility.UrlEncode(arg.Value)}");
+ return new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
+ }
}
}