/// <summary> /// Parse raw http request data into usable HTTPRequest form /// </summary> /// <param name="rawData">Raw HTTP request data</param> public static HTTPRequest Parse(string rawData) { if (rawData != null) { HTTPHeader header = HTTPHeader.Parse(rawData); //split raw data into lines string[] lines = rawData.Split(new char[] { '\n' }); for (int i = 0; i < lines.Length; i++) { //remove line breaks and carriage returns lines[i] = lines[i].Replace("\n", "").Replace("\r", ""); } //take first line, split at all spaces, first string should be method string method = lines[0].Words()[0]; //choose corelating method Type extractedType = Type.DELETE; switch (method) { case "GET": extractedType = Type.GET; break; case "HEAD": extractedType = Type.HEAD; break; case "POST": extractedType = Type.POST; break; case "PUT": extractedType = Type.PUT; break; case "DELETE": extractedType = Type.DELETE; break; case "TRACE": extractedType = Type.TRACE; break; case "CONNECT": extractedType = Type.CONNECT; break; default: break; } //take first line, split at all spaces, second string should be URL. string url = lines[0].Words()[1]; //http version is everything after HTTP/ string version = lines[0].Substring(lines[0].IndexOf("HTTP/") + 5); string body = rawData.Substring(rawData.IndexOf("\r\n\r\n") + 4); return(new HTTPRequest(extractedType, url, version, header, body)); } return(null); }