This commit is contained in:
Michael Nolan 2022-04-23 13:25:45 -05:00
parent 620abb15e4
commit ccdcf0b8f6
3 changed files with 363 additions and 15 deletions

View File

@ -7,6 +7,9 @@ namespace Tesses.WebServer
{
public class ServerContext
{
/// <summary>
/// Method (ex GET, POST, HEAD)
/// </summary>
public string Method { get; set; }
public ServerContext(string method,Stream strm,string path,Dictionary<string,List<string>> headers)
{
@ -15,6 +18,7 @@ namespace Tesses.WebServer
RequestHeaders = headers;
ResponseHeaders = new Dictionary<string, List<string>>();
QueryParams = new Dictionary<string, List<string>>();
RawUrl=path;
StatusCode = 200;
// /joel/path/luigi?local=jim&john_surname=connor&demi_surname=lovato&local=tim
@ -24,6 +28,7 @@ namespace Tesses.WebServer
if (splitUrl.Length > 0)
{
UrlPath = splitUrl[0];
OriginalUrlPath=splitUrl[0];
if (splitUrl.Length == 2)
{
//local=jim&john_surname=connor&demi_surname=lovato&local=tim
@ -38,6 +43,9 @@ namespace Tesses.WebServer
}
}
/// <summary>
/// Reset query parms (If api sets them)
/// </summary>
public void ResetQueryParms()
{
QueryParams.Clear();
@ -67,14 +75,77 @@ namespace Tesses.WebServer
return Server.Address.ToString();
}
string q_parm;
/// <summary>
/// the /somepath/file?s=42&joel=file relative to Mount
/// </summary>
public string UrlAndQuery {get {
if(!string.IsNullOrWhiteSpace(q_parm))
{
return UrlPath + "?" + q_parm;
}
return UrlPath;
}}
/// <summary>
/// Original Url Path
/// </summary>
/// <value></value>
public string OriginalUrlPath {get; private set;}
/// <summary>
/// Original Url path (includes query)
/// </summary>
/// <value></value>
public string RawUrl {get;private set;}
/// <summary>
/// Query parms string only
/// </summary>
public string QueryParamsString {get {return q_parm;}}
/// <summary>
/// Server ip
/// </summary>
public IPEndPoint Server { get; set; }
/// <summary>
/// Client ip
/// </summary>
public IPEndPoint Client { get; set; }
/// <summary>
/// Host name
/// </summary>
/// <returns></returns>
public string Host { get { return get_host(); } }
/// <summary>
/// Url path (can be eet by Moutable)
/// </summary>
public string UrlPath { get; set; }
/// <summary>
/// Query Params
/// </summary>
public Dictionary<string,List<string>> QueryParams { get; set; }
/// <summary>
/// Request headers
/// </summary>
public Dictionary<string,List<string>> RequestHeaders { get; set; }
/// <summary>
/// Response headers
/// </summary>
public Dictionary<string,List<string>> ResponseHeaders { get; set; }
/// <summary>
/// TCP Stream for http server
/// </summary>
public Stream NetworkStream { get; set; }
public int StatusCode { get; internal set; }
/// <summary>
/// Status code for resource
/// </summary>
public int StatusCode { get; set; }
}
}
}

View File

@ -5,15 +5,18 @@
<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>
<Description>A TCP Listener HTTP(s) Server</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>HTTP, WebServer, Website</PackageTags>
<RepositoryUrl>https://gitlab.tesses.cf/tesses50/tesses.webserver</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MimeTypesMap" Version="1.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<ItemGroup>
<PackageReference Include="MimeTypesMap" Version="1.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>

View File

@ -18,6 +18,11 @@ namespace Tesses.WebServer
public static class Extensions
{
/// <summary>
/// Write headers to stream
/// </summary>
/// <param name="ctx">ServerContext</param>
public static async Task WriteHeadersAsync(this ServerContext ctx)
{
string status_line = $"HTTP/1.1 {ctx.StatusCode} {StatusCodeMap.GetStatusString(ctx.StatusCode)}\r\n";
@ -33,6 +38,12 @@ namespace Tesses.WebServer
var data = Encoding.UTF8.GetBytes(b.ToString());
await ctx.NetworkStream.WriteAsync(data, 0, data.Length);
}
/// <summary>
/// Send file to client (supports range partial content)
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="file">the file to serve</param>
public static async Task SendFileAsync(this ServerContext ctx, string file)
{
using (var strm = File.OpenRead(file))
@ -40,6 +51,12 @@ namespace Tesses.WebServer
await ctx.SendStreamAsync( strm, MimeTypesMap.GetMimeType(file));
}
}
/// <summary>
/// Send exception to client
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="ex">the Exception</param>
public static async Task SendExceptionAsync(this ServerContext ctx, Exception ex)
{
string name = ex.GetType().FullName;
@ -47,14 +64,44 @@ namespace Tesses.WebServer
ctx.StatusCode = 500;
await ctx.SendTextAsync(j);
}
/// <summary>
/// Send object as json to client
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="value">an object to serialize with newtonsoft.json</param>
public static async Task SendJsonAsync(this ServerContext ctx,object value)
{
await ctx.SendTextAsync(JsonConvert.SerializeObject(value), "application/json");
}
/// <summary>
/// Send text to client
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="data">some text</param>
/// <param name="contentType">mime type</param>
public static async Task SendTextAsync(this ServerContext ctx, string data, string contentType = "text/html")
{
await ctx.SendBytesAsync(Encoding.UTF8.GetBytes(data), contentType);
}
/// <summary>
/// Send redirect
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="url">Url to redirect to</param>
public static async Task SendRedirectAsync(this ServerContext ctx,string url)
{
ctx.StatusCode = 301;
ctx.ResponseHeaders.Add("Cache-Control","no-cache");
ctx.ResponseHeaders.Add("Location",url);
await ctx.WriteHeadersAsync();
}
/// <summary>
/// Send byte[] to client
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="array">a byte[] array</param>
/// <param name="contentType">mime type</param>
public static async Task SendBytesAsync(this ServerContext ctx, byte[] array, string contentType = "application/octet-stream")
{
using (var ms = new MemoryStream(array))
@ -62,11 +109,27 @@ namespace Tesses.WebServer
await ctx.SendStreamAsync( ms, contentType);
}
}
/// <summary>
/// Get first item in Dictionary<T1,List<T2>> based on key
/// </summary>
/// <param name="args">the dictionary with list<T2> value</param>
/// <param name="key">some key</param>
/// <typeparam name="T1">key type</typeparam>
/// <typeparam name="T2">value type</typeparam>
/// <returns></returns>
public static T2 GetFirst<T1,T2>(this Dictionary<T1,List<T2>> args,T1 key)
{
return args[key][0];
}
/// <summary>
/// Try to get first item in Dictionary<T1,List<T2>> based on key
/// </summary>
/// <param name="args">the dictionary with list<T2> value</param>
/// <param name="key">the key to check</param>
/// <param name="value">the value returned</param>
/// <typeparam name="T1">key type</typeparam>
/// <typeparam name="T2">value type</typeparam>
/// <returns>true if found else false if not found</returns>
public static bool TryGetFirst<T1,T2>(this Dictionary<T1,List<T2>> args,T1 key,out T2 value)
{
List<T2> ls;
@ -81,6 +144,14 @@ namespace Tesses.WebServer
value = default(T2);
return false;
}
/// <summary>
/// Add item to the Dictionary<T1,List<T2>> with specified key (will create key in dictionary if not exist)
/// </summary>
/// <param name="list">the dictionary with list<T2> value</param>
/// <param name="key">the key to add or to add to</param>
/// <param name="item">a item</param>
/// <typeparam name="T1">key type</typeparam>
/// <typeparam name="T2">value type</typeparam>
public static void Add<T1,T2>(this Dictionary<T1,List<T2>> list,T1 key,T2 item)
{
if (list.ContainsKey(key))
@ -94,7 +165,14 @@ namespace Tesses.WebServer
list.Add(key, items);
}
}
/// <summary>
/// Add multiple items to the Dictionary<T1,List<T2>> with specified key (will create key in dictionary if not exist)
/// </summary>
/// <param name="list">the dictionary with list<T2> value</param>
/// <param name="key">the key to add or to add to</param>
/// <param name="items">IEnumerable<T2></param>
/// <typeparam name="T1">key type</typeparam>
/// <typeparam name="T2">value type</typeparam>
public static void AddRange<T1,T2>(this Dictionary<T1,List<T2>> list,T1 key,IEnumerable<T2> items)
{
if (list.ContainsKey(key))
@ -109,7 +187,13 @@ namespace Tesses.WebServer
}
}
/// <summary>
/// StringBuilder ends with
/// </summary>
/// <param name="sb">string builder</param>
/// <param name="test">text to check</param>
/// <param name="comparison">comparison type</param>
/// <returns>true if sb ends with test, false if it does not</returns>
public static bool EndsWith(this StringBuilder sb, string test,
StringComparison comparison)
{
@ -120,12 +204,22 @@ namespace Tesses.WebServer
return end.Equals(test, comparison);
}
}
/// <summary>
/// returns 404 not found page
/// </summary>
public class NotFoundServer : Server
{
/// <summary>
/// 404 not found custom html use "{url}" in your html as url
/// </summary>
/// <param name="html">the custom html</param>
public NotFoundServer(string html)
{
_html = html;
}
/// <summary>
///404 not found default html
/// </summary>
public NotFoundServer()
{
_html = "<html><head><title>File {url} not found</title></head><body><h1>404 Not Found</h1><h4>{url}</h4></body></html>";
@ -134,14 +228,20 @@ namespace Tesses.WebServer
public override async Task GetAsync(ServerContext ctx)
{
ctx.StatusCode = 404;
await ctx.SendTextAsync( _html.Replace("{url}", WebUtility.HtmlEncode(ctx.UrlPath)));
await ctx.SendTextAsync( _html.Replace("{url}", WebUtility.HtmlEncode(ctx.OriginalUrlPath)));
}
}
/// <summary>
/// Serve static files (doesnt allow listing files)
/// </summary>
public class StaticServer : Server
{
string _path;
IServer _server;
/// <summary>
/// construct with path
/// </summary>
/// <param name="path">directory for server</param>
public StaticServer(string path)
{
_path = path;
@ -149,6 +249,12 @@ namespace Tesses.WebServer
_defaultFileNames = new string[] {"index.html","index.htm","default.html","default.htm" };
}
string[] _defaultFileNames;
/// <summary>
/// construct with path, custom filenames, and server for not found
/// </summary>
/// <param name="path">directory for server</param>
/// <param name="defaultFileNames">like index.html, index.htm, default.html, default.htm</param>
/// <param name="notfoundserver">404 not found server</param>
public StaticServer(string path,string[] defaultFileNames,IServer notfoundserver)
{
_path = path;
@ -193,9 +299,72 @@ namespace Tesses.WebServer
}
}
/// <summary>
/// Server where you can change inner server
/// </summary>
public class ChangeableServer : Server
{
/// <summary>
/// The inner server to change
/// </summary>
public IServer Server {get;set;}
/// <summary>
/// Construct with default value
/// </summary>
public ChangeableServer()
{
Server=null;
}
/// <summary>
/// Construct with server
/// </summary>
/// <param name="svr">the inner server</param>
public ChangeableServer(IServer svr)
{
Server=svr;
}
public override async Task<bool> BeforeAsync(ServerContext ctx)
{
return await Guaranteed(Server).BeforeAsync(ctx);
}
public override async Task GetAsync(ServerContext ctx)
{
await Guaranteed(Server).GetAsync(ctx);
}
public override async Task OptionsAsync(ServerContext ctx)
{
await Guaranteed(Server).OptionsAsync(ctx);
}
public override async Task OtherAsync(ServerContext ctx)
{
await Guaranteed(Server).OtherAsync(ctx);
}
public override async Task PostAsync(ServerContext ctx)
{
await Guaranteed(Server).PostAsync(ctx);
}
}
/// <summary>
/// Abstract class for server
/// </summary>
public abstract class Server : IServer
{
/// <summary>
/// Returns 404 Not found
/// </summary>
public static readonly NotFoundServer ServerNull = new NotFoundServer();
/// <summary>
/// You are guarenteed to have a server
/// </summary>
/// <param name="svr">any server object</param>
/// <returns>if null return ServerNull otherwise return svr</returns>
public IServer Guaranteed(IServer svr)
{
if(svr != null)
@ -204,31 +373,59 @@ namespace Tesses.WebServer
}
return ServerNull;
}
/// <summary>
/// Put cors header
/// </summary>
public bool CorsHeader = true;
/// <summary>
/// Called on GET Request
/// </summary>
/// <param name="ctx">ServerContext</param>
public abstract Task GetAsync(ServerContext ctx);
/// <summary>
/// Called on POST Request
/// </summary>
/// <param name="ctx">ServerContext</param>
public virtual async Task PostAsync(ServerContext ctx)
{
ctx.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
await ctx.SendTextAsync("Method Not Supported");
}
/// <summary>
/// Called on OPTIONS Request
/// </summary>
/// <param name="ctx">ServerContext</param>
public virtual async Task OptionsAsync(ServerContext ctx)
{
await ctx.WriteHeadersAsync();
ctx.NetworkStream.Close();
}
/// <summary>
/// Called on any other Request method
/// </summary>
/// <param name="ctx">ServerContext</param>
public virtual async Task OtherAsync(ServerContext ctx)
{
ctx.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
await ctx.SendTextAsync("Method Not Supported");
}
/// <summary>
/// Called before request was made
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <returns>true to cancel request, false to continue request</returns>
public virtual async Task<bool> BeforeAsync(ServerContext ctx)
{
return await Task.FromResult(false);
}
/// <summary>
/// Add cors header
/// </summary>
/// <param name="ctx">Server Context</param>
public void AddCors(ServerContext ctx)
{
if (CorsHeader)
@ -242,7 +439,9 @@ namespace Tesses.WebServer
}
}
}
/// <summary>
/// mount multiple servers at different url paths
/// </summary>
public sealed class MountableServer : Server
{
Dictionary<string, IServer> _servers = new Dictionary<string, IServer>();
@ -286,10 +485,17 @@ namespace Tesses.WebServer
{
_servers.Add(url, server);
}
/// <summary>
/// Unmount a server
/// </summary>
/// <param name="url">Url</param>
public void Unmount(string url)
{
_servers.Remove(url);
}
/// <summary>
/// Unmount all servers
/// </summary>
public void UnmountAll()
{
_servers.Clear();
@ -338,14 +544,49 @@ namespace Tesses.WebServer
await v.Value.OtherAsync(ctx);
}
}
/// <summary>
/// Check username and password are correct or if request can be anonymous
/// </summary>
/// <param name="username">Username, can and will be "" on first request for resource</param>
/// <param name="password">Password, can and will be "" on first request for resource</param>
/// <returns>true for authorized, false for unauthorized</returns>
public delegate bool Authenticate(string username, string password);
/// <summary>
/// Check username and password are correct or if request can be anonymous
/// </summary>
/// <param name="context">Server Context</param>
/// <param name="username">Username, can and will be "" on first request for resource</param>
/// <param name="password">Password, can and will be "" on first request for resource</param>
/// <returns>true for authorized, false for unauthorized</returns>
public delegate bool AuthenticateWithContext(ServerContext context,string username,string password);
/// <summary>
/// Protect server with password
/// </summary>
public class BasicAuthServer : Server
{
/// <summary>
/// Construct server for user authorization
/// </summary>
/// <param name="auth">callback for authorization</param>
/// <param name="inner">server to protect</param>
/// <param name="realm">realm parameter in WWW-Auhenticate Header</param>
public BasicAuthServer(Authenticate auth,IServer inner,string realm="SampleRealm")
{
Authenticate = auth;
InnerServer = inner;
Realm = realm;
}
/// <summary>
/// Construct server for user authorization (With ServerContext in callback)
/// </summary>
/// <param name="auth">callback for authorization</param>
/// <param name="inner">server to protect</param>
/// <param name="realm">realm parameter in WWW-Auhenticate Header</param>
public BasicAuthServer(AuthenticateWithContext auth,IServer inner,string realm = "SampleRealm")
{
AuthenticateWithContext=auth;
InnerServer=inner;
Realm = realm;
}
public override async Task<bool> BeforeAsync(ServerContext ctx)
{
@ -382,13 +623,29 @@ namespace Tesses.WebServer
await Guaranteed(InnerServer).OptionsAsync(ctx);
}
/// <summary>
/// Server to protect
/// </summary>
public IServer InnerServer { get; set; }
/// <summary>
/// Authentication callback without ServerContext
/// </summary>
public Authenticate Authenticate { get; set; }
/// <summary>
/// Authentication callback with ServerContext
/// </summary>
public AuthenticateWithContext AuthenticateWithContext {get;set;}
/// <summary>
/// Realm parameter in WWW-Authenticate header
/// </summary>
public string Realm { get; set; }
private bool ValidAuth(ServerContext ctx)
{
string auth;
if(Authenticate == null && AuthenticateWithContext == null) return true;
if (ctx.RequestHeaders.TryGetFirst("Authorization", out auth))
{
string[] authorization = auth.Split(' ');
@ -398,14 +655,27 @@ namespace Tesses.WebServer
{
string[] userPass = Encoding.UTF8.GetString(Convert.FromBase64String(authorization[1])).Split(new char[] { ':' },2);
//return userPass.Equals($"{config.UserName}:{config.Password}", StringComparison.Ordinal);
return Authenticate(userPass[0], userPass[1]);
if(Authenticate != null)
return Authenticate(userPass[0], userPass[1]);
if(AuthenticateWithContext != null)
return AuthenticateWithContext(ctx,userPass[0],userPass[2]);
}
}else{
if(Authenticate != null)
return Authenticate("", "");
if(AuthenticateWithContext != null)
return AuthenticateWithContext(ctx,"","");
}
return false;
}
private async Task<bool> Authorize(ServerContext ctx)
{
if (Authenticate == null)
if (Authenticate == null && AuthenticateWithContext == null)
return true;
if (ValidAuth(ctx))
@ -491,13 +761,17 @@ namespace Tesses.WebServer
public sealed class HttpServerListener
{
/// <summary>
/// Print urls when running
/// </summary>
/// <value>true if verbose, false if not</value>
public bool PrintUrls {get;set;}
bool https;
X509Certificate cert;
IServer _server;
TcpListener _listener;
SslProtocols protocols;
//
public HttpServerListener(IPEndPoint endPoint,IServer server)
{
_listener = new TcpListener(endPoint);