Doing some fixup

This commit is contained in:
2024-03-30 20:01:24 -05:00
parent ee57c7a3ca
commit d07cd21b98
7 changed files with 401 additions and 13 deletions
+265 -2
View File
@@ -15,6 +15,7 @@ using System.Security.Authentication;
using System.Web;
using Tesses.VirtualFilesystem;
using System.Net.Mime;
using System.Numerics;
namespace Tesses.WebServer
{
@@ -43,6 +44,41 @@ namespace Tesses.WebServer
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;
@@ -116,7 +152,11 @@ namespace Tesses.WebServer
try{
EventHandler<SendEventArgs> cb= (sender,e0)=>{
if(__connected)
ctx.NetworkStream.Write($"data: {e0.Data}\n\n");
{
ctx.NetworkStream.Write($"data: {e0.Data}\n\n");
ctx.NetworkStream.Flush();
}
};
evt.EventReceived += cb;
while(ctx.Connected);
@@ -483,6 +523,229 @@ namespace Tesses.WebServer
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>
@@ -1948,7 +2211,7 @@ namespace Tesses.WebServer
}
count = Math.Min(count,this.buffer.Length-this.offset);
Array.Copy(this.buffer,this.offset,buffer,offset,count);
this.offset+=count;
return count;
}