HtmlUtility: Helper functions for Html text building.

This commit is contained in:
2019-05-04 11:00:51 +02:00
parent 33ee2d4a47
commit 37025d7c07
3 changed files with 204 additions and 2 deletions

View File

@@ -30,9 +30,38 @@ namespace VAR.HttpServer.MiniServerTest
{ {
public void HandleRequest(HttpProcessor p) 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.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");
});
});
});
} }
} }
} }

View File

@@ -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("&amp;");
break;
case '>':
output.Append("&gt;");
break;
case '<':
output.Append("&lt;");
break;
case '"':
output.Append("&quot;");
break;
case '\'':
output.Append("&#39;");
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<string, string> attributes = null, Action writeBody = null, string body = null)
{
processor.OutputStream.Write("<{0}", tag);
if (attributes != null)
{
foreach (KeyValuePair<string, string> 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("</{0}>", tag);
}
public static void WriteDocument(this HttpProcessor processor, string title, Action writeHead = null, Action writeBody = null, Dictionary<string, string> bodyAttributes = null)
{
processor.OutputStream.WriteLine("<!DOCTYPE html>");
processor.WriteTag("html", null, () =>
{
processor.WriteTag("head", writeBody: () =>
{
processor.WriteTag("meta", new Dictionary<string, string> { { "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<string, string> attributes = null, Action writeBody = null)
{
processor.WriteTag("p", attributes, writeBody: writeBody, body: text);
}
public static void WriteHeading1(this HttpProcessor processor, string text, Dictionary<string, string> attributes = null, Action writeBody = null)
{
processor.WriteTag("h1", attributes, writeBody: writeBody, body: text);
}
public static void WriteHeading2(this HttpProcessor processor, string text, Dictionary<string, string> attributes = null, Action writeBody = null)
{
processor.WriteTag("h2", attributes, writeBody: writeBody, body: text);
}
public static void WriteHeading3(this HttpProcessor processor, string text, Dictionary<string, string> 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<string, string> attributes = new Dictionary<string, string>
{
{ "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<string, string> attributes = new Dictionary<string, string>
{
{ "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);
}
}
}

View File

@@ -41,6 +41,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="HtmlUtility.cs" />
<Compile Include="HttpProcessor.cs" /> <Compile Include="HttpProcessor.cs" />
<Compile Include="HttpServer.cs" /> <Compile Include="HttpServer.cs" />
<Compile Include="HttpUtility.cs" /> <Compile Include="HttpUtility.cs" />