diff --git a/Sunfish/Sunfish/FServiceConf.Designer.cs b/Sunfish/Sunfish/FServiceConf.Designer.cs index 1cdd2ad..7c67a7c 100644 --- a/Sunfish/Sunfish/FServiceConf.Designer.cs +++ b/Sunfish/Sunfish/FServiceConf.Designer.cs @@ -60,7 +60,7 @@ this.cbType.FormattingEnabled = true; this.cbType.Location = new System.Drawing.Point(25, 32); this.cbType.Name = "cbType"; - this.cbType.Size = new System.Drawing.Size(451, 28); + this.cbType.Size = new System.Drawing.Size(380, 28); this.cbType.TabIndex = 1; this.toolTip1.SetToolTip(this.cbType, "Type of service."); this.cbType.SelectedIndexChanged += new System.EventHandler(this.cbType_SelectedIndexChanged); @@ -80,7 +80,7 @@ | System.Windows.Forms.AnchorStyles.Right))); this.tbName.Location = new System.Drawing.Point(25, 100); this.tbName.Name = "tbName"; - this.tbName.Size = new System.Drawing.Size(370, 26); + this.tbName.Size = new System.Drawing.Size(299, 26); this.tbName.TabIndex = 3; this.toolTip1.SetToolTip(this.tbName, "Name of the service.\r\n(only for management)"); // @@ -88,7 +88,7 @@ // this.cbActive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.cbActive.AutoSize = true; - this.cbActive.Location = new System.Drawing.Point(401, 102); + this.cbActive.Location = new System.Drawing.Point(330, 102); this.cbActive.Name = "cbActive"; this.cbActive.Size = new System.Drawing.Size(78, 24); this.cbActive.TabIndex = 4; @@ -110,14 +110,14 @@ | System.Windows.Forms.AnchorStyles.Right))); this.tbLocation.Location = new System.Drawing.Point(25, 166); this.tbLocation.Name = "tbLocation"; - this.tbLocation.Size = new System.Drawing.Size(451, 26); + this.tbLocation.Size = new System.Drawing.Size(380, 26); this.tbLocation.TabIndex = 6; this.toolTip1.SetToolTip(this.tbLocation, "Document root on the server:\r\nThis works as a virtual directory in the url."); // // btOk // this.btOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btOk.Location = new System.Drawing.Point(282, 227); + this.btOk.Location = new System.Drawing.Point(211, 227); this.btOk.Name = "btOk"; this.btOk.Size = new System.Drawing.Size(113, 38); this.btOk.TabIndex = 7; @@ -129,7 +129,7 @@ // this.btCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.btCancel.Location = new System.Drawing.Point(408, 227); + this.btCancel.Location = new System.Drawing.Point(337, 227); this.btCancel.Name = "btCancel"; this.btCancel.Size = new System.Drawing.Size(82, 38); this.btCancel.TabIndex = 8; @@ -143,7 +143,7 @@ | System.Windows.Forms.AnchorStyles.Right))); this.pScreen.Location = new System.Drawing.Point(12, 210); this.pScreen.Name = "pScreen"; - this.pScreen.Size = new System.Drawing.Size(478, 1); + this.pScreen.Size = new System.Drawing.Size(407, 1); this.pScreen.TabIndex = 9; // // btAdv @@ -151,7 +151,7 @@ this.btAdv.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btAdv.Location = new System.Drawing.Point(12, 227); this.btAdv.Name = "btAdv"; - this.btAdv.Size = new System.Drawing.Size(196, 38); + this.btAdv.Size = new System.Drawing.Size(167, 38); this.btAdv.TabIndex = 10; this.btAdv.Text = "Advanced Settings"; this.btAdv.UseVisualStyleBackColor = true; @@ -162,7 +162,7 @@ this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.btCancel; - this.ClientSize = new System.Drawing.Size(502, 277); + this.ClientSize = new System.Drawing.Size(431, 277); this.Controls.Add(this.btAdv); this.Controls.Add(this.pScreen); this.Controls.Add(this.btCancel); diff --git a/Sunfish/Sunfish/FServiceConf.cs b/Sunfish/Sunfish/FServiceConf.cs index 09d5d4f..ae396e2 100644 --- a/Sunfish/Sunfish/FServiceConf.cs +++ b/Sunfish/Sunfish/FServiceConf.cs @@ -14,6 +14,7 @@ { InitializeComponent(); panelOffset = ClientSize.Height - pScreen.Top; + cbType.Items.AddRange(SunfishService.GetTypes()); } public static bool Execute(SunfishServiceConfiguration ssc) @@ -28,8 +29,16 @@ private void LoadData() { - //cbType.SelectedItem = ssc.Type; - cbType.Text = ssc.Type; + if (ssc.Type != null) + for (int i = 0; i < cbType.Items.Count; i++) + { + object o = cbType.Items[i]; + if (o != null && o.ToString() == ssc.Type) + { + cbType.SelectedIndex = i; + break; + } + } cbActive.Checked = ssc.Enabled; tbName.Text = ssc.Name; tbLocation.Text = ssc.Name; @@ -145,7 +154,6 @@ private void FServiceConf_Load(object sender, EventArgs e) { - cbType.Items.AddRange(SunfishService.GetTypes()); } private void button1_Click(object sender, EventArgs e) diff --git a/Sunfish/Sunfish/Form1.Designer.cs b/Sunfish/Sunfish/Form1.Designer.cs index b4bc9d8..f1a11b4 100644 --- a/Sunfish/Sunfish/Form1.Designer.cs +++ b/Sunfish/Sunfish/Form1.Designer.cs @@ -30,7 +30,7 @@ { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); - this.button1 = new System.Windows.Forms.Button(); + this.btDone = new System.Windows.Forms.Button(); this.nudPort = new System.Windows.Forms.NumericUpDown(); this.cbActive = new System.Windows.Forms.CheckBox(); this.btAdd = new System.Windows.Forms.Button(); @@ -48,18 +48,18 @@ this.cmsItem.SuspendLayout(); this.SuspendLayout(); // - // button1 + // btDone // - this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.button1.Location = new System.Drawing.Point(213, 514); - this.button1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(138, 35); - this.button1.TabIndex = 1; - this.button1.Text = "Ok"; - this.toolTip1.SetToolTip(this.button1, "Hide"); - this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); + this.btDone.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btDone.Location = new System.Drawing.Point(213, 514); + this.btDone.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.btDone.Name = "btDone"; + this.btDone.Size = new System.Drawing.Size(138, 35); + this.btDone.TabIndex = 1; + this.btDone.Text = "Ok"; + this.toolTip1.SetToolTip(this.btDone, "Hide"); + this.btDone.UseVisualStyleBackColor = true; + this.btDone.Click += new System.EventHandler(this.button1_Click); // // nudPort // @@ -221,7 +221,7 @@ this.Controls.Add(this.btAdd); this.Controls.Add(this.cbActive); this.Controls.Add(this.nudPort); - this.Controls.Add(this.button1); + this.Controls.Add(this.btDone); this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; @@ -239,7 +239,7 @@ #endregion - private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button btDone; private System.Windows.Forms.NumericUpDown nudPort; private System.Windows.Forms.CheckBox cbActive; private System.Windows.Forms.Button btAdd; diff --git a/Sunfish/Sunfish/Form1.cs b/Sunfish/Sunfish/Form1.cs index 688dd2a..933e49d 100644 --- a/Sunfish/Sunfish/Form1.cs +++ b/Sunfish/Sunfish/Form1.cs @@ -54,21 +54,24 @@ { if (FServiceConf.Execute(ssc)) { - if (oldService == null) try - { - lbPaths.Items.Add(Sunfish.AddService(ssc)); - Sunfish.Save(); - } - catch (Exception ex) - { - ex.Show(); - } - else + try { - //oldService - //TODO: How to update service according to configuration changes? - //Sunfish. - lbPaths.Update(); + if (oldService == null) + lbPaths.Items.Add(Sunfish.AddService(ssc)); + else + { + SunfishService s = Sunfish.ReplaceService(oldService, ssc); + int idx = lbPaths.Items.IndexOf(oldService); + if (idx < 0) + lbPaths.Items.Add(s); + else + lbPaths.Items[idx] = s; + } + Sunfish.Save(); + } + catch (Exception ex) + { + ex.Show(); } } } @@ -243,6 +246,8 @@ ssc.Location = Path.GetFileName(fil); ssc.Enabled = true; ssc.Settings[Services.WebServiceConfigurator.CFG_PATH] = fil; + ssc.Settings[Services.WebServiceConfigurator.CFG_SHARE] = true; + ssc.Settings[Services.WebServiceConfigurator.CFG_NAVIGATION] = true; EditConfiguration(ssc, null); } Activate(); diff --git a/Sunfish/Sunfish/Form1.resx b/Sunfish/Sunfish/Form1.resx index 79d5451..7eed72d 100644 --- a/Sunfish/Sunfish/Form1.resx +++ b/Sunfish/Sunfish/Form1.resx @@ -128,7 +128,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAAq - AQAAAk1TRnQBSQFMAgEBAgEAAXABAAFwAQABEAEAARABAAT/AQUBAAj/AUIBTQF2BwABdgMAASgDAAFA + AQAAAk1TRnQBSQFMAgEBAgEAAXgBAAF4AQABEAEAARABAAT/AQUBAAj/AUIBTQF2BwABdgMAASgDAAFA AwABEAMAAQEBAAEEBgABAhgAAYACAAGAAwACgAEAAYADAAGAAQABgAEAAoACAAPAAQADgAMAAf8CAAH/ AwAC/wEAAf8DAAH/AQAB/wEAAv8CAAP//wD/AAMAAUIBTQE+BwABPgMAASgDAAFAAwABEAMAAQEBAAEB BQABgBcAA/8BAAT/BAAE/wQABP8EAAH8AT8C/wQAAfwBPwL/BAAB/AE/Av8EAAHgAQcB4AEHBAAB4AEH @@ -136,9 +136,6 @@ BAAL - - 17, 17 - 185, 17 diff --git a/Sunfish/Sunfish/Middleware/HttpServer.cs b/Sunfish/Sunfish/Middleware/HttpServer.cs index 04f6b58..10a31c1 100644 --- a/Sunfish/Sunfish/Middleware/HttpServer.cs +++ b/Sunfish/Sunfish/Middleware/HttpServer.cs @@ -8,7 +8,7 @@ namespace DolphinWebXplorer2.Middleware { - public class HttpServer + class HttpServer { private int port; private HttpListener lis = null; @@ -27,10 +27,10 @@ this.port = port; } - public bool Start() + public void Start() { if (lis != null) - return false; + return; lis = new HttpListener(); lis.Prefixes.Add("http://+:" + port + "/"); try @@ -40,11 +40,11 @@ catch (HttpListenerException e) { lis = null; - return false; + throw e; } loop = new Thread(ServerLoop); loop.Start(); - return true; + //fwRule = WindowsFirewall.Allow(port); } private void ServerLoop() @@ -76,7 +76,7 @@ private void CallNewProcessor(HttpListenerContext ctx) { - HttpServerProcessor proc = CreateProcessor == null ? new HttpServerProcessor() : CreateProcessor(this); + HttpServerProcessor proc = CreateProcessor(this); proc.server = this; proc.Process(ctx); } @@ -104,6 +104,7 @@ } finally { + //WindowsFirewall.Remove(fwRule); } return true; } @@ -120,7 +121,7 @@ public bool MultiThread { get { return multiThread; } set { multiThread = value; } } } - public class HttpServerProcessor + public abstract class HttpServerProcessor { protected HttpListenerRequest Request; protected HttpListenerResponse Response; @@ -249,10 +250,7 @@ return HttpUtility.UrlDecode(url); } - virtual protected void Process() - { - Error404(); - } + protected abstract void Process(); protected Stream OutStream { get { return Response.OutputStream; } } protected StreamWriter Out { get { return swout; } } diff --git a/Sunfish/Sunfish/Sunfish.cs b/Sunfish/Sunfish/Sunfish.cs index fe21647..4f640e5 100644 --- a/Sunfish/Sunfish/Sunfish.cs +++ b/Sunfish/Sunfish/Sunfish.cs @@ -1,6 +1,9 @@ -using Json.Net; +using DolphinWebXplorer2.Middleware; +using Json.Net; +using System; using System.Collections.Generic; using System.IO; +using System.Net; namespace DolphinWebXplorer2 { @@ -17,6 +20,7 @@ public const string DEFAULT_CONF_FILE = "sunfish2"; private static SunfishConfiguration conf = new SunfishConfiguration(); private static List srvs = new List(); + private static HttpServer server; #region Configuration & Initialization static Sunfish() @@ -49,10 +53,43 @@ { if (conf.Active == act) return; - // INIT SERVER HERE + if (act) + { + server = new HttpServer(conf.Port); + server.CreateProcessor += server_CreateProcessor; + server.Error += server_Error; + try + { + server.Start(); + } + catch (Exception e) + { + server = null; + throw e; + } + } + else + { + if (server != null) + { + server.Stop(); + server = null; + } + } conf.Active = act; } + static HttpServerProcessor server_CreateProcessor(HttpServer server) + { + return new SunfishServerProcessor(); + } + + static void server_Error(HttpServer server, Exception e) + { + //if (!(e is ObjectDisposedException) && !(e is HttpListenerException)) + // MessageBox.Show(e.Message + "\r\n" + e.StackTrace, e.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + private static void SetPort(int port) { bool act = Active; @@ -65,6 +102,21 @@ { SunfishService s; srvs.Add(s = SunfishService.Instance(ssc)); + conf.Services.Add(ssc); + return s; + } + + public static SunfishService ReplaceService(SunfishService sold, SunfishServiceConfiguration ssc) + { + SunfishService s = SunfishService.Instance(ssc); + int i = srvs.IndexOf(sold); + if (i < 0) + srvs.Add(s); + else + srvs[i] = s; + i = conf.Services.IndexOf(ssc); + if (i < 0) + conf.Services.Add(ssc); return s; } diff --git a/Sunfish/Sunfish/Sunfish.csproj b/Sunfish/Sunfish/Sunfish.csproj index 167130c..edef0dd 100644 --- a/Sunfish/Sunfish/Sunfish.csproj +++ b/Sunfish/Sunfish/Sunfish.csproj @@ -102,6 +102,7 @@ + @@ -121,6 +122,7 @@ True + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/Sunfish/Sunfish/SunfishServerProcessor.cs b/Sunfish/Sunfish/SunfishServerProcessor.cs new file mode 100644 index 0000000..c39a7f3 --- /dev/null +++ b/Sunfish/Sunfish/SunfishServerProcessor.cs @@ -0,0 +1,17 @@ +using DolphinWebXplorer2.Middleware; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2 +{ + class SunfishServerProcessor : HttpServerProcessor + { + protected override void Process() + { + Error404(); + } + } +} diff --git a/Sunfish/Sunfish/SunfishService.cs b/Sunfish/Sunfish/SunfishService.cs index 4eff264..7e2115c 100644 --- a/Sunfish/Sunfish/SunfishService.cs +++ b/Sunfish/Sunfish/SunfishService.cs @@ -6,7 +6,7 @@ namespace DolphinWebXplorer2 { - public abstract class SunfishService + public abstract class SunfishService:IDisposable { #region Static / Type management @@ -78,6 +78,11 @@ configurator = SunfishServiceConfigurator.GetConfiguratorForService(GetType()); } + public void Dispose() + { + Stop(); + } + protected abstract void Start(); protected abstract void Stop(); diff --git a/Sunfish/Sunfish/app.manifest b/Sunfish/Sunfish/app.manifest new file mode 100644 index 0000000..ff3d8ae --- /dev/null +++ b/Sunfish/Sunfish/app.manifest @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Sunfish/Sunfish/wx/WebXplorer.cs b/Sunfish/Sunfish/wx/WebXplorer.cs index 68c3614..ddf6299 100644 --- a/Sunfish/Sunfish/wx/WebXplorer.cs +++ b/Sunfish/Sunfish/wx/WebXplorer.cs @@ -94,8 +94,7 @@ server = new HttpServer(port); server.CreateProcessor += new HttpServer.CreateProcessorHandler(server_CreateProcessor); server.Error += new HttpServer.ErrorEventHandler(server_Error); - if (!server.Start()) - server = null; + server.Start(); } static void server_Error(HttpServer server, Exception e)