/// <summary> /// Unpacks the URL of the Request into a WalkableQueue. /// </summary> /// <param name="request">The HttpRequest to take the URL from.</param> /// <returns>A Walkable Queue containing the URL.</returns> public WalkableQueue <Tuple <ID, string> > UnpackUrlActions(HttpRequest request) { List <string> links = request.RequestUrl.Split('/').ToList(); links.Remove(""); WalkableQueue <Tuple <ID, string> > ret = new WalkableQueue <Tuple <ID, string> >(); ret.Push(new Tuple <ID, string>(new ID(new ulong[] { 0xFFFFFFFFul }), null)); for (int i = 0; i < links.Count; i++) { if (string.IsNullOrWhiteSpace(links[i])) { break; } string action = request.VariablesHttpHead[$"action_{i}"]; try { ret.Push(new Tuple <ID, string>(new ID(links[i]), action.DecodeUrl())); } catch { break; } } return(ret); }
/// <inheritdoc /> public HttpResponse GetResponse(HttpRequest requestPacket, System.Diagnostics.Stopwatch currentStopwatch) { SessionData sessionData = new HttpSessionData(requestPacket); WalkableQueue <Tuple <ID, string> > unpackedUrls = UnpackUrlActions(requestPacket); HElement e = _debugNode.GetContents(sessionData, unpackedUrls.Peek()?.Item2, unpackedUrls); PageBuilder pb = new PageBuilder("LamestWebserver Debug View") { StylesheetCode = StyleSheet, Elements = { new HContainer { Elements ={ e }, Class = "main" }, new HContainer { Elements = { new HText($"LamestWebserver DebugView v{typeof(DebugResponse).Assembly.GetName().Version}") }, Class = "footer" } } }; return(new HttpResponse(requestPacket, pb.GetContent(sessionData)) { Cookies = ((HttpSessionData)(sessionData)).Cookies.ToList() }); }
/// <summary> /// Creates a link to another DebugNode. /// </summary> /// <param name="text">The text of the link.</param> /// <param name="subUrl">The subUrl to link to.</param> /// <param name="walkableQueue">The current walkableQueue.</param> /// <param name="requestedAction">The requested Action for the linked DebugNode.</param> /// <returns>The link as HLink.</returns> public static HLink GetLink(string text, ID subUrl, WalkableQueue <Tuple <ID, string> > walkableQueue, string requestedAction = null) { if (text == null) { throw new ArgumentNullException(nameof(text)); } if (subUrl == null) { throw new ArgumentNullException(nameof(subUrl)); } if (walkableQueue == null) { throw new ArgumentNullException(nameof(walkableQueue)); } List <Tuple <ID, string> > already = walkableQueue.GetPassed(); if (!walkableQueue.AtEnd()) { already.Add(walkableQueue.Peek()); } string ret = "/"; foreach (Tuple <ID, string> tuple in already) { ret += tuple.Item1 + "/"; } ret += "?"; for (int i = 0; i < already.Count - 1; i++) { if (!string.IsNullOrEmpty(already[i].Item2)) { ret += $"action_{i}={already[i].Item2}&"; } } if (!string.IsNullOrEmpty(requestedAction)) { ret += $"action_{already.Count - 1}={requestedAction.EncodeUrl()}"; } return(new HLink(text, ret)); }
/// <summary> /// Creates a link to another DebugNode. /// </summary> /// <param name="text">The text of the link.</param> /// <param name="subUrl">The subUrl to link to.</param> /// <param name="walkableQueue">The current walkableQueue.</param> /// <param name="position">The maximum Position to get from in the walkable queue.</param> /// <param name="requestedAction">The requested Action for the linked DebugNode.</param> /// <returns>The link as HLink.</returns> public static HLink GetLink(string text, ID subUrl, WalkableQueue <Tuple <ID, string> > walkableQueue, int position, string requestedAction = null) { if (text == null) { throw new ArgumentNullException(nameof(text)); } if (subUrl == null) { throw new ArgumentNullException(nameof(subUrl)); } if (walkableQueue == null) { throw new ArgumentNullException(nameof(walkableQueue)); } List <Tuple <ID, string> > nodes = walkableQueue.GetRange(1, position); string ret = "/"; foreach (Tuple <ID, string> tuple in nodes) { ret += tuple.Item1 + "/"; } ret += subUrl + "?"; for (int i = 0; i < nodes.Count - 1; i++) { if (!string.IsNullOrEmpty(nodes[i].Item2)) { ret += $"action_{i}={nodes[i].Item2}&"; } } if (!string.IsNullOrEmpty(requestedAction)) { ret += $"action_{nodes.Count - 1}={requestedAction.EncodeUrl()}"; } return(new HLink(text, ret)); }
/// <inheritdoc /> public override HElement GetContents(SessionData sessionData, string requestedAction, WalkableQueue <Tuple <ID, string> > positionQueue) { if (positionQueue.AtEnd()) { HElement list = new HList(HList.EListType.UnorderedList, (from s in _subNodes select GetLink(s.Value.Name, s.Key, positionQueue, positionQueue.Position, null)).ToArray()) { Class = "subnodes" }; if (((HList)list).IsEmpty()) { list = new HItalic("This DebugNode includes no Subnodes.") { Class = "subnodes" } } ; else { list = new HHeadline("Subnodes of this DebugNode", 3) { Class = "subnodes" } } +list; return(new HHeadline(Name) + new HLine() { Class = "start" } +(_description == null ? new HText(_description) : (HElement) new HString()) + new HContainer(GetElements(sessionData)) + new HLine() { Class = "subnodes" } +list); } else { HLink navlink = GetLink(this); navlink.Class = "nav"; HInlineContainer name = new HInlineContainer() { Elements = { navlink } }; DebugResponseNode node = null; positionQueue.Pop(); if (_subNodes) { node = _subNodes[positionQueue.Peek().Item1]; } if (ReferenceEquals(node, null)) { HElement list = new HList(HList.EListType.UnorderedList, (from s in _subNodes select GetLink(s.Value.Name, s.Value.URL)).ToArray()) { Class = "subnodes" }; if (((HList)list).IsEmpty()) { list = new HItalic("This DebugNode includes no Subnodes.") { Class = "subnodes" } } ; else { list = new HHeadline("Subnodes of this DebugNode", 3) { Class = "subnodes" } } +list; return(name + new HHeadline(Name) + new HLine() { Class = "start" } +new HText($"The ID '{positionQueue.Peek().Item1.Value}' is not a child of this {nameof(DebugContainerResponseNode)}.") { Class = "invalid" } +new HLine() { Class = "subnodes" } +list); } else { return(name + node.GetContents(sessionData, positionQueue.Peek().Item2, positionQueue)); } } }
/// <summary> /// Retrieves the contents of the DebugView node as HElement. /// </summary> /// <param name="sessionData">The current SessionData.</param> /// <param name="requestedAction">The requested Action for this particular node (if any).</param> /// <param name="walkableQueue">The current WalkableQueue containing all Subnodes of the requested URL.</param> /// <returns></returns> public abstract HElement GetContents(SessionData sessionData, string requestedAction, WalkableQueue <Tuple <ID, string> > walkableQueue);
public void TestWalkableQueue() { WalkableQueue <string> walkableQueue = new WalkableQueue <string>(); Assert.AreEqual(0, walkableQueue.Count); Assert.IsTrue(walkableQueue.AtEnd()); walkableQueue.Push(""); Assert.IsTrue(walkableQueue.AtEnd()); Assert.AreEqual(1, walkableQueue.Count); Assert.AreEqual("", walkableQueue.Peek()); Assert.AreEqual("", walkableQueue.Pop()); Assert.IsTrue(walkableQueue.AtEnd()); walkableQueue.Push("a"); Assert.AreEqual(2, walkableQueue.Count); Assert.AreEqual("a", walkableQueue.Peek()); Assert.AreEqual("", walkableQueue.Current); Assert.AreEqual("a", walkableQueue.Pop()); Assert.IsTrue(walkableQueue.AtEnd()); List <string> passed = walkableQueue.GetPassed(); Assert.AreEqual(2, passed.Count); Assert.AreEqual("", passed[0]); Assert.AreEqual("a", passed[1]); walkableQueue.Push("b"); walkableQueue.Push("c"); walkableQueue.Push("d"); List <string> consumable = walkableQueue.GetConsumable(); Assert.AreEqual(3, consumable.Count); Assert.AreEqual("b", consumable[0]); Assert.AreEqual("c", consumable[1]); Assert.AreEqual("d", consumable[2]); Assert.IsFalse(walkableQueue.AtEnd()); Assert.AreEqual(5, walkableQueue.Count); Assert.AreEqual("a", walkableQueue.Current); Assert.AreEqual("b", walkableQueue.Peek()); Assert.IsFalse(walkableQueue.AtEnd()); Assert.AreEqual("b", walkableQueue.Pop()); Assert.IsFalse(walkableQueue.AtEnd()); Assert.AreEqual("c", walkableQueue.Peek()); Assert.AreEqual("b", walkableQueue.Current); Assert.IsFalse(walkableQueue.AtEnd()); Assert.AreEqual("c", walkableQueue.Pop()); Assert.AreEqual("c", walkableQueue.Current); Assert.IsTrue(walkableQueue.AtEnd()); Assert.AreEqual("d", walkableQueue.Pop()); Assert.IsTrue(walkableQueue.AtEnd()); List <string> all = walkableQueue.GetAll(); Assert.AreEqual(5, all.Count); walkableQueue.ResetPosition(); Assert.AreEqual("", walkableQueue.Pop()); Assert.AreEqual("a", walkableQueue.Pop()); Assert.AreEqual("b", walkableQueue.Pop()); Assert.AreEqual("c", walkableQueue.Pop()); Assert.AreEqual("d", walkableQueue.Pop()); walkableQueue.Clear(); Assert.AreEqual(0, walkableQueue.Count); walkableQueue = new WalkableQueue <string>(new List <string> { "0", "1", "2", "3", "4" }); Assert.AreEqual(5, walkableQueue.Count); for (int i = 0; i < 5; i++) { Assert.AreEqual(i.ToString(), walkableQueue[i]); walkableQueue[i] = new string('-', i); Assert.AreEqual(new string('-', i), walkableQueue[i]); } int index = 0; foreach (string s in walkableQueue) { Assert.AreEqual(index++, s.Length); } List <string> range = walkableQueue.GetRange(2, 2); index = 2; foreach (string s in range) { Assert.AreEqual(index++, s.Length); } }