diff --git a/Sunfish/Sunfish/FServiceConf.cs b/Sunfish/Sunfish/FServiceConf.cs index 4934143..678c523 100644 --- a/Sunfish/Sunfish/FServiceConf.cs +++ b/Sunfish/Sunfish/FServiceConf.cs @@ -42,12 +42,23 @@ cbActive.Checked = ssc.Enabled; tbName.Text = ssc.Name; tbLocation.Text = ssc.Location; + if (string.IsNullOrWhiteSpace(tbName.Text)) + tbName.Text = "new service"; + if (string.IsNullOrWhiteSpace(tbLocation.Text)) + tbLocation.Text = "/nsrv"; LoadScreen(); } private bool ValidateData() { bool valid = true; + if (cbType.SelectedItem == null) + { + valid = false; + lbType.ForeColor = Color.LightCoral; + } + else + lbType.ForeColor = SystemColors.ControlText; if (string.IsNullOrWhiteSpace(tbName.Text)) { valid = false; @@ -71,6 +82,8 @@ ssc.Enabled = cbActive.Checked; ssc.Name = tbName.Text; ssc.Location = tbLocation.Text; + if (ssc.Location.Length > 0 && ssc.Location[0] != '/') + ssc.Location = '/' + ssc.Location; SaveDynamicScreen(); } diff --git a/Sunfish/Sunfish/Form1.cs b/Sunfish/Sunfish/Form1.cs index 06af58f..b2db193 100644 --- a/Sunfish/Sunfish/Form1.cs +++ b/Sunfish/Sunfish/Form1.cs @@ -68,7 +68,6 @@ else lbPaths.Items[idx] = s; } - Sunfish.Save(); } catch (Exception ex) { @@ -86,6 +85,11 @@ private void Form1_FormClosed(object sender, FormClosedEventArgs e) { + try + { + Sunfish.Save(); + } + catch { }; Sunfish.Active = false; } diff --git a/Sunfish/Sunfish/Middleware/VFS.cs b/Sunfish/Sunfish/Middleware/VFS.cs new file mode 100644 index 0000000..52f556f --- /dev/null +++ b/Sunfish/Sunfish/Middleware/VFS.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Middleware +{ + public class VFS + { + private Dictionary vf = new Dictionary(); + + public void AddVirtualFolder(string path,VFSFolder folder) + { + if (string.IsNullOrWhiteSpace(path)) + path = "/"; + else + { + path = path.Trim().ToLower(); + if (path[0] != '/') + path = '/' + path; + } + vf[path] = folder; + } + + public Stream OpenRead(string path) + { + return null; + } + + public Stream OpenWrite(string path) + { + return null; + } + + public string[] ListFiles(string path) + { + return null; + } + + public string[] ListDirectories(string path) + { + return null; + } + } +} diff --git a/Sunfish/Sunfish/Middleware/VFSFolder.cs b/Sunfish/Sunfish/Middleware/VFSFolder.cs new file mode 100644 index 0000000..42fdf2d --- /dev/null +++ b/Sunfish/Sunfish/Middleware/VFSFolder.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Middleware +{ + public abstract class VFSFolder + { + } +} diff --git a/Sunfish/Sunfish/Middleware/VFSFolderFileSystem.cs b/Sunfish/Sunfish/Middleware/VFSFolderFileSystem.cs new file mode 100644 index 0000000..647710e --- /dev/null +++ b/Sunfish/Sunfish/Middleware/VFSFolderFileSystem.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Middleware +{ + public class VFSFolderFileSystem:VFSFolder + { + public VFSFolderFileSystem(string path) + { + + } + + public bool AllowSubfolder { get; set; } = true; + public bool ReadOnly { get; set; } = true; + } +} diff --git a/Sunfish/Sunfish/Services/AdminService.cs b/Sunfish/Sunfish/Services/AdminService.cs index 4d5647e..ae688a7 100644 --- a/Sunfish/Sunfish/Services/AdminService.cs +++ b/Sunfish/Sunfish/Services/AdminService.cs @@ -1,14 +1,20 @@ -namespace DolphinWebXplorer2.Services +using DolphinWebXplorer2.Middleware; + +namespace DolphinWebXplorer2.Services { [DefineConfigurator(typeof(AdminServiceConfigurator))] class AdminService : SunfishService { - private const string CFG_ADMIN_PWD = "adminPwd"; public AdminService(SunfishServiceConfiguration ssc) : base(ssc) { } + public override void Process(string path, HttpCall call) + { + throw new System.NotImplementedException(); + } + protected override void Start() { } diff --git a/Sunfish/Sunfish/Services/ErrorService.cs b/Sunfish/Sunfish/Services/ErrorService.cs index 320bb12..63aedd6 100644 --- a/Sunfish/Sunfish/Services/ErrorService.cs +++ b/Sunfish/Sunfish/Services/ErrorService.cs @@ -1,4 +1,7 @@ -namespace DolphinWebXplorer2.Services +using DolphinWebXplorer2.Middleware; +using System.Net; + +namespace DolphinWebXplorer2.Services { [DefineConfigurator(typeof(ErrorServiceConfigurator))] class ErrorService : SunfishService @@ -7,6 +10,22 @@ { } + public override void Process(string path, HttpCall call) + { + call.Response.Headers[HttpResponseHeader.ContentType] = "text/plain"; + call.Write("Error"); + } + + public static void Process404(HttpCall call) + { + //call.Response.Headers[HttpResponseHeader.ContentType] = "text/plain"; + call.Response.StatusCode = 404; + //call.Response.StatusDescription = "Not found"; + //call.Write("Not found"); + call.Out.Close(); + } + + protected override void Start() { } diff --git a/Sunfish/Sunfish/Services/WebService.cs b/Sunfish/Sunfish/Services/WebService.cs index c1fb328..9b9539a 100644 --- a/Sunfish/Sunfish/Services/WebService.cs +++ b/Sunfish/Sunfish/Services/WebService.cs @@ -1,10 +1,38 @@ -namespace DolphinWebXplorer2.Services +using DolphinWebXplorer2.Middleware; +using System.IO; + +namespace DolphinWebXplorer2.Services { [DefineConfigurator(typeof(WebServiceConfigurator))] class WebService : SunfishService { + private string index; + private bool allowNavigation; + private bool allowSubfolderNavigation; public WebService(SunfishServiceConfiguration ssc) : base(ssc) { + vfs.AddVirtualFolder(null, new VFSFolderFileSystem(ssc.GetConf(WebServiceConfigurator.CFG_PATH))); + index = ssc.GetConf(WebServiceConfigurator.CFG_INDEX); + if (string.IsNullOrWhiteSpace(index)) + index = null; + allowNavigation = ssc.GetConf(WebServiceConfigurator.CFG_SHARE); + allowSubfolderNavigation = ssc.GetConf(WebServiceConfigurator.CFG_NAVIGATION); + } + + public override void Process(string path, HttpCall call) + { + if (path.EndsWith("/")) + { + //Directory entry, go for index file or navigation + } + else + { + using (Stream s = vfs.OpenRead(path)) + { + //call.Out. + } + } + call.Write("PoFale!!"); } protected override void Start() diff --git a/Sunfish/Sunfish/Sunfish.cs b/Sunfish/Sunfish/Sunfish.cs index 54b0b4b..c37fe35 100644 --- a/Sunfish/Sunfish/Sunfish.cs +++ b/Sunfish/Sunfish/Sunfish.cs @@ -1,4 +1,5 @@ using DolphinWebXplorer2.Middleware; +using DolphinWebXplorer2.Services; using Json.Net; using System; using System.Collections.Generic; @@ -55,7 +56,7 @@ return; if (act) { - server = new HttpServer(conf.Port, server_CreateProcessor); + server = new HttpServer(conf.Port, server_Process); server.Error += server_Error; try { @@ -78,9 +79,15 @@ conf.Active = act; } - static void server_CreateProcessor(HttpServer server, HttpCall call) + static void server_Process(HttpServer server, HttpCall call) { - new SunfishServerProcessor(call); + var path = call.Request.Url.LocalPath; + SunfishService s = GetServiceForPath(ref path); + if (s == null || !s.Enabled) + ErrorService.Process404(call); + else + s.Process(path,call); + call.Close(); } static void server_Error(HttpServer server, Exception e) @@ -122,16 +129,23 @@ public static void DeleteService(SunfishService srv) { - + conf.Services.Remove(srv.Configuration); } - public static SunfishService GetServiceForPath(string path) + public static SunfishService GetServiceForPath(ref string path) { + string candidateRelativePath=""; SunfishService candidate = null; foreach (SunfishService s in srvs) if (path.StartsWith(s.Configuration.Location) && (candidate == null || (candidate.Configuration.Location.Length < s.Configuration.Location.Length))) + { candidate = s; + candidateRelativePath = path.Substring(s.Configuration.Location.Length); + } + path = candidateRelativePath; + if (string.IsNullOrWhiteSpace(path)) + path = "/"; return candidate; } diff --git a/Sunfish/Sunfish/Sunfish.csproj b/Sunfish/Sunfish/Sunfish.csproj index 22e470c..54dfa8a 100644 --- a/Sunfish/Sunfish/Sunfish.csproj +++ b/Sunfish/Sunfish/Sunfish.csproj @@ -71,6 +71,7 @@ 3.5 + @@ -105,7 +106,9 @@ - + + + diff --git a/Sunfish/Sunfish/SunfishServerProcessor.cs b/Sunfish/Sunfish/SunfishServerProcessor.cs deleted file mode 100644 index f458ec0..0000000 --- a/Sunfish/Sunfish/SunfishServerProcessor.cs +++ /dev/null @@ -1,39 +0,0 @@ -using DolphinWebXplorer2.Middleware; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; - -namespace DolphinWebXplorer2 -{ - class SunfishServerProcessor - { - private HttpCall call; - - public SunfishServerProcessor(HttpCall call) - { - this.call = call; - Process(); - } - private void Process() - { - SunfishService s = Sunfish.GetServiceForPath(call.Request.Url.LocalPath); - //Response.Headers[HttpResponseHeader.ContentType] = "text/plain"; - //call.OpenOutput(); - return; - //if (s == null) - // if (Sunfish.RootMenu) - // { - // Error404(); - // return; - // } - // else - // { - // Error404(); - // return; - // } - } - } -} diff --git a/Sunfish/Sunfish/SunfishService.cs b/Sunfish/Sunfish/SunfishService.cs index 7e2115c..c628303 100644 --- a/Sunfish/Sunfish/SunfishService.cs +++ b/Sunfish/Sunfish/SunfishService.cs @@ -1,4 +1,5 @@ using DolphinWebXplorer2.Configurator; +using DolphinWebXplorer2.Middleware; using DolphinWebXplorer2.Services; using System; using System.Collections.Generic; @@ -6,7 +7,7 @@ namespace DolphinWebXplorer2 { - public abstract class SunfishService:IDisposable + public abstract class SunfishService : IDisposable { #region Static / Type management @@ -71,6 +72,7 @@ #endregion private SunfishServiceConfigurator configurator; + protected VFS vfs = new VFS(); public SunfishService(SunfishServiceConfiguration ssc) { @@ -83,6 +85,8 @@ Stop(); } + public abstract void Process(string path, HttpCall call); + protected abstract void Start(); protected abstract void Stop();