allow non-form-encoded content
This commit is contained in:
parent
19d4876834
commit
38bf02d406
@ -1,6 +1,7 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Data.SqlTypes;
|
using System.Data.SqlTypes;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
using MBS.Core;
|
using MBS.Core;
|
||||||
|
|
||||||
namespace MBS.Web;
|
namespace MBS.Web;
|
||||||
@ -21,8 +22,9 @@ public class WebRequest
|
|||||||
public WebHeaderCollection Headers { get; }
|
public WebHeaderCollection Headers { get; }
|
||||||
|
|
||||||
public ReadOnlyDictionary<string, string> Form { get; }
|
public ReadOnlyDictionary<string, string> Form { get; }
|
||||||
|
public string Content { get; }
|
||||||
|
|
||||||
public WebRequest(string version, string method, string path, WebHeaderCollection headers, Dictionary<string, string> pathVariables, Dictionary<string, string> form)
|
public WebRequest(string version, string method, string path, WebHeaderCollection headers, Dictionary<string, string> pathVariables)
|
||||||
{
|
{
|
||||||
Version = version;
|
Version = version;
|
||||||
Method = method;
|
Method = method;
|
||||||
@ -67,9 +69,40 @@ public class WebRequest
|
|||||||
}
|
}
|
||||||
Headers = headers;
|
Headers = headers;
|
||||||
PathVariables = pathVariables;
|
PathVariables = pathVariables;
|
||||||
|
}
|
||||||
|
public WebRequest(string version, string method, string path, WebHeaderCollection headers, Dictionary<string, string> pathVariables, string content) : this(version, method, path, headers, pathVariables)
|
||||||
|
{
|
||||||
|
Content = content;
|
||||||
|
Form = ReadOnlyDictionary<string, string>.Empty;
|
||||||
|
}
|
||||||
|
public WebRequest(string version, string method, string path, WebHeaderCollection headers, Dictionary<string, string> pathVariables, Dictionary<string, string> form) : this(version, method, path, headers, pathVariables)
|
||||||
|
{
|
||||||
|
Content = UrlAndFormEncode(form);
|
||||||
Form = new ReadOnlyDictionary<string, string>(form);
|
Form = new ReadOnlyDictionary<string, string>(form);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string UrlAndFormEncode(string v)
|
||||||
|
{
|
||||||
|
v = v.UrlEncode();
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
private string UrlAndFormEncode(IEnumerable<KeyValuePair<string, string>> v)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
int i = 0;
|
||||||
|
foreach (KeyValuePair<string, string> kvp in v)
|
||||||
|
{
|
||||||
|
sb.Append(UrlAndFormEncode(kvp.Key));
|
||||||
|
sb.Append('=');
|
||||||
|
sb.Append(UrlAndFormEncode(kvp.Value));
|
||||||
|
if (i < v.Count())
|
||||||
|
{
|
||||||
|
sb.Append('&');
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
private string UrlAndFormDecode(string v)
|
private string UrlAndFormDecode(string v)
|
||||||
{
|
{
|
||||||
v = v.Replace('+', ' '); // must be done first to not decode '%..' => ' '
|
v = v.Replace('+', ' '); // must be done first to not decode '%..' => ' '
|
||||||
|
|||||||
@ -257,6 +257,11 @@ public class WebServer
|
|||||||
Dictionary<string, string> form = new Dictionary<string, string>();
|
Dictionary<string, string> form = new Dictionary<string, string>();
|
||||||
|
|
||||||
string contentLength = headers["Content-Length"];
|
string contentLength = headers["Content-Length"];
|
||||||
|
string contentType = headers["Content-Type"];
|
||||||
|
|
||||||
|
// I never thought I'd ever actually use this!
|
||||||
|
Union<string, Dictionary<string, string>> content;
|
||||||
|
|
||||||
if (contentLength != "")
|
if (contentLength != "")
|
||||||
{
|
{
|
||||||
int contentLengthInt = Int32.Parse(contentLength);
|
int contentLengthInt = Int32.Parse(contentLength);
|
||||||
@ -265,16 +270,24 @@ public class WebServer
|
|||||||
char[] buffer = new char[contentLengthInt];
|
char[] buffer = new char[contentLengthInt];
|
||||||
sr.ReadBlock(buffer, 0, contentLengthInt);
|
sr.ReadBlock(buffer, 0, contentLengthInt);
|
||||||
|
|
||||||
string content = new string(buffer);
|
string contentString = new string(buffer);
|
||||||
Console.Error.WriteLine(content);
|
Console.Error.WriteLine(contentString);
|
||||||
|
|
||||||
if (!String.IsNullOrEmpty(content))
|
if (!String.IsNullOrEmpty(contentString))
|
||||||
{
|
{
|
||||||
string[] kvps = content.Split(new char[] { '&' });
|
if (contentType == "application/x-www-form-urlencoded")
|
||||||
foreach (string kvp in kvps)
|
|
||||||
{
|
{
|
||||||
string[] pv = kvp.Split(new char[] { '=' });
|
string[] kvps = contentString.Split(new char[] { '&' });
|
||||||
form[pv[0].UrlDecode()] = pv[1].UrlDecode().Replace('+', ' ');
|
foreach (string kvp in kvps)
|
||||||
|
{
|
||||||
|
string[] pv = kvp.Split(new char[] { '=' });
|
||||||
|
form[pv[0].UrlDecode()] = pv[1].UrlDecode().Replace('+', ' ');
|
||||||
|
}
|
||||||
|
content = form;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
content = contentString;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user