mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-29 17:02:05 +13:00
Added XML (application/xml) body support to custom uploader
This commit is contained in:
parent
1f316ca7d5
commit
efc3e0facc
10 changed files with 113 additions and 24 deletions
|
@ -24,6 +24,8 @@ You should have received a copy of the GNU General Public License
|
|||
#endregion License Information (GPL v3)
|
||||
|
||||
using Microsoft.Win32;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using ShareX.HelpersLib.Properties;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -49,6 +51,8 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Windows.Forms;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ShareX.HelpersLib
|
||||
{
|
||||
|
@ -1381,5 +1385,37 @@ public static bool IsTabletMode()
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static string JSONFormat(string json, Newtonsoft.Json.Formatting formatting)
|
||||
{
|
||||
return JToken.Parse(json).ToString(formatting);
|
||||
}
|
||||
|
||||
public static string XMLFormat(string xml)
|
||||
{
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.Unicode))
|
||||
{
|
||||
// Load the XmlDocument with the XML.
|
||||
XmlDocument document = new XmlDocument();
|
||||
document.LoadXml(xml);
|
||||
|
||||
writer.Formatting = System.Xml.Formatting.Indented;
|
||||
|
||||
// Write the XML into a formatting XmlTextWriter
|
||||
document.WriteContentTo(writer);
|
||||
writer.Flush();
|
||||
ms.Flush();
|
||||
|
||||
// Have to rewind the MemoryStream in order to read its contents.
|
||||
ms.Position = 0;
|
||||
|
||||
// Read MemoryStream contents into a StreamReader.
|
||||
StreamReader sReader = new StreamReader(ms);
|
||||
|
||||
// Extract the text from the StreamReader.
|
||||
return sReader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -83,7 +83,7 @@ public bool ShouldSerializeArguments() => (Body == CustomUploaderBody.MultipartF
|
|||
[DefaultValue("")]
|
||||
public string Data { get; set; }
|
||||
|
||||
public bool ShouldSerializeData() => Body == CustomUploaderBody.JSON && !string.IsNullOrEmpty(Data);
|
||||
public bool ShouldSerializeData() => (Body == CustomUploaderBody.JSON || Body == CustomUploaderBody.XML) && !string.IsNullOrEmpty(Data);
|
||||
|
||||
[DefaultValue(ResponseType.Text)]
|
||||
public ResponseType ResponseType { get; set; }
|
||||
|
@ -163,11 +163,25 @@ public Dictionary<string, string> GetParameters(CustomUploaderInput input)
|
|||
return parameters;
|
||||
}
|
||||
|
||||
public string GetContentType()
|
||||
{
|
||||
switch (Body)
|
||||
{
|
||||
case CustomUploaderBody.JSON:
|
||||
return UploadHelpers.ContentTypeJSON;
|
||||
case CustomUploaderBody.XML:
|
||||
return UploadHelpers.ContentTypeXML;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public string GetData(CustomUploaderInput input)
|
||||
{
|
||||
CustomUploaderParser parser = new CustomUploaderParser(input);
|
||||
parser.UseNameParser = true;
|
||||
parser.JSONEncode = Body == CustomUploaderBody.JSON;
|
||||
parser.XMLEncode = Body == CustomUploaderBody.XML;
|
||||
|
||||
return parser.Parse(Data);
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Forms;
|
||||
|
@ -50,6 +51,7 @@ public class CustomUploaderParser
|
|||
public List<Match> RegexMatches { get; set; }
|
||||
public bool URLEncode { get; set; } // Only URL encodes filename and input
|
||||
public bool JSONEncode { get; set; }
|
||||
public bool XMLEncode { get; set; }
|
||||
public bool UseNameParser { get; set; }
|
||||
|
||||
public bool SkipSyntaxParse { get; set; }
|
||||
|
@ -143,6 +145,10 @@ private string Parse(string text, bool isOutput)
|
|||
{
|
||||
syntaxResult = URLHelpers.JSONEncode(syntaxResult);
|
||||
}
|
||||
else if (XMLEncode)
|
||||
{
|
||||
syntaxResult = SecurityElement.Escape(syntaxResult);
|
||||
}
|
||||
|
||||
sbResult.Append(syntaxResult);
|
||||
}
|
||||
|
|
|
@ -295,12 +295,14 @@ public enum CustomUploaderBody
|
|||
None,
|
||||
[Description("Form data (multipart/form-data)")]
|
||||
MultipartFormData,
|
||||
[Description("Form URL encoded (application/x-www-form-urlencoded)")]
|
||||
FormURLEncoded,
|
||||
[Description("JSON (application/json)")]
|
||||
JSON,
|
||||
[Description("XML (application/xml)")]
|
||||
XML,
|
||||
[Description("Binary")]
|
||||
Binary,
|
||||
[Description("Form URL encoded (application/x-www-form-urlencoded)")]
|
||||
FormURLEncoded
|
||||
Binary
|
||||
}
|
||||
|
||||
[Flags]
|
||||
|
|
|
@ -3560,7 +3560,18 @@ private void rtbCustomUploaderData_TextChanged(object sender, EventArgs e)
|
|||
|
||||
private void btnCustomUploaderDataBeautify_Click(object sender, EventArgs e)
|
||||
{
|
||||
CustomUploaderFormatJsonData(Formatting.Indented);
|
||||
CustomUploaderItem uploader = CustomUploaderGetSelected();
|
||||
if (uploader != null)
|
||||
{
|
||||
if (uploader.Body == CustomUploaderBody.JSON)
|
||||
{
|
||||
CustomUploaderFormatJsonData(Formatting.Indented);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.XML)
|
||||
{
|
||||
CustomUploaderFormatXMLData();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void btnCustomUploaderDataMinify_Click(object sender, EventArgs e)
|
||||
|
|
|
@ -40,6 +40,7 @@ You should have received a copy of the GNU General Public License
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ShareX.UploadersLib
|
||||
{
|
||||
|
@ -1059,8 +1060,9 @@ private void CustomUploaderUpdateRequestFormatState()
|
|||
{
|
||||
pCustomUploaderBodyArguments.Visible = uploader.Body == CustomUploaderBody.MultipartFormData ||
|
||||
uploader.Body == CustomUploaderBody.FormURLEncoded;
|
||||
pCustomUploaderBodyData.Visible = uploader.Body == CustomUploaderBody.JSON;
|
||||
lblCustomUploaderFileFormName.Visible = txtCustomUploaderFileFormName.Visible = uploader.Body == CustomUploaderBody.MultipartFormData;
|
||||
pCustomUploaderBodyData.Visible = uploader.Body == CustomUploaderBody.JSON || uploader.Body == CustomUploaderBody.XML;
|
||||
btnCustomUploaderDataMinify.Visible = uploader.Body == CustomUploaderBody.JSON;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1487,7 +1489,24 @@ private void CustomUploaderFormatJsonData(Formatting formatting)
|
|||
{
|
||||
try
|
||||
{
|
||||
rtbCustomUploaderData.Text = JToken.Parse(json).ToString(formatting);
|
||||
rtbCustomUploaderData.Text = Helpers.JSONFormat(json, formatting);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
MessageBox.Show(e.Message, "ShareX - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CustomUploaderFormatXMLData()
|
||||
{
|
||||
string xml = rtbCustomUploaderData.Text;
|
||||
|
||||
if (!string.IsNullOrEmpty(xml))
|
||||
{
|
||||
try
|
||||
{
|
||||
rtbCustomUploaderData.Text = Helpers.XMLFormat(xml);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
|
@ -39,6 +39,7 @@ internal static class UploadHelpers
|
|||
{
|
||||
public const string ContentTypeMultipartFormData = "multipart/form-data";
|
||||
public const string ContentTypeJSON = "application/json";
|
||||
public const string ContentTypeXML = "application/xml";
|
||||
public const string ContentTypeURLEncoded = "application/x-www-form-urlencoded";
|
||||
public const string ContentTypeOctetStream = "application/octet-stream";
|
||||
|
||||
|
|
|
@ -88,16 +88,16 @@ public override UploadResult ShareURL(string url)
|
|||
result.Response = SendRequestMultiPart(uploader.GetRequestURL(input), uploader.GetArguments(input), uploader.GetHeaders(input), null,
|
||||
uploader.ResponseType, uploader.RequestMethod);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.JSON)
|
||||
{
|
||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetData(input), UploadHelpers.ContentTypeJSON,
|
||||
null, uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.FormURLEncoded)
|
||||
{
|
||||
result.Response = SendRequestURLEncoded(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetArguments(input),
|
||||
uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.JSON || uploader.Body == CustomUploaderBody.XML)
|
||||
{
|
||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetData(input), uploader.GetContentType(),
|
||||
null, uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unsupported request format: " + uploader.Body);
|
||||
|
|
|
@ -104,9 +104,14 @@ public override UploadResult UploadText(string text, string fileName)
|
|||
}
|
||||
}
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.JSON)
|
||||
else if (uploader.Body == CustomUploaderBody.FormURLEncoded)
|
||||
{
|
||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetData(input), UploadHelpers.ContentTypeJSON,
|
||||
result.Response = SendRequestURLEncoded(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetArguments(input),
|
||||
uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.JSON || uploader.Body == CustomUploaderBody.XML)
|
||||
{
|
||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetData(input), uploader.GetContentType(),
|
||||
null, uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.Binary)
|
||||
|
@ -118,11 +123,6 @@ public override UploadResult UploadText(string text, string fileName)
|
|||
null, uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.FormURLEncoded)
|
||||
{
|
||||
result.Response = SendRequestURLEncoded(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetArguments(input),
|
||||
uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unsupported request format: " + uploader.Body);
|
||||
|
|
|
@ -87,16 +87,16 @@ public override UploadResult ShortenURL(string url)
|
|||
result.Response = SendRequestMultiPart(uploader.GetRequestURL(input), uploader.GetArguments(input), uploader.GetHeaders(input), null,
|
||||
uploader.ResponseType, uploader.RequestMethod);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.JSON)
|
||||
{
|
||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetData(input), UploadHelpers.ContentTypeJSON,
|
||||
null, uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.FormURLEncoded)
|
||||
{
|
||||
result.Response = SendRequestURLEncoded(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetArguments(input),
|
||||
uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else if (uploader.Body == CustomUploaderBody.JSON || uploader.Body == CustomUploaderBody.XML)
|
||||
{
|
||||
result.Response = SendRequest(uploader.RequestMethod, uploader.GetRequestURL(input), uploader.GetData(input), uploader.GetContentType(),
|
||||
null, uploader.GetHeaders(input), null, uploader.ResponseType);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unsupported request format: " + uploader.Body);
|
||||
|
|
Loading…
Reference in a new issue