public void ProcessStaticFiles(HttpListenerContext context) { const string indexDefault = "/index.html"; var path = context.Request.RawUrl; if (path == "/") { path = indexDefault; } var pathParts = new[] { ".", "static" }.Concat(path.Split('/')).Where(d => !string.IsNullOrEmpty(d)).ToArray(); var filename = Path.Combine(pathParts); filename = filename.Split('?')[0]; if (File.Exists(filename)) { try { Stream input = new FileStream(filename, FileMode.Open); //Adding permanent http response headers var extension = Path.GetExtension(filename); var contentType = MimeTypeMappings.ContainsKey(extension) ? MimeTypeMappings[extension] : "application/octet-stream"; context.Response.ContentType = contentType; context.Response.ContentLength64 = input.Length; context.Response.AddHeader("Date", DateTime.Now.ToString("r")); context.Response.AddHeader("Last-Modified", File.GetLastWriteTime(filename).ToString("r")); var buffer = new byte[1024 * 16]; int nbytes; while ((nbytes = input.Read(buffer, 0, buffer.Length)) > 0) { context.Response.OutputStream.Write(buffer, 0, nbytes); } input.Close(); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.OutputStream.Flush(); } catch (Exception ex) { WriteMessage(ex); context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; } } else { context.Response.StatusCode = (int)HttpStatusCode.NotFound; } context.Response.OutputStream.Close(); }
/// <summary> /// Form constructor logic where we will establish settings, registry keys, /// (optionally) start self-hosting web server and invoke any configured startup processes /// (in case you want to load a nodejs web server, for example). /// </summary> public MainForm() { mappings = Classes.MimeTypeMappings.Load(mappingsPath); globalSettings = Classes.GlobalSettings.Load(globalSettingsPath); // Preserve default exoskeleton icon for app history launcher, if needed this.DefaultIcon = this.Icon; this.ImageListDictionary = new Dictionary <string, ImageList>(); InitializeSettings(); SetupRegistryKeys(); if (this.Settings.WebServerSelfHost) { startServer(); } // if configured with startup commands, run them now... // we might use this to start a node process like a webserver foreach (string cmd in this.Settings.StartupCommands) { Process.Start(cmd); } InitializeComponent(); if (this.Settings.DefaultToNativeUi) { SwitchToNativeUi(); } HostMenuStrip.Visible = this.Settings.ScriptingMenuEnabled; HostToolStrip.Visible = this.Settings.ScriptingToolStripEnabled; HostStatusStrip.Visible = this.Settings.ScriptingStatusStripEnabled; hostWindows.Add(this); InitializeIcon(); this.Width = this.Settings.WindowWidth; this.Height = this.Settings.WindowHeight; this.Text = this.Settings.WindowTitle; }