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

View File

@ -12,6 +12,21 @@ namespace Tesses.WebServer.ConsoleApp
} }
Random rand = new Random(); Random rand = new Random();
int count = 0; int count = 0;
public override async Task PostAsync(ServerContext ctx)
{
ctx.ParseBody();
foreach(var item in ctx.QueryParams)
{
Console.WriteLine($"{item.Key}:");
foreach(var p in item.Value)
{
Console.WriteLine($"\t{p}");
}
Console.WriteLine();
}
await ctx.SendTextAsync("HELLO");
}
public override async Task GetAsync(ServerContext ctx) public override async Task GetAsync(ServerContext ctx)
{ {
//Console.WriteLine("HANDLE"); //Console.WriteLine("HANDLE");

View File

@ -17,6 +17,11 @@ internal class SizedStream : Stream
this.len=len; this.len=len;
} }
public override int ReadByte()
{
if(pos >= len) return -1;
return strm.ReadByte();
}
public override bool CanRead => strm.CanRead; public override bool CanRead => strm.CanRead;
public override bool CanSeek => false; public override bool CanSeek => false;
@ -35,8 +40,10 @@ internal class SizedStream : Stream
public override int Read(byte[] buffer, int offset, int count) public override int Read(byte[] buffer, int offset, int count)
{ {
int read=(int)Math.Min(count,len-pos); int read=(int)Math.Min(count,len-pos);
if(read == 0) return 0;
read=strm.Read(buffer,offset,read); read=strm.Read(buffer,offset,read);
pos+=read; pos+=read;
return read; return read;
@ -219,9 +226,11 @@ internal class SizedStream : Stream
{ {
if(long.TryParse(len_Str,out len)) if(long.TryParse(len_Str,out len))
{ {
//DajuricSimpleHttpExtensions.Print($"Content-Length: {len}");
return new SizedStream(NetworkStream,len); return new SizedStream(NetworkStream,len);
} }
} }
//DajuricSimpleHttpExtensions.Print("Returns NetworkStream");
return NetworkStream; return NetworkStream;
} }
} }

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -22,6 +23,11 @@ namespace Tesses.WebServer
public static class DajuricSimpleHttpExtensions 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) static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
{ {
key = tuple.Key; key = tuple.Key;
@ -31,22 +37,26 @@ namespace Tesses.WebServer
static bool ParseForm(this ServerContext ctx) static bool ParseForm(this ServerContext ctx)
{ {
//Print("Enter ParseForm(this ServerContext ctx)");
var args = ctx.QueryParams; var args = ctx.QueryParams;
string content_type = ctx.RequestHeaders.GetFirst("Content-Type"); string content_type = ctx.RequestHeaders.GetFirst("Content-Type");
if (content_type != "application/x-www-form-urlencoded") if (content_type != "application/x-www-form-urlencoded")
return false; return false;
//Print("Before BodyAsString");
var str = ctx.BodyAsString(); var str = ctx.BodyAsString();
//Print("After BodyAsString");
if (str == null) if (str == null)
return false; return false;
//Print("Before For Loop");
foreach (var pair in str.Split('&')) foreach (var pair in str.Split('&'))
{ {
var nameValue = pair.Split('='); var nameValue = pair.Split('=');
if (nameValue.Length != (1 + 1)) if (nameValue.Length != (1 + 1))
continue; continue;
//Print($"Before Add: {nameValue[0]}: {nameValue[1]}");
args.Add(nameValue[0], WebUtility.UrlDecode(nameValue[1])); args.Add(nameValue[0], WebUtility.UrlDecode(nameValue[1]));
//Print($"After Add: {nameValue[0]}: {nameValue[1]}");
} }
return true; return true;
@ -59,7 +69,11 @@ namespace Tesses.WebServer
string str = null; string str = null;
using (var reader = new StreamReader(ctx.GetRequestStream())) using (var reader = new StreamReader(ctx.GetRequestStream()))
{ {
//Print("Before ReadToEnd");
str = reader.ReadToEnd(); str = reader.ReadToEnd();
//Print("After ReadToEnd");
} }
return str; return str;
@ -125,11 +139,15 @@ namespace Tesses.WebServer
var files = new Dictionary<string, HttpFile>(); var files = new Dictionary<string, HttpFile>();
var inputStream = new BufferedStream(serverCtx.GetRequestStream()); var inputStream = new BufferedStream(serverCtx.GetRequestStream());
//Print("Before ParseUntillBoundaryEnd");
parseUntillBoundaryEnd(inputStream, new MemoryStream(), boundary); parseUntillBoundaryEnd(inputStream, new MemoryStream(), boundary);
//Print("After ParseUntillBoundaryEnd");
while (true) while (true)
{ {
//Print("Before ParseSection");
var (n, v, fn, ct) = parseSection(inputStream, "\r\n" + boundary, onFile); var (n, v, fn, ct) = parseSection(inputStream, "\r\n" + boundary, onFile);
//Print("After ParseSection");
if (String.IsNullOrEmpty(n)) break; if (String.IsNullOrEmpty(n)) break;
v.Position = 0; 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) 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); var (n, fn, ct) = readContentDisposition(source);
//Print("After ReadContentDisposition");
source.ReadByte(); source.ReadByte(); //\r\n (empty row) source.ReadByte(); source.ReadByte(); //\r\n (empty row)
var dst = String.IsNullOrEmpty(fn) ? new MemoryStream() : onFile(n, fn, ct); var dst = String.IsNullOrEmpty(fn) ? new MemoryStream() : onFile(n, fn, ct);
if (dst == null) if (dst == null)
throw new ArgumentException(nameof(onFile), "The on-file callback must return a stream."); throw new ArgumentException(nameof(onFile), "The on-file callback must return a stream.");
//Print("Before ParseUntillBodyEnd");
parseUntillBoundaryEnd(source, dst, boundary); parseUntillBoundaryEnd(source, dst, boundary);
//Print("Before ParseUntillBodyEnd");
return (n, dst, fn, ct); return (n, dst, fn, ct);
} }
@ -248,6 +268,7 @@ namespace Tesses.WebServer
/// <returns>Name-file pair collection.</returns> /// <returns>Name-file pair collection.</returns>
public static Dictionary<string, HttpFile> ParseBody(this ServerContext ctx) public static Dictionary<string, HttpFile> ParseBody(this ServerContext ctx)
{ {
return ctx.ParseBody( (n, fn, ct) => new MemoryStream()); return ctx.ParseBody( (n, fn, ct) => new MemoryStream());
} }