allow non-form-encoded content

This commit is contained in:
Michael Becker 2025-01-17 22:13:32 -05:00
parent 19d4876834
commit 38bf02d406
2 changed files with 54 additions and 8 deletions

View File

@ -1,6 +1,7 @@
using System.Collections.ObjectModel;
using System.Data.SqlTypes;
using System.Net;
using System.Text;
using MBS.Core;
namespace MBS.Web;
@ -21,8 +22,9 @@ public class WebRequest
public WebHeaderCollection Headers { 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;
Method = method;
@ -67,9 +69,40 @@ public class WebRequest
}
Headers = headers;
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);
}
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)
{
v = v.Replace('+', ' '); // must be done first to not decode '%..' => ' '

View File

@ -257,6 +257,11 @@ public class WebServer
Dictionary<string, string> form = new Dictionary<string, string>();
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 != "")
{
int contentLengthInt = Int32.Parse(contentLength);
@ -265,16 +270,24 @@ public class WebServer
char[] buffer = new char[contentLengthInt];
sr.ReadBlock(buffer, 0, contentLengthInt);
string content = new string(buffer);
Console.Error.WriteLine(content);
string contentString = new string(buffer);
Console.Error.WriteLine(contentString);
if (!String.IsNullOrEmpty(content))
if (!String.IsNullOrEmpty(contentString))
{
string[] kvps = content.Split(new char[] { '&' });
foreach (string kvp in kvps)
if (contentType == "application/x-www-form-urlencoded")
{
string[] pv = kvp.Split(new char[] { '=' });
form[pv[0].UrlDecode()] = pv[1].UrlDecode().Replace('+', ' ');
string[] kvps = contentString.Split(new char[] { '&' });
foreach (string kvp in kvps)
{
string[] pv = kvp.Split(new char[] { '=' });
form[pv[0].UrlDecode()] = pv[1].UrlDecode().Replace('+', ' ');
}
content = form;
}
else
{
content = contentString;
}
}
}