Example #1
0
        public void Append()
        {
            StringBuilder sb = new StringBuilder();

            DateFormatter.Append(_expectedTime, sb);
            Assert.Equal("Sun, 06 Nov 1994 08:49:37 GMT", sb.ToString());
        }
Example #2
0
        public string Encode(ICookie cookie)
        {
            Contract.Requires(cookie != null);

            string name  = cookie.Name ?? nameof(cookie);
            string value = cookie.Value ?? "";

            this.ValidateCookie(name, value);

            StringBuilder buf = StringBuilder();

            if (cookie.Wrap)
            {
                AddQuoted(buf, name, value);
            }
            else
            {
                Add(buf, name, value);
            }

            if (cookie.MaxAge != long.MinValue)
            {
                Add(buf, (string)CookieHeaderNames.MaxAge, cookie.MaxAge);
                DateTime expires = DateTime.UtcNow.AddMilliseconds(cookie.MaxAge * 1000);
                buf.Append(CookieHeaderNames.Expires);
                buf.Append((char)HttpConstants.EqualsSign);
                DateFormatter.Append(expires, buf);
                buf.Append((char)HttpConstants.Semicolon);
                buf.Append((char)HttpConstants.HorizontalSpace);
            }

            if (cookie.Path != null)
            {
                Add(buf, (string)CookieHeaderNames.Path, cookie.Path);
            }

            if (cookie.Domain != null)
            {
                Add(buf, (string)CookieHeaderNames.Domain, cookie.Domain);
            }

            if (cookie.IsSecure)
            {
                Add(buf, (string)CookieHeaderNames.Secure);
            }

            if (cookie.IsHttpOnly)
            {
                Add(buf, (string)CookieHeaderNames.HttpOnly);
            }

            return(StripTrailingSeparator(buf));
        }
Example #3
0
        public string Encode(ICookie cookie)
        {
            if (cookie is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.cookie);
            }

            string name  = cookie.Name ?? nameof(cookie);
            string value = cookie.Value ?? "";

            this.ValidateCookie(name, value);

            StringBuilder buf = StringBuilder();

            if (cookie.Wrap)
            {
                AddQuoted(buf, name, value);
            }
            else
            {
                Add(buf, name, value);
            }

            if (cookie.MaxAge != long.MinValue)
            {
                Add(buf, (string)CookieHeaderNames.MaxAge, cookie.MaxAge);
                DateTime expires = DateTime.UtcNow.AddMilliseconds(cookie.MaxAge * 1000);
                _ = buf
                    .Append(CookieHeaderNames.Expires)
                    .Append(HttpConstants.EqualsSignChar);
                _ = DateFormatter.Append(expires, buf);
                _ = buf
                    .Append(HttpConstants.SemicolonChar)
                    .Append(HttpConstants.HorizontalSpaceChar);
            }

            if (cookie.Path is object)
            {
                Add(buf, (string)CookieHeaderNames.Path, cookie.Path);
            }

            if (cookie.Domain is object)
            {
                Add(buf, (string)CookieHeaderNames.Domain, cookie.Domain);
            }

            if (cookie.IsSecure)
            {
                Add(buf, (string)CookieHeaderNames.Secure);
            }

            if (cookie.IsHttpOnly)
            {
                Add(buf, (string)CookieHeaderNames.HttpOnly);
            }

            if (cookie is DefaultCookie defaultCookie && defaultCookie.SameSite.HasValue)
            {
                Add(buf, (string)CookieHeaderNames.SameSite, defaultCookie.SameSite.Value.ToString());
            }

            return(StripTrailingSeparator(buf));
        }