/// <summary> /// Parst den Request und erzeugt ein Request-Objekt /// </summary> /// <param name="request">Der Request in Stringform</param> /// <returns>Der Request als Objekt</returns> public static Request Parse(string request, string client) { if (string.IsNullOrWhiteSpace(request)) { return(null); } var split = request.Split('\n'); var methode = string.Join("\n", split.Take(1)); var options = split.Skip(1).TakeWhile(x => !string.IsNullOrWhiteSpace(x)).ToList(); var content = string.Join("\n", split.SkipWhile(x => !string.IsNullOrWhiteSpace(x))).Trim(); var match = Regex.Match(methode, @"^(GET|POST|PUT|DELETE) (.*) (.*)$", RegexOptions.IgnoreCase); var type = RequestMethod.NONE; Match m = null; if (match.Groups.Count >= 2 && match.Groups[1].Success) { switch (match.Groups[1].Value.ToLower()) { case "get": type = RequestMethod.GET; break; case "post": type = RequestMethod.POST; break; case "put": type = RequestMethod.PUT; break; case "delete": type = RequestMethod.GET; break; case "head": type = RequestMethod.HEAD; break; } } var url = ""; if (match.Groups.Count >= 3 && match.Groups[2].Success) { url = match.Groups[2].Value; m = Regex.Match(url, @"^(.*)\?(.*)$", RegexOptions.IgnoreCase); if (m.Success && m.Groups.Count >= 3 && match.Groups[1].Success && match.Groups[2].Success) { url = m.Groups[1].Value; } } var version = ""; if (match.Groups.Count >= 4 && match.Groups[3].Success) { version = match.Groups[3].Value; } var req = new Request(type, url, version, RequestHeaderFields.Parse(options), content, client); if (m.Success && m.Groups.Count >= 3 && match.Groups[1].Success && match.Groups[2].Success) { foreach (var v in m.Groups[2].Value.Split('&')) { Regex regex = new Regex("[=]{1}"); var kv = regex.Split(v, 2); req.AddParam(kv[0], kv.Count() > 1 ? kv[1] : ""); } } return(req); }