tesses.webserver/Tesses.WebServer.NetStandard/TessesServer.cs

1014 lines
35 KiB
C#
Raw Normal View History

2022-04-02 18:59:12 +00:00
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using HeyRed.Mime;
using Newtonsoft.Json;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
2022-04-03 15:36:38 +00:00
using System.Security.Authentication;
2022-04-02 18:59:12 +00:00
namespace Tesses.WebServer
{
2022-04-02 21:15:20 +00:00
public static class Extensions
{
2022-04-24 18:56:06 +00:00
/// <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;
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Write headers to stream
/// </summary>
/// <param name="ctx">ServerContext</param>
2022-04-02 21:15:20 +00:00
public static async Task WriteHeadersAsync(this ServerContext ctx)
2022-04-02 18:59:12 +00:00
{
string status_line = $"HTTP/1.1 {ctx.StatusCode} {StatusCodeMap.GetStatusString(ctx.StatusCode)}\r\n";
StringBuilder b = new StringBuilder(status_line);
foreach (var hdr in ctx.ResponseHeaders)
{
foreach (var v in hdr.Value)
{
b.Append($"{hdr.Key}: {v}\r\n");
}
}
b.Append("\r\n");
var data = Encoding.UTF8.GetBytes(b.ToString());
await ctx.NetworkStream.WriteAsync(data, 0, data.Length);
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Send file to client (supports range partial content)
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="file">the file to serve</param>
2022-04-02 18:59:12 +00:00
public static async Task SendFileAsync(this ServerContext ctx, string file)
{
using (var strm = File.OpenRead(file))
{
await ctx.SendStreamAsync( strm, MimeTypesMap.GetMimeType(file));
}
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Send exception to client
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="ex">the Exception</param>
2022-04-02 18:59:12 +00:00
public static async Task SendExceptionAsync(this ServerContext ctx, Exception ex)
{
string name = ex.GetType().FullName;
string j = $"<html><head><title>{WebUtility.HtmlEncode(name)} thrown</title></head><body><h1>{WebUtility.HtmlEncode(name)} thrown</h1><h3>Description: {WebUtility.HtmlEncode(ex.Message)}</h3></body></html>";
ctx.StatusCode = 500;
await ctx.SendTextAsync(j);
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Send object as json to client
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="value">an object to serialize with newtonsoft.json</param>
2022-04-02 18:59:12 +00:00
public static async Task SendJsonAsync(this ServerContext ctx,object value)
{
await ctx.SendTextAsync(JsonConvert.SerializeObject(value), "application/json");
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Send text to client
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <param name="data">some text</param>
/// <param name="contentType">mime type</param>
2022-04-02 21:15:20 +00:00
public static async Task SendTextAsync(this ServerContext ctx, string data, string contentType = "text/html")
2022-04-02 18:59:12 +00:00
{
2022-04-02 21:15:20 +00:00
await ctx.SendBytesAsync(Encoding.UTF8.GetBytes(data), contentType);
2022-04-02 18:59:12 +00:00
}
2022-04-23 18:25:45 +00:00
/// <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>
2022-04-02 21:15:20 +00:00
public static async Task SendBytesAsync(this ServerContext ctx, byte[] array, string contentType = "application/octet-stream")
2022-04-02 18:59:12 +00:00
{
using (var ms = new MemoryStream(array))
{
2022-04-02 21:15:20 +00:00
await ctx.SendStreamAsync( ms, contentType);
2022-04-02 18:59:12 +00:00
}
}
2022-04-23 18:25:45 +00:00
/// <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>
2022-04-02 18:59:12 +00:00
public static T2 GetFirst<T1,T2>(this Dictionary<T1,List<T2>> args,T1 key)
{
return args[key][0];
}
2022-04-23 18:25:45 +00:00
/// <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;
if (args.TryGetValue(key,out ls))
{
if(ls.Count > 0)
{
value = ls[0];
return true;
}
}
value = default(T2);
return false;
}
2022-04-23 18:25:45 +00:00
/// <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>
2022-04-02 18:59:12 +00:00
public static void Add<T1,T2>(this Dictionary<T1,List<T2>> list,T1 key,T2 item)
{
if (list.ContainsKey(key))
{
list[key].Add(item);
}
else
{
List<T2> items = new List<T2>();
items.Add(item);
list.Add(key, items);
}
}
2022-04-23 18:25:45 +00:00
/// <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>
2022-04-02 18:59:12 +00:00
public static void AddRange<T1,T2>(this Dictionary<T1,List<T2>> list,T1 key,IEnumerable<T2> items)
{
if (list.ContainsKey(key))
{
list[key].AddRange(items);
}
else
{
List<T2> items2 = new List<T2>();
items2.AddRange(items);
list.Add(key, items2);
}
}
2022-04-23 18:25:45 +00:00
/// <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>
2022-04-02 18:59:12 +00:00
public static bool EndsWith(this StringBuilder sb, string test,
StringComparison comparison)
{
if (sb.Length < test.Length)
return false;
string end = sb.ToString(sb.Length - test.Length, test.Length);
return end.Equals(test, comparison);
}
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// returns 404 not found page
/// </summary>
2022-04-02 18:59:12 +00:00
public class NotFoundServer : Server
{
2022-04-23 18:25:45 +00:00
/// <summary>
/// 404 not found custom html use "{url}" in your html as url
/// </summary>
/// <param name="html">the custom html</param>
2022-04-02 18:59:12 +00:00
public NotFoundServer(string html)
{
_html = html;
}
2022-04-23 18:25:45 +00:00
/// <summary>
///404 not found default html
/// </summary>
2022-04-02 18:59:12 +00:00
public NotFoundServer()
{
_html = "<html><head><title>File {url} not found</title></head><body><h1>404 Not Found</h1><h4>{url}</h4></body></html>";
}
string _html;
public override async Task GetAsync(ServerContext ctx)
{
ctx.StatusCode = 404;
2022-04-23 18:25:45 +00:00
await ctx.SendTextAsync( _html.Replace("{url}", WebUtility.HtmlEncode(ctx.OriginalUrlPath)));
2022-04-02 18:59:12 +00:00
}
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Serve static files (doesnt allow listing files)
/// </summary>
2022-04-02 18:59:12 +00:00
public class StaticServer : Server
{
string _path;
IServer _server;
2022-04-23 18:25:45 +00:00
/// <summary>
/// construct with path
/// </summary>
/// <param name="path">directory for server</param>
2022-04-02 18:59:12 +00:00
public StaticServer(string path)
{
_path = path;
_server = new NotFoundServer();
_defaultFileNames = new string[] {"index.html","index.htm","default.html","default.htm" };
}
string[] _defaultFileNames;
2022-04-23 18:25:45 +00:00
/// <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>
2022-04-02 18:59:12 +00:00
public StaticServer(string path,string[] defaultFileNames,IServer notfoundserver)
{
_path = path;
_server = notfoundserver;
_defaultFileNames = defaultFileNames;
}
public bool DefaultFileExists(string path,out string name)
{
foreach(var def in _defaultFileNames)
{
name = Path.Combine(path, def);
if(File.Exists(name))
{
return true;
}
}
name = "";
return false;
}
public override async Task GetAsync(ServerContext ctx)
{
string someUrl = Path.Combine(_path,WebUtility.UrlDecode(ctx.UrlPath.Substring(1)).Replace('/', Path.DirectorySeparatorChar));
2022-04-03 15:36:38 +00:00
//Console.WriteLine(someUrl);
2022-04-02 18:59:12 +00:00
if (Directory.Exists(someUrl))
{
string name;
if(DefaultFileExists(someUrl,out name))
{
await ctx.SendFileAsync(name);
}
}
else if (File.Exists(someUrl))
{
await ctx.SendFileAsync(someUrl);
}
else
{
await _server.GetAsync(ctx);
}
}
}
2022-04-23 18:25:45 +00:00
/// <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>
2022-04-02 18:59:12 +00:00
public abstract class Server : IServer
{
2022-04-23 18:25:45 +00:00
/// <summary>
/// Returns 404 Not found
/// </summary>
public static readonly NotFoundServer ServerNull = new NotFoundServer();
2022-04-23 18:25:45 +00:00
/// <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)
{
return svr;
}
return ServerNull;
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Put cors header
/// </summary>
2022-04-02 21:15:20 +00:00
public bool CorsHeader = true;
2022-04-23 18:25:45 +00:00
/// <summary>
/// Called on GET Request
/// </summary>
/// <param name="ctx">ServerContext</param>
2022-04-02 18:59:12 +00:00
public abstract Task GetAsync(ServerContext ctx);
2022-04-23 18:25:45 +00:00
/// <summary>
/// Called on POST Request
/// </summary>
/// <param name="ctx">ServerContext</param>
2022-04-02 18:59:12 +00:00
public virtual async Task PostAsync(ServerContext ctx)
{
ctx.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
await ctx.SendTextAsync("Method Not Supported");
2022-04-02 21:15:20 +00:00
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Called on OPTIONS Request
/// </summary>
/// <param name="ctx">ServerContext</param>
2022-04-02 21:15:20 +00:00
public virtual async Task OptionsAsync(ServerContext ctx)
{
await ctx.WriteHeadersAsync();
ctx.NetworkStream.Close();
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Called on any other Request method
/// </summary>
/// <param name="ctx">ServerContext</param>
2022-04-02 21:15:20 +00:00
public virtual async Task OtherAsync(ServerContext ctx)
{
ctx.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
await ctx.SendTextAsync("Method Not Supported");
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Called before request was made
/// </summary>
/// <param name="ctx">ServerContext</param>
/// <returns>true to cancel request, false to continue request</returns>
2022-04-02 21:15:20 +00:00
public virtual async Task<bool> BeforeAsync(ServerContext ctx)
{
return await Task.FromResult(false);
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Add cors header
/// </summary>
/// <param name="ctx">Server Context</param>
public void AddCors(ServerContext ctx)
{
if (CorsHeader)
{
ctx.ResponseHeaders.Add("Access-Control-Allow-Origin", "*");
ctx.ResponseHeaders.Add("Access-Control-Allow-Headers", "Cache-Control, Pragma, Accept, Origin, Authorization, Content-Type, X-Requested-With");
ctx.ResponseHeaders.Add("Access-Control-Allow-Methods", "GET, POST");
ctx.ResponseHeaders.Add("Access-Control-Allow-Credentials", "true");
}
}
2022-04-02 21:15:20 +00:00
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// mount multiple servers at different url paths
/// </summary>
2022-04-02 21:15:20 +00:00
public sealed class MountableServer : Server
{
Dictionary<string, IServer> _servers = new Dictionary<string, IServer>();
public MountableServer(IServer root)
{
_root = root;
}
IServer _root;
private (string Key,IServer Value) GetFromPath(ServerContext ctx)
2022-04-02 21:15:20 +00:00
{
//bool j = false;
foreach(var item in _servers.Reverse())
{
if(ctx.UrlPath.StartsWith(item.Key,StringComparison.Ordinal))
{
return (item.Key,Guaranteed(item.Value));
}
if (ctx.UrlPath == item.Key.TrimEnd('/'))
{
ctx.UrlPath += "/";
return (item.Key,Guaranteed(item.Value));
2022-04-02 21:15:20 +00:00
}
}
//Console.WriteLine("HERE WE ARE");
return ("/",Guaranteed(_root));
2022-04-02 21:15:20 +00:00
}
/// <summary>
/// Mount the specified url and server.
/// Must mount like this
/// /somePath0
/// /somePath0/someSubPath0
/// /somePath0/someSubPath0/someSubSubPath0
/// /somePath0/someSubPath0/someSubSubPath1
/// /somePath0/someSubPath1
/// /somePath0/someSubPath1/someSubSubPath0
/// /somePath0/someSubPath1/someSubSubPath1
/// </summary>
/// <param name="url">URL.</param>
/// <param name="server">Server.</param>
public void Mount(string url,IServer server)
{
_servers.Add(url, server);
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Unmount a server
/// </summary>
/// <param name="url">Url</param>
2022-04-02 21:15:20 +00:00
public void Unmount(string url)
{
_servers.Remove(url);
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Unmount all servers
/// </summary>
2022-04-02 21:15:20 +00:00
public void UnmountAll()
{
_servers.Clear();
}
public override async Task GetAsync(ServerContext ctx)
{
var v = GetFromPath(ctx);
string url = '/' + ctx.UrlPath.Substring(v.Key.Length).TrimStart('/');
ctx.UrlPath = url;
await v.Value.GetAsync(ctx);
}
public override async Task PostAsync(ServerContext ctx)
{
var v = GetFromPath(ctx);
string url = '/' + ctx.UrlPath.Substring(v.Key.Length).TrimStart('/');
ctx.UrlPath = url;
await v.Value.PostAsync(ctx);
}
public override async Task<bool> BeforeAsync(ServerContext ctx)
{
var v = GetFromPath(ctx);
string old=ctx.UrlPath;
string url = '/' + ctx.UrlPath.Substring(v.Key.Length).TrimStart('/');
ctx.UrlPath = url;
var res=await v.Value.BeforeAsync(ctx);
ctx.UrlPath = old;
return res;
}
public override async Task OptionsAsync(ServerContext ctx)
{
var v = GetFromPath(ctx);
string url = '/' + ctx.UrlPath.Substring(v.Key.Length).TrimStart('/');
ctx.UrlPath = url;
await v.Value.OptionsAsync(ctx);
}
public override async Task OtherAsync(ServerContext ctx)
{
var v = GetFromPath(ctx);
string url = '/' + ctx.UrlPath.Substring(v.Key.Length).TrimStart('/');
ctx.UrlPath = url;
await v.Value.OtherAsync(ctx);
2022-04-02 18:59:12 +00:00
}
}
2022-04-23 18:25:45 +00:00
/// <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);
2022-04-23 18:25:45 +00:00
/// <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
{
2022-04-23 18:25:45 +00:00
/// <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;
2022-04-23 18:25:45 +00:00
}
/// <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)
{
if(await Authorize(ctx))
{
return await Guaranteed(InnerServer).BeforeAsync(ctx);
}
return true;
}
public override async Task GetAsync(ServerContext ctx)
{
await Guaranteed(InnerServer).GetAsync(ctx);
}
public override async Task PostAsync(ServerContext ctx)
{
await Guaranteed(InnerServer).PostAsync(ctx);
}
public override async Task OtherAsync(ServerContext ctx)
{
await Guaranteed(InnerServer).OtherAsync(ctx);
}
public override async Task OptionsAsync(ServerContext ctx)
{
await Guaranteed(InnerServer).OptionsAsync(ctx);
}
2022-04-23 18:25:45 +00:00
/// <summary>
/// Server to protect
/// </summary>
public IServer InnerServer { get; set; }
2022-04-23 18:25:45 +00:00
/// <summary>
/// Authentication callback without ServerContext
/// </summary>
public Authenticate Authenticate { get; set; }
2022-04-23 18:25:45 +00:00
/// <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;
2022-04-23 18:25:45 +00:00
if(Authenticate == null && AuthenticateWithContext == null) return true;
if (ctx.RequestHeaders.TryGetFirst("Authorization", out auth))
{
string[] authorization = auth.Split(' ');
//authorization_basic
if (authorization[0] == "Basic")
{
string[] userPass = Encoding.UTF8.GetString(Convert.FromBase64String(authorization[1])).Split(new char[] { ':' },2);
//return userPass.Equals($"{config.UserName}:{config.Password}", StringComparison.Ordinal);
2022-04-23 18:25:45 +00:00
if(Authenticate != null)
return Authenticate(userPass[0], userPass[1]);
if(AuthenticateWithContext != null)
return AuthenticateWithContext(ctx,userPass[0],userPass[2]);
}
2022-04-23 18:25:45 +00:00
}else{
if(Authenticate != null)
return Authenticate("", "");
if(AuthenticateWithContext != null)
return AuthenticateWithContext(ctx,"","");
}
return false;
}
private async Task<bool> Authorize(ServerContext ctx)
{
2022-04-23 18:25:45 +00:00
if (Authenticate == null && AuthenticateWithContext == null)
return true;
if (ValidAuth(ctx))
return true;
ctx.ResponseHeaders.Add("WWW-Authenticate", $"Basic realm=\"{Realm}\"");
ctx.StatusCode = 401;
await UnauthorizedPage(ctx);
return false;
}
protected virtual async Task UnauthorizedPage(ServerContext ctx)
{
await ctx.SendTextAsync("Unauthorized");
}
}
public class HostDomainServer : Server
{
public HostDomainServer(IServer alt)
{
Default = alt;
Servers = new Dictionary<string, IServer>();
}
public void Clear()
{
Servers.Clear();
}
public void Remove(string fqdn_or_ip)
{
Servers.Remove(fqdn_or_ip);
}
public IServer Default { get; set; }
Dictionary<string, IServer> Servers;
2022-04-02 18:59:12 +00:00
public void AddDomain(string fqdn_or_ip,IServer svr)
{
Servers.Add(fqdn_or_ip, svr);
}
public override async Task<bool> BeforeAsync(ServerContext ctx)
{
return await GetDomain(ctx).BeforeAsync(ctx);
}
public override async Task PostAsync(ServerContext ctx)
{
await GetDomain(ctx).PostAsync(ctx);
}
public override async Task OtherAsync(ServerContext ctx)
{
await GetDomain(ctx).OtherAsync(ctx);
}
public override async Task OptionsAsync(ServerContext ctx)
{
await GetDomain(ctx).OptionsAsync(ctx);
}
public override async Task GetAsync(ServerContext ctx)
{
await GetDomain(ctx).GetAsync(ctx);
}
private IServer GetDomain(ServerContext ctx)
{
string fqdn_or_ip = ctx.Host;
foreach(var item in Servers)
{
if(item.Key.Equals(fqdn_or_ip,StringComparison.Ordinal))
{
return Guaranteed(item.Value);
}
}
return Guaranteed(Default);
}
}
2022-04-02 18:59:12 +00:00
public interface IServer
{
void AddCors(ServerContext ctx);
2022-04-02 21:15:20 +00:00
Task<bool> BeforeAsync(ServerContext ctx);
2022-04-02 18:59:12 +00:00
Task GetAsync(ServerContext ctx);
Task PostAsync(ServerContext ctx);
2022-04-02 21:15:20 +00:00
Task OptionsAsync(ServerContext ctx);
Task OtherAsync(ServerContext ctx);
2022-04-02 18:59:12 +00:00
}
public sealed class HttpServerListener
{
2022-04-23 18:25:45 +00:00
/// <summary>
/// Print urls when running
/// </summary>
/// <value>true if verbose, false if not</value>
2022-04-03 15:36:38 +00:00
public bool PrintUrls {get;set;}
bool https;
X509Certificate cert;
2022-04-02 18:59:12 +00:00
IServer _server;
TcpListener _listener;
2022-04-03 15:36:38 +00:00
SslProtocols protocols;
2022-04-24 18:56:06 +00:00
2022-04-02 18:59:12 +00:00
public HttpServerListener(IPEndPoint endPoint,IServer server)
{
_listener = new TcpListener(endPoint);
_server = server;
https = false;
2022-04-03 15:36:38 +00:00
PrintUrls=false;
}
public HttpServerListener(IServer server)
{
_listener = new TcpListener(new IPEndPoint(IPAddress.Any, 3251));
_server = server;
https = false;
2022-04-03 15:36:38 +00:00
PrintUrls=false;
}
2022-04-03 15:36:38 +00:00
public HttpServerListener(IPEndPoint endpoint,IServer server,X509Certificate cert,SslProtocols protocols=SslProtocols.Default)
{
_listener = new TcpListener(endpoint);
_server = server;
https = cert != null;
this.cert = cert;
2022-04-03 15:36:38 +00:00
this.protocols=protocols;
PrintUrls=false;
2022-04-02 18:59:12 +00:00
}
public async Task ListenAsync(CancellationToken token)
{
_listener.Start();
using (var r = token.Register(() => _listener.Stop())) {
while (!token.IsCancellationRequested)
{
var socket=await _listener.AcceptTcpClientAsync();
await CommunicateHostAsync(socket).ConfigureAwait(false);
2022-04-02 18:59:12 +00:00
}
}
}
private string ReadHeaders(Stream strm)
2022-04-02 18:59:12 +00:00
{
StringBuilder s = new StringBuilder();
var decoder = Encoding.UTF8.GetDecoder();
var nextChar = new char[1];
while (!s.EndsWith("\r\n\r\n",StringComparison.Ordinal))
{
int data = strm.ReadByte();
if(data == -1)
{
break;
}
int charCount=decoder.GetChars(new byte[] { (byte)data }, 0, 1, nextChar, 0);
if (charCount == 0) continue;
s.Append(nextChar);
}
return s.ToString();
}
private Dictionary<string,List<string>> Headers(string s,out string req_line)
{
Dictionary<string, List<string>> items = new Dictionary<string, List<string>>();
string[] lines = s.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
req_line = lines[0];
for(int i=1;i<lines.Length;i++)
{
var line_split=lines[i].Split(new[] { ": " },2,StringSplitOptions.None);
if (line_split.Length == 2)
{
items.Add(line_split[0], line_split[1]);
}
}
return items;
}
public Stream GetStream(TcpClient clt)
{
if(https)
{
SslStream sslStream = new SslStream(
clt.GetStream(), false);
try
{
2022-04-03 15:36:38 +00:00
sslStream.AuthenticateAsServer(cert,false,protocols,true);
}
catch (Exception ex)
{
_ = ex;
}
return sslStream;
}
return clt.GetStream();
}
2022-04-02 18:59:12 +00:00
private async Task CommunicateHostAsync(TcpClient clt)
{
//<METHOD> <PATH> HTTP/1.1\r\n
//HEADER1\r\n
//HEADER2\r\n
//......
//HEADERN\r\n
//\r\n
//OPTIONAL REQUEST BODY
//RESPONSE
using (Stream strm = GetStream(clt))
2022-04-02 18:59:12 +00:00
{
string request_line = "";
string res=ReadHeaders(strm);
var headers=Headers(res,out request_line);
// {Method} {Path} HTTP/1.1
ServerContext ctx=null;
string[] request=request_line.Split(new char[] { ' ' }, 3);
string method = request[0];
try
{
2022-04-02 21:15:20 +00:00
string path = request[1];
string ver = request[2];
ctx = new ServerContext(method, strm, path, headers);
ctx.Server = clt.Client.LocalEndPoint as IPEndPoint;
ctx.Client = clt.Client.RemoteEndPoint as IPEndPoint;
_server.AddCors(ctx);
2022-04-03 15:36:38 +00:00
if(PrintUrls)
{
Console.WriteLine(path);
}
2022-04-02 21:15:20 +00:00
if (!await _server.BeforeAsync(ctx))
2022-04-02 18:59:12 +00:00
{
2022-04-02 21:15:20 +00:00
switch (method)
{
case "HEAD":
case "GET":
await _server.GetAsync(ctx);
break;
case "POST":
await _server.PostAsync(ctx);
break;
case "OPTIONS":
await _server.OptionsAsync(ctx);
break;
default:
await _server.OtherAsync(ctx);
break;
}
2022-04-02 18:59:12 +00:00
}
}catch(Exception ex)
{
try
{
await ctx.SendExceptionAsync(ex);
}catch(Exception ex2)
{
_ = ex2;
}
}
}
}
//protected abstract Task<IResult> Get(string url,Dictionary<string,string> headers);
//protected abstract Task GetAsync(ServerContext ctx);
}
}