diff --git a/Tesses.WebServer.Console/Server.cs b/Tesses.WebServer.Console/Server.cs index d8e95b0..c5ffd9c 100644 --- a/Tesses.WebServer.Console/Server.cs +++ b/Tesses.WebServer.Console/Server.cs @@ -12,6 +12,21 @@ namespace Tesses.WebServer.ConsoleApp } Random rand = new Random(); 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) { //Console.WriteLine("HANDLE"); diff --git a/Tesses.WebServer.NetStandard/ServerContext.cs b/Tesses.WebServer.NetStandard/ServerContext.cs index 454b969..981f4a6 100644 --- a/Tesses.WebServer.NetStandard/ServerContext.cs +++ b/Tesses.WebServer.NetStandard/ServerContext.cs @@ -17,6 +17,11 @@ internal class SizedStream : Stream this.len=len; } + public override int ReadByte() + { + if(pos >= len) return -1; + return strm.ReadByte(); + } public override bool CanRead => strm.CanRead; public override bool CanSeek => false; @@ -35,8 +40,10 @@ internal class SizedStream : Stream public override int Read(byte[] buffer, int offset, int count) { int read=(int)Math.Min(count,len-pos); - + if(read == 0) return 0; + read=strm.Read(buffer,offset,read); + pos+=read; return read; @@ -219,9 +226,11 @@ internal class SizedStream : Stream { if(long.TryParse(len_Str,out len)) { + //DajuricSimpleHttpExtensions.Print($"Content-Length: {len}"); return new SizedStream(NetworkStream,len); } } + //DajuricSimpleHttpExtensions.Print("Returns NetworkStream"); return NetworkStream; } } diff --git a/Tesses.WebServer.NetStandard/SimpleHttpCode.cs b/Tesses.WebServer.NetStandard/SimpleHttpCode.cs index 43cd31c..4368900 100644 --- a/Tesses.WebServer.NetStandard/SimpleHttpCode.cs +++ b/Tesses.WebServer.NetStandard/SimpleHttpCode.cs @@ -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(this KeyValuePair 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(); 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 /// Name-file pair collection. public static Dictionary ParseBody(this ServerContext ctx) { + return ctx.ParseBody( (n, fn, ct) => new MemoryStream()); }