From 068c06f714d7aa1bc026c375716996df6968b629 Mon Sep 17 00:00:00 2001 From: Berrysoft Date: Sun, 1 Dec 2019 21:30:31 +0800 Subject: [PATCH] Add RunFromString & RunFromResolver. --- src/WebWindow.Blazor/ComponentsDesktop.cs | 58 ++++++++++++++++------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/WebWindow.Blazor/ComponentsDesktop.cs b/src/WebWindow.Blazor/ComponentsDesktop.cs index e9f9a52..bb2b4dd 100644 --- a/src/WebWindow.Blazor/ComponentsDesktop.cs +++ b/src/WebWindow.Blazor/ComponentsDesktop.cs @@ -8,6 +8,7 @@ using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; +using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -23,7 +24,46 @@ public static class ComponentsDesktop internal static WebWindow webWindow; + public static void RunFromString(string windowTitle, string indexContent, string rootPath) + { + var contentRootAbsolute = Path.GetDirectoryName(Path.GetFullPath(rootPath)); + + RunFromResolver(windowTitle, DefaultBlazorResolver(contentRootAbsolute, (string url, out string contentType) => + { + contentType = "text/html"; + return new MemoryStream(Encoding.Default.GetBytes(indexContent)); + })); + } + public static void Run(string windowTitle, string hostHtmlPath) + { + var contentRootAbsolute = Path.GetDirectoryName(Path.GetFullPath(hostHtmlPath)); + + RunFromResolver(windowTitle, DefaultBlazorResolver(contentRootAbsolute, (string url, out string contentType) => + { + contentType = GetContentType(hostHtmlPath); + return File.Exists(hostHtmlPath) ? File.OpenRead(hostHtmlPath) : null; + })); + } + + private static ResolveWebResourceDelegate DefaultBlazorResolver(string contentRootAbsolute, ResolveWebResourceDelegate indexResolver) + { + return (string url, out string contentType) => + { + // TODO: Only intercept for the hostname 'app' and passthrough for others + // TODO: Prevent directory traversal? + var appFile = Path.Combine(contentRootAbsolute, new Uri(url).AbsolutePath.Substring(1)); + if (appFile == contentRootAbsolute) + { + return indexResolver(url, out contentType); + } + + contentType = GetContentType(appFile); + return File.Exists(appFile) ? File.OpenRead(appFile) : null; + }; + } + + public static void RunFromResolver(string windowTitle, ResolveWebResourceDelegate resolver) { DesktopSynchronizationContext.UnhandledException += (sender, exception) => { @@ -32,21 +72,7 @@ public static void Run(string windowTitle, string hostHtmlPath) webWindow = new WebWindow(windowTitle, options => { - var contentRootAbsolute = Path.GetDirectoryName(Path.GetFullPath(hostHtmlPath)); - - options.SchemeHandlers.Add(BlazorAppScheme, (string url, out string contentType) => - { - // TODO: Only intercept for the hostname 'app' and passthrough for others - // TODO: Prevent directory traversal? - var appFile = Path.Combine(contentRootAbsolute, new Uri(url).AbsolutePath.Substring(1)); - if (appFile == contentRootAbsolute) - { - appFile = hostHtmlPath; - } - - contentType = GetContentType(appFile); - return File.Exists(appFile) ? File.OpenRead(appFile) : null; - }); + options.SchemeHandlers.Add(BlazorAppScheme, resolver); // framework:// is resolved as embedded resources options.SchemeHandlers.Add("framework", (string url, out string contentType) => @@ -57,7 +83,7 @@ public static void Run(string windowTitle, string hostHtmlPath) }); CancellationTokenSource appLifetimeCts = new CancellationTokenSource(); - Task.Factory.StartNew(async() => + Task.Factory.StartNew(async () => { try {