Hello Nart,
First, thanks for the great library, it was exactly what I needed! However, I did run into a few small problems while using it. First, your redirecting function was accidentally hard-coded with Goolge as the target:
public void Redirect(string Url) { string rs = "<meta http-equiv="refresh" content="0; url=http://www.google.com">"; byte[] databytes = UTF8Encoding.UTF8.GetBytes(rs); this.connection.Send(databytes, 0, databytes.Length, SocketFlags.None); }
It also doesn't send a header when called, so my browser treated it as plain text. To fix that, I modified it a little like this:
public void Redirect(string Url) { string rs = "<meta http-equiv="refresh" content="0; url=" + Url + "">"; byte[] headerbytes = UTF8Encoding.UTF8.GetBytes(HtmlPageHeader + "; charset=utf-8rnContent-Length: " + rs.Length.ToString() + "rnrn"); byte[] databytes = UTF8Encoding.UTF8.GetBytes(rs); connection.Send(headerbytes, 0, headerbytes.Length, SocketFlags.None); connection.Send(databytes, 0, databytes.Length, SocketFlags.None); }
Similarly, the Write function doesn't send a header, so the HTML content sent also rendered as plain text, so I added the header there as well:
public void Write(string Str) { byte[] Data = UTF8Encoding.UTF8.GetBytes(Str); int datalength = Data.Length; byte[] HEADER = UTF8Encoding.UTF8.GetBytes(HtmlPageHeader + "; charset=utf-8rnContent-Length: " + datalength.ToString() + "rnrn"); connection.Send(HEADER, 0, HEADER.Length, SocketFlags.None); int i = 0; while (datalength > 256) { connection.Send(Data, i, 256, SocketFlags.None); i += 256; datalength -= 256; } connection.Send(Data, i, datalength, SocketFlags.None); }
Lastly, I was having problems with GET variables, I was getting false positives. For instance, if I asked for the following URL:
http://myserver.com/test.htm?r=5
This would happen:
Debug.Print(request["t"]);//Output ==> 5
It turns out, the function to parse GET variables was finding the "t" in "test.htm" and then finding "=" and putting them together. So I modified it a little bit using RegEx to only match the full requested value. It also saves a little time, and becomes a bit more robust, by only looking at the first line of the request, since that's the only line where GET variables should be.
public string this[string value] { get { string firstLine = request_buffer.Substring(0, request_buffer.IndexOf("rn")); int IndexOfAndOrSpace; Match contains = Regex.Match(firstLine, @"[?,&]" + value + "="); if (contains.Success) { int IndexOfValue = firstLine.IndexOf(contains.Value); for (IndexOfAndOrSpace = IndexOfValue + 1; firstLine[IndexOfAndOrSpace] != '&' && firstLine[IndexOfAndOrSpace] != ' '; IndexOfAndOrSpace++) ; int Length = IndexOfAndOrSpace - (IndexOfValue + contains.Length); return firstLine.Substring(IndexOfValue + contains.Length, Length); } else return null; }
For personal use, I also worked in MIME types for CSS, .png files, XML, JavaScript files, and JASON, as well as cookie support (for simple cookie based authentication) and the ability to leverage browser caching to lessen the server load for repeated requests. If anyone is interested, just let me know and I'll be happy to share that code.
Anyway, thanks again for the great library! I hope these suggestions prove helpful.