added reading from request
This commit is contained in:
parent
ccdcf0b8f6
commit
7886cc120c
|
@ -7,6 +7,11 @@ namespace Tesses.WebServer
|
|||
{
|
||||
public class ServerContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Some user data
|
||||
/// </summary>
|
||||
|
||||
public object Tag {get;set;}
|
||||
/// <summary>
|
||||
/// Method (ex GET, POST, HEAD)
|
||||
/// </summary>
|
||||
|
|
|
@ -52,7 +52,7 @@ namespace Tesses.WebServer
|
|||
return true;
|
||||
}
|
||||
|
||||
static string BodyAsString(this ServerContext ctx)
|
||||
private static string BodyAsString(this ServerContext ctx)
|
||||
{
|
||||
|
||||
|
||||
|
@ -65,6 +65,7 @@ namespace Tesses.WebServer
|
|||
return str;
|
||||
}
|
||||
|
||||
|
||||
public static async Task SendStreamAsync(this ServerContext ctx, Stream strm, string contentType = "application/octet-stream")
|
||||
{
|
||||
//ctx.StatusCode = 200;
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
<PackageId>Tesses.WebServer</PackageId>
|
||||
<Author>Mike Nolan</Author>
|
||||
<Company>Tesses</Company>
|
||||
<Version>1.0.2.0</Version>
|
||||
<AssemblyVersion>1.0.2.0</AssemblyVersion>
|
||||
<FileVersion>1.0.2.0</FileVersion>
|
||||
<Version>1.0.3.0</Version>
|
||||
<AssemblyVersion>1.0.3.0</AssemblyVersion>
|
||||
<FileVersion>1.0.3.0</FileVersion>
|
||||
<Description>A TCP Listener HTTP(s) Server</Description>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<PackageTags>HTTP, WebServer, Website</PackageTags>
|
||||
|
|
|
@ -18,6 +18,77 @@ namespace Tesses.WebServer
|
|||
|
||||
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>
|
||||
/// Write headers to stream
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue