added reading from request

This commit is contained in:
Michael Nolan 2022-04-24 13:56:06 -05:00
parent ccdcf0b8f6
commit 7886cc120c
4 changed files with 82 additions and 5 deletions

View File

@ -7,6 +7,11 @@ namespace Tesses.WebServer
{ {
public class ServerContext public class ServerContext
{ {
/// <summary>
/// Some user data
/// </summary>
public object Tag {get;set;}
/// <summary> /// <summary>
/// Method (ex GET, POST, HEAD) /// Method (ex GET, POST, HEAD)
/// </summary> /// </summary>

View File

@ -52,7 +52,7 @@ namespace Tesses.WebServer
return true; return true;
} }
static string BodyAsString(this ServerContext ctx) private static string BodyAsString(this ServerContext ctx)
{ {
@ -64,6 +64,7 @@ namespace Tesses.WebServer
return str; return str;
} }
public static async Task SendStreamAsync(this ServerContext ctx, Stream strm, string contentType = "application/octet-stream") public static async Task SendStreamAsync(this ServerContext ctx, Stream strm, string contentType = "application/octet-stream")
{ {

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.2.0</Version> <Version>1.0.3.0</Version>
<AssemblyVersion>1.0.2.0</AssemblyVersion> <AssemblyVersion>1.0.3.0</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion> <FileVersion>1.0.3.0</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>

View File

@ -18,6 +18,77 @@ namespace Tesses.WebServer
public static class Extensions public static class Extensions
{ {
/// <summary>
/// Read string from request body
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <returns>the contents of request</returns>
public static async Task<string> ReadStringAsync(this ServerContext ctx)
{
string str = null;
using (var reader = new StreamReader(ctx.NetworkStream))
{
str = await reader.ReadToEndAsync();
}
return str;
}
/// <summary>
/// Read json from request body
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <typeparam name="T">type of object (for scema)</typeparam>
/// <returns>object of type T with deserialized json data</returns>
public static async Task<T> ReadJsonAsync<T>(this ServerContext ctx)
{
var json=await ctx.ReadStringAsync();
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// Read request body to array
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <returns>Request body data</returns>
public static async Task<byte[]> ReadBytesAsync(this ServerContext ctx)
{
MemoryStream strm = new MemoryStream();
await ctx.ReadToStreamAsync(strm);
return strm.ToArray();
}
/// <summary>
/// Read request body to stream
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="strm">Stream to write to</param>
public static async Task ReadToStreamAsync(this ServerContext ctx,Stream strm)
{
await ctx.NetworkStream.CopyToAsync(strm);
}
/// <summary>
/// Read request body to file
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="filename">name of file to write too, can be without extension</param>
/// <returns>file path with extension unless mimetype header is missing</returns>
public static async Task<string> ReadToFileAsync(this ServerContext ctx,string filename)
{
if(string.IsNullOrWhiteSpace(Path.GetExtension(filename)))
{
string val;
if(ctx.RequestHeaders.TryGetFirst("Content-Type",out val))
{
filename += $".{MimeTypesMap.GetExtension(val)}";
}
}
using(var f = File.Create(filename))
{
await ctx.NetworkStream.CopyToAsync(f);
}
return filename;
}
/// <summary> /// <summary>
/// Write headers to stream /// Write headers to stream
/// </summary> /// </summary>
@ -771,7 +842,7 @@ namespace Tesses.WebServer
IServer _server; IServer _server;
TcpListener _listener; TcpListener _listener;
SslProtocols protocols; SslProtocols protocols;
public HttpServerListener(IPEndPoint endPoint,IServer server) public HttpServerListener(IPEndPoint endPoint,IServer server)
{ {
_listener = new TcpListener(endPoint); _listener = new TcpListener(endPoint);