コード例 #1
0
        public static bool TryParse(string value, string domain, out Cookie cookie)
        {
            cookie = null;
            if (value == null) {
                return false;
            }

            var segments = value.Split(SEGMENT_SEPARATOR);
            if (segments.Length == 0) {
                return false;
            }

            var tmpCookie = new Cookie();
            tmpCookie.Domain = domain;
            tmpCookie.Path = "/";

            foreach (string segment in segments) {
                if (!ParseSegment(segment, tmpCookie)) {
                    return false;
                }
            }

            if (tmpCookie.Name == null || tmpCookie.Value == null) {
                return false;
            }

            cookie = tmpCookie;
            return true;
        }
コード例 #2
0
        public static bool TryParse(string value, string domain, out Cookie cookie)
        {
            cookie = null;
            if (value == null)
            {
                return(false);
            }

            var segments = value.Split(SEGMENT_SEPARATOR);

            if (segments.Length == 0)
            {
                return(false);
            }

            var tmpCookie = new Cookie();

            tmpCookie.Domain = domain;
            tmpCookie.Path   = "/";

            foreach (string segment in segments)
            {
                if (!ParseSegment(segment, tmpCookie))
                {
                    return(false);
                }
            }

            if (tmpCookie.Name == null || tmpCookie.Value == null)
            {
                return(false);
            }

            cookie = tmpCookie;
            return(true);
        }
コード例 #3
0
        private static bool ParseSegment(string segment, Cookie cookie)
        {
            if (segment == null || segment.Trim().Length == 0)
            {
                return(true);
            }

            var    nameValue = segment.Split(NAME_VALUE_SEPARATOR);
            string name      = nameValue[0].Trim();

            if (String.Equals(name, ExpiresToken, StringComparison.OrdinalIgnoreCase))
            {
                string         value = GetSegmentValue(nameValue, null);
                DateTimeOffset expires;
                if (DateTimeOffset.TryParseExact(value, dateFormats, DateTimeFormatInfo.InvariantInfo,
                                                 DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal,
                                                 out expires))
                {
                    cookie.Expires = expires.DateTime;
                    return(true);
                }
                return(false);
            }

            if (String.Equals(name, MaxAgeToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                int    maxAge;
                if (Int32.TryParse(value, out maxAge))
                {
                    var offset = new TimeSpan(0, 0, maxAge);
                    cookie.Expires = DateTime.Now.Add(offset);
                    return(true);
                }
                return(false);
            }

            if (String.Equals(name, DomainToken, StringComparison.OrdinalIgnoreCase))
            {
                cookie.Domain = GetSegmentValue(nameValue, null);
                return(true);
            }

            if (String.Equals(name, PathToken, StringComparison.OrdinalIgnoreCase))
            {
                cookie.Path = GetSegmentValue(nameValue, "/");
                return(true);
            }

            if (String.Equals(name, SecureToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!StringEx.IsNullOrWhiteSpace(value))
                {
                    return(false);
                }
                cookie.Secure = true;
                return(true);
            }

            if (String.Equals(name, HttpOnlyToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!StringEx.IsNullOrWhiteSpace(value))
                {
                    return(false);
                }
                cookie.HttpOnly = true;
                return(true);
            }

            string cookieValue = GetSegmentValue(nameValue, null);

            if (StringEx.IsNullOrWhiteSpace(cookieValue))
            {
                return(false);
            }

            cookie.Name  = name;
            cookie.Value = cookieValue;

            return(true);
        }
コード例 #4
0
        private static bool ParseSegment(string segment, Cookie cookie)
        {
            if (segment == null || segment.Trim().Length == 0) {
                return true;
            }

            var nameValue = segment.Split(NAME_VALUE_SEPARATOR);
            string name = nameValue[0].Trim();

            if (String.Equals(name, ExpiresToken, StringComparison.OrdinalIgnoreCase)) {
                string value = GetSegmentValue(nameValue, null);
                DateTimeOffset expires;
                if (DateTimeOffset.TryParseExact(value, dateFormats, DateTimeFormatInfo.InvariantInfo,
                    DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal,
                    out expires))
                {
                    cookie.Expires = expires.DateTime;
                    return true;
                }
                return false;
            }

            if (String.Equals(name, MaxAgeToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                int maxAge;
                if (Int32.TryParse(value, out maxAge))
                {
                    var offset = new TimeSpan(0, 0, maxAge);
                    cookie.Expires = DateTime.Now.Add(offset);
                    return true;
                }
                return false;
            }

            if (String.Equals(name, DomainToken, StringComparison.OrdinalIgnoreCase))
            {
                cookie.Domain = GetSegmentValue(nameValue, null);
                return true;
            }

            if (String.Equals(name, PathToken, StringComparison.OrdinalIgnoreCase))
            {
                cookie.Path = GetSegmentValue(nameValue, "/");
                return true;
            }

            if (String.Equals(name, SecureToken, StringComparison.OrdinalIgnoreCase))
            {
                string value = GetSegmentValue(nameValue, null);
                if (!StringEx.IsNullOrWhiteSpace(value))
                {
                    return false;
                }
                cookie.Secure = true;
                return true;
            }

            if (String.Equals(name, HttpOnlyToken, StringComparison.OrdinalIgnoreCase)) {
                string value = GetSegmentValue(nameValue, null);
                if (!StringEx.IsNullOrWhiteSpace(value)) {
                    return false;
                }
                cookie.HttpOnly = true;
                return true;
            }

            string cookieValue = GetSegmentValue(nameValue, null);
            if (StringEx.IsNullOrWhiteSpace(cookieValue)) {
                return false;
            }

            cookie.Name = name;
            cookie.Value = cookieValue;

            return true;
        }