private void Process(HttpListenerContext context) { bool ImageUploaded = true; // TODO: Clean this up. Need a consistent way to read in values // and cleanly set settings. For now having this ugly is a good // tradeoff to let me learn how to do it better in the future if (context.Request.QueryString.Count > 0) { int refreshDirectories = 0; // track if a directory change is made. 0 = no change. int refreshSettings = 0; // track i fa setting change is made. 0 = no change. if (context.Request.QueryString.Get("debug") != null) { return; } Logger.LogComment("SIMPLEHTTPSERVER: Checking query string for values to edit.."); // Set in App Settings takes the querystring and the Appsettings.Default value name refreshSettings += Helpers.SetIntAppSetting(context.Request.QueryString.Get("rotation"), "Rotation"); refreshSettings += Helpers.SetIntAppSetting(context.Request.QueryString.Get("infobarfontsize"), "InfoBarFontSize"); refreshSettings += Helpers.SetIntAppSetting(context.Request.QueryString.Get("slideshowduration"), "SlideshowTransitionTime"); refreshSettings += Helpers.SetIntAppSetting(context.Request.QueryString.Get("ipaddresstime"), "NumberOfSecondsToShowIP"); refreshSettings += Helpers.SetIntAppSetting(context.Request.QueryString.Get("transitiontime"), "FadeTransitionTime"); refreshDirectories += Helpers.SetBoolAppSetting(context.Request.QueryString.Get("Shuffle"), "Shuffle"); Helpers.SetBoolAppSetting(context.Request.QueryString.Get("syncenabled"), "IsSyncEnabled"); Helpers.SetBoolAppSetting(context.Request.QueryString.Get("VideoVolume"), "VideoVolume"); Helpers.SetBoolAppSetting(context.Request.QueryString.Get("ExpandDirectoriesByDefault"), "ExpandDirectoriesByDefault"); Helpers.SetBoolAppSetting(context.Request.QueryString.Get("EnableLogging"), "EnableLogging"); refreshSettings += Helpers.SetStringAppSetting(context.Request.QueryString.Get("DateTimeFormat"), "DateTimeFormat"); refreshSettings += Helpers.SetStringAppSetting(context.Request.QueryString.Get("DateTimeFontFamily"), "DateTimeFontFamily"); refreshSettings += Helpers.SetStringAppSetting(context.Request.QueryString.Get("VideoStretch"), "VideoStretch"); refreshSettings += Helpers.SetStringAppSetting(context.Request.QueryString.Get("ShowInfoIP"), "ShowInfoIP"); // setup commands if (context.Request.QueryString.Get("COMMAND") != null) { if (context.Request.QueryString.Get("COMMAND") == "INFOBAR_DATETIME_On" || context.Request.QueryString.Get("COMMAND") == "INFOBAR_DATETIME_Off") { if (context.Request.QueryString.Get("on") == "false") { refreshSettings += Helpers.SetBoolAppSetting("on", "ShowInfoDateTime"); refreshSettings += Helpers.SetBoolAppSetting("off", "ShowInfoFileName"); refreshSettings += Helpers.SetStringAppSetting("false", "ShowInfoIP"); } else { refreshSettings += Helpers.SetBoolAppSetting("off", "ShowInfoDateTime"); } } if (context.Request.QueryString.Get("COMMAND") == "INFOBAR_FILENAME_On" || context.Request.QueryString.Get("COMMAND") == "INFOBAR_FILENAME_Off") { if (context.Request.QueryString.Get("on") == "false") { refreshSettings += Helpers.SetBoolAppSetting("on", "ShowInfoFileName"); refreshSettings += Helpers.SetBoolAppSetting("off", "ShowInfoDateTime"); refreshSettings += Helpers.SetStringAppSetting("false", "ShowInfoIP"); } else { refreshSettings += Helpers.SetBoolAppSetting("off", "ShowInfoFileName"); } } if (context.Request.QueryString.Get("COMMAND") == "INFOBAR_IP_On" || context.Request.QueryString.Get("COMMAND") == "INFOBAR_IP_Off") { if (context.Request.QueryString.Get("on") == "false") { refreshSettings += Helpers.SetStringAppSetting("true", "ShowInfoIP"); refreshSettings += Helpers.SetBoolAppSetting("off", "ShowInfoDateTime"); refreshSettings += Helpers.SetBoolAppSetting("off", "ShowInfoFileName"); } else { refreshSettings += Helpers.SetStringAppSetting("false", "ShowInfoIP"); } } if (context.Request.QueryString.Get("COMMAND") == "SCREENON") { refreshSettings += Helpers.SetBoolAppSetting("on", "ScreenStatus"); } else if (context.Request.QueryString.Get("COMMAND") == "SCREENOFF") { refreshSettings += Helpers.SetBoolAppSetting("off", "ScreenStatus"); } if (context.Request.QueryString.Get("COMMAND") == "CONTROL_PAUSE_On") { refreshSettings += Helpers.SetBoolAppSetting("on", "SlideShowPaused"); } else if (context.Request.QueryString.Get("COMMAND") == "CONTROL_PAUSE_Off") { refreshSettings += Helpers.SetBoolAppSetting("off", "SlideShowPaused"); } if (context.Request.QueryString.Get("COMMAND") == "SHUFFLE_On") { refreshSettings += Helpers.SetBoolAppSetting("on", "Shuffle"); } else if (context.Request.QueryString.Get("COMMAND") == "SHUFFLE_Off") { refreshSettings += Helpers.SetBoolAppSetting("off", "Shuffle"); } //Upload Images if (context.Request.QueryString.Get("COMMAND") == "UTILITY_UPLOADFILE") { if (context.Request.ContentType != null) { ImageUploaded = CommandProcessor.SaveFile(context.Request.ContentEncoding, CommandProcessor.GetBoundary(context.Request.ContentType), context.Request.InputStream, context.Request.QueryString.Get("Extension")); } } else if (context.Request.QueryString.Get("COMMAND") == "UTILITY_DELETEFILE") { CommandProcessor.DeleteFile(context.Request.QueryString.Get("FILENAME")); } else { CommandProcessor.ProcessCommand(context.Request.QueryString.Get("COMMAND")); } refreshSettings++; // Any commands should invoke a settings refresh } if (context.Request.QueryString.Get("SETFILE") != null) { CommandProcessor.ProcessSetFile(context.Request.QueryString.Get("SETFILE")); } // Setup synced frames if ((context.Request.QueryString.Get("ClientIP") != null) && (context.Request.QueryString.Get("ClientIP") != "")) { string ip = context.Request.QueryString.Get("ClientIP"); if (!AppSettings.Default.RemoteClients.Contains(ip)) { AppSettings.Default.RemoteClients.Add(context.Request.QueryString.Get("ClientIP")); } SyncedFrame.SyncEngine.AddFrame(ip); Logger.LogComment("Added Frame to be Synced..."); } if (context.Request.QueryString.Get("CLEARSYNCLIST") != null) { AppSettings.Default.RemoteClients.Clear(); SyncedFrame.SyncEngine.syncedFrames.Clear(); } #region SpecialCasesWhichNeedCleanup // Process 'directory' if passed string dir = context.Request.QueryString.Get("dir"); if (dir != null) { string pictureDir = dir.Replace("'", ""); AppSettings.Default.CurrentDirectory = pictureDir; refreshDirectories++; } // see if 'rem' (remove) was passed try { string rem = context.Request.QueryString.Get("rem"); if (rem != null) { Logger.LogComment("SimpleHTTPServer: Removing directory..." + rem); string removeDir = rem.Replace("'", ""); AppSettings.Default.SearchDirectories.Remove(rem); List <string> ToRemove = new List <string>(); // Go through and find items to remove. We can't modify the list 'in place' so make a target // list and then go through that. Awkward but too tired to figure out the right way to fix // this right now. Logger.LogComment("Emumerating directories to remove.."); foreach (string subdirectory in AppSettings.Default.CurrentPlayList) { if (subdirectory.Contains(removeDir)) { ToRemove.Add(subdirectory); } } foreach (string del in ToRemove) { if (AppSettings.Default.CurrentPlayList.Contains(del)) { AppSettings.Default.CurrentPlayList.Remove(del); Logger.LogComment("Removing subdirectory: " + del); } } Logger.LogComment("New Playlist is as follows----------------"); foreach (string playlistDir in AppSettings.Default.CurrentPlayList) { Logger.LogComment(playlistDir); } Logger.LogComment("End Playlist dump -------------"); refreshDirectories++; } } catch (Exception exc) { // Note: Lots of potentially dangerous work in removing, so adding a check // to backstop any issues. Logger.LogComment("Excpetion when trying to remove directory! " + exc.ToString()); } string imagestretchVal = context.Request.QueryString.Get("imagescaling"); if (imagestretchVal != null) { refreshSettings++; switch (imagestretchVal) { case "Uniform": { AppSettings.Default.ImageStretch = Avalonia.Media.Stretch.Uniform; break; } case "UniformToFill": { AppSettings.Default.ImageStretch = Avalonia.Media.Stretch.UniformToFill; break; } case "Fill": { AppSettings.Default.ImageStretch = Avalonia.Media.Stretch.Fill; break; } case "None": { AppSettings.Default.ImageStretch = Avalonia.Media.Stretch.None; break; } default: break; } } string directoryToAdd = context.Request.QueryString.Get("directoryAdd"); if (directoryToAdd != null) { // We use search directories to add things like NAS drives and usb drives to the system. // This checks to make sure it exists when they're adding it, and that it isn't already in the list.. if (Directory.Exists(directoryToAdd) && (!AppSettings.Default.SearchDirectories.Contains(directoryToAdd))) { AppSettings.Default.SearchDirectories.Add(directoryToAdd); // Cleanup. Somewhere this is getting doubled up sometimes still. It's a small list so this should be fast. AppSettings.Default.SearchDirectories = AppSettings.Default.SearchDirectories.Distinct().ToList(); refreshDirectories++; } } string subdirectorytoadd = context.Request.QueryString.Get("cbDirectory"); if (subdirectorytoadd != null) { Logger.LogComment("SIMPLEHTTPSERVER: Adding directory: " + subdirectorytoadd); AppSettings.Default.CurrentPlayList.Clear(); // We use search directories to add things like NAS drives and usb drives to the system. // This checks to make sure it exists when they're adding it, and that it isn't already in the list.. string[] directoryList = subdirectorytoadd.Split(','); foreach (string newdir in directoryList) { string decodedDirectory = System.Web.HttpUtility.UrlDecode(newdir); if (Directory.Exists(decodedDirectory) && (!AppSettings.Default.CurrentPlayList.Contains(decodedDirectory))) { AppSettings.Default.CurrentPlayList.Add(decodedDirectory); } } refreshDirectories++; } #endregion if (refreshDirectories > 0) { AppSettings.Default.RefreshDirctories = true; } if (refreshSettings > 0) { AppSettings.Default.ReloadSettings = true; } Logger.LogComment("SimpleHTTPServer: saving appsettings"); AppSettings.Default.Save(); } // return the default page back: if (context.Request.QueryString.Get("COMMAND") == "PAGE_UPLOADFILE" || context.Request.QueryString.Get("COMMAND") == "UTILITY_UPLOADFILE" || context.Request.QueryString.Get("COMMAND") == "UTILITY_DELETEFILE") { GetUploadPage(ImageUploaded); } else if (context.Request.RawUrl == "/AppData.htm") { GetAppDataPage(); } else { GetDefaultPage(); } string filename = context.Request.Url.AbsolutePath; filename = filename.Substring(1); if (string.IsNullOrEmpty(filename)) { foreach (string indexFile in _indexFiles) { if (File.Exists(Path.Combine(_rootDirectory, indexFile))) { filename = indexFile; break; } } } filename = Path.Combine(_rootDirectory, filename); //need to change 2nd condition later, need a more appropriate condition if (File.Exists(filename) && !filename.Contains("upload.htm") && context.Request.RawUrl != "/AppData.htm" && context.Request.RawUrl != "/Log.htm" && context.Request.RawUrl != "/ico/favicon-32x32.png") { try { Stream input = new FileStream(filename, FileMode.Open); //Adding permanent http response headers string mime; context.Response.ContentType = _mimeTypeMappings.TryGetValue(Path.GetExtension(filename), out mime) ? mime : "application/octet-stream"; context.Response.ContentLength64 = input.Length; context.Response.AddHeader("Date", DateTime.Now.ToString("r")); context.Response.AddHeader("Last-Modified", System.IO.File.GetLastWriteTime(filename).ToString("r")); byte[] 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) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; Debug.WriteLine("Exception processing HTTPContext: " + ex.ToString()); } } else { string response = GetDefaultPage(); if (context.Request.QueryString.Get("COMMAND") == "PAGE_UPLOADFILE" || context.Request.QueryString.Get("COMMAND") == "UTILITY_UPLOADFILE" || context.Request.QueryString.Get("COMMAND") == "UTILITY_DELETEFILE") { response = GetUploadPage(ImageUploaded); } else if (context.Request.RawUrl == "/AppData.htm") { response = GetAppDataPage(); } else if (context.Request.RawUrl == "/Log.htm") { response = GetLogPage(); } else { response = GetDefaultPage(); } byte[] buffer = Encoding.ASCII.GetBytes(response); context.Response.OutputStream.Write(buffer, 0, buffer.Length); context.Response.OutputStream.Flush(); context.Response.StatusCode = (int)HttpStatusCode.OK; } context.Response.OutputStream.Close(); }