public abstract void Process(HTTP.HttpServer server, HttpPack pack, Socket client);
public static HttpPack Parse(string text) { HttpPack pack = new HttpPack(); bool work = true; int startIndex = 0; int endIndex = 0; while (!isCTL(text[endIndex]) && !isSeparator(text[endIndex])) endIndex++; switch (text.Substring(0, endIndex)) { case "GET": { pack.Method = Method.GET; break; } case "POST": { pack.Method = Method.POST; break; } default: throw new ArgumentException("Invalid http method " + text.Substring(0, endIndex)); } while (char.IsWhiteSpace(text[endIndex])) endIndex++; startIndex = endIndex; while (!char.IsWhiteSpace(text[endIndex])) endIndex++; pack.Path = text.Substring(startIndex, endIndex - startIndex); pack.Request = pack.Path.Substring((pack.Path.Length + pack.Path.IndexOf('?')) % pack.Path.Length + 1); if (pack.Request != "") pack.Path = pack.Path.Substring(0, pack.Path.Length - pack.Request.Length - 1); while (char.IsWhiteSpace(text[endIndex])) endIndex++; startIndex = endIndex; while (!char.IsWhiteSpace(text[endIndex])) endIndex++; pack.Version = text.Substring(startIndex, endIndex - startIndex); while (char.IsWhiteSpace(text[endIndex])) endIndex++; startIndex = endIndex; var encoding = Encoding.Default; while (work) { endIndex = text.IndexOf("\n", startIndex + 1, StringComparison.Ordinal); if (endIndex == -1) break; int i = startIndex; while (!isCTL(text[i]) && !isSeparator(text[i])) i++; string name = text.Substring(startIndex, i - startIndex).ToLower(); if (name == "" || name == "\r") break; while (char.IsWhiteSpace(text[i])) i++; if (text[i] != ':') throw new ArgumentException(); do i++; while (char.IsWhiteSpace(text[i])); if (name == "cookie") { var c = text.Substring(i, endIndex - i).Trim().Split(new[] { '=' }, 2); pack.Cookies.Add(new Cookie(c[0], c.Length > 1 ? c[1] : "")); } else { switch (name) { case "host": { pack.Host = text.Substring(i, endIndex - i).Trim(); break; } case "content-type": { var ct = text.Substring(i, endIndex - i).Trim(); string enc = (ct.IndexOf("charset") != -1) ? ct.Substring(ct.IndexOf("charset") + 8) : encoding.WebName; encoding = Encoding.GetEncoding(enc); break; } default: { pack.Fields[name] = text.Substring(i, endIndex - i).Trim(); break; } } } startIndex = endIndex + 1; } pack.Body = text.Substring(endIndex + 1); if (encoding != Encoding.Default) pack.Body = encoding.GetString(Encoding.Default.GetBytes(pack.Body)); return pack; }
public static HttpPack Parse(string text) { HttpPack pack = new HttpPack(); bool work = true; int startIndex = 0; int endIndex = 0; while (!isCTL(text[endIndex]) && !isSeparator(text[endIndex])) { endIndex++; } switch (text.Substring(0, endIndex)) { case "GET": { pack.Method = Method.GET; break; } case "POST": { pack.Method = Method.POST; break; } default: throw new ArgumentException("Invalid http method " + text.Substring(0, endIndex)); } while (char.IsWhiteSpace(text[endIndex])) { endIndex++; } startIndex = endIndex; while (!char.IsWhiteSpace(text[endIndex])) { endIndex++; } pack.Path = text.Substring(startIndex, endIndex - startIndex); pack.Request = pack.Path.Substring((pack.Path.Length + pack.Path.IndexOf('?')) % pack.Path.Length + 1); if (pack.Request != "") { pack.Path = pack.Path.Substring(0, pack.Path.Length - pack.Request.Length - 1); } while (char.IsWhiteSpace(text[endIndex])) { endIndex++; } startIndex = endIndex; while (!char.IsWhiteSpace(text[endIndex])) { endIndex++; } pack.Version = text.Substring(startIndex, endIndex - startIndex); while (char.IsWhiteSpace(text[endIndex])) { endIndex++; } startIndex = endIndex; var encoding = Encoding.Default; while (work) { endIndex = text.IndexOf("\n", startIndex + 1, StringComparison.Ordinal); if (endIndex == -1) { break; } int i = startIndex; while (!isCTL(text[i]) && !isSeparator(text[i])) { i++; } string name = text.Substring(startIndex, i - startIndex).ToLower(); if (name == "" || name == "\r") { break; } while (char.IsWhiteSpace(text[i])) { i++; } if (text[i] != ':') { throw new ArgumentException(); } do { i++; } while (char.IsWhiteSpace(text[i])); if (name == "cookie") { var c = text.Substring(i, endIndex - i).Trim().Split(new[] { '=' }, 2); pack.Cookies.Add(new Cookie(c[0], c.Length > 1 ? c[1] : "")); } else { switch (name) { case "host": { pack.Host = text.Substring(i, endIndex - i).Trim(); break; } case "content-type": { var ct = text.Substring(i, endIndex - i).Trim(); string enc = (ct.IndexOf("charset") != -1) ? ct.Substring(ct.IndexOf("charset") + 8) : encoding.WebName; encoding = Encoding.GetEncoding(enc); break; } default: { pack.Fields[name] = text.Substring(i, endIndex - i).Trim(); break; } } } startIndex = endIndex + 1; } pack.Body = text.Substring(endIndex + 1); if (encoding != Encoding.Default) { pack.Body = encoding.GetString(Encoding.Default.GetBytes(pack.Body)); } return(pack); }