diff --git a/Tesses.WebServer.NetStandard/ServerContext.cs b/Tesses.WebServer.NetStandard/ServerContext.cs index dc8899d..421688b 100644 --- a/Tesses.WebServer.NetStandard/ServerContext.cs +++ b/Tesses.WebServer.NetStandard/ServerContext.cs @@ -7,6 +7,11 @@ namespace Tesses.WebServer { public class ServerContext { + /// + /// Some user data + /// + + public object Tag {get;set;} /// /// Method (ex GET, POST, HEAD) /// diff --git a/Tesses.WebServer.NetStandard/SimpleHttpCode.cs b/Tesses.WebServer.NetStandard/SimpleHttpCode.cs index eed81e4..feeebbf 100644 --- a/Tesses.WebServer.NetStandard/SimpleHttpCode.cs +++ b/Tesses.WebServer.NetStandard/SimpleHttpCode.cs @@ -52,7 +52,7 @@ namespace Tesses.WebServer return true; } - static string BodyAsString(this ServerContext ctx) + private static string BodyAsString(this ServerContext ctx) { @@ -64,6 +64,7 @@ namespace Tesses.WebServer return str; } + public static async Task SendStreamAsync(this ServerContext ctx, Stream strm, string contentType = "application/octet-stream") { diff --git a/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj b/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj index ab2f410..f91745e 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.2.0 - 1.0.2.0 - 1.0.2.0 + 1.0.3.0 + 1.0.3.0 + 1.0.3.0 A TCP Listener HTTP(s) Server MIT HTTP, WebServer, Website diff --git a/Tesses.WebServer.NetStandard/TessesServer.cs b/Tesses.WebServer.NetStandard/TessesServer.cs index 758a263..68ff86a 100644 --- a/Tesses.WebServer.NetStandard/TessesServer.cs +++ b/Tesses.WebServer.NetStandard/TessesServer.cs @@ -18,6 +18,77 @@ namespace Tesses.WebServer public static class Extensions { + /// + /// Read string from request body + /// + /// ServerContext + /// the contents of request + public static async Task ReadStringAsync(this ServerContext ctx) + { + string str = null; + using (var reader = new StreamReader(ctx.NetworkStream)) + { + str = await reader.ReadToEndAsync(); + } + + return str; + } + /// + /// Read json from request body + /// + /// ServerContext + /// type of object (for scema) + /// object of type T with deserialized json data + public static async Task ReadJsonAsync(this ServerContext ctx) + { + var json=await ctx.ReadStringAsync(); + return JsonConvert.DeserializeObject(json); + } + /// + /// Read request body to array + /// + /// ServerContext + /// Request body data + public static async Task ReadBytesAsync(this ServerContext ctx) + { + MemoryStream strm = new MemoryStream(); + await ctx.ReadToStreamAsync(strm); + return strm.ToArray(); + } + /// + /// Read request body to stream + /// + /// ServerContext + /// Stream to write to + + public static async Task ReadToStreamAsync(this ServerContext ctx,Stream strm) + { + await ctx.NetworkStream.CopyToAsync(strm); + } + /// + /// Read request body to file + /// + /// ServerContext + /// name of file to write too, can be without extension + /// file path with extension unless mimetype header is missing + public static async Task 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; + } + /// /// Write headers to stream /// @@ -771,7 +842,7 @@ namespace Tesses.WebServer IServer _server; TcpListener _listener; SslProtocols protocols; - + public HttpServerListener(IPEndPoint endPoint,IServer server) { _listener = new TcpListener(endPoint);