public HttpResponseMessage getResponse(HttpListenerRequest req) { if (req.AcceptTypes == null) { //TODO: topkek return new HttpResponseMessage(HttpStatusCode.NotAcceptable); } var msg = new HttpResponseMessage(); var ruri = req.RawUrl; if (ruri.Contains("?")) { ruri = ruri.Remove(ruri.IndexOf("?")); } var locList = ruri.Split('/').ToList(); locList.RemoveAll(a => a == ""); msg.StatusCode = HttpStatusCode.OK; LineLog.Debug("rendering response..."); return(this.Render(req, locList.ToArray())); //<html><head><script src='/script.js'></script></head><body><button id='button1' style='width:50px' onclick='javascript:startProgress();'>Start</button></body></html> }
public static LoadSaveResult SaveLoadouts(string filename = "loads.json") { LineLog.Debug($"Saving loads to {filename}"); if (Loads.Count > 0) { try { // keep a single recent backup if (File.Exists(filename)) { File.Copy(filename, filename + ".backup", true); } var str = JsonConvert.SerializeObject(Loads, Formatting.Indented); File.WriteAllText(filename, str); return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while saving loads {e.GetType()}", e); throw; } } return(LoadSaveResult.EmptyFile); }
public static LoadSaveResult LoadBuilds(string filename = "builds.json") { if (!File.Exists(filename)) { LineLog.Error($"{filename} wasn't found."); return(LoadSaveResult.FileNotFound); } LineLog.Debug($"Loading {filename} as builds."); var bstr = File.ReadAllText(filename); return(LoadBuildData(bstr)); }
public static LoadSaveResult SaveBuilds(string filename = "builds.json") { LineLog.Debug($"Saving builds to {filename}"); // TODO: fix this mess foreach (Build bb in Builds) { if (bb.Mon != null && bb.Mon.FullName != "Missingno") { if (!bb.DownloadAwake || (Program.Data.GetMonster(bb.Mon.FullName) != null && Program.Data.GetMonster(bb.Mon.FullName).FullName != "Missingno")) { bb.MonName = bb.Mon.FullName; bb.MonId = bb.Mon.Id; } else { if (Program.Data.GetMonster(bb.Mon.Id).FullName != "Missingno") { bb.MonId = bb.Mon.Id; bb.MonName = Program.Data.GetMonster(bb.Mon.Id).FullName; } } } } // only write if there are builds, may save some files if (Program.Builds.Count > 0) { try { // keep a single recent backup if (File.Exists(filename)) { File.Copy(filename, filename + ".backup", true); } var str = JsonConvert.SerializeObject(Program.Builds, Formatting.Indented); File.WriteAllText(filename, str); return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while saving builds {e.GetType()}", e); throw; //MessageBox.Show(e.ToString()); } } return(LoadSaveResult.Failure); }
public static LoadSaveResult SaveGoals(string filename = "goals.json") { LineLog.Debug($"Saving loads to {filename}"); try { // keep a single recent backup var str = JsonConvert.SerializeObject(Goals, Formatting.Indented); File.WriteAllText(filename, str); return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while saving loads {e.GetType()}", e); throw; } }
public static LoadSaveResult LoadGoals(string filename = "goals.json") { try { LineLog.Debug("Loading goals"); if (File.Exists(filename)) { string text = File.ReadAllText(filename); Goals = JsonConvert.DeserializeObject <Goals>(text); } else { Goals = new Goals(); } return(LoadSaveResult.Success); } catch (Exception e) { LineLog.Error($"Error while loading goals {e.GetType()}", e); File.WriteAllText("error_goals.txt", e.ToString()); throw; } }
public async void RemoteManageLoop(HttpListenerContext context) { try { LineLog.Debug("serving: " + context.Request.RawUrl); var req = context.Request; var resp = context.Response; var msg = getResponse(req); resp.StatusCode = (int)msg.StatusCode; LineLog.Debug("returning: " + resp.StatusCode); foreach (var h in msg.Headers) { foreach (var v in h.Value) { resp.Headers.Add(h.Key, v); } } if (resp.StatusCode != 303) { using (var output = resp.OutputStream) { byte[] outBytes = Encoding.UTF8.GetBytes("Critical Failure"); bool expires = false; if (msg.Content is StringContent) { var qw = msg.Content as StringContent; string qq = await qw.ReadAsStringAsync(); resp.ContentType = mimeTypes.Where(t => req.Url.AbsolutePath.EndsWith(t.Key)).FirstOrDefault().Value; if (resp.ContentType == null) { resp.ContentType = "text/html"; // Default to HTML } outBytes = Encoding.UTF8.GetBytes(qq); } else if (msg.Content is FileContent) { var qw = msg.Content as FileContent; resp.ContentType = qw.Type; if (resp.ContentType == null) { resp.ContentType = "application/octet-stream"; // Should always be set, but bin just incase } //resp.Headers.Add("Content-Disposition", $"attachment; filename=\"{qw.FileName}\""); outBytes = await qw.ReadAsByteArrayAsync(); } else if (msg.Content is ByteArrayContent) { var qw = msg.Content as ByteArrayContent; resp.ContentType = mimeTypes.Where(t => req.Url.AbsolutePath.EndsWith(t.Key)).FirstOrDefault().Value; if (resp.ContentType == null) { resp.ContentType = "application/octet-stream"; // Default to binary } outBytes = await qw.ReadAsByteArrayAsync(); } else if (msg.Content is StreamContent) { var qw = msg.Content as StreamContent; using (var ms = new MemoryStream()) { var stream = await qw.ReadAsStreamAsync(); stream.CopyTo(ms); //resp.ContentType = "application/octet-stream" outBytes = ms.ToArray(); } } //resp.Headers.Add("Content-language", "en-au"); resp.Headers.Add("Access-Control-Allow-Origin: *"); resp.Headers.Add("Access-Control-Allow-Methods: *"); if (expires) { resp.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate"); resp.Headers.Add("Pragma", "no-cache"); resp.Headers.Add("Expires", "Wed, 16 Jul 1969 13:32:00 UTC"); } if ((outBytes.Length > 0) && (resp.ContentType != "image/png")) // Don't compress empty responses or compressed file types { var enc = req.Headers.GetValues("Accept-Encoding"); if (enc?.Contains("gzip") ?? false) { using (MemoryStream ms = new MemoryStream()) using (System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress)) { gs.Write(outBytes, 0, outBytes.Length); gs.Flush(); gs.Close(); // https://stackoverflow.com/questions/3722192/how-do-i-use-gzipstream-with-system-io-memorystream#comment3929538_3722263 ms.Flush(); outBytes = ms.ToArray(); } resp.Headers.Add("Content-Encoding", "gzip"); } else if (enc?.Any(a => a.ToLowerInvariant() == "deflate") ?? false) { using (MemoryStream ms = new MemoryStream()) { using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress)) { ds.Write(outBytes, 0, outBytes.Length); ds.Flush(); } ms.Flush(); outBytes = ms.ToArray(); } resp.Headers.Add("Content-Encoding", "deflate"); } } resp.ContentLength64 = outBytes.Length; try { output.Write(outBytes, 0, outBytes.Length); } catch (Exception ex) { Program.LineLog.Error("Failed to write " + ex.GetType().ToString() + " " + ex.Message); } } } else { resp.OutputStream.Close(); } } catch (Exception e) { LineLog.Error("Something went wrong.", e); var resp = context.Response; resp.StatusCode = 500; using (var output = resp.OutputStream) { byte[] outBytes = Encoding.UTF8.GetBytes(e.GetType() + ": " + e.Message); resp.ContentLength64 = outBytes.Length; try { output.Write(outBytes, 0, outBytes.Length); } catch (Exception ex) { Program.LineLog.Error("Failed to write " + ex.GetType().ToString() + " " + ex.Message); } } } }
public static void SanitizeBuilds() { LineLog.Debug("processing builds"); int current_pri = 1; foreach (Build b in Program.Builds.OrderBy(bu => bu.Priority)) { int id = b.ID; if (b.ID == 0 || Program.Builds.Where(bu => bu != b).Select(bu => bu.ID).Any(bid => bid == b.ID)) { //id = buildList.Items.Count + 1; id = 1; while (Program.Builds.Any(bu => bu.ID == id)) { id++; } foreach (var lb in Program.Builds.Where(bu => bu.LinkId == b.ID)) { lb.LinkId = id; } b.ID = id; } if (b.Type == BuildType.Lock) { b.Priority = 0; } else { b.Priority = current_pri++; } // make sure bad things are removed foreach (var ftab in b.RuneFilters) { foreach (var filter in ftab.Value) { if (filter.Key == "SPD") { filter.Value.Percent = null; } if (filter.Key == "ACC" || filter.Key == "RES" || filter.Key == "CR" || filter.Key == "CD") { filter.Value.Flat = null; } } } // upgrade builds, hopefully while (b.VERSIONNUM < Create.VERSIONNUM) { switch (b.VERSIONNUM) { case 0: // unversioned to 1 b.Threshold = b.Maximum; b.Maximum = new Stats(); break; case 1: foreach (var tabN in b.RuneScoring.Keys.ToArray()) { var tab = b.RuneScoring[tabN]; if (tab.Type == FilterType.SumN) { tab.Count = (int?)(tab.Value); tab.Value = null; b.RuneScoring[tabN] = tab; } } break; case 2: b.AutoRuneAmount = Build.AutoRuneAmountDefault; break; } b.VERSIONNUM++; } } }