2559 lines
93 KiB
C#
2559 lines
93 KiB
C#
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;
|
|
using System.Security.Authentication;
|
|
using System.Web;
|
|
using Tesses.VirtualFilesystem;
|
|
using System.Net.Mime;
|
|
using System.Numerics;
|
|
|
|
namespace Tesses.WebServer
|
|
{
|
|
internal class SendEventArgs : EventArgs
|
|
{
|
|
public string Data {get;set;}
|
|
}
|
|
public class SendEvents
|
|
{
|
|
internal event EventHandler<SendEventArgs> EventReceived;
|
|
|
|
public void SendEvent(object data)
|
|
{
|
|
SendEvent(JsonConvert.SerializeObject(data));
|
|
}
|
|
public void SendEvent(string e)
|
|
{
|
|
try{
|
|
EventReceived?.Invoke(this,new SendEventArgs(){Data=e});
|
|
}catch(Exception ex)
|
|
{
|
|
_=ex;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class Extensions
|
|
{
|
|
public static string GetRealRootUrl(this ServerContext ctx)
|
|
{
|
|
if(ctx.RequestHeaders.TryGetFirst("X-Forwarded-Path",out var xfwp))
|
|
{
|
|
return $"{xfwp.TrimEnd('/')}/";
|
|
}
|
|
else if(ctx.RequestHeaders.TryGetFirst("X-Forwarded-Host",out var host))
|
|
{
|
|
if(!ctx.RequestHeaders.TryGetFirst("X-Forwarded-Proto",out var proto)) proto="http";
|
|
return $"{proto}://{host}/";
|
|
}
|
|
else if(ctx.RequestHeaders.TryGetFirst("Host",out var theHost))
|
|
{
|
|
return $"http://{theHost}/";
|
|
}
|
|
return $"http://{ctx.Client}/";
|
|
}
|
|
public static string GetRealUrl(this ServerContext ctx,string url)
|
|
{
|
|
return $"{ctx.GetRealRootUrl()}{url.TrimStart('/')}";
|
|
}
|
|
public static string GetCurrentServerPath(this ServerContext ctx)
|
|
{
|
|
if(ctx.UrlPath == ctx.OriginalUrlPath) return "/";
|
|
return $"{ctx.OriginalUrlPath.Remove(ctx.OriginalUrlPath.Length-ctx.UrlPath.Length).TrimEnd('/')}/";
|
|
}
|
|
public static string GetRealUrlRelativeToCurrentServer(this ServerContext ctx, string url)
|
|
{
|
|
return ctx.GetRealUrl($"{ctx.GetCurrentServerPath()}{url.TrimStart('/')}");
|
|
}
|
|
public static ServerContext WithStatusCode(this ServerContext ctx, int statusCode)
|
|
{
|
|
ctx.StatusCode = statusCode;
|
|
return ctx;
|
|
}
|
|
public static ServerContext WithStatusCode(this ServerContext ctx, HttpStatusCode statusCode)
|
|
{
|
|
ctx.StatusCode = (int)statusCode;
|
|
return ctx;
|
|
}
|
|
public static void SendNotFound(this ServerContext ctx)
|
|
{
|
|
ctx.StatusCode=404;
|
|
string url=WebUtility.HtmlEncode(ctx.OriginalUrlPath);
|
|
ctx.SendText($"<html><head><title>File {url} not found</title></head><body><h1>404 Not Found</h1><h4>{url}</h4></body></html>");
|
|
}
|
|
public static async Task SendNotFoundAsync(this ServerContext ctx)
|
|
{
|
|
ctx.StatusCode=404;
|
|
string url=WebUtility.HtmlEncode(ctx.OriginalUrlPath);
|
|
await ctx.SendTextAsync($"<html><head><title>File {url} not found</title></head><body><h1>404 Not Found</h1><h4>{url}</h4></body></html>");
|
|
}
|
|
public static ServerContext WithDate(this ServerContext ctx, DateTime dateTime)
|
|
{
|
|
ctx.ResponseHeaders.Add("Date",dateTime.ToString("R"));
|
|
return ctx;
|
|
}
|
|
public static ServerContext WithLastModified(this ServerContext ctx, DateTime dateTime)
|
|
{
|
|
ctx.ResponseHeaders.Add("Last-Modified",dateTime.ToString("R"));
|
|
return ctx;
|
|
}
|
|
public static ServerContext WithMimeType(this ServerContext ctx, string mimeType)
|
|
{
|
|
if(ctx.ResponseHeaders.ContainsKey("Content-Type"))
|
|
{
|
|
ctx.ResponseHeaders["Content-Type"].Clear();
|
|
ctx.ResponseHeaders["Content-Type"].Add(mimeType);
|
|
}
|
|
else
|
|
{
|
|
ctx.ResponseHeaders.Add("Content-Type",mimeType);
|
|
}
|
|
return ctx;
|
|
}
|
|
public static ServerContext WithMimeTypeFromFileName(this ServerContext ctx, string filename)
|
|
{
|
|
return ctx.WithMimeType(HeyRed.Mime.MimeTypesMap.GetMimeType(filename));
|
|
}
|
|
public static ServerContext WithFileName(this ServerContext ctx, string filename, bool inline)
|
|
{
|
|
ContentDisposition disposition=new ContentDisposition();
|
|
disposition.FileName = filename;
|
|
disposition.Inline = inline;
|
|
ctx.ResponseHeaders.Add("Content-Disposition",disposition.ToString());
|
|
return ctx;
|
|
}
|
|
public static async Task WriteAsync(this Stream strm,string text)
|
|
{
|
|
var data=Encoding.UTF8.GetBytes(text);
|
|
await strm.WriteAsync(data,0,data.Length);
|
|
await strm.FlushAsync();
|
|
}
|
|
public static void Write(this Stream strm,string text)
|
|
{
|
|
var data=Encoding.UTF8.GetBytes(text);
|
|
strm.Write(data,0,data.Length);
|
|
strm.Flush();
|
|
}
|
|
public static void ServerSentEvents(this ServerContext ctx,SendEvents evt)
|
|
{
|
|
bool __connected=true;
|
|
ctx.ResponseHeaders.Add("Content-Type","text/event-stream");
|
|
ctx.ResponseHeaders.Add("Cache-Control","no-cache");
|
|
ctx.WriteHeaders();
|
|
try{
|
|
EventHandler<SendEventArgs> cb= (sender,e0)=>{
|
|
if(__connected)
|
|
{
|
|
ctx.NetworkStream.Write($"data: {e0.Data}\n\n");
|
|
ctx.NetworkStream.Flush();
|
|
}
|
|
|
|
};
|
|
evt.EventReceived += cb;
|
|
while(ctx.Connected);
|
|
evt.EventReceived -= cb;
|
|
__connected=false;
|
|
}catch(Exception ex)
|
|
{
|
|
_=ex;
|
|
}
|
|
}
|
|
/// <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 = ctx.GetRequestStreamReader())
|
|
{
|
|
str = await reader.ReadToEndAsync();
|
|
}
|
|
|
|
return str;
|
|
}
|
|
public static StreamReader GetRequestStreamReader(this ServerContext ctx)
|
|
{
|
|
return new StreamReader(ctx.GetRequestStream(),Encoding.UTF8,false,4096,false);
|
|
}
|
|
public static StreamWriter GetResponseStreamWriter(this ServerContext ctx,string mimetype)
|
|
{
|
|
return ctx.WithMimeType(mimetype).GetResponseStreamWriter();
|
|
}
|
|
public static StreamWriter GetResponseStreamWriter(this ServerContext ctx)
|
|
{
|
|
return new StreamWriter(ctx.GetResponseStream(),Encoding.UTF8,4096,false);
|
|
}
|
|
public static Stream GetResponseStream(this ServerContext ctx)
|
|
{
|
|
ctx.ResponseHeaders.Add("Transfer-Encoding","chunked");
|
|
ctx.WriteHeaders();
|
|
return new ChunkedStream(ctx.NetworkStream,false);
|
|
}
|
|
public static Stream GetResponseStream(this ServerContext ctx, string mimetype)
|
|
{
|
|
return ctx.WithMimeType(mimetype).GetResponseStream();
|
|
|
|
}
|
|
/// <summary>
|
|
/// Read string from request body
|
|
/// </summary>
|
|
/// <param name="ctx">ServerContext</param>
|
|
/// <returns>the contents of request</returns>
|
|
public static string ReadString(this ServerContext ctx)
|
|
{
|
|
string str = null;
|
|
using (var reader = ctx.GetRequestStreamReader())
|
|
{
|
|
str = reader.ReadToEnd();
|
|
}
|
|
|
|
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 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 T ReadJson<T>(this ServerContext ctx)
|
|
{
|
|
var json= ctx.ReadString();
|
|
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 array
|
|
/// </summary>
|
|
/// <param name="ctx">ServerContext</param>
|
|
/// <returns>Request body data</returns>
|
|
public static byte[] ReadBytes(this ServerContext ctx)
|
|
{
|
|
MemoryStream strm = new MemoryStream();
|
|
ctx.ReadToStream(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.GetRequestStream().CopyToAsync(strm);
|
|
}
|
|
/// <summary>
|
|
/// Read request body to stream
|
|
/// </summary>
|
|
/// <param name="ctx">ServerContext</param>
|
|
/// <param name="strm">Stream to write to</param>
|
|
|
|
public static void ReadToStream(this ServerContext ctx,Stream strm)
|
|
{
|
|
ctx.GetRequestStream().CopyTo(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.ReadToStreamAsync(f);
|
|
}
|
|
return filename;
|
|
}
|
|
/// <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 string ReadToFile(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))
|
|
{
|
|
ctx.ReadToStream(f);
|
|
}
|
|
return filename;
|
|
}
|
|
|
|
/// <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";
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// Write headers to stream
|
|
/// </summary>
|
|
/// <param name="ctx">ServerContext</param>
|
|
|
|
public static void WriteHeaders(this ServerContext ctx)
|
|
{
|
|
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());
|
|
ctx.NetworkStream.Write(data, 0, data.Length);
|
|
}
|
|
|
|
/// <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;
|
|
string j = $"<html><head><title>{WebUtility.HtmlEncode(name)} thrown</title></head><body><h1>{WebUtility.HtmlEncode(name)} thrown</h1><h3>Description: {WebUtility.HtmlEncode(ex.ToString())}</h3></body></html>";
|
|
ctx.StatusCode = 500;
|
|
await ctx.SendTextAsync(j);
|
|
}
|
|
/// <summary>
|
|
/// Send exception to client
|
|
/// </summary>
|
|
/// <param name="ctx">ServerContext</param>
|
|
/// <param name="ex">the Exception</param>
|
|
|
|
public static void SendException(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.ToString())}</h3></body></html>";
|
|
ctx.StatusCode = 500;
|
|
ctx.SendText(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 object as json to client
|
|
/// </summary>
|
|
/// <param name="ctx">ServerContext</param>
|
|
/// <param name="value">an object to serialize with newtonsoft.json</param>
|
|
public static void SendJson(this ServerContext ctx,object value)
|
|
{
|
|
ctx.SendText(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 text to client
|
|
/// </summary>
|
|
/// <param name="ctx">ServerContext</param>
|
|
/// <param name="data">some text</param>
|
|
/// <param name="contentType">mime type</param>
|
|
|
|
public static void SendText(this ServerContext ctx, string data, string contentType = "text/html")
|
|
{
|
|
ctx.SendBytes(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 redirect
|
|
/// </summary>
|
|
/// <param name="ctx">ServerContext</param>
|
|
/// <param name="url">Url to redirect to</param>
|
|
public static void SendRedirect(this ServerContext ctx,string url)
|
|
{
|
|
ctx.StatusCode = 301;
|
|
ctx.ResponseHeaders.Add("Cache-Control","no-cache");
|
|
ctx.ResponseHeaders.Add("Location",url);
|
|
ctx.WriteHeaders();
|
|
}
|
|
/// <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))
|
|
{
|
|
await ctx.SendStreamAsync( ms, contentType);
|
|
}
|
|
}
|
|
/// <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 void SendBytes(this ServerContext ctx, byte[] array, string contentType = "application/octet-stream")
|
|
{
|
|
using (var ms = new MemoryStream(array))
|
|
{
|
|
ctx.SendStream( 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;
|
|
if (args.TryGetValue(key,out ls))
|
|
{
|
|
if(ls.Count > 0)
|
|
{
|
|
value = ls[0];
|
|
return true;
|
|
}
|
|
}
|
|
value = default(T2);
|
|
return false;
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, object value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, int value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, short value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, long value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, Guid value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, byte value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, sbyte value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, uint value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, ushort value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, ulong value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, bool value)
|
|
{
|
|
args.Add(key,value ? "true" : "false");
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, float value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, double value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
public static void Add<T1>(this Dictionary<T1,List<string>> args,T1 key, decimal value)
|
|
{
|
|
args.Add(key,value.ToString());
|
|
}
|
|
|
|
public static bool TryGetFirstInt64<T1>(this Dictionary<T1,List<string>> args,T1 key, out long value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= long.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstInt32<T1>(this Dictionary<T1,List<string>> args,T1 key, out int value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= int.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstInt16<T1>(this Dictionary<T1,List<string>> args,T1 key, out short value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= short.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstInt8<T1>(this Dictionary<T1,List<string>> args,T1 key, out sbyte value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= sbyte.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
public static bool GetFirstBoolean<T1>(this Dictionary<T1,List<string>> args,T1 key)
|
|
{
|
|
if(args.TryGetFirst(key,out var value))
|
|
{
|
|
value=value.ToLower();
|
|
if(value == "off" || value == "no" || value == "false" || value == "0") return false;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstGuid<T1>(this Dictionary<T1,List<string>> args,T1 key, out Guid value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= Guid.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=Guid.Empty;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstUInt8<T1>(this Dictionary<T1,List<string>> args,T1 key, out byte value)
|
|
{
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= byte.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstUInt16<T1>(this Dictionary<T1,List<string>> args,T1 key, out ushort value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= ushort.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstUInt64<T1>(this Dictionary<T1,List<string>> args,T1 key, out ulong value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= ulong.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstUInt32<T1>(this Dictionary<T1,List<string>> args,T1 key, out uint value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= uint.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
|
|
public static bool TryGetFirstFloat<T1>(this Dictionary<T1,List<string>> args,T1 key, out float value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= float.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0.0f;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstBigInteger<T1>(this Dictionary<T1,List<string>> args,T1 key, out BigInteger value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= BigInteger.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstDecimal<T1>(this Dictionary<T1,List<string>> args,T1 key, out decimal value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= decimal.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0.0M;
|
|
return false;
|
|
}
|
|
public static bool TryGetFirstDouble<T1>(this Dictionary<T1,List<string>> args,T1 key, out double value)
|
|
{
|
|
|
|
if(args.TryGetFirst(key,out var str))
|
|
{
|
|
bool res= double.TryParse(str,out var val);
|
|
value = val;
|
|
return res;
|
|
}
|
|
value=0.0;
|
|
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))
|
|
{
|
|
list[key].Add(item);
|
|
}
|
|
else
|
|
{
|
|
List<T2> items = new List<T2>();
|
|
items.Add(item);
|
|
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))
|
|
{
|
|
list[key].AddRange(items);
|
|
}
|
|
else
|
|
{
|
|
List<T2> items2 = new List<T2>();
|
|
items2.AddRange(items);
|
|
list.Add(key, items2);
|
|
|
|
}
|
|
}
|
|
/// <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)
|
|
{
|
|
if (sb.Length < test.Length)
|
|
return false;
|
|
|
|
string end = sb.ToString(sb.Length - test.Length, test.Length);
|
|
return end.Equals(test, comparison);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// returns 404 not found page
|
|
/// </summary>
|
|
public class NotFoundServer : SameServer
|
|
{
|
|
/// <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) : base(html)
|
|
{
|
|
|
|
}
|
|
/// <summary>
|
|
///404 not found default html
|
|
/// </summary>
|
|
public NotFoundServer() : base("<html><head><title>File {url} not found</title></head><body><h1>404 Not Found</h1><h4>{url}</h4></body></html>")
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
public class SameServer : Server
|
|
{
|
|
/// <summary>
|
|
/// html use "{url}" in your html as url
|
|
/// </summary>
|
|
/// <param name="html">the custom html</param>
|
|
/// <param name="statusCode">the status code</param>
|
|
public SameServer(string html,int statusCode=404)
|
|
{
|
|
_html=html;
|
|
_statusCode = statusCode;
|
|
}
|
|
int _statusCode;
|
|
string _html;
|
|
public override async Task GetAsync(ServerContext ctx)
|
|
{
|
|
|
|
ctx.StatusCode = _statusCode;
|
|
await ctx.SendTextAsync( _html.Replace("{url}", WebUtility.HtmlEncode(ctx.OriginalUrlPath)));
|
|
}
|
|
|
|
}
|
|
|
|
public enum WebServerPathType
|
|
{
|
|
NotFound=-1,
|
|
File=0,
|
|
Directory=1
|
|
}
|
|
public sealed class WebServerPathEntry
|
|
{
|
|
public WebServerPathEntry(string fname,WebServerPathType type,string url)
|
|
{
|
|
FileName = fname;
|
|
Type =type;
|
|
Url = url;
|
|
}
|
|
public string FileName {get;set;}
|
|
public string Url {get;set;}
|
|
|
|
public WebServerPathType Type {get;set;}
|
|
}
|
|
public interface IFileHandler
|
|
{
|
|
IEnumerable<string> ListDirectory(string path);
|
|
bool DefaultFileExists(string path,out string name);
|
|
WebServerPathEntry GetPath(string path);
|
|
Stream CreateFile(string path);
|
|
Stream Open(WebServerPathEntry entry);
|
|
void CreateDirectory(string dir);
|
|
}
|
|
public sealed class WebServerStyleFilesystemHandler : IFileHandler
|
|
{
|
|
string[] _defaultFileNames;
|
|
IVirtualFilesystem _fs;
|
|
/// <summary>
|
|
/// construct with filesystem
|
|
/// </summary>
|
|
/// <param name="fs">filesystem for root</param>
|
|
public WebServerStyleFilesystemHandler(IVirtualFilesystem fs)
|
|
{
|
|
_defaultFileNames = new string[] {"index.html","index.htm","default.html","default.htm" };
|
|
_fs=fs;
|
|
}
|
|
/// <summary>
|
|
/// construct with filesystem, custom filenames
|
|
/// </summary>
|
|
/// <param name="path">filesystem for root</param>
|
|
/// <param name="defaultFileNames">like index.html, index.htm, default.html, default.htm</param>
|
|
|
|
public WebServerStyleFilesystemHandler(IVirtualFilesystem fs,string[] defaultFileNames)
|
|
{
|
|
_fs = fs;
|
|
|
|
_defaultFileNames = defaultFileNames;
|
|
}
|
|
|
|
public void CreateDirectory(string dir)
|
|
{
|
|
_fs.CreateDirectory(Special.Root / dir);
|
|
}
|
|
|
|
public Stream CreateFile(string name)
|
|
{
|
|
return _fs.Open(Special.Root / name,FileMode.Create,FileAccess.ReadWrite,FileShare.None);
|
|
}
|
|
|
|
public bool DefaultFileExists(string path, out string name)
|
|
{
|
|
foreach(var def in _defaultFileNames)
|
|
{
|
|
var _name = Special.Root / path / def; //woops we need the def here
|
|
name=_name.Path;
|
|
if(_fs.FileExists(_name))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
name = "";
|
|
return false;
|
|
}
|
|
|
|
public WebServerPathEntry GetPath(string path)
|
|
{
|
|
UnixPath someUrl = WebUtility.UrlDecode(path);
|
|
|
|
if (_fs.DirectoryExists(someUrl))
|
|
{
|
|
string name;
|
|
if(DefaultFileExists(someUrl.Path,out name))
|
|
{
|
|
return new WebServerPathEntry(name,WebServerPathType.File,path);
|
|
}else{
|
|
return new WebServerPathEntry(someUrl.Path,WebServerPathType.Directory,path);
|
|
}
|
|
}
|
|
else if (_fs.FileExists(someUrl))
|
|
{
|
|
return new WebServerPathEntry(someUrl.Path,WebServerPathType.File,path);
|
|
}
|
|
else
|
|
{
|
|
return new WebServerPathEntry(someUrl.Path,WebServerPathType.NotFound,path);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<string> ListDirectory(string path)
|
|
{
|
|
foreach(var item in _fs.EnumerateDirectories(Special.Root/path))
|
|
{
|
|
yield return item.Name + "/";
|
|
}
|
|
foreach(var item in _fs.EnumerateFiles(Special.Root/path))
|
|
{
|
|
yield return item.Name;
|
|
}
|
|
}
|
|
public Stream Open(WebServerPathEntry path)
|
|
{
|
|
return _fs.Open(Special.Root / path.FileName,FileMode.Open,FileAccess.Read,FileShare.Read);
|
|
}
|
|
}
|
|
public sealed class WebServerStyleFileHandler : IFileHandler
|
|
{
|
|
string[] _defaultFileNames;
|
|
string _path;
|
|
/// <summary>
|
|
/// construct with path
|
|
/// </summary>
|
|
/// <param name="path">directory for root</param>
|
|
public WebServerStyleFileHandler(string path)
|
|
{
|
|
_defaultFileNames = new string[] {"index.html","index.htm","default.html","default.htm" };
|
|
_path=path;
|
|
}
|
|
/// <summary>
|
|
/// construct with path, custom filenames
|
|
/// </summary>
|
|
/// <param name="path">directory for root</param>
|
|
/// <param name="defaultFileNames">like index.html, index.htm, default.html, default.htm</param>
|
|
|
|
public WebServerStyleFileHandler(string path,string[] defaultFileNames)
|
|
{
|
|
_path = path;
|
|
|
|
_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;
|
|
}
|
|
//fix a nasty security risk, that allows people to access parent files in filesystem
|
|
private string FixDotPath(string url)
|
|
{
|
|
List<string> strs=new List<string>();
|
|
foreach(var item in url.Split(new char[]{'/'}, StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
if(item != ".." && item != ".")
|
|
{
|
|
strs.Add(item);
|
|
}
|
|
}
|
|
return string.Join("/",strs);
|
|
}
|
|
public WebServerPathEntry GetPath(string url)
|
|
{
|
|
|
|
string someUrl = Path.Combine(_path,WebUtility.UrlDecode(FixDotPath(url)).Replace('/', Path.DirectorySeparatorChar));
|
|
//Console.WriteLine(someUrl);
|
|
if (Directory.Exists(someUrl))
|
|
{
|
|
string name;
|
|
if(DefaultFileExists(someUrl,out name))
|
|
{
|
|
return new WebServerPathEntry(name,WebServerPathType.File,url);
|
|
}else{
|
|
return new WebServerPathEntry(someUrl,WebServerPathType.Directory,url);
|
|
}
|
|
}
|
|
else if (File.Exists(someUrl))
|
|
{
|
|
return new WebServerPathEntry(someUrl,WebServerPathType.File,url);
|
|
}
|
|
else
|
|
{
|
|
return new WebServerPathEntry(someUrl,WebServerPathType.NotFound,url);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<string> ListDirectory(string path)
|
|
{
|
|
string someUrl = Path.Combine(_path,WebUtility.UrlDecode(path.Substring(1)).Replace('/', Path.DirectorySeparatorChar));
|
|
|
|
foreach(var item in Directory.GetDirectories(someUrl))
|
|
{
|
|
yield return $"{Path.GetFileName(item)}/";
|
|
}
|
|
foreach(var item in Directory.GetFiles(someUrl))
|
|
{
|
|
yield return Path.GetFileName(item);
|
|
|
|
}
|
|
}
|
|
public Stream CreateFile(string entry)
|
|
{
|
|
string someUrl = Path.Combine(_path,WebUtility.UrlDecode(entry.Substring(1)).Replace('/', Path.DirectorySeparatorChar));
|
|
|
|
return File.Create(someUrl);
|
|
}
|
|
public Stream Open(WebServerPathEntry entry)
|
|
{
|
|
return File.OpenRead(entry.FileName);
|
|
}
|
|
|
|
public void CreateDirectory(string dir)
|
|
{
|
|
Directory.CreateDirectory(Path.Combine(_path,dir.Substring(1).Replace('/', Path.DirectorySeparatorChar)));
|
|
}
|
|
}
|
|
|
|
public class StaticServerFileHandler : EventArgs
|
|
{
|
|
public string RealPath {get;set;}
|
|
public string CurrentHttpPath {get;set;}
|
|
|
|
public bool HasBeenHandled {get;set;}=false;
|
|
|
|
public ServerContext Context {get;set;}
|
|
|
|
public bool Cancel {get;set;}=false;
|
|
}
|
|
public enum UploadAllowedResponse
|
|
{
|
|
Yes=0,
|
|
NoISentResponse=1,
|
|
No=2
|
|
}
|
|
/// <summary>
|
|
/// Serve static files (doesnt allow listing files)
|
|
/// </summary>
|
|
public class StaticServer : Server
|
|
{
|
|
public bool AllowUpload {get;set;}=false;
|
|
bool allow=false;
|
|
|
|
public Func<ServerContext,UploadAllowedResponse> AllowedToUpload {get;set;}=DefaultAllowedToUpload;
|
|
|
|
public static UploadAllowedResponse DefaultAllowedToUpload(ServerContext ctx)
|
|
{
|
|
return UploadAllowedResponse.Yes;
|
|
}
|
|
private bool? getEnvVar()
|
|
{
|
|
string allowListStr=Environment.GetEnvironmentVariable("TESSES_WEBSERVER_ALLOW_LISTING");
|
|
if(!string.IsNullOrWhiteSpace(allowListStr))
|
|
{
|
|
string allowLst = allowListStr.ToLower();
|
|
if(allowLst == "1" || allowLst == "true" || allowLst == "on" || allowLst == "yes" || allowLst == "y" || allowLst == "t" || allowLst == "allow" || allowLst=="allowed" || allowLst == "a")
|
|
{
|
|
return true;
|
|
}
|
|
if(allowLst == "0" || allowLst == "false" || allowLst == "off" || allowLst == "no" || allowLst == "n" || allowLst == "f" || allowLst == "deny" || allowLst == "denied" || allowLst == "d")
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public bool AllowListingDirectories {get {
|
|
bool? var= getEnvVar();
|
|
if(!var.HasValue)
|
|
{
|
|
return allow;
|
|
}
|
|
return var.Value;
|
|
} set {
|
|
allow=value;
|
|
}}
|
|
public EventHandler<StaticServerFileHandler> FileHandler;
|
|
IServer _server;
|
|
IFileHandler fileHandler;
|
|
IServer _forbidden;
|
|
/// <summary>
|
|
/// construct with filesystem
|
|
/// </summary>
|
|
/// <param name="path">filesystem for server</param>
|
|
public StaticServer(IVirtualFilesystem fs)
|
|
{
|
|
_server = new NotFoundServer();
|
|
fileHandler=new WebServerStyleFilesystemHandler(fs);
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
}
|
|
|
|
/// <summary>
|
|
/// construct with filesystem, custom filenames, and server for not found
|
|
/// </summary>
|
|
/// <param name="path">filesystem 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(IVirtualFilesystem fs,string[] defaultFileNames,IServer notfoundserver)
|
|
{
|
|
|
|
_server = notfoundserver;
|
|
fileHandler=new WebServerStyleFilesystemHandler(fs,defaultFileNames);
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
|
|
}
|
|
/// <summary>
|
|
/// construct with filesystem, custom filenames, and server for not found
|
|
/// </summary>
|
|
/// <param name="path">filesystem for server</param>
|
|
/// <param name="defaultFileNames">like index.html, index.htm, default.html, default.htm</param>
|
|
/// <param name="notfoundserver">404 not found server</param>
|
|
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
|
|
public StaticServer(IVirtualFilesystem fs,string[] defaultFileNames,IServer notfoundserver,bool allowListing)
|
|
{
|
|
_server = notfoundserver;
|
|
fileHandler=new WebServerStyleFilesystemHandler(fs,defaultFileNames);
|
|
AllowListingDirectories = allowListing;
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
|
|
}
|
|
/// <summary>
|
|
/// construct with filesystem, custom filenames, and server for not found, server for forbidden and with option to allow/deny listing directories
|
|
/// </summary>
|
|
/// <param name="path">filesystem for server</param>
|
|
/// <param name="defaultFileNames">like index.html, index.htm, default.html, default.htm</param>
|
|
/// <param name="notfoundserver">404 not found server</param>
|
|
/// <param name="forbidden">server for forbidden files/denied listing</param>
|
|
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
|
|
|
|
public StaticServer(IVirtualFilesystem fs,string[] defaultFileNames,IServer notfoundserver,IServer forbidden,bool allowListing)
|
|
{
|
|
_server = notfoundserver;
|
|
fileHandler= fileHandler=new WebServerStyleFilesystemHandler(fs,defaultFileNames);
|
|
AllowListingDirectories = allowListing;
|
|
_forbidden=forbidden;
|
|
}
|
|
/// <summary>
|
|
/// construct with filesystem, custom filenames and with option to allow/deny listing directories
|
|
/// </summary>
|
|
/// <param name="path">filesystem for server</param>
|
|
/// <param name="defaultFileNames">like index.html, index.htm, default.html, default.htm</param>
|
|
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
|
|
|
|
public StaticServer(IVirtualFilesystem fs,string[] defaultFileNames,bool allowListing)
|
|
{
|
|
_server = new NotFoundServer();
|
|
fileHandler=new WebServerStyleFilesystemHandler(fs,defaultFileNames);
|
|
AllowListingDirectories = allowListing;
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
|
|
}
|
|
/// <summary>
|
|
/// construct with filesystem and with option to allow/deny listing directories
|
|
/// </summary>
|
|
/// <param name="path">filesystem for server</param>
|
|
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
|
|
|
|
public StaticServer(IVirtualFilesystem fs,bool allowListing)
|
|
{
|
|
_server = new NotFoundServer();
|
|
fileHandler=new WebServerStyleFilesystemHandler(fs);
|
|
AllowListingDirectories = allowListing;
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// construct with path
|
|
/// </summary>
|
|
/// <param name="path">directory for server</param>
|
|
public StaticServer(string path)
|
|
{
|
|
_server = new NotFoundServer();
|
|
fileHandler=new WebServerStyleFileHandler(path);
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
|
|
_server = notfoundserver;
|
|
fileHandler=new WebServerStyleFileHandler(path,defaultFileNames);
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
|
|
}
|
|
/// <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>
|
|
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
|
|
public StaticServer(string path,string[] defaultFileNames,IServer notfoundserver,bool allowListing)
|
|
{
|
|
_server = notfoundserver;
|
|
fileHandler=new WebServerStyleFileHandler(path,defaultFileNames);
|
|
AllowListingDirectories = allowListing;
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
|
|
}
|
|
/// <summary>
|
|
/// construct with path, custom filenames, and server for not found, server for forbidden and with option to allow/deny listing directories
|
|
/// </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>
|
|
/// <param name="forbidden">server for forbidden files/denied listing</param>
|
|
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
|
|
|
|
public StaticServer(string path,string[] defaultFileNames,IServer notfoundserver,IServer forbidden,bool allowListing)
|
|
{
|
|
_server = notfoundserver;
|
|
fileHandler=new WebServerStyleFileHandler(path,defaultFileNames);
|
|
AllowListingDirectories = allowListing;
|
|
_forbidden=forbidden;
|
|
}
|
|
/// <summary>
|
|
/// construct with path, custom filenames and with option to allow/deny listing directories
|
|
/// </summary>
|
|
/// <param name="path">directory for server</param>
|
|
/// <param name="defaultFileNames">like index.html, index.htm, default.html, default.htm</param>
|
|
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
|
|
|
|
public StaticServer(string path,string[] defaultFileNames,bool allowListing)
|
|
{
|
|
_server = new NotFoundServer();
|
|
fileHandler=new WebServerStyleFileHandler(path,defaultFileNames);
|
|
AllowListingDirectories = allowListing;
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
|
|
}
|
|
public bool RedirectToRootInsteadOfNotFound {get;set;}=false;
|
|
/// <summary>
|
|
/// construct with path and with option to allow/deny listing directories
|
|
/// </summary>
|
|
/// <param name="path">directory for server</param>
|
|
/// <param name="allowListing">whether to allow listing directory or not (overridable by environment variable with TESSES_WEBSERVER_ALLOW_LISTING=true|false)
|
|
|
|
public StaticServer(string path,bool allowListing)
|
|
{
|
|
_server = new NotFoundServer();
|
|
fileHandler=new WebServerStyleFileHandler(path);
|
|
AllowListingDirectories = allowListing;
|
|
_forbidden = new SameServer("<html><head><title>File {url} not accessable</title></head><body><h1>403 Forbidden</h1><h4>{url}</h4></body></html>",403);
|
|
|
|
}
|
|
public bool DefaultFileExists(string path,out string name)
|
|
{
|
|
return fileHandler.DefaultFileExists(path,out name);
|
|
}
|
|
public override async Task OtherAsync(ServerContext ctx)
|
|
{
|
|
if(ctx.Method == "PUT")
|
|
{
|
|
if(AllowUpload)
|
|
{
|
|
var res=AllowedToUpload(ctx);
|
|
if(res == UploadAllowedResponse.Yes)
|
|
{
|
|
bool fileExists = fileHandler.GetPath(ctx.UrlPath).Type == WebServerPathType.File;
|
|
using(var file = fileHandler.CreateFile(ctx.UrlPath))
|
|
{
|
|
await ctx.ReadToStreamAsync(file);
|
|
}
|
|
ctx.StatusCode = fileExists ? 204 : 201;
|
|
ctx.ResponseHeaders.Add("Content-Location",ctx.UrlPath);
|
|
await ctx.WriteHeadersAsync();
|
|
}else if(res == UploadAllowedResponse.No)
|
|
{
|
|
await _forbidden.GetAsync(ctx);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public override async Task PostAsync(ServerContext ctx)
|
|
{
|
|
|
|
var fileEntry = fileHandler.GetPath(ctx.UrlPath);
|
|
|
|
if(fileEntry.Type == WebServerPathType.Directory)
|
|
{
|
|
if(AllowUpload)
|
|
{
|
|
var res=AllowedToUpload(ctx);
|
|
if(res == UploadAllowedResponse.Yes)
|
|
{
|
|
//upload files and dirs
|
|
|
|
var res0= ctx.ParseBody((fieldName,fileName,ctype)=>{
|
|
return fileHandler.CreateFile($"{ctx.UrlPath}/{fileName.TrimStart('/')}");
|
|
});
|
|
foreach(var item in res0)
|
|
{
|
|
item.Value.Dispose();
|
|
}
|
|
List<string> dirs=new List<string>();
|
|
if(ctx.QueryParams.TryGetValue("mkdir",out dirs))
|
|
{
|
|
foreach(var dir in dirs)
|
|
{
|
|
fileHandler.CreateDirectory($"{ctx.UrlPath}/{dir.TrimStart('/')}");
|
|
}
|
|
}
|
|
|
|
}else if(res == UploadAllowedResponse.No)
|
|
{
|
|
await _forbidden.GetAsync(ctx);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
public override async Task GetAsync(ServerContext ctx)
|
|
{
|
|
var fileEntry = fileHandler.GetPath(ctx.UrlPath);
|
|
try{
|
|
switch(fileEntry.Type)
|
|
{
|
|
case WebServerPathType.File:
|
|
using(var strm = fileHandler.Open(fileEntry))
|
|
await ctx.SendStreamAsync(strm,HeyRed.Mime.MimeTypesMap.GetMimeType(fileEntry.FileName));
|
|
break;
|
|
case WebServerPathType.Directory:
|
|
if(AllowListingDirectories)
|
|
{
|
|
DirectoryLister lister=new DirectoryLister();
|
|
lister.FromDirectory(fileHandler,ctx.UrlPath,ctx.OriginalUrlPath);
|
|
await lister.GetAsync(ctx);
|
|
}
|
|
else
|
|
{
|
|
await _forbidden.GetAsync(ctx);
|
|
}
|
|
break;
|
|
case WebServerPathType.NotFound:
|
|
if(RedirectToRootInsteadOfNotFound)
|
|
{
|
|
|
|
var fileEntry2 = fileHandler.GetPath("/");
|
|
switch(fileEntry2.Type)
|
|
{
|
|
case WebServerPathType.File:
|
|
using(var strm = fileHandler.Open(fileEntry2))
|
|
await ctx.SendStreamAsync(strm,HeyRed.Mime.MimeTypesMap.GetMimeType(fileEntry2.FileName));
|
|
break;
|
|
case WebServerPathType.Directory:
|
|
if(AllowListingDirectories)
|
|
{
|
|
DirectoryLister lister=new DirectoryLister();
|
|
lister.FromDirectory(fileHandler,"/",ctx.OriginalUrlPath);
|
|
await lister.GetAsync(ctx);
|
|
}
|
|
else
|
|
{
|
|
await _forbidden.GetAsync(ctx);
|
|
}
|
|
break;
|
|
case WebServerPathType.NotFound:
|
|
await _server.GetAsync(ctx);
|
|
break;
|
|
}
|
|
}else{
|
|
await _server.GetAsync(ctx);
|
|
}
|
|
break;
|
|
}
|
|
}catch(UnauthorizedAccessException ex)
|
|
{
|
|
_=ex;
|
|
await _forbidden.GetAsync(ctx);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public class DirectoryLister : Server
|
|
{
|
|
public static string GetFileName(string url)
|
|
{
|
|
if(Path.DirectorySeparatorChar == '/')
|
|
{
|
|
return Path.GetFileName(url);
|
|
}
|
|
return Path.GetFileName(url.Replace('/',Path.DirectorySeparatorChar));
|
|
}
|
|
private static string _getFileNameWithoutQuery(string url)
|
|
{
|
|
int r = url.IndexOf('?');
|
|
if(r > -1)
|
|
{
|
|
return GetFileName(url.Remove(r).TrimEnd('/')) + (url.EndsWith("/") ? "/" : "");
|
|
}
|
|
return GetFileName(url.TrimEnd('/')) + (url.EndsWith("/") ? "/" : "");
|
|
}
|
|
static string defaultText=GetEnumerable("Default","",new string[0]);
|
|
|
|
string curText=defaultText;
|
|
|
|
public static string GetEnumerable(string title,string desc,IEnumerable<string> entries)
|
|
{
|
|
/*if(!this.ln.Path.EndsWith("/"))
|
|
{
|
|
SendRedirect(this.ln.Path + '/');
|
|
return;
|
|
}*/
|
|
StringBuilder b=new StringBuilder();
|
|
b.Append($"<!doctype><html><head><title>{HttpUtility.HtmlEncode(title)}</title></head><body><center><h1>{HttpUtility.HtmlEncode(title)}</h1></center><center>{HttpUtility.HtmlEncode(desc)}</center><hr><a href=\"..\">../</a><br>");
|
|
foreach(var entry in entries)
|
|
{
|
|
//<a href=
|
|
b.Append($"<a href=\"{HttpUtility.HtmlAttributeEncode(entry)}\">{HttpUtility.HtmlEncode(_getFileNameWithoutQuery(entry))}</a><br>");
|
|
}
|
|
b.Append("<hr><br><center><a href=\"https://www.nuget.org/packages/Tesses.WebServer\">Tesses.WebServer</a></center></body></html>");
|
|
|
|
return b.ToString();
|
|
}
|
|
public string Title {get;set;}="Directory Listing";
|
|
public string Description {get;set;}="";
|
|
public void FromEnumerable(string title,string desc,IEnumerable<string> entries)
|
|
{
|
|
curText = GetEnumerable(title,desc,entries);
|
|
}
|
|
public void FromDirectory(string title,string desc,string directory,string reqPath)
|
|
{
|
|
List<string> items=new List<string>();
|
|
foreach(var item in Directory.EnumerateDirectories(directory))
|
|
{
|
|
items.Add(item + "/");
|
|
}
|
|
foreach(var item in Directory.EnumerateFiles(directory))
|
|
{
|
|
items.Add(item);
|
|
}
|
|
|
|
FromEnumerable(title,desc,items);
|
|
|
|
|
|
}
|
|
public void FromDirectory(string directory,string reqPath)
|
|
{
|
|
FromDirectory($"Index of {reqPath}","Directory listing",directory,reqPath);
|
|
}
|
|
public void FromDirectory(IFileHandler handler,string reqPath,string ogPath="/")
|
|
{
|
|
FromEnumerable($"Index of {ogPath}","Directory listing",handler.ListDirectory(reqPath));
|
|
}
|
|
|
|
public override async Task GetAsync(ServerContext ctx)
|
|
{
|
|
await ctx.SendTextAsync(curText);
|
|
}
|
|
}
|
|
|
|
/// <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>
|
|
/// use values inside path like this /hello/YOUR_VALUE/somepage
|
|
/// </summary>
|
|
public class PathValueServer : Server
|
|
{
|
|
public IServer Server {get;set;}
|
|
int paths;
|
|
/// <summary>
|
|
/// Construct a PathValueServer with NotFoundServer
|
|
/// </summary>
|
|
/// <param name="paths">How many path parts do you want, eg if your path is /jim/john/joel, 1: jim 2: jim/john 3: jim/john/joel</param>
|
|
public PathValueServer(int paths=1)
|
|
{
|
|
Server = new NotFoundServer();
|
|
this.paths=paths;
|
|
}
|
|
/// <summary>
|
|
/// Construct a PathValueServer with your server
|
|
/// </summary>
|
|
/// <param name="inner">Your server</param>
|
|
/// <param name="paths">How many path parts do you want, eg if your path is /jim/john/joel, 1: jim 2: jim/john 3: jim/john/joel</param>
|
|
public PathValueServer(IServer inner,int paths=1)
|
|
{
|
|
|
|
Server = inner;
|
|
this.paths = paths;
|
|
}
|
|
/// <summary>
|
|
/// Use this inside your inner server to get string value
|
|
/// </summary>
|
|
/// <param name="ctx">the ServerContext</param>
|
|
/// <returns>the string value from the url part</returns>
|
|
public string GetValue(ServerContext ctx)
|
|
{
|
|
lock(kvps)
|
|
{
|
|
if(kvps.ContainsKey(ctx))
|
|
{
|
|
return kvps[ctx];
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
private void SetValue(ServerContext ctx,string val)
|
|
{
|
|
lock(kvps)
|
|
{
|
|
if(kvps.ContainsKey(ctx))
|
|
{
|
|
kvps[ctx] = val;
|
|
}
|
|
else
|
|
{
|
|
kvps.Add(ctx,val);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RemoveValue(ServerContext ctx)
|
|
{
|
|
lock(kvps)
|
|
{
|
|
kvps.Remove(ctx);
|
|
}
|
|
}
|
|
|
|
Dictionary<ServerContext,string> kvps=new Dictionary<ServerContext, string>();
|
|
public override async Task GetAsync(ServerContext ctx)
|
|
{
|
|
string[] path=ctx.UrlPath.Split(new char[]{'/'},System.StringSplitOptions.RemoveEmptyEntries);
|
|
SetValue(ctx,string.Join("/",path.Take(paths)));
|
|
ctx.UrlPath = $"/{string.Join("/",path.Skip(paths))}";
|
|
|
|
await Server.GetAsync(ctx);
|
|
RemoveValue(ctx);
|
|
}
|
|
public override async Task OptionsAsync(ServerContext ctx)
|
|
{
|
|
string[] path=ctx.UrlPath.Split(new char[]{'/'},System.StringSplitOptions.RemoveEmptyEntries);
|
|
SetValue(ctx,string.Join("/",path.Take(paths)));
|
|
ctx.UrlPath = $"/{string.Join("/",path.Skip(paths))}";
|
|
|
|
await Server.OptionsAsync(ctx);
|
|
RemoveValue(ctx);
|
|
|
|
}
|
|
public override async Task OtherAsync(ServerContext ctx)
|
|
{
|
|
string[] path=ctx.UrlPath.Split(new char[]{'/'},System.StringSplitOptions.RemoveEmptyEntries);
|
|
SetValue(ctx,string.Join("/",path.Take(paths)));
|
|
ctx.UrlPath = $"/{string.Join("/",path.Skip(paths))}";
|
|
|
|
await Server.OtherAsync(ctx);
|
|
RemoveValue(ctx);
|
|
|
|
}
|
|
public override async Task PostAsync(ServerContext ctx)
|
|
{
|
|
string[] path=ctx.UrlPath.Split(new char[]{'/'},System.StringSplitOptions.RemoveEmptyEntries);
|
|
SetValue(ctx,string.Join("/",path.Take(paths)));
|
|
ctx.UrlPath = $"/{string.Join("/",path.Skip(paths))}";
|
|
|
|
await Server.PostAsync(ctx);
|
|
RemoveValue(ctx);
|
|
|
|
}
|
|
public override async Task<bool> BeforeAsync(ServerContext ctx)
|
|
{
|
|
var urlPath = ctx.UrlPath;
|
|
string[] path=ctx.UrlPath.Split(new char[]{'/'},System.StringSplitOptions.RemoveEmptyEntries);
|
|
SetValue(ctx,string.Join("/",path.Take(paths)));
|
|
ctx.UrlPath = $"/{string.Join("/",path.Skip(paths))}";
|
|
|
|
var r= await Server.BeforeAsync(ctx);
|
|
ctx.UrlPath = urlPath;
|
|
return r;
|
|
}
|
|
}
|
|
/// <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)
|
|
{
|
|
return svr;
|
|
}
|
|
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)
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
}
|
|
|
|
public static Server FromCallback(HttpActionAsync cb)
|
|
{
|
|
return new CallbackServer(cb);
|
|
}
|
|
private class CallbackServer : Server
|
|
{
|
|
HttpActionAsync cb;
|
|
public CallbackServer(HttpActionAsync cb)
|
|
{
|
|
this.cb = cb;
|
|
}
|
|
|
|
public override async Task GetAsync(ServerContext ctx)
|
|
{
|
|
await cb(ctx);
|
|
}
|
|
public override async Task OptionsAsync(ServerContext ctx)
|
|
{
|
|
await cb(ctx);
|
|
}
|
|
public override async Task OtherAsync(ServerContext ctx)
|
|
{
|
|
await cb(ctx);
|
|
}
|
|
public override async Task PostAsync(ServerContext ctx)
|
|
{
|
|
await cb(ctx);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// mount multiple servers at different url paths
|
|
/// </summary>
|
|
public sealed class MountableServer : Server
|
|
{
|
|
Dictionary<string, IServer> _servers = new Dictionary<string, IServer>();
|
|
public MountableServer(IServer root)
|
|
{
|
|
_root = root;
|
|
}
|
|
public MountableServer() : this(new NotFoundServer())
|
|
{
|
|
|
|
}
|
|
IServer _root;
|
|
private (string Key,IServer Value) GetFromPath(ServerContext ctx)
|
|
{
|
|
//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));
|
|
}
|
|
}
|
|
//Console.WriteLine("HERE WE ARE");
|
|
return ("/",Guaranteed(_root));
|
|
}
|
|
/// <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);
|
|
}
|
|
/// <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();
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
/// <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)
|
|
{
|
|
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);
|
|
|
|
}
|
|
/// <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(' ');
|
|
//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);
|
|
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 && 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;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
public interface IServer
|
|
{
|
|
void AddCors(ServerContext ctx);
|
|
Task<bool> BeforeAsync(ServerContext ctx);
|
|
Task GetAsync(ServerContext ctx);
|
|
Task PostAsync(ServerContext ctx);
|
|
Task OptionsAsync(ServerContext ctx);
|
|
Task OtherAsync(ServerContext ctx);
|
|
}
|
|
|
|
public sealed class ChunkedStream : Stream
|
|
{
|
|
int offset=0;
|
|
byte[] buffer=new byte[0];
|
|
Stream strm;
|
|
bool receive;
|
|
public ChunkedStream(Stream strm, bool receive)
|
|
{
|
|
this.receive = receive;
|
|
this.strm=strm;
|
|
}
|
|
public override bool CanRead => receive;
|
|
|
|
public override bool CanSeek => false;
|
|
|
|
public override bool CanWrite => !receive;
|
|
|
|
public override long Length => throw new NotImplementedException();
|
|
|
|
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
|
|
|
public override void Flush()
|
|
{
|
|
strm.Flush();
|
|
}
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
{
|
|
if(!receive) throw new IOException("Cannot read from a writeonly stream");
|
|
if(EOF) throw new EndOfStreamException();
|
|
if(this.offset>=this.buffer.Length)
|
|
{
|
|
|
|
//we need to read a new one
|
|
StringBuilder b=new StringBuilder();
|
|
int read = 0;
|
|
while((read=strm.ReadByte()) != '\r')
|
|
{
|
|
b.Append((char)read);
|
|
}
|
|
if(read == '\r')
|
|
{
|
|
read=strm.ReadByte();
|
|
if(read != '\n')
|
|
{
|
|
throw new IOException("Must end with \r\n");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new IOException("Must end with \r\n");
|
|
}
|
|
|
|
if(int.TryParse(b.ToString(),System.Globalization.NumberStyles.HexNumber,null,out int val))
|
|
{
|
|
if(val == 0)
|
|
{
|
|
EOF=true;
|
|
if(strm.ReadByte()!='\r' || strm.ReadByte() != '\n')
|
|
{
|
|
throw new IOException("Must end with \r\n");
|
|
}
|
|
return 0;
|
|
}
|
|
this.offset = 0;
|
|
this.buffer=new byte[val];
|
|
strm.Read(this.buffer,0,this.buffer.Length);
|
|
|
|
if(strm.ReadByte()!='\r' || strm.ReadByte() != '\n')
|
|
{
|
|
throw new IOException("Must end with \r\n");
|
|
}
|
|
|
|
}
|
|
}
|
|
count = Math.Min(count,this.buffer.Length-this.offset);
|
|
Array.Copy(this.buffer,this.offset,buffer,offset,count);
|
|
|
|
this.offset+=count;
|
|
return count;
|
|
}
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override void SetLength(long value)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{
|
|
if(receive) throw new IOException("Cannot write to readonly stream");
|
|
if(count == 0 && EOF) return;
|
|
var len = System.Text.Encoding.ASCII.GetBytes($"{count.ToString("X")}\r\n");
|
|
|
|
strm.Write(len,0,len.Length);
|
|
strm.Write(buffer,offset,count);
|
|
strm.Write(crlf,0,crlf.Length);
|
|
|
|
if(count==0) EOF=true;
|
|
}
|
|
private static readonly byte[] crlf = new byte[]{(byte)'\r',(byte)'\n'};
|
|
public bool EOF {get;private set;}=false;
|
|
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
|
{
|
|
if(receive) throw new IOException("Cannot write to readonly stream");
|
|
if(count == 0 && EOF) return;
|
|
var len = System.Text.Encoding.ASCII.GetBytes($"{count.ToString("X")}\r\n");
|
|
|
|
await strm.WriteAsync(len,0,len.Length,cancellationToken);
|
|
await strm.WriteAsync(buffer,offset,count,cancellationToken);
|
|
await strm.WriteAsync(crlf,0,crlf.Length,cancellationToken);
|
|
|
|
if(count==0) EOF=true;
|
|
}
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if(disposing)
|
|
{
|
|
if(!EOF)
|
|
{
|
|
if(!receive)
|
|
Write(crlf,0,0);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
ChangeableServer _server;
|
|
|
|
|
|
TcpListener _listener;
|
|
SslProtocols protocols;
|
|
public HttpServerListener(int port)
|
|
{
|
|
_server=new ChangeableServer();
|
|
_listener = new TcpListener(new IPEndPoint(IPAddress.Any,port));
|
|
https = false;
|
|
PrintUrls=false;
|
|
}
|
|
public HttpServerListener(IPEndPoint endpoint)
|
|
{
|
|
|
|
_server=new ChangeableServer();
|
|
_listener = new TcpListener(endpoint);
|
|
https = false;
|
|
PrintUrls=false;
|
|
|
|
}
|
|
public HttpServerListener(int port,IServer server)
|
|
{
|
|
_server=new ChangeableServer();
|
|
_listener = new TcpListener(new IPEndPoint(IPAddress.Any,port));
|
|
_server.Server=server;
|
|
https = false;
|
|
PrintUrls=false;
|
|
}
|
|
public HttpServerListener(IPEndPoint endPoint,IServer server)
|
|
{
|
|
|
|
_server=new ChangeableServer();
|
|
_listener = new TcpListener(endPoint);
|
|
_server.Server=server;
|
|
https = false;
|
|
PrintUrls=false;
|
|
}
|
|
public HttpServerListener(IServer server)
|
|
{
|
|
_server=new ChangeableServer();
|
|
_listener = new TcpListener(new IPEndPoint(IPAddress.Any, 3251));
|
|
_server.Server = server;
|
|
https = false;
|
|
PrintUrls=false;
|
|
}
|
|
public HttpServerListener()
|
|
{
|
|
_server=new ChangeableServer();
|
|
_listener = new TcpListener(new IPEndPoint(IPAddress.Any, 3251));
|
|
https = false;
|
|
PrintUrls=false;
|
|
}
|
|
public HttpServerListener(int port,IServer server,X509Certificate cert,SslProtocols protocols=SslProtocols.Default)
|
|
{
|
|
_server=new ChangeableServer();
|
|
_listener = new TcpListener(new IPEndPoint(IPAddress.Any,port));
|
|
_server.Server = server;
|
|
https = cert != null;
|
|
this.cert = cert;
|
|
this.protocols=protocols;
|
|
PrintUrls=false;
|
|
|
|
}
|
|
public HttpServerListener(IPEndPoint endpoint,IServer server,X509Certificate cert,SslProtocols protocols=SslProtocols.Default)
|
|
{
|
|
_server=new ChangeableServer();
|
|
_listener = new TcpListener(endpoint);
|
|
_server.Server = server;
|
|
https = cert != null;
|
|
this.cert = cert;
|
|
this.protocols=protocols;
|
|
PrintUrls=false;
|
|
}
|
|
public void Listen()
|
|
{
|
|
ListenAsync().Wait();
|
|
}
|
|
public void Listen(CancellationToken token)
|
|
{
|
|
ListenAsync(token).Wait();
|
|
}
|
|
public async Task ListenAsync()
|
|
{
|
|
await ListenAsync(CancellationToken.None);
|
|
}
|
|
public async Task ListenAsync(CancellationToken token)
|
|
{
|
|
|
|
_listener.Start();
|
|
using (var r = token.Register(() => _listener.Stop())) {
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
try{
|
|
var socket=await _listener.AcceptTcpClientAsync();
|
|
Task.Factory.StartNew(async()=>{
|
|
try{
|
|
await CommunicateHostAsync(socket,()=>{
|
|
return socket.Connected;
|
|
});
|
|
}catch(Exception ex)
|
|
{
|
|
_=ex;
|
|
}
|
|
}).Wait(0);
|
|
|
|
}catch(Exception ex)
|
|
{
|
|
_=ex;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public async Task PushAsync(Stream strm,EndPoint local,EndPoint remote)
|
|
{
|
|
await PushAsync(strm,local,remote,null);
|
|
}
|
|
public async Task PushAsync(Stream strm,EndPoint local,EndPoint remote,Func<bool> isConnected)
|
|
{
|
|
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
|
|
{
|
|
string path = request[1];
|
|
string ver = request[2];
|
|
ctx = new ServerContext(method, strm, path, headers,isConnected);
|
|
ctx.Server =local as IPEndPoint;
|
|
ctx.Client = remote as IPEndPoint;
|
|
_server.AddCors(ctx);
|
|
if(PrintUrls)
|
|
{
|
|
Console.WriteLine(path);
|
|
}
|
|
if (!await _server.BeforeAsync(ctx))
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}catch(Exception ex)
|
|
{
|
|
try
|
|
{
|
|
await ctx.SendExceptionAsync(ex);
|
|
}catch(Exception ex2)
|
|
{
|
|
_ = ex2;
|
|
}
|
|
}
|
|
|
|
}
|
|
public async Task ListenAsync(CancellationToken token,Action<IPEndPoint> endpoint)
|
|
{
|
|
_listener.Start();
|
|
if(endpoint != null)
|
|
{
|
|
endpoint((IPEndPoint)_listener.LocalEndpoint);
|
|
}
|
|
using (var r = token.Register(() => _listener.Stop())) {
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
try{
|
|
var socket=await _listener.AcceptTcpClientAsync();
|
|
await CommunicateHostAsync(socket,()=>{
|
|
return socket.Connected;
|
|
}).ConfigureAwait(false);
|
|
}catch(Exception ex)
|
|
{
|
|
_=ex;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private string ReadHeaders(Stream strm)
|
|
{
|
|
StringBuilder s = new StringBuilder();
|
|
|
|
|
|
while (!s.EndsWith("\r\n\r\n",StringComparison.Ordinal))
|
|
{
|
|
int data = strm.ReadByte();
|
|
if(data == -1)
|
|
{
|
|
break;
|
|
}
|
|
if(data > 127) continue;
|
|
s.Append((char)data); //its ascii now
|
|
}
|
|
|
|
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
|
|
{
|
|
sslStream.AuthenticateAsServer(cert,false,protocols,true);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_ = ex;
|
|
}
|
|
return sslStream;
|
|
}
|
|
return clt.GetStream();
|
|
}
|
|
private async Task CommunicateHostAsync(TcpClient clt,Func<bool> isConnected)
|
|
{
|
|
try{
|
|
//<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))
|
|
{
|
|
|
|
await PushAsync(strm,clt.Client.LocalEndPoint,clt.Client.RemoteEndPoint,isConnected);
|
|
}
|
|
}catch(Exception ex)
|
|
{
|
|
_=ex;
|
|
}
|
|
|
|
}
|
|
|
|
//protected abstract Task<IResult> Get(string url,Dictionary<string,string> headers);
|
|
|
|
//protected abstract Task GetAsync(ServerContext ctx);
|
|
}
|
|
}
|