private static CookieCollection ParseRequest(string value) { var cookies = new CookieCollection(); Cookie cookie = null; var ver = 0; var pairs = SplitCookieHeaderValue(value); foreach (var t in pairs) { var pair = t.Trim(); if (pair.Length == 0) { continue; } if (pair.StartsWith("$version", StringComparison.OrdinalIgnoreCase)) { ver = int.Parse(GetValue(pair, true)); } else if (pair.StartsWith("$path", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Path = GetValue(pair); } else if (pair.StartsWith("$domain", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Domain = GetValue(pair); } else if (pair.StartsWith("$port", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Port = pair.Equals("$port", StringComparison.OrdinalIgnoreCase) ? "\"\"" : GetValue(pair); } else { if (cookie != null) { cookies.Add(cookie); } string name; var val = string.Empty; var pos = pair.IndexOf('='); if (pos == -1) { name = pair; } else if (pos == pair.Length - 1) { name = pair.Substring(0, pos).TrimEnd(' '); } else { name = pair.Substring(0, pos).TrimEnd(' '); val = pair.Substring(pos + 1).TrimStart(' '); } cookie = new Cookie(name, val); if (ver != 0) { cookie.Version = ver; } } } if (cookie != null) { cookies.Add(cookie); } return(cookies); }
private static CookieCollection ParseResponse(string value) { var cookies = new CookieCollection(); Cookie cookie = null; var pairs = SplitCookieHeaderValue(value); for (var i = 0; i < pairs.Length; i++) { var pair = pairs[i].Trim(); if (pair.Length == 0) { continue; } if (pair.StartsWith("version", StringComparison.OrdinalIgnoreCase)) { if (cookie != null) { cookie.Version = int.Parse(GetValue(pair, true)); } } else if (pair.StartsWith("expires", StringComparison.OrdinalIgnoreCase)) { var buff = new StringBuilder(GetValue(pair), 32); if (i < pairs.Length - 1) { buff.AppendFormat(", {0}", pairs[++i].Trim()); } if (!DateTime.TryParseExact( buff.ToString(), new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" }, new CultureInfo("en-US"), DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out DateTime expires)) { expires = DateTime.Now; } if (cookie != null && cookie.Expires == DateTime.MinValue) { cookie.Expires = expires.ToLocalTime(); } } else if (pair.StartsWith("max-age", StringComparison.OrdinalIgnoreCase) && cookie != null) { var max = int.Parse(GetValue(pair, true)); cookie.Expires = DateTime.Now.AddSeconds(max); } else if (pair.StartsWith("path", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Path = GetValue(pair); } else if (pair.StartsWith("domain", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Domain = GetValue(pair); } else if (pair.StartsWith("port", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Port = pair.Equals("port", StringComparison.OrdinalIgnoreCase) ? "\"\"" : GetValue(pair); } else if (pair.StartsWith("comment", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Comment = WebUtility.UrlDecode(GetValue(pair)); } else if (pair.StartsWith("commenturl", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.CommentUri = GetValue(pair, true).ToUri(); } else if (pair.StartsWith("discard", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Discard = true; } else if (pair.StartsWith("secure", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.Secure = true; } else if (pair.StartsWith("httponly", StringComparison.OrdinalIgnoreCase) && cookie != null) { cookie.HttpOnly = true; } else { if (cookie != null) { cookies.Add(cookie); } string name; var val = string.Empty; var pos = pair.IndexOf('='); if (pos == -1) { name = pair; } else if (pos == pair.Length - 1) { name = pair.Substring(0, pos).TrimEnd(' '); } else { name = pair.Substring(0, pos).TrimEnd(' '); val = pair.Substring(pos + 1).TrimStart(' '); } cookie = new Cookie(name, val); } } if (cookie != null) { cookies.Add(cookie); } return(cookies); }
internal void AddHeader(string header) { var colon = header.IndexOf(':'); if (colon == -1 || colon == 0) { _context.ErrorMessage = "Bad Request"; _context.ErrorStatus = 400; return; } var name = header.Substring(0, colon).Trim(); var val = header.Substring(colon + 1).Trim(); var lower = name.ToLowerInvariant(); Headers.Set(name, val); switch (lower) { case "accept-language": UserLanguages = val.Split(','); // yes, only split with a ',' break; case "accept": AcceptTypes = val.Split(','); // yes, only split with a ',' break; case "content-length": try { //TODO: max. content_length? ContentLength64 = long.Parse(val.Trim()); if (ContentLength64 < 0) { _context.ErrorMessage = "Invalid Content-Length."; } _clSet = true; } catch { _context.ErrorMessage = "Invalid Content-Length."; } break; case "referer": try { UrlReferrer = new Uri(val); } catch { UrlReferrer = new Uri("http://someone.is.screwing.with.the.headers.com/"); } break; case "cookie": if (_cookies == null) { _cookies = new CookieCollection(); } var cookieStrings = val.Split(',', ';'); Cookie current = null; var version = 0; foreach (var cookieString in cookieStrings) { var str = cookieString.Trim(); if (str.Length == 0) { continue; } if (str.StartsWith("$Version")) { version = int.Parse(str.Substring(str.IndexOf('=') + 1).Unquote()); } else if (str.StartsWith("$Path")) { if (current != null) { current.Path = str.Substring(str.IndexOf('=') + 1).Trim(); } } else if (str.StartsWith("$Domain")) { if (current != null) { current.Domain = str.Substring(str.IndexOf('=') + 1).Trim(); } } else if (str.StartsWith("$Port")) { if (current != null) { current.Port = str.Substring(str.IndexOf('=') + 1).Trim(); } } else { if (current != null) { _cookies.Add(current); } current = new Cookie(); var idx = str.IndexOf('='); if (idx > 0) { current.Name = str.Substring(0, idx).Trim(); current.Value = str.Substring(idx + 1).Trim(); } else { current.Name = str.Trim(); current.Value = string.Empty; } current.Version = version; } } if (current != null) { _cookies.Add(current); } break; } }