From 0ff38b0211cb02c3c8319c3dd0c39f9a10abe82f Mon Sep 17 00:00:00 2001 From: Michael Nolan Date: Tue, 26 Jul 2022 21:52:32 -0500 Subject: [PATCH] Fix POST ctx.ParseBody(); --- Tesses.WebServer.NetStandard/ServerContext.cs | 70 +++++++++++++++++++ .../SimpleHttpCode.cs | 4 +- .../Tesses.WebServer.NetStandard.csproj | 6 +- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Tesses.WebServer.NetStandard/ServerContext.cs b/Tesses.WebServer.NetStandard/ServerContext.cs index c7e954e..454b969 100644 --- a/Tesses.WebServer.NetStandard/ServerContext.cs +++ b/Tesses.WebServer.NetStandard/ServerContext.cs @@ -5,6 +5,62 @@ using System.Net; namespace Tesses.WebServer { + +internal class SizedStream : Stream +{ + Stream strm; + long len; + long pos=0; + public SizedStream(Stream src,long len) + { + this.strm=src; + this.len=len; + } + + public override bool CanRead => strm.CanRead; + + public override bool CanSeek => false; + + public override bool CanWrite => strm.CanWrite; + + public override long Length => len; + + public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + + public override void Flush() + { + strm.Flush(); + } + + public override int Read(byte[] buffer, int offset, int count) + { + int read=(int)Math.Min(count,len-pos); + + read=strm.Read(buffer,offset,read); + pos+=read; + + return read; + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotImplementedException(); + } + + public override void SetLength(long value) + { + throw new NotImplementedException(); + } + + public override void Write(byte[] buffer, int offset, int count) + { + int read=(int)Math.Min(count,len-pos); + pos+=read; + strm.Write(buffer,offset,read); + + + } +} public class ServerContext { /// @@ -154,5 +210,19 @@ namespace Tesses.WebServer /// public int StatusCode { get; set; } + + public Stream GetRequestStream() + { + string len_Str; + long len; + if(RequestHeaders.TryGetFirst("Content-Length",out len_Str)) + { + if(long.TryParse(len_Str,out len)) + { + return new SizedStream(NetworkStream,len); + } + } + return NetworkStream; + } } } diff --git a/Tesses.WebServer.NetStandard/SimpleHttpCode.cs b/Tesses.WebServer.NetStandard/SimpleHttpCode.cs index b07f382..43cd31c 100644 --- a/Tesses.WebServer.NetStandard/SimpleHttpCode.cs +++ b/Tesses.WebServer.NetStandard/SimpleHttpCode.cs @@ -57,7 +57,7 @@ namespace Tesses.WebServer string str = null; - using (var reader = new StreamReader(ctx.NetworkStream)) + using (var reader = new StreamReader(ctx.GetRequestStream())) { str = reader.ReadToEnd(); } @@ -124,7 +124,7 @@ namespace Tesses.WebServer var files = new Dictionary(); - var inputStream = new BufferedStream(serverCtx.NetworkStream); + var inputStream = new BufferedStream(serverCtx.GetRequestStream()); parseUntillBoundaryEnd(inputStream, new MemoryStream(), boundary); while (true) diff --git a/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj b/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj index aa0daf9..36b4c89 100644 --- a/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj +++ b/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj @@ -5,9 +5,9 @@ Tesses.WebServer Mike Nolan Tesses - 1.0.3.5 - 1.0.3.5 - 1.0.3.5 + 1.0.3.6 + 1.0.3.6 + 1.0.3.6 A TCP Listener HTTP(s) Server MIT HTTP, WebServer, Website