Fix POST ctx.ParseBody(); (Actually fixed it this time)

This commit is contained in:
Michael Nolan
2022-07-27 18:30:52 -05:00
parent 0ff38b0211
commit 8a912e2a86
3 changed files with 53 additions and 8 deletions
+28 -7
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@@ -22,6 +23,11 @@ namespace Tesses.WebServer
public static class DajuricSimpleHttpExtensions
{
/* Thanks to you we fixed this
public static void Print(string text,[CallerLineNumber] int lineNumber = 0)
{
Console.WriteLine($"[LINE {lineNumber}] {text}");
}*/
static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
{
key = tuple.Key;
@@ -31,22 +37,26 @@ namespace Tesses.WebServer
static bool ParseForm(this ServerContext ctx)
{
//Print("Enter ParseForm(this ServerContext ctx)");
var args = ctx.QueryParams;
string content_type = ctx.RequestHeaders.GetFirst("Content-Type");
if (content_type != "application/x-www-form-urlencoded")
return false;
//Print("Before BodyAsString");
var str = ctx.BodyAsString();
//Print("After BodyAsString");
if (str == null)
return false;
//Print("Before For Loop");
foreach (var pair in str.Split('&'))
{
var nameValue = pair.Split('=');
if (nameValue.Length != (1 + 1))
continue;
//Print($"Before Add: {nameValue[0]}: {nameValue[1]}");
args.Add(nameValue[0], WebUtility.UrlDecode(nameValue[1]));
//Print($"After Add: {nameValue[0]}: {nameValue[1]}");
}
return true;
@@ -54,12 +64,16 @@ namespace Tesses.WebServer
private static string BodyAsString(this ServerContext ctx)
{
string str = null;
using (var reader = new StreamReader(ctx.GetRequestStream()))
{
//Print("Before ReadToEnd");
str = reader.ReadToEnd();
//Print("After ReadToEnd");
}
return str;
@@ -125,11 +139,15 @@ namespace Tesses.WebServer
var files = new Dictionary<string, HttpFile>();
var inputStream = new BufferedStream(serverCtx.GetRequestStream());
//Print("Before ParseUntillBoundaryEnd");
parseUntillBoundaryEnd(inputStream, new MemoryStream(), boundary);
//Print("After ParseUntillBoundaryEnd");
while (true)
{
//Print("Before ParseSection");
var (n, v, fn, ct) = parseSection(inputStream, "\r\n" + boundary, onFile);
//Print("After ParseSection");
if (String.IsNullOrEmpty(n)) break;
v.Position = 0;
@@ -144,15 +162,17 @@ namespace Tesses.WebServer
private static (string Name, Stream Value, string FileName, string ContentType) parseSection(Stream source, string boundary, OnFile onFile)
{
//Print("Before ReadContentDisposition");
var (n, fn, ct) = readContentDisposition(source);
//Print("After ReadContentDisposition");
source.ReadByte(); source.ReadByte(); //\r\n (empty row)
var dst = String.IsNullOrEmpty(fn) ? new MemoryStream() : onFile(n, fn, ct);
if (dst == null)
throw new ArgumentException(nameof(onFile), "The on-file callback must return a stream.");
//Print("Before ParseUntillBodyEnd");
parseUntillBoundaryEnd(source, dst, boundary);
//Print("Before ParseUntillBodyEnd");
return (n, dst, fn, ct);
}
@@ -248,6 +268,7 @@ namespace Tesses.WebServer
/// <returns>Name-file pair collection.</returns>
public static Dictionary<string, HttpFile> ParseBody(this ServerContext ctx)
{
return ctx.ParseBody( (n, fn, ct) => new MemoryStream());
}