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
@@ -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
{
/// <summary>
@@ -154,5 +210,19 @@ namespace Tesses.WebServer
/// </summary>
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;
}
}
}