/// <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"), UserEducation = GetOptionsValue(options, "User-Education"), AcceptEncoding = GetOptionsValue(options, "Accept-Encoding"), AcceptLanguage = GetOptionsValue(options, "Accept-Language") }; 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 client) { Method = type; URL = url; Param = new Dictionary <string, Parameter>(); Version = version; HeaderFields = options; Client = client; }
/// <summary> /// Parst den Request und erzeugt ein Request-Objekt /// </summary> /// <param name="request">Der Request in Stringform</param> /// <returns>Der Request als Objekt</returns> private static Request Parse(ICollection <string> request, string client) { if (request == null || request.Count == 0) { return(null); } var type = RequestMethod.NONE; var url = string.Empty; var version = string.Empty; var urlMatch = null as Match; var methode = request.Take(1).FirstOrDefault(); var options = request.Skip(1).TakeWhile(x => !string.IsNullOrWhiteSpace(x)).ToList(); var len = methode.Length; var match = Regex.Match(methode, @"(GET|POST|PUT|DELETE) (.*) (.*)$", RegexOptions.IgnoreCase | RegexOptions.Compiled); if (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; } } if (match.Groups[2].Success) { url = match.Groups[2].Value; urlMatch = Regex.Match(url, @"^(.*)\?(.*)$", RegexOptions.IgnoreCase | RegexOptions.Compiled); if (urlMatch.Groups[1].Success) { url = urlMatch.Groups[1].Value; } } if (match.Groups[3].Success) { version = match.Groups[3].Value; } var req = new Request(type, url, version, RequestHeaderFields.Parse(options), client); if (urlMatch.Groups[2].Success && urlMatch.Groups[2].Length > 0) { foreach (var v in urlMatch.Groups[2].Value.Split('&')) { var regex = new Regex("[=]{1}"); var kv = regex.Split(v, 2); req.AddParam(kv[0], kv.Count() > 1 ? kv[1] : ""); } } return(req); }