2017-06-14 01:37:03 +00:00
|
|
|
using System;
|
2017-06-15 07:40:08 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2017-06-27 01:48:57 +00:00
|
|
|
using System.Linq;
|
2017-06-15 07:40:08 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Net;
|
2018-03-13 20:16:59 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2020-03-09 00:26:41 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2023-03-17 08:08:32 +00:00
|
|
|
using Tesses.WebServer;
|
2017-06-14 01:37:03 +00:00
|
|
|
namespace Ooui
|
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
public class UI : Server
|
2017-06-14 01:37:03 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
public bool Verbose {get;set;}=true;
|
|
|
|
private async Task HandleAsync(ServerContext ctx)
|
|
|
|
{
|
|
|
|
string upgrade;
|
|
|
|
if(ctx.RequestHeaders.TryGetFirst("Upgrade",out upgrade) && upgrade == "websocket")
|
|
|
|
{
|
|
|
|
ProcessWebSocketRequest (ctx, CancellationToken.None);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ProcessRequest(ctx,CancellationToken.None);
|
|
|
|
}
|
|
|
|
await Task.FromResult(0);
|
|
|
|
}
|
|
|
|
public override async Task GetAsync(ServerContext ctx)
|
|
|
|
{
|
|
|
|
|
|
|
|
await HandleAsync(ctx);
|
|
|
|
}
|
|
|
|
public override async Task OptionsAsync(ServerContext ctx)
|
|
|
|
{
|
|
|
|
await HandleAsync(ctx);
|
|
|
|
}
|
|
|
|
public override async Task PostAsync(ServerContext ctx)
|
|
|
|
{
|
|
|
|
await HandleAsync(ctx);
|
|
|
|
}
|
|
|
|
public override async Task OtherAsync(ServerContext ctx)
|
|
|
|
{
|
|
|
|
await HandleAsync(ctx);
|
|
|
|
}
|
2018-03-09 23:14:51 +00:00
|
|
|
public const int MaxFps = 30;
|
2018-03-09 20:56:32 +00:00
|
|
|
|
2018-03-10 05:28:39 +00:00
|
|
|
static readonly ManualResetEvent started = new ManualResetEvent (false);
|
2017-12-10 01:02:04 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
//static CancellationTokenSource serverCts;
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2020-03-09 00:26:41 +00:00
|
|
|
static readonly Dictionary<string, PublishedPath> publishedPaths =
|
|
|
|
new Dictionary<string, PublishedPath> ();
|
2017-06-15 07:40:08 +00:00
|
|
|
|
|
|
|
static readonly byte[] clientJsBytes;
|
2017-12-10 01:02:04 +00:00
|
|
|
static readonly string clientJsEtag;
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2017-11-10 05:00:15 +00:00
|
|
|
public static byte[] ClientJsBytes => clientJsBytes;
|
2017-12-10 01:02:04 +00:00
|
|
|
public static string ClientJsEtag => clientJsEtag;
|
2017-11-10 05:00:15 +00:00
|
|
|
|
2018-02-22 00:32:12 +00:00
|
|
|
public static string HeadHtml { get; set; } = @"<link rel=""stylesheet"" href=""https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"" />";
|
|
|
|
public static string BodyHeaderHtml { get; set; } = @"";
|
|
|
|
public static string BodyFooterHtml { get; set; } = @"";
|
2017-06-19 02:12:37 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
static UI ()
|
|
|
|
{
|
|
|
|
var asm = typeof(UI).Assembly;
|
|
|
|
// System.Console.WriteLine("ASM = {0}", asm);
|
|
|
|
// foreach (var n in asm.GetManifestResourceNames()) {
|
|
|
|
// System.Console.WriteLine(" {0}", n);
|
|
|
|
// }
|
|
|
|
using (var s = asm.GetManifestResourceStream ("Ooui.Client.js")) {
|
2017-07-07 19:51:00 +00:00
|
|
|
if (s == null)
|
|
|
|
throw new Exception ("Missing Client.js");
|
2017-06-15 07:40:08 +00:00
|
|
|
using (var r = new StreamReader (s)) {
|
|
|
|
clientJsBytes = Encoding.UTF8.GetBytes (r.ReadToEnd ());
|
|
|
|
}
|
|
|
|
}
|
2019-02-04 22:22:45 +00:00
|
|
|
clientJsEtag = "\"" + Utilities.GetShaHash (clientJsBytes) + "\"";
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 00:26:41 +00:00
|
|
|
class PublishedPath
|
|
|
|
{
|
|
|
|
public string Path { get; }
|
|
|
|
public Regex RegexPath { get; }
|
|
|
|
public RequestHandler Handler { get; }
|
|
|
|
public PublishedPath (string path, RequestHandler handler)
|
|
|
|
{
|
|
|
|
Path = path;
|
2020-03-09 01:33:28 +00:00
|
|
|
RegexPath = new Regex ("^" + path + "$");
|
2020-03-09 00:26:41 +00:00
|
|
|
Handler = handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
void Publish (string path, RequestHandler handler)
|
2017-06-15 07:40:08 +00:00
|
|
|
{
|
2018-03-14 00:45:56 +00:00
|
|
|
//Console.WriteLine ($"PUBLISH {path} {handler}");
|
2020-03-09 00:26:41 +00:00
|
|
|
lock (publishedPaths) publishedPaths[path] = new PublishedPath (path, handler);
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void Publish (string path, Func<UIContext, Element> elementCtor, bool disposeElementWhenDone = true)
|
2017-07-08 02:38:04 +00:00
|
|
|
{
|
2018-04-15 23:30:08 +00:00
|
|
|
Publish (path, new ElementHandler (elementCtor, disposeElementWhenDone));
|
2017-07-08 02:38:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void Publish (string path, Func<Element> elementCtor, bool disposeElementWhenDone = true)
|
2020-03-09 00:26:41 +00:00
|
|
|
{
|
|
|
|
Publish (path, new ElementHandler (_ => elementCtor (), disposeElementWhenDone));
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void Publish (string path, Element element, bool disposeElementWhenDone = true)
|
2017-06-15 07:40:08 +00:00
|
|
|
{
|
2018-04-15 23:30:08 +00:00
|
|
|
Publish (path, () => element, disposeElementWhenDone);
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void PublishFile (string filePath)
|
2017-07-08 02:38:04 +00:00
|
|
|
{
|
|
|
|
var path = "/" + System.IO.Path.GetFileName (filePath);
|
|
|
|
PublishFile (path, filePath);
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void PublishFile (string path, string filePath, string contentType = null)
|
2017-07-08 02:38:04 +00:00
|
|
|
{
|
|
|
|
var data = System.IO.File.ReadAllBytes (filePath);
|
|
|
|
if (contentType == null) {
|
|
|
|
contentType = GuessContentType (path, filePath);
|
|
|
|
}
|
2019-02-04 22:22:45 +00:00
|
|
|
var etag = "\"" + Utilities.GetShaHash (data) + "\"";
|
2017-12-11 01:51:05 +00:00
|
|
|
Publish (path, new DataHandler (data, etag, contentType));
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void PublishFile (string path, byte[] data, string contentType)
|
2017-12-11 01:51:05 +00:00
|
|
|
{
|
2019-02-04 22:22:45 +00:00
|
|
|
var etag = "\"" + Utilities.GetShaHash (data) + "\"";
|
2017-12-11 01:51:05 +00:00
|
|
|
Publish (path, new DataHandler (data, etag, contentType));
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void PublishFile (string path, byte[] data, string etag, string contentType)
|
2017-12-11 01:51:05 +00:00
|
|
|
{
|
|
|
|
Publish (path, new DataHandler (data, etag, contentType));
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public bool TryGetFileContentAtPath (string path, out FileContent file)
|
2017-12-11 01:51:05 +00:00
|
|
|
{
|
2020-03-09 00:26:41 +00:00
|
|
|
PublishedPath pp;
|
2017-12-11 01:51:05 +00:00
|
|
|
lock (publishedPaths) {
|
2020-03-09 00:26:41 +00:00
|
|
|
if (!publishedPaths.TryGetValue (path, out pp)) {
|
2017-12-11 01:51:05 +00:00
|
|
|
file = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 00:26:41 +00:00
|
|
|
if (pp.Handler is DataHandler dh) {
|
2017-12-11 01:51:05 +00:00
|
|
|
file = new FileContent {
|
|
|
|
Etag = dh.Etag,
|
|
|
|
Content = dh.Data,
|
|
|
|
ContentType = dh.ContentType,
|
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
file = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class FileContent
|
|
|
|
{
|
|
|
|
public string ContentType { get; set; }
|
|
|
|
public string Etag { get; set; }
|
|
|
|
public byte[] Content { get; set; }
|
2017-07-08 03:27:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void PublishJson (string path, Func<object> ctor)
|
2020-03-09 00:26:41 +00:00
|
|
|
{
|
|
|
|
Publish (path, new JsonHandler (_ => ctor ()));
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void PublishJson (string path, Func<UIContext, object> ctor)
|
2017-07-08 03:27:03 +00:00
|
|
|
{
|
|
|
|
Publish (path, new JsonHandler (ctor));
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void PublishJson (string path, object value)
|
2017-07-08 03:27:03 +00:00
|
|
|
{
|
|
|
|
var data = JsonHandler.GetData (value);
|
2019-02-04 22:22:45 +00:00
|
|
|
var etag = "\"" + Utilities.GetShaHash (data) + "\"";
|
2017-12-11 01:51:05 +00:00
|
|
|
Publish (path, new DataHandler (data, etag, JsonHandler.ContentType));
|
2017-07-08 02:38:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public void PublishCustomResponse (string path, Action<ServerContext, CancellationToken> responder)
|
2017-08-23 03:26:01 +00:00
|
|
|
{
|
|
|
|
Publish (path, new CustomHandler (responder));
|
|
|
|
}
|
|
|
|
|
2017-07-08 02:38:04 +00:00
|
|
|
static string GuessContentType (string path, string filePath)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
// public static void WaitUntilStarted () => started.WaitOne ();
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
void ProcessRequest (ServerContext listenerContext, CancellationToken token)
|
2017-06-15 07:40:08 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
var path = listenerContext.UrlPath;
|
|
|
|
|
|
|
|
if(this.Verbose)
|
|
|
|
Console.WriteLine ($"{listenerContext.Method} {path}");
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2017-06-19 02:12:37 +00:00
|
|
|
if (path == "/ooui.js") {
|
2023-03-17 08:08:32 +00:00
|
|
|
string inm="";
|
|
|
|
listenerContext.RequestHeaders.TryGetFirst ("If-None-Match",out inm);
|
|
|
|
if ( string.IsNullOrEmpty (inm) || inm != clientJsEtag) {
|
|
|
|
listenerContext.StatusCode = 200;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
listenerContext.ResponseHeaders.Add ("Cache-Control", "public, max-age=60");
|
|
|
|
listenerContext.ResponseHeaders.Add ("Etag", clientJsEtag);
|
|
|
|
|
|
|
|
listenerContext.SendBytes(clientJsBytes,"appllication/json");
|
|
|
|
|
2017-12-10 01:02:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-03-17 08:08:32 +00:00
|
|
|
listenerContext.StatusCode=304;
|
|
|
|
listenerContext.WriteHeaders();
|
|
|
|
listenerContext.NetworkStream.Close();
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-06-16 00:35:20 +00:00
|
|
|
var found = false;
|
2020-03-09 00:26:41 +00:00
|
|
|
PublishedPath pp;
|
|
|
|
var variables = new Dictionary<string, string> ();
|
|
|
|
lock (publishedPaths) found = publishedPaths.TryGetValue (path, out pp);
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
// Try regex
|
|
|
|
List<PublishedPath> pps;
|
|
|
|
lock (publishedPaths) pps = publishedPaths.Values.ToList();
|
|
|
|
foreach (var p in pps) {
|
|
|
|
var m = p.RegexPath.Match (path);
|
|
|
|
if (m.Success) {
|
|
|
|
pp = p;
|
|
|
|
found = true;
|
|
|
|
for (var ig = 1; ig < m.Groups.Count; ig++) {
|
|
|
|
variables[p.RegexPath.GroupNameFromNumber(ig)] = m.Groups[ig].Value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 00:35:20 +00:00
|
|
|
if (found) {
|
2017-07-06 23:01:21 +00:00
|
|
|
try {
|
2020-03-09 00:26:41 +00:00
|
|
|
var context = UIContext.ForListenerContext (listenerContext, variables);
|
|
|
|
pp.Handler.Respond (listenerContext, context, token);
|
2017-07-06 23:01:21 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
2017-07-06 23:12:34 +00:00
|
|
|
Error ("Handler failed to respond", ex);
|
2017-07-06 23:01:21 +00:00
|
|
|
try {
|
2023-03-17 08:08:32 +00:00
|
|
|
listenerContext.StatusCode = 500;
|
|
|
|
listenerContext.WriteHeaders();
|
|
|
|
listenerContext.NetworkStream.Close ();
|
2017-07-06 23:01:21 +00:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
// Ignore ending the response errors
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 00:35:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-03-17 08:08:32 +00:00
|
|
|
listenerContext.StatusCode = 500;
|
|
|
|
listenerContext.WriteHeaders();
|
|
|
|
listenerContext.NetworkStream.Close ();
|
|
|
|
|
2017-06-16 00:35:20 +00:00
|
|
|
}
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 23:01:21 +00:00
|
|
|
abstract class RequestHandler
|
2017-06-19 02:12:37 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
public abstract void Respond (ServerContext listenerContext, UIContext context, CancellationToken token);
|
2017-06-19 02:12:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 23:01:21 +00:00
|
|
|
class ElementHandler : RequestHandler
|
2017-06-15 07:40:08 +00:00
|
|
|
{
|
2020-03-09 00:26:41 +00:00
|
|
|
readonly Func<UIContext, Element> ctor;
|
2017-07-06 23:01:21 +00:00
|
|
|
|
2018-04-15 23:30:08 +00:00
|
|
|
public bool DisposeElementWhenDone { get; }
|
|
|
|
|
2020-03-09 00:26:41 +00:00
|
|
|
public ElementHandler (Func<UIContext, Element> ctor, bool disposeElementWhenDone)
|
2017-07-06 23:01:21 +00:00
|
|
|
{
|
2020-03-09 00:26:41 +00:00
|
|
|
this.ctor = ctor;
|
2018-04-15 23:30:08 +00:00
|
|
|
DisposeElementWhenDone = disposeElementWhenDone;
|
2017-07-06 23:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 00:26:41 +00:00
|
|
|
public Element GetElement (UIContext context) => ctor (context);
|
2017-07-06 23:01:21 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public override void Respond (ServerContext listenerContext, UIContext context, CancellationToken token)
|
2017-07-06 23:01:21 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
var path = listenerContext.UrlPath;
|
|
|
|
|
|
|
|
listenerContext.StatusCode = 200;
|
|
|
|
listenerContext.SendText(RenderTemplate (path));
|
|
|
|
|
2017-07-06 23:01:21 +00:00
|
|
|
}
|
2017-11-10 03:34:37 +00:00
|
|
|
}
|
2017-07-06 23:01:21 +00:00
|
|
|
|
2018-02-02 04:18:16 +00:00
|
|
|
public static string RenderTemplate (string webSocketPath, string title = "", string initialHtml = "")
|
2017-11-10 03:34:37 +00:00
|
|
|
{
|
2018-02-22 00:32:12 +00:00
|
|
|
using (var w = new System.IO.StringWriter ()) {
|
|
|
|
RenderTemplate (w, webSocketPath, title, initialHtml);
|
|
|
|
return w.ToString ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 05:28:39 +00:00
|
|
|
static string EscapeHtml (string text)
|
|
|
|
{
|
|
|
|
return text.Replace ("&", "&").Replace ("<", "<");
|
|
|
|
}
|
|
|
|
|
2018-02-22 00:32:12 +00:00
|
|
|
public static void RenderTemplate (TextWriter writer, string webSocketPath, string title, string initialHtml)
|
|
|
|
{
|
|
|
|
writer.Write (@"<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>");
|
2018-03-10 05:28:39 +00:00
|
|
|
writer.Write (EscapeHtml (title));
|
2018-02-22 00:32:12 +00:00
|
|
|
writer.Write (@"</title>
|
|
|
|
<meta name=""viewport"" content=""width=device-width, initial-scale=1"" />
|
|
|
|
");
|
|
|
|
writer.WriteLine (HeadHtml);
|
|
|
|
writer.WriteLine (@" <style>");
|
|
|
|
writer.WriteLine (rules.ToString ());
|
|
|
|
writer.WriteLine (@" </style>
|
|
|
|
</head>
|
|
|
|
<body>");
|
|
|
|
writer.WriteLine (BodyHeaderHtml);
|
2018-04-16 04:21:45 +00:00
|
|
|
writer.WriteLine (@"<div id=""ooui-body"" class=""container-fluid"" style=""padding:0;margin:0"">");
|
2018-02-22 00:32:12 +00:00
|
|
|
writer.WriteLine (initialHtml);
|
|
|
|
writer.Write (@"</div>
|
|
|
|
|
|
|
|
<script src=""/ooui.js""></script>
|
|
|
|
<script>ooui(""");
|
|
|
|
writer.Write (webSocketPath);
|
|
|
|
writer.WriteLine (@""");</script>");
|
|
|
|
writer.WriteLine (BodyFooterHtml);
|
|
|
|
writer.WriteLine (@"</body>
|
|
|
|
</html>");
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-08 02:38:04 +00:00
|
|
|
class DataHandler : RequestHandler
|
|
|
|
{
|
|
|
|
readonly byte[] data;
|
2017-12-11 01:51:05 +00:00
|
|
|
readonly string etag;
|
2017-07-08 02:38:04 +00:00
|
|
|
readonly string contentType;
|
|
|
|
|
2017-12-11 01:51:05 +00:00
|
|
|
public byte[] Data => data;
|
|
|
|
public string Etag => etag;
|
|
|
|
public string ContentType => contentType;
|
|
|
|
|
|
|
|
public DataHandler (byte[] data, string etag, string contentType = null)
|
2017-07-08 02:38:04 +00:00
|
|
|
{
|
|
|
|
this.data = data;
|
2017-12-11 01:51:05 +00:00
|
|
|
this.etag = etag;
|
2017-07-08 02:38:04 +00:00
|
|
|
this.contentType = contentType;
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public override void Respond (ServerContext listenerContext, UIContext context, CancellationToken token)
|
2017-07-08 02:38:04 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
var url = listenerContext.UrlPath;
|
|
|
|
|
|
|
|
|
|
|
|
string inm="";
|
|
|
|
if (listenerContext.RequestHeaders.TryGetFirst("If-None-Match",out inm) && !string.IsNullOrEmpty (inm) && inm == etag) {
|
|
|
|
listenerContext.StatusCode = 304;
|
|
|
|
listenerContext.WriteHeaders();
|
|
|
|
listenerContext.NetworkStream.Close();
|
2017-12-11 01:51:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-03-17 08:08:32 +00:00
|
|
|
string content_type = "application/octet-stream";
|
|
|
|
listenerContext.StatusCode = 200;
|
|
|
|
listenerContext.ResponseHeaders.Add ("Etag", etag);
|
2017-12-11 01:51:05 +00:00
|
|
|
if (!string.IsNullOrEmpty (contentType))
|
2023-03-17 08:08:32 +00:00
|
|
|
content_type= contentType;
|
|
|
|
listenerContext.SendBytes(data,content_type);
|
2017-07-08 03:27:03 +00:00
|
|
|
}
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-07-08 03:27:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class JsonHandler : RequestHandler
|
|
|
|
{
|
|
|
|
public const string ContentType = "application/json; charset=utf-8";
|
|
|
|
|
2020-03-09 00:26:41 +00:00
|
|
|
readonly Func<UIContext, object> ctor;
|
2017-07-08 03:27:03 +00:00
|
|
|
|
2020-03-09 00:26:41 +00:00
|
|
|
public JsonHandler (Func<UIContext, object> ctor)
|
2017-07-08 03:27:03 +00:00
|
|
|
{
|
|
|
|
this.ctor = ctor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] GetData (object obj)
|
|
|
|
{
|
2018-03-08 21:28:28 +00:00
|
|
|
var r = Ooui.JsonConvert.SerializeObject (obj);
|
|
|
|
var e = new UTF8Encoding (false);
|
|
|
|
return e.GetBytes (r);
|
2017-07-08 03:27:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public override void Respond (ServerContext listenerContext, UIContext context, CancellationToken token)
|
2017-07-08 03:27:03 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-07-08 03:27:03 +00:00
|
|
|
|
2020-03-09 00:26:41 +00:00
|
|
|
var data = GetData (ctor (context));
|
2017-07-08 03:27:03 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
listenerContext.StatusCode = 200;
|
|
|
|
|
|
|
|
listenerContext.SendBytes(data,ContentType);
|
2017-07-08 02:38:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 03:26:01 +00:00
|
|
|
class CustomHandler : RequestHandler
|
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
readonly Action<ServerContext, CancellationToken> responder;
|
2017-08-23 03:26:01 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public CustomHandler (Action<ServerContext, CancellationToken> responder)
|
2017-08-23 03:26:01 +00:00
|
|
|
{
|
|
|
|
this.responder = responder;
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public override void Respond (ServerContext listenerContext, UIContext context, CancellationToken token)
|
2017-08-23 03:26:01 +00:00
|
|
|
{
|
|
|
|
responder (listenerContext, token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
void ProcessWebSocketRequest (ServerContext listenerContext, CancellationToken serverToken)
|
2017-06-15 07:40:08 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
//
|
|
|
|
// Find the element
|
|
|
|
//
|
2023-03-17 08:08:32 +00:00
|
|
|
var path = listenerContext.UrlPath;
|
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2020-03-09 00:26:41 +00:00
|
|
|
PublishedPath pp;
|
2017-06-16 00:35:20 +00:00
|
|
|
var found = false;
|
2020-03-09 00:26:41 +00:00
|
|
|
var variables = new Dictionary<string, string> ();
|
|
|
|
|
|
|
|
lock (publishedPaths) found = publishedPaths.TryGetValue (path, out pp);
|
|
|
|
if (!found) {
|
|
|
|
List<PublishedPath> pps;
|
|
|
|
lock (publishedPaths) pps = publishedPaths.Values.ToList ();
|
|
|
|
foreach (var p in pps) {
|
|
|
|
var m = p.RegexPath.Match (path);
|
|
|
|
if (m.Success) {
|
|
|
|
pp = p;
|
|
|
|
found = true;
|
2020-03-09 01:45:21 +00:00
|
|
|
for (var ig = 1; ig < m.Groups.Count; ig++) {
|
|
|
|
variables[p.RegexPath.GroupNameFromNumber (ig)] = m.Groups[ig].Value;
|
|
|
|
}
|
2020-03-09 00:26:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var elementHandler = pp?.Handler as ElementHandler;
|
2017-07-06 23:01:21 +00:00
|
|
|
if (!found || elementHandler == null) {
|
2023-03-17 08:08:32 +00:00
|
|
|
listenerContext.StatusCode = 404;
|
|
|
|
listenerContext.WriteHeaders();
|
|
|
|
listenerContext.NetworkStream.Close();
|
2017-06-15 07:40:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element element = null;
|
2018-04-15 23:30:08 +00:00
|
|
|
bool disposeElementWhenDone = true;
|
2017-06-15 07:40:08 +00:00
|
|
|
try {
|
2020-03-09 00:26:41 +00:00
|
|
|
var context = UIContext.ForListenerContext (listenerContext, variables);
|
2024-03-26 17:45:36 +00:00
|
|
|
context.QueryParams = listenerContext.QueryParams;
|
2020-03-09 00:26:41 +00:00
|
|
|
element = elementHandler.GetElement (context);
|
2018-04-15 23:30:08 +00:00
|
|
|
disposeElementWhenDone = elementHandler.DisposeElementWhenDone;
|
2017-11-09 07:57:04 +00:00
|
|
|
|
|
|
|
if (element == null)
|
|
|
|
throw new Exception ("Handler returned a null element");
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
2023-03-17 08:08:32 +00:00
|
|
|
listenerContext.StatusCode = 500;
|
|
|
|
listenerContext.WriteHeaders();
|
|
|
|
listenerContext.NetworkStream.Close();
|
2017-06-15 07:40:08 +00:00
|
|
|
Error ("Failed to create element", ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Connect the web socket
|
|
|
|
//
|
2023-03-17 08:08:32 +00:00
|
|
|
//listenerContext.RequestHeaders.Add("Sec-WebSocket-Protocol","ooui");
|
|
|
|
|
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
|
2017-12-09 23:34:48 +00:00
|
|
|
//
|
|
|
|
// Set the element's dimensions
|
|
|
|
//
|
2023-03-17 08:08:32 +00:00
|
|
|
var query = listenerContext.QueryParams;
|
|
|
|
if (!query.TryGetFirst ("w", out var wValue) || string.IsNullOrEmpty (wValue)) {
|
2017-12-09 23:34:48 +00:00
|
|
|
wValue = "640";
|
|
|
|
}
|
2023-03-17 08:08:32 +00:00
|
|
|
if (!query.TryGetFirst ("h", out var hValue) || string.IsNullOrEmpty (hValue)) {
|
2017-12-09 23:34:48 +00:00
|
|
|
hValue = "480";
|
|
|
|
}
|
|
|
|
var icult = System.Globalization.CultureInfo.InvariantCulture;
|
|
|
|
if (!double.TryParse (wValue, System.Globalization.NumberStyles.Any, icult, out var w))
|
|
|
|
w = 640;
|
|
|
|
if (!double.TryParse (hValue, System.Globalization.NumberStyles.Any, icult, out var h))
|
|
|
|
h = 480;
|
|
|
|
|
2017-06-15 09:39:19 +00:00
|
|
|
//
|
2017-06-16 04:21:38 +00:00
|
|
|
// Create a new session and let it handle everything from here
|
2017-06-15 07:40:08 +00:00
|
|
|
//
|
|
|
|
try {
|
2023-03-17 13:58:08 +00:00
|
|
|
var session = new WebSocketSession (element, disposeElementWhenDone, w, h, Error, serverToken);
|
2023-03-17 08:08:32 +00:00
|
|
|
listenerContext.StartWebSocketConnection(session.Opened,
|
|
|
|
session.Arrived,
|
|
|
|
session.Closed); }
|
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
catch (Exception ex) {
|
2017-06-15 09:39:19 +00:00
|
|
|
Error ("Web socket failed", ex);
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
void Error (string message, Exception ex)
|
2017-06-14 01:37:03 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
if(!Verbose) return;
|
2017-06-15 07:40:08 +00:00
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
2023-03-17 08:08:32 +00:00
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
Console.WriteLine ("{0}: {1}", message, ex);
|
|
|
|
Console.ResetColor ();
|
2017-06-14 01:37:03 +00:00
|
|
|
}
|
2017-06-16 04:21:38 +00:00
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
|
|
|
|
|
2017-06-27 01:48:57 +00:00
|
|
|
|
2018-03-09 20:56:32 +00:00
|
|
|
|
|
|
|
static readonly Dictionary<string, Style> styles =
|
|
|
|
new Dictionary<string, Style> ();
|
|
|
|
static readonly StyleSelectors rules = new StyleSelectors ();
|
|
|
|
|
|
|
|
public static StyleSelectors Styles => rules;
|
|
|
|
|
2017-06-27 01:48:57 +00:00
|
|
|
public class StyleSelectors
|
|
|
|
{
|
|
|
|
public Style this[string selector] {
|
|
|
|
get {
|
|
|
|
var key = selector ?? "";
|
|
|
|
lock (styles) {
|
2017-07-06 23:12:34 +00:00
|
|
|
if (!styles.TryGetValue (key, out Style r)) {
|
2017-06-27 01:48:57 +00:00
|
|
|
r = new Style ();
|
|
|
|
styles.Add (key, r);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
var key = selector ?? "";
|
|
|
|
lock (styles) {
|
|
|
|
if (value == null) {
|
|
|
|
styles.Remove (key);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
styles[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Clear ()
|
|
|
|
{
|
|
|
|
lock (styles) {
|
|
|
|
styles.Clear ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
lock (styles) {
|
|
|
|
var q =
|
|
|
|
from s in styles
|
|
|
|
let v = s.Value.ToString ()
|
|
|
|
where v.Length > 0
|
|
|
|
select s.Key + " {" + s.Value.ToString () + "}";
|
|
|
|
return String.Join ("\n", q);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-14 01:37:03 +00:00
|
|
|
}
|
2020-03-09 00:26:41 +00:00
|
|
|
|
|
|
|
public class UIContext
|
|
|
|
{
|
2024-03-26 17:45:36 +00:00
|
|
|
public Dictionary<string,List<string>> QueryParams {get;set;}=new Dictionary<string, List<string>>();
|
2020-03-09 00:26:41 +00:00
|
|
|
readonly Dictionary<string, string> variables = new Dictionary<string, string> ();
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public string RequestUrl { get; }
|
2020-03-09 00:26:41 +00:00
|
|
|
|
|
|
|
public string this[string key] => variables.TryGetValue (key, out var v) ? v : "";
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public UIContext (string requestUrl)
|
2020-03-09 00:26:41 +00:00
|
|
|
{
|
|
|
|
RequestUrl = requestUrl ?? throw new ArgumentNullException (nameof (requestUrl));
|
|
|
|
}
|
|
|
|
|
2023-03-17 08:08:32 +00:00
|
|
|
public static UIContext ForListenerContext (ServerContext listenerContext, Dictionary<string, string> variables)
|
2020-03-09 00:26:41 +00:00
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
var c = new UIContext (listenerContext.UrlPath);
|
2020-03-09 00:26:41 +00:00
|
|
|
foreach (var kv in variables) {
|
|
|
|
c.variables.Add (kv.Key, kv.Value);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static UIContext ForWebAssemblySession (string sessionId, string elementPath)
|
|
|
|
{
|
2023-03-17 08:08:32 +00:00
|
|
|
throw new NotSupportedException("Not supported on this fork");
|
2020-03-09 00:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 01:37:03 +00:00
|
|
|
}
|