diff --git a/VAR.HttpServer.MiniServerTest/Program.cs b/VAR.HttpServer.MiniServerTest/Program.cs index 71357c4..03ce54b 100644 --- a/VAR.HttpServer.MiniServerTest/Program.cs +++ b/VAR.HttpServer.MiniServerTest/Program.cs @@ -30,9 +30,38 @@ namespace VAR.HttpServer.MiniServerTest { public void HandleRequest(HttpProcessor p) { - Console.WriteLine("Responding to {0}", p.Socket.Client.RemoteEndPoint); + Console.WriteLine("Responding to {0} requesting: {1}", p.Socket.Client.RemoteEndPoint, p.HttpResource); + + // Process + string greetedName = "World"; + if (p.HttpParams.ContainsKey("txtName")) + { + greetedName = p.HttpParams["txtName"]; + } + if (p.HttpParams.ContainsKey("btnReset")) + { + greetedName = "World"; + } + string strGreeting = string.Format("Hello {0}!", greetedName); + + // Render p.ResponseSuccess(); - p.OutputStream.WriteLine("Hello World!"); + p.WriteDocument(strGreeting, writeBody: () => + { + p.WriteHeading2(strGreeting); + p.WriteForm("post", "", () => + { + p.WriteParagraph(null, writeBody: () => + { + p.WriteTextbox("txtName", greetedName); + }); + p.WriteParagraph(null, writeBody: () => + { + p.WriteButton("btnRefresh", "Refresh"); + p.WriteButton("btnReset", "Reset"); + }); + }); + }); } } } diff --git a/VAR.HttpServer/HtmlUtility.cs b/VAR.HttpServer/HtmlUtility.cs new file mode 100644 index 0000000..a4eceee --- /dev/null +++ b/VAR.HttpServer/HtmlUtility.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace VAR.HttpServer +{ + public static class HtmlUtility + { + public static string HtmlEncode(string s) + { + if (string.IsNullOrEmpty(s)) + { + return s; + } + + int len = s.Length; + + bool needEncode = false; + for (int i = 0; i < len; i++) + { + char c = s[i]; + if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159 || c == '\'') + { + needEncode = true; + break; + } + } + if (needEncode == false) { return s; } + + StringBuilder output = new StringBuilder(); + for (int i = 0; i < len; i++) + { + char c = s[i]; + switch (c) + { + case '&': + output.Append("&"); + break; + case '>': + output.Append(">"); + break; + case '<': + output.Append("<"); + break; + case '"': + output.Append("""); + break; + case '\'': + output.Append("'"); + break; + + default: + if (c > 159 && c < 256) + { + output.Append("&#"); + output.Append(Convert.ToString((int)c)); + output.Append(";"); + } + else + { + output.Append(c); + } + break; + } + } + + return output.ToString(); + } + + public static void WriteTag(this HttpProcessor processor, string tag, Dictionary attributes = null, Action writeBody = null, string body = null) + { + processor.OutputStream.Write("<{0}", tag); + if (attributes != null) + { + foreach (KeyValuePair attribute in attributes) + { + processor.OutputStream.Write(" {0}=\"{1}\"", attribute.Key, attribute.Value); + } + } + if (writeBody == null && body == null) + { + processor.OutputStream.Write(" />"); + return; + } + processor.OutputStream.Write(">"); + if (string.IsNullOrEmpty(body) == false) + { + processor.OutputStream.Write(body); + } + writeBody?.Invoke(); + processor.OutputStream.Write("", tag); + } + + public static void WriteDocument(this HttpProcessor processor, string title, Action writeHead = null, Action writeBody = null, Dictionary bodyAttributes = null) + { + processor.OutputStream.WriteLine(""); + processor.WriteTag("html", null, () => + { + processor.WriteTag("head", writeBody: () => + { + processor.WriteTag("meta", new Dictionary { { "charset", "utf-8" } }); + processor.WriteTag("title", body: title); + writeHead?.Invoke(); + }); + processor.WriteTag("body", attributes: bodyAttributes, writeBody: writeBody); + }); + } + + public static void WriteParagraph(this HttpProcessor processor, string text, Dictionary attributes = null, Action writeBody = null) + { + processor.WriteTag("p", attributes, writeBody: writeBody, body: text); + } + + public static void WriteHeading1(this HttpProcessor processor, string text, Dictionary attributes = null, Action writeBody = null) + { + processor.WriteTag("h1", attributes, writeBody: writeBody, body: text); + } + + public static void WriteHeading2(this HttpProcessor processor, string text, Dictionary attributes = null, Action writeBody = null) + { + processor.WriteTag("h2", attributes, writeBody: writeBody, body: text); + } + + public static void WriteHeading3(this HttpProcessor processor, string text, Dictionary attributes = null, Action writeBody = null) + { + processor.WriteTag("h3", attributes, writeBody: writeBody, body: text); + } + + public static void WriteForm(this HttpProcessor processor, string method, string action, Action writeBody = null) + { + Dictionary attributes = new Dictionary + { + { "method", method }, + { "action", action }, + }; + processor.WriteTag("form", attributes: attributes, writeBody: writeBody); + } + + public static void WriteInput(this HttpProcessor processor, string type, string name, string value = null, int size = 0) + { + Dictionary attributes = new Dictionary + { + { "type", type }, + { "name", name }, + }; + if (size != 0) + { + attributes.Add("size", Convert.ToString(size)); + } + if (string.IsNullOrEmpty(value) == false) + { + attributes.Add("value", value); + } + processor.WriteTag("input", attributes); + } + + public static void WriteButton(this HttpProcessor processor, string name, string text) + { + WriteInput(processor, "submit", name, HtmlEncode(text)); + } + + public static void WriteTextbox(this HttpProcessor processor, string name, string value = null, int size = 0) + { + WriteInput(processor, "text", name, value, size); + } + + public static void WriteHidden(this HttpProcessor processor, string name, string value = null, int size = 0) + { + WriteInput(processor, "hidden", name, value, size); + } + } +} diff --git a/VAR.HttpServer/VAR.HttpServer.csproj b/VAR.HttpServer/VAR.HttpServer.csproj index b12f637..6626fbc 100644 --- a/VAR.HttpServer/VAR.HttpServer.csproj +++ b/VAR.HttpServer/VAR.HttpServer.csproj @@ -41,6 +41,7 @@ +