Beispiel #1
0
        public void set_cookie(string name, string value, string domain=null, DateTime? expires=null, string path="/",
                   int? expires_days=null, Dictionary<string, string> kwargs=null)
        {
            /*Sets the given cookie name/value with the given options.

            Additional keyword arguments are set on the Cookie.Morsel
            directly.
            See http://docs.python.org/library/cookie.html#morsel-objects
            for available attributes.
            */
            // The cookie library only accepts type str, in both python 2 and 3
            name = escape.native_str(name);
            value = escape.native_str(value);
            if (Regex.IsMatch(name + value, "[\x00-\x20]"))
                // Don't let us accidentally inject bad stuff
                throw new ValueError(string.Format("Invalid cookie {0}: {1}", name, value));
            if (_new_cookie == null)
                _new_cookie = new SimpleCookie();
            if (_new_cookie.ContainsKey(name)) 
                _new_cookie.Remove(name);
            _new_cookie[name] = new HttpCookie(name, value);
            var morsel = _new_cookie[name];
            if (domain != null)
                morsel.Domain = domain;
            if (expires_days != null && expires == null)
                expires = DateTime.UtcNow.AddDays(expires_days.Value);
            if (expires != null)
                morsel.Expires = expires.Value;
                //timestamp = calendar.timegm(expires.utctimetuple())
                //morsel["expires"] = email.utils.formatdate(
                //    timestamp, localtime=False, usegmt=True)
            if (path != null)
                morsel.Path = path;
            if(kwargs != null)
                foreach(var kvp in kwargs)
                {
                    var k = kvp.Key;
                    if(k == "max age")
                        k = "max-age"; 
                    morsel.Values[k] = kvp.Value;
                }
        }
Beispiel #2
0
 public static void AddCookie(this IHttpService http, SimpleCookie cookie)
 {
     http.AddCookie(cookie.ToCookie());
 }