Support published regex urls
This commit is contained in:
parent
ce22521c7b
commit
6914ccbc86
148
Ooui/UI.cs
148
Ooui/UI.cs
|
@ -7,6 +7,7 @@ using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Ooui
|
namespace Ooui
|
||||||
{
|
{
|
||||||
|
@ -18,8 +19,8 @@ namespace Ooui
|
||||||
|
|
||||||
static CancellationTokenSource serverCts;
|
static CancellationTokenSource serverCts;
|
||||||
|
|
||||||
static readonly Dictionary<string, RequestHandler> publishedPaths =
|
static readonly Dictionary<string, PublishedPath> publishedPaths =
|
||||||
new Dictionary<string, RequestHandler> ();
|
new Dictionary<string, PublishedPath> ();
|
||||||
|
|
||||||
static readonly byte[] clientJsBytes;
|
static readonly byte[] clientJsBytes;
|
||||||
static readonly string clientJsEtag;
|
static readonly string clientJsEtag;
|
||||||
|
@ -88,16 +89,34 @@ namespace Ooui
|
||||||
clientJsEtag = "\"" + Utilities.GetShaHash (clientJsBytes) + "\"";
|
clientJsEtag = "\"" + Utilities.GetShaHash (clientJsBytes) + "\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class PublishedPath
|
||||||
|
{
|
||||||
|
public string Path { get; }
|
||||||
|
public Regex RegexPath { get; }
|
||||||
|
public RequestHandler Handler { get; }
|
||||||
|
public PublishedPath (string path, RequestHandler handler)
|
||||||
|
{
|
||||||
|
Path = path;
|
||||||
|
RegexPath = new Regex (path);
|
||||||
|
Handler = handler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void Publish (string path, RequestHandler handler)
|
static void Publish (string path, RequestHandler handler)
|
||||||
{
|
{
|
||||||
//Console.WriteLine ($"PUBLISH {path} {handler}");
|
//Console.WriteLine ($"PUBLISH {path} {handler}");
|
||||||
lock (publishedPaths) publishedPaths[path] = handler;
|
lock (publishedPaths) publishedPaths[path] = new PublishedPath (path, handler);
|
||||||
Start ();
|
Start ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Publish (string path, Func<UIContext, Element> elementCtor, bool disposeElementWhenDone = true)
|
||||||
|
{
|
||||||
|
Publish (path, new ElementHandler (elementCtor, disposeElementWhenDone));
|
||||||
|
}
|
||||||
|
|
||||||
public static void Publish (string path, Func<Element> elementCtor, bool disposeElementWhenDone = true)
|
public static void Publish (string path, Func<Element> elementCtor, bool disposeElementWhenDone = true)
|
||||||
{
|
{
|
||||||
Publish (path, new ElementHandler (elementCtor, disposeElementWhenDone));
|
Publish (path, new ElementHandler (_ => elementCtor (), disposeElementWhenDone));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Publish (string path, Element element, bool disposeElementWhenDone = true)
|
public static void Publish (string path, Element element, bool disposeElementWhenDone = true)
|
||||||
|
@ -134,14 +153,14 @@ namespace Ooui
|
||||||
|
|
||||||
public static bool TryGetFileContentAtPath (string path, out FileContent file)
|
public static bool TryGetFileContentAtPath (string path, out FileContent file)
|
||||||
{
|
{
|
||||||
RequestHandler handler;
|
PublishedPath pp;
|
||||||
lock (publishedPaths) {
|
lock (publishedPaths) {
|
||||||
if (!publishedPaths.TryGetValue (path, out handler)) {
|
if (!publishedPaths.TryGetValue (path, out pp)) {
|
||||||
file = null;
|
file = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (handler is DataHandler dh) {
|
if (pp.Handler is DataHandler dh) {
|
||||||
file = new FileContent {
|
file = new FileContent {
|
||||||
Etag = dh.Etag,
|
Etag = dh.Etag,
|
||||||
Content = dh.Data,
|
Content = dh.Data,
|
||||||
|
@ -161,6 +180,11 @@ namespace Ooui
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void PublishJson (string path, Func<object> ctor)
|
public static void PublishJson (string path, Func<object> ctor)
|
||||||
|
{
|
||||||
|
Publish (path, new JsonHandler (_ => ctor ()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void PublishJson (string path, Func<UIContext, object> ctor)
|
||||||
{
|
{
|
||||||
Publish (path, new JsonHandler (ctor));
|
Publish (path, new JsonHandler (ctor));
|
||||||
}
|
}
|
||||||
|
@ -301,11 +325,30 @@ namespace Ooui
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var found = false;
|
var found = false;
|
||||||
RequestHandler handler;
|
PublishedPath pp;
|
||||||
lock (publishedPaths) found = publishedPaths.TryGetValue (path, out handler);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (found) {
|
if (found) {
|
||||||
try {
|
try {
|
||||||
handler.Respond (listenerContext, token);
|
var context = UIContext.ForListenerContext (listenerContext, variables);
|
||||||
|
pp.Handler.Respond (listenerContext, context, token);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
Error ("Handler failed to respond", ex);
|
Error ("Handler failed to respond", ex);
|
||||||
|
@ -327,24 +370,24 @@ namespace Ooui
|
||||||
|
|
||||||
abstract class RequestHandler
|
abstract class RequestHandler
|
||||||
{
|
{
|
||||||
public abstract void Respond (HttpListenerContext listenerContext, CancellationToken token);
|
public abstract void Respond (HttpListenerContext listenerContext, UIContext context, CancellationToken token);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ElementHandler : RequestHandler
|
class ElementHandler : RequestHandler
|
||||||
{
|
{
|
||||||
readonly Lazy<Element> element;
|
readonly Func<UIContext, Element> ctor;
|
||||||
|
|
||||||
public bool DisposeElementWhenDone { get; }
|
public bool DisposeElementWhenDone { get; }
|
||||||
|
|
||||||
public ElementHandler (Func<Element> ctor, bool disposeElementWhenDone)
|
public ElementHandler (Func<UIContext, Element> ctor, bool disposeElementWhenDone)
|
||||||
{
|
{
|
||||||
element = new Lazy<Element> (ctor);
|
this.ctor = ctor;
|
||||||
DisposeElementWhenDone = disposeElementWhenDone;
|
DisposeElementWhenDone = disposeElementWhenDone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element GetElement () => element.Value;
|
public Element GetElement (UIContext context) => ctor (context);
|
||||||
|
|
||||||
public override void Respond (HttpListenerContext listenerContext, CancellationToken token)
|
public override void Respond (HttpListenerContext listenerContext, UIContext context, CancellationToken token)
|
||||||
{
|
{
|
||||||
var url = listenerContext.Request.Url;
|
var url = listenerContext.Request.Url;
|
||||||
var path = url.LocalPath;
|
var path = url.LocalPath;
|
||||||
|
@ -422,7 +465,7 @@ namespace Ooui
|
||||||
this.contentType = contentType;
|
this.contentType = contentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Respond (HttpListenerContext listenerContext, CancellationToken token)
|
public override void Respond (HttpListenerContext listenerContext, UIContext context, CancellationToken token)
|
||||||
{
|
{
|
||||||
var url = listenerContext.Request.Url;
|
var url = listenerContext.Request.Url;
|
||||||
var path = url.LocalPath;
|
var path = url.LocalPath;
|
||||||
|
@ -451,9 +494,9 @@ namespace Ooui
|
||||||
{
|
{
|
||||||
public const string ContentType = "application/json; charset=utf-8";
|
public const string ContentType = "application/json; charset=utf-8";
|
||||||
|
|
||||||
readonly Func<object> ctor;
|
readonly Func<UIContext, object> ctor;
|
||||||
|
|
||||||
public JsonHandler (Func<object> ctor)
|
public JsonHandler (Func<UIContext, object> ctor)
|
||||||
{
|
{
|
||||||
this.ctor = ctor;
|
this.ctor = ctor;
|
||||||
}
|
}
|
||||||
|
@ -465,11 +508,11 @@ namespace Ooui
|
||||||
return e.GetBytes (r);
|
return e.GetBytes (r);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Respond (HttpListenerContext listenerContext, CancellationToken token)
|
public override void Respond (HttpListenerContext listenerContext, UIContext context, CancellationToken token)
|
||||||
{
|
{
|
||||||
var response = listenerContext.Response;
|
var response = listenerContext.Response;
|
||||||
|
|
||||||
var data = GetData (ctor ());
|
var data = GetData (ctor (context));
|
||||||
|
|
||||||
response.StatusCode = 200;
|
response.StatusCode = 200;
|
||||||
response.ContentType = ContentType;
|
response.ContentType = ContentType;
|
||||||
|
@ -491,7 +534,7 @@ namespace Ooui
|
||||||
this.responder = responder;
|
this.responder = responder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Respond (HttpListenerContext listenerContext, CancellationToken token)
|
public override void Respond (HttpListenerContext listenerContext, UIContext context, CancellationToken token)
|
||||||
{
|
{
|
||||||
responder (listenerContext, token);
|
responder (listenerContext, token);
|
||||||
}
|
}
|
||||||
|
@ -505,10 +548,25 @@ namespace Ooui
|
||||||
var url = listenerContext.Request.Url;
|
var url = listenerContext.Request.Url;
|
||||||
var path = url.LocalPath;
|
var path = url.LocalPath;
|
||||||
|
|
||||||
RequestHandler handler;
|
PublishedPath pp;
|
||||||
var found = false;
|
var found = false;
|
||||||
lock (publishedPaths) found = publishedPaths.TryGetValue (path, out handler);
|
var variables = new Dictionary<string, string> ();
|
||||||
var elementHandler = handler as ElementHandler;
|
|
||||||
|
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;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var elementHandler = pp?.Handler as ElementHandler;
|
||||||
if (!found || elementHandler == null) {
|
if (!found || elementHandler == null) {
|
||||||
listenerContext.Response.StatusCode = 404;
|
listenerContext.Response.StatusCode = 404;
|
||||||
listenerContext.Response.Close ();
|
listenerContext.Response.Close ();
|
||||||
|
@ -518,7 +576,8 @@ namespace Ooui
|
||||||
Element element = null;
|
Element element = null;
|
||||||
bool disposeElementWhenDone = true;
|
bool disposeElementWhenDone = true;
|
||||||
try {
|
try {
|
||||||
element = elementHandler.GetElement ();
|
var context = UIContext.ForListenerContext (listenerContext, variables);
|
||||||
|
element = elementHandler.GetElement (context);
|
||||||
disposeElementWhenDone = elementHandler.DisposeElementWhenDone;
|
disposeElementWhenDone = elementHandler.DisposeElementWhenDone;
|
||||||
|
|
||||||
if (element == null)
|
if (element == null)
|
||||||
|
@ -600,13 +659,14 @@ namespace Ooui
|
||||||
public static void StartWebAssemblySession (string sessionId, string elementPath, string initialSize)
|
public static void StartWebAssemblySession (string sessionId, string elementPath, string initialSize)
|
||||||
{
|
{
|
||||||
Element element;
|
Element element;
|
||||||
RequestHandler handler;
|
PublishedPath pp;
|
||||||
lock (publishedPaths) {
|
lock (publishedPaths) {
|
||||||
publishedPaths.TryGetValue (elementPath, out handler);
|
publishedPaths.TryGetValue (elementPath, out pp);
|
||||||
}
|
}
|
||||||
var disposeElementWhenDone = true;
|
var disposeElementWhenDone = true;
|
||||||
if (handler is ElementHandler eh) {
|
if (pp.Handler is ElementHandler eh) {
|
||||||
element = eh.GetElement ();
|
var context = UIContext.ForWebAssemblySession (sessionId, elementPath);
|
||||||
|
element = eh.GetElement (context);
|
||||||
disposeElementWhenDone = eh.DisposeElementWhenDone;
|
disposeElementWhenDone = eh.DisposeElementWhenDone;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -687,4 +747,32 @@ namespace Ooui
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class UIContext
|
||||||
|
{
|
||||||
|
readonly Dictionary<string, string> variables = new Dictionary<string, string> ();
|
||||||
|
|
||||||
|
public Uri RequestUrl { get; }
|
||||||
|
|
||||||
|
public string this[string key] => variables.TryGetValue (key, out var v) ? v : "";
|
||||||
|
|
||||||
|
public UIContext (Uri requestUrl)
|
||||||
|
{
|
||||||
|
RequestUrl = requestUrl ?? throw new ArgumentNullException (nameof (requestUrl));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UIContext ForListenerContext (HttpListenerContext listenerContext, Dictionary<string, string> variables)
|
||||||
|
{
|
||||||
|
var c = new UIContext (listenerContext.Request.Url);
|
||||||
|
foreach (var kv in variables) {
|
||||||
|
c.variables.Add (kv.Key, kv.Value);
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UIContext ForWebAssemblySession (string sessionId, string elementPath)
|
||||||
|
{
|
||||||
|
return new UIContext (new Uri ("/" + elementPath, UriKind.Relative));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,39 @@ namespace Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string DownloadUI (string url)
|
||||||
|
{
|
||||||
|
UI.WaitUntilStarted ();
|
||||||
|
var c = new System.Net.WebClient ();
|
||||||
|
var r = c.DownloadString (UI.GetUrl (url));
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void PublishElementPatternUrl ()
|
||||||
|
{
|
||||||
|
UI.Publish ("/pattern/(?<id>[a-z0-9]+)", x => {
|
||||||
|
Assert.AreEqual ("fhe48yf", x["id"]);
|
||||||
|
return new Paragraph (x["id"]);
|
||||||
|
});
|
||||||
|
var r = DownloadUI ("/pattern/fhe48yf");
|
||||||
|
Assert.IsTrue (r.Length > 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void PublishJsonPatternUrl ()
|
||||||
|
{
|
||||||
|
bool gotRequest = false;
|
||||||
|
UI.PublishJson ("/pattern/(?<id>[a-z0-9]+)", x => {
|
||||||
|
gotRequest = true;
|
||||||
|
Assert.AreEqual ("nvirueh4", x["id"]);
|
||||||
|
return x["id"];
|
||||||
|
});
|
||||||
|
var r = DownloadUI ("/pattern/nvirueh4");
|
||||||
|
Assert.IsTrue (gotRequest);
|
||||||
|
Assert.AreEqual ("\"nvirueh4\"", r);
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void PublishEmptyFile ()
|
public void PublishEmptyFile ()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue