# HG changeset patch # User Ivo Smits # Date 1403304033 -7200 # Node ID 3c1bba376dca53c28b079319147a1370d7f5daa8 # Parent d467cd38b34eac24ab6f6694383c567ef371ba16 HTTP: Added support for cookies, improved prefix selector diff -r d467cd38b34e -r 3c1bba376dca Net/HTTP.cs --- a/Net/HTTP.cs Tue Apr 22 16:40:35 2014 +0200 +++ b/Net/HTTP.cs Sat Jun 21 00:40:33 2014 +0200 @@ -96,7 +96,7 @@ private PrebufferingStream Reader; private List RequestHeaders; private HTTPConnectionState State = HTTPConnectionState.Starting; - private KeyValuePair[] QueryParameters = null, PostParameters = null; + private KeyValuePair[] QueryParameters = null, PostParameters = null, Cookies = null; private HTTPOutputStream ResponseStream = null; private HTTPInputStream RequestStream = null; @@ -539,6 +539,7 @@ return list.ToArray(); } public KeyValuePair[] GetQueryParameters() { + if (RequestQuery == null) return new KeyValuePair[0]; if (QueryParameters == null) QueryParameters = DecodeUrlEncodedFields(RequestQuery); return QueryParameters; } @@ -565,6 +566,51 @@ return PostParameters; } + public String GetCookie(String name) { + foreach (KeyValuePair kvp in GetCookies()) if (kvp.Key == name) return kvp.Value; + return null; + } + public String[] GetCookies(String name) { + List list = new List(); + foreach (KeyValuePair kvp in GetCookies()) if (kvp.Key == name) list.Add(kvp.Value); + return list.ToArray(); + } + public KeyValuePair[] GetCookies() { + if (Cookies == null) { + String cookie = GetRequestHeader("Cookie"); + List> list = new List>(); + if (cookie != null) { + foreach (String part in cookie.Split(';', ',')) { + String[] subparts = part.Split('='); + String key = subparts[0].Trim(' ', '\t', '"'); + String value = (subparts.Length < 2) ? null : subparts[1].Trim(' ', '\t', '"'); + list.Add(new KeyValuePair(key, value)); + } + } + Cookies = list.ToArray(); + } + return Cookies; + } + + public void SetCookie(String name, String value) { + SendHeader("Set-Cookie", String.Format("{0}={1}", name, value)); + } + public void SetCookie(String name, String value, DateTime expire) { + SendHeader("Set-Cookie", String.Format("{0}={1}; Expires={2:R}", name, value, expire)); + } + public void SetCookie(String name, String value, DateTime? expire, String path, String domain, Boolean secure, Boolean httponly) { + StringBuilder sb = new StringBuilder(); + sb.Append(name); + sb.Append("="); + sb.Append(value); + if (expire != null) sb.AppendFormat("; Expires={0:R}", expire.Value.ToUniversalTime()); + if (path != null) sb.AppendFormat("; Path={0}", path); + if (domain != null) sb.AppendFormat("; Domain={0}", domain); + if (secure) sb.Append("; Secure"); + if (httponly) sb.Append("; HttpOnly"); + SendHeader("Set-Cookie", sb.ToString()); + } + public Stream OpenRequestStream() { if (RequestStream == null) RequestStream = new HTTPInputStream(this); return RequestStream; @@ -708,6 +754,9 @@ } public void AddPrefix(String prefix, IHTTPContentProvider contentProvider) { Prefixes.Add(new KeyValuePair(prefix, contentProvider)); + Prefixes.Sort(delegate(KeyValuePair a, KeyValuePair b) { + return -String.CompareOrdinal(a.Key, b.Key); + }); } public void DeletePrefix(String prefix) { Prefixes.RemoveAll(delegate(KeyValuePair item) { return prefix.Equals(item.Key, PrefixComparison); });