/// <summary> /// Optionen parsen /// </summary> /// <param name="options"></param> /// <returns></returns> public static RequestHeaderFields Parse(List <string> options) { var obj = new RequestHeaderFields() { Host = GetOptionsValue(options, "Host"), Connection = GetOptionsValue(options, "Connection"), ContentType = GetOptionsValue(options, "Content-Type"), ContentLanguage = GetOptionsValue(options, "Content-Language"), UserAgent = GetOptionsValue(options, "User-Agent"), AcceptEncoding = GetOptionsValue(options, "Accept-Encoding") }; var authorization = GetOptionsValue(options, "Authorization"); obj.Authorization = string.IsNullOrWhiteSpace(authorization) ? null : RequestAuthorization.Parse(authorization); try { obj.ContentLength = Convert.ToInt32(GetOptionsValue(options, "Content-Length")); } catch { obj.ContentLength = -1; } var cookie = GetOptionsValue(options, "Cookie"); if (!string.IsNullOrWhiteSpace(cookie)) { obj.Cookies = (from c in cookie.Split(';') select new Cookie(c)).ToList(); } return(obj); }
/// <summary> /// Konstruktor /// </summary> protected Request(RequestMethod type, string url, string version, RequestHeaderFields options, string content, string client) { Method = type; URL = url; Param = new Dictionary <string, Parameter>(); Version = version; HeaderFields = options; Client = client; Content = content; }
/// <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); }