コード例 #1
0
        private static void SetActionOption(string value, Fields tokenType, ref ChangeCookieAction?action)
        {
            Debug.Assert(action != null || tokenType == Fields.Name);

            switch (tokenType)
            {
            case Fields.Name:
                action = new ChangeCookieAction(value);
                break;

            case Fields.Value:
                action !.Value = value;
                break;

            case Fields.Domain:
                // despite what spec says, an empty domain field is allowed in mod_rewrite
                // by specifying NAME:VALUE:;
                action !.Domain = string.IsNullOrEmpty(value) || value == ";"
                        ? null
                        : value;
                break;

            case Fields.Lifetime:
                if (string.IsNullOrEmpty(value))
                {
                    break;
                }

                uint minutes;
                if (!uint.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out minutes))
                {
                    throw new FormatException(Resources.FormatError_CouldNotParseInteger(value));
                }

                action !.Lifetime = TimeSpan.FromMinutes(minutes);
                break;

            case Fields.Path:
                action !.Path = value;
                break;

            case Fields.Secure:
                action !.Secure = "secure".Equals(value, StringComparison.OrdinalIgnoreCase) ||
                                  "true".Equals(value, StringComparison.OrdinalIgnoreCase) ||
                                  value == "1";
                break;

            case Fields.HttpOnly:
                action !.HttpOnly = "httponly".Equals(value, StringComparison.OrdinalIgnoreCase) ||
                                    "true".Equals(value, StringComparison.OrdinalIgnoreCase) ||
                                    value == "1";
                break;
            }
        }
コード例 #2
0
        public void UnsetCookie()
        {
            var context = new RewriteContext {
                HttpContext = new DefaultHttpContext()
            };
            var action = new ChangeCookieAction("Cookie");

            action.ApplyAction(context, null, null);

            var cookieHeaders = context.HttpContext.Response.Headers[HeaderNames.SetCookie];
            var header        = Assert.Single(cookieHeaders);

            Assert.Equal($"Cookie=", header);
        }
コード例 #3
0
        public void ZeroLifetime()
        {
            var context = new RewriteContext {
                HttpContext = new DefaultHttpContext()
            };
            var action = new ChangeCookieAction("Cookie")
            {
                Value = "Chocolate Chip",
            };

            action.ApplyAction(context, null, null);

            var cookieHeaders = context.HttpContext.Response.Headers[HeaderNames.SetCookie];
            var header        = Assert.Single(cookieHeaders);

            Assert.Equal($"Cookie=Chocolate%20Chip", header);
        }
コード例 #4
0
        /// <summary>
        ///  Creates a <see cref="ChangeCookieAction" /> <see href="https://httpd.apache.org/docs/current/rewrite/flags.html#flag_co" /> for details.
        /// </summary>
        /// <param name="flagValue">The flag</param>
        /// <returns>The action</returns>
        public ChangeCookieAction Create(string flagValue)
        {
            if (string.IsNullOrEmpty(flagValue))
            {
                throw new ArgumentException(nameof(flagValue));
            }

            var i         = 0;
            var separator = ':';

            if (flagValue[0] == ';')
            {
                separator = ';';
                i++;
            }

            ChangeCookieAction action = null;
            var currentField          = Fields.Name;
            var start = i;

            for (; i < flagValue.Length; i++)
            {
                if (flagValue[i] == separator)
                {
                    var length = i - start;
                    SetActionOption(flagValue.Substring(start, length).Trim(), currentField, ref action);

                    currentField++;
                    start = i + 1;
                }
            }

            if (i != start)
            {
                SetActionOption(flagValue.Substring(start).Trim(new[] { ' ', separator }), currentField, ref action);
            }

            if (currentField < Fields.Domain)
            {
                throw new FormatException(Resources.FormatError_InvalidChangeCookieFlag(flagValue));
            }

            return(action);
        }
コード例 #5
0
        /// <summary>
        ///  Creates a <see cref="ChangeCookieAction" /> <see href="https://httpd.apache.org/docs/current/rewrite/flags.html#flag_co" /> for details.
        /// </summary>
        /// <param name="flagValue">The flag</param>
        /// <returns>The action</returns>
        public ChangeCookieAction Create(string flagValue)
        {
            if (string.IsNullOrEmpty(flagValue))
            {
                throw new ArgumentException(nameof(flagValue));
            }

            var i         = 0;
            var separator = ':';

            if (flagValue[0] == ';')
            {
                separator = ';';
                i++;
            }

            ChangeCookieAction action = null;
            var currentField          = Fields.Name;
            var start = i;

            for (; i < flagValue.Length; i++)
            {
                if (flagValue[i] == separator)
                {
                    var length = i - start;
                    SetActionOption(flagValue.Substring(start, length).Trim(), currentField, ref action);

                    currentField++;
                    start = i + 1;
                }
            }

            if (i != start)
            {
                SetActionOption(flagValue.Substring(start).Trim(new[] { ' ', separator }), currentField, ref action);
            }

            if (currentField < Fields.Domain)
            {
                throw new FormatException($"Error parsing the mod_rewrite rule. The cookie flag (CO) has an incorrect format '{flagValue}'.");
            }

            return(action);
        }
コード例 #6
0
        public void SetsCookie()
        {
            var now     = DateTimeOffset.UtcNow;
            var context = new RewriteContext {
                HttpContext = new DefaultHttpContext()
            };
            var action = new ChangeCookieAction("Cookie", () => now)
            {
                Value    = "Chocolate Chip",
                Domain   = "contoso.com",
                Lifetime = TimeSpan.FromMinutes(1440),
                Path     = "/recipes",
                Secure   = true,
                HttpOnly = true
            };

            action.ApplyAction(context, null, null);

            var cookieHeaders = context.HttpContext.Response.Headers[HeaderNames.SetCookie];
            var header        = Assert.Single(cookieHeaders);

            Assert.Equal($"Cookie=Chocolate%20Chip; expires={HeaderUtilities.FormatDate(now.AddMinutes(1440))}; domain=contoso.com; path=/recipes; secure; httponly", header);
        }