Fix POST ctx.ParseBody();

This commit is contained in:
Michael Nolan 2022-07-26 21:52:32 -05:00
parent 0b5eb9085c
commit 0ff38b0211
3 changed files with 75 additions and 5 deletions

View File

@ -5,6 +5,62 @@ using System.Net;
namespace Tesses.WebServer 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 public class ServerContext
{ {
/// <summary> /// <summary>
@ -154,5 +210,19 @@ namespace Tesses.WebServer
/// </summary> /// </summary>
public int StatusCode { get; set; } 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;
}
} }
} }

View File

@ -57,7 +57,7 @@ namespace Tesses.WebServer
string str = null; string str = null;
using (var reader = new StreamReader(ctx.NetworkStream)) using (var reader = new StreamReader(ctx.GetRequestStream()))
{ {
str = reader.ReadToEnd(); str = reader.ReadToEnd();
} }
@ -124,7 +124,7 @@ namespace Tesses.WebServer
var files = new Dictionary<string, HttpFile>(); var files = new Dictionary<string, HttpFile>();
var inputStream = new BufferedStream(serverCtx.NetworkStream); var inputStream = new BufferedStream(serverCtx.GetRequestStream());
parseUntillBoundaryEnd(inputStream, new MemoryStream(), boundary); parseUntillBoundaryEnd(inputStream, new MemoryStream(), boundary);
while (true) while (true)

View File

@ -5,9 +5,9 @@
<PackageId>Tesses.WebServer</PackageId> <PackageId>Tesses.WebServer</PackageId>
<Author>Mike Nolan</Author> <Author>Mike Nolan</Author>
<Company>Tesses</Company> <Company>Tesses</Company>
<Version>1.0.3.5</Version> <Version>1.0.3.6</Version>
<AssemblyVersion>1.0.3.5</AssemblyVersion> <AssemblyVersion>1.0.3.6</AssemblyVersion>
<FileVersion>1.0.3.5</FileVersion> <FileVersion>1.0.3.6</FileVersion>
<Description>A TCP Listener HTTP(s) Server</Description> <Description>A TCP Listener HTTP(s) Server</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>HTTP, WebServer, Website</PackageTags> <PackageTags>HTTP, WebServer, Website</PackageTags>