public void CookieParser_DateWithDifferentCulture()
        {
            var cookieHeader = new List <string>()
            {
                "Name=Value; Expires=Wed, 13 Jan 2021, 22:23:01 GMT"
            };

            var initialCulture = Thread.CurrentThread.CurrentCulture;

            try
            {
                // Using Saudi Arabia culture, as reported in bug #4.
                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ar-SA");

                var result = CookieParser.Parse(cookieHeader).Single();

                var expected = new HttpCookie("Name", "Value")
                {
                    Expires = new DateTime(2021, 01, 13, 22, 23, 01, DateTimeKind.Utc).ToLocalTime(),
                    Path    = null
                };

                result.ShouldBeEquivalentTo(expected);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = initialCulture;
            }
        }
예제 #2
0
        private bool SetCookie(string header)
        {
            if (cookieCollection == null)
            {
                cookieCollection = new CookieCollection();
            }

            bool at_least_one_set = false;
            var  parser           = new CookieParser(header);

            foreach (var cookie in parser.Parse())
            {
                if (cookie.Domain == "")
                {
                    cookie.Domain    = uri.Host;
                    cookie.HasDomain = false;
                }

                if (cookie.HasDomain && !CookieContainer.CheckSameOrigin(uri, cookie.Domain))
                {
                    continue;
                }

                cookieCollection.Add(cookie);
                if (cookie_container != null)
                {
                    cookie_container.Add(uri, cookie);
                    at_least_one_set = true;
                }
            }

            return(at_least_one_set);
        }
        public void ParseNameAndValueCorrectly()
        {
            var cookieParser = new CookieParser();
            var cookie       = cookieParser.Parse("foo=bar");

            Assert.Equal("foo", cookie.Name);
            Assert.Equal("bar", cookie.Value);
        }
        public void ParseNameAndValueAndExpiresCorrectly()
        {
            var cookieParser = new CookieParser();
            var cookie       = cookieParser.Parse("foo=bar; Expires=Wed, 21 Oct 2015 07:28:00 GMT");

            Assert.Equal("foo", cookie.Name);
            Assert.Equal("bar", cookie.Value);
            Assert.Equal(new DateTime(2015, 10, 21, 7, 28, 0, DateTimeKind.Utc), cookie.Expires);
        }
        public void ParseNameAndValueAndDomainCorrectly()
        {
            var cookieParser = new CookieParser();
            var cookie       = cookieParser.Parse("foo=bar; domain=domain.ext");

            Assert.Equal("foo", cookie.Name);
            Assert.Equal("bar", cookie.Value);
            Assert.Equal("domain.ext", cookie.Domain);
        }
        public void ParseNameAndValueAndSecureCorrectly()
        {
            var cookieParser = new CookieParser();
            var cookie       = cookieParser.Parse("foo=bar; secure");

            Assert.Equal("foo", cookie.Name);
            Assert.Equal("bar", cookie.Value);
            Assert.True(cookie.Secure, "secure mismatch");
        }
        public void ParseNameAndValueAndHttpOnlyCorrectly()
        {
            var cookieParser = new CookieParser();
            var cookie       = cookieParser.Parse("foo=bar; httponly");

            Assert.Equal("foo", cookie.Name);
            Assert.Equal("bar", cookie.Value);
            Assert.True(cookie.HttpOnly, "httponly mismatch");
        }
        public void ParseNameAndValueAndPathAndDomainExpiresCorrectly()
        {
            var cookieParser = new CookieParser();
            var cookie       = cookieParser.Parse("qwerty=219ffwef9w0f; Domain=somecompany.co.uk; Path=/; Expires=Fri, 30 Aug 2019 00:00:00 GMT");

            Assert.Equal("qwerty", cookie.Name);
            Assert.Equal("219ffwef9w0f", cookie.Value);
            Assert.Equal("somecompany.co.uk", cookie.Domain);
            Assert.Equal(new DateTime(2019, 8, 30, 0, 0, 0, DateTimeKind.Utc), cookie.Expires);
        }
예제 #9
0
        private static void SetRecievedCookies(List <string> cookiesList)
        {
            HttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            foreach (string cookie in cookiesList)
            {
                ParsedCookie parsedCookie = CookieParser.Parse(cookie);
                httpContextAccessor.HttpContext.Response.Cookies.Append(parsedCookie.Name, parsedCookie.Value, parsedCookie.CookieOptions);
            }
        }
예제 #10
0
        public void TestParser()
        {
            CookieCollection c;

            CookieParser parser = new CookieParser();
            CookieHeader header = (CookieHeader)parser.Parse("Cookie", new StringReader("name: \"Value\";cookie2: value2;cookieName;last:one"));

            Assert.Equal("Value", header.Cookies["name"].Value);
            Assert.Equal("value2", header.Cookies["cookie2"].Value);
            //Assert.Equal(string.Empty, header.Cookies["cookieName"].Value);
            Assert.Equal("one", header.Cookies["last"].Value);
        }
예제 #11
0
        public void TestParser()
        {
            CookieCollection c;
            
            CookieParser parser = new CookieParser();
            CookieHeader header = (CookieHeader)parser.Parse("Cookie", new StringReader("name: \"Value\";cookie2: value2;cookieName;last:one"));
            Assert.Equal("Value", header.Cookies["name"].Value);
            Assert.Equal("value2", header.Cookies["cookie2"].Value);
            //Assert.Equal(string.Empty, header.Cookies["cookieName"].Value);
            Assert.Equal("one", header.Cookies["last"].Value);

        }
        public void CookieParser_NameAndValueOnly()
        {
            var cookieHeader = new List <string>()
            {
                "Name=Value"
            };

            var result = CookieParser.Parse(cookieHeader).Single();

            var expected = new HttpCookie("Name", "Value")
            {
                Path = null
            };

            result.ShouldBeEquivalentTo(expected);
        }
예제 #13
0
        public Cookie[] LoadCookies()
        {
            try
            {
                if (Settings.Driver.Cookies != null)
                {
                    Cookies = CookieParser.Parse(Settings.Driver.Cookies.RelativeToBaseDirectory());
                }
            }
            catch (Exception e)
            {
                log.LogError($"Failed to parse Cookies file: {e.Message}");
                Error = Error.InvalidArgs;
            }

            return(Cookies);
        }
예제 #14
0
        public void WebSocketHandshaker_CanParseCookies()
        {
            CookieParser parser = new CookieParser();

            var parsed = parser.Parse("cookie1=uno").ToArray();

            Assert.IsNotNull(parsed);
            Assert.AreEqual(1, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);

            parsed = parser.Parse("cookie1=uno;cookie2=dos").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("dos", parsed[1].Value);

            parsed = parser.Parse("cookie1=uno; cookie2=dos ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("dos", parsed[1].Value);

            parsed = parser.Parse("cookie1=uno; cookie2===dos== ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("==dos==", parsed[1].Value);

            parsed = parser.Parse(null).ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);

            parsed = parser.Parse(String.Empty).ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);

            parsed = parser.Parse("   ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);
        }
        public void WebSocketHandshaker_CanParseCookies()
        {
            CookieParser parser = new CookieParser();

            var parsed = parser.Parse("cookie1=uno").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(1, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);

            parsed = parser.Parse("cookie1=uno;cookie2=dos").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("dos", parsed[1].Value);

            parsed = parser.Parse("cookie1=uno; cookie2=dos ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("dos", parsed[1].Value);

            parsed = parser.Parse("cookie1=uno; cookie2===dos== ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("==dos==", parsed[1].Value);

            parsed = parser.Parse(null).ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);

            parsed = parser.Parse(String.Empty).ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);

            parsed = parser.Parse("   ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);
        }
        public void CookieParser_StrangeCasing()
        {
            var cookieHeader = new List <string>()
            {
                "Name=Value; dOmAin=example.com; ExPIres=Wed, 13 Jan 2021, 22:23:01 GMT; hTTPoNLy; SeCUre; PaTH=/SomePath"
            };

            var result = CookieParser.Parse(cookieHeader).Single();

            var expected = new HttpCookie("Name", "Value")
            {
                Domain   = "example.com",
                Expires  = new DateTime(2021, 01, 13, 22, 23, 01, DateTimeKind.Utc).ToLocalTime(),
                HttpOnly = true,
                Path     = "/SomePath",
                Secure   = true
            };

            result.ShouldBeEquivalentTo(expected);
        }
예제 #17
0
    private static void AddAuthenticationCookies(HttpContext context, System.Net.HttpWebRequest req)
    {
        // Copy user identity to new cookie
        if (context.User != null && context.User.Identity != null)
        {
            var cookiesString0 = req.Headers[HttpRequestHeader.Cookie];
            var cookies        = CookieParser.Parse(cookiesString0).ToList();

            // Pass username
            var userCookie = new Cookie("")
            {
                Name = "username", Value = context.User.Identity.Name
            };
            cookies.Add(userCookie);// Always add because this

            // Pass user groups
            var windowsIdentity = context.User.Identity as System.Security.Principal.WindowsIdentity;
            var groupNames      = new string[] { };
            if (windowsIdentity.Groups != null)
            {
                groupNames = windowsIdentity.Groups.Select(g => g.Translate(typeof(System.Security.Principal.NTAccount)).Value).ToArray();
            }
            var groupsCookie = new Cookie("")
            {
                Name = "usergroups", Value = string.Join(",", groupNames)
            };
            cookies.Add(groupsCookie);

            // Authentication ticket
            var ticket       = TicketHandler.IssueTicket();
            var ticketCookie = new Cookie("")
            {
                Name = "winauthticket", Value = ticket
            };
            cookies.Add(ticketCookie);

            var cookiesString = CookieParser.ToString(cookies.ToArray());
            req.Headers[HttpRequestHeader.Cookie] = cookiesString;
        }
    }
예제 #18
0
 private void ParseCookies(WebSocketHandshake handshake)
 {
     if (handshake.Request.Headers.HeaderNames.Contains("Cookie"))
     {
         var cookieString = handshake.Request.Headers["Cookie"];
         try
         {
             CookieParser parser = new CookieParser();
             foreach (var cookie in parser.Parse(cookieString))
             {
                 cookie.Domain = handshake.Request.Headers.Host;
                 cookie.Path = String.Empty;
                 handshake.Request.Cookies.Add(cookie);
             }
         }
         catch (Exception ex)
         {
             throw new WebSocketException("Cannot parse cookie string: '" + (cookieString ?? "") + "' because: " + ex.Message);
         }
     }
 }
예제 #19
0
        public void WebSocketHandshaker_CanParseCookies()
        {
            CookieParser parser = new CookieParser();

            var parsed = parser.Parse("cookie1=uno").ToArray();

            Assert.IsNotNull(parsed);
            Assert.AreEqual(1, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);

            parsed = parser.Parse("cookie1=uno;cookie2=dos").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("dos", parsed[1].Value);

            parsed = parser.Parse("cookie1=uno; cookie2=dos ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("dos", parsed[1].Value);

            parsed = parser.Parse("cookie1=uno; cookie2===dos== ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("==dos==", parsed[1].Value);

            parsed = parser.Parse("language=ru; _ym_uid=1111111111111; _ym_isad=2; __test; settings=%7B%22market_730_onPage%22%3A24%7D; timezoneOffset=10800").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(6, parsed.Length);
            Assert.AreEqual("language", parsed[0].Name);
            Assert.AreEqual("ru", parsed[0].Value);
            Assert.AreEqual("_ym_uid", parsed[1].Name);
            Assert.AreEqual("1111111111111", parsed[1].Value);
            Assert.AreEqual("_ym_isad", parsed[2].Name);
            Assert.AreEqual("2", parsed[2].Value);
            Assert.AreEqual("__test", parsed[3].Name);
            Assert.AreEqual("", parsed[3].Value);
            Assert.AreEqual("settings", parsed[4].Name);
            Assert.AreEqual("{\"market_730_onPage\":24}", parsed[4].Value);
            Assert.AreEqual("timezoneOffset", parsed[5].Name);
            Assert.AreEqual("10800", parsed[5].Value);

            parsed = parser.Parse(null).ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);

            parsed = parser.Parse(String.Empty).ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);

            parsed = parser.Parse("   ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);
        }
        public void WebSocketHandshaker_CanParseCookies()
        {
            CookieParser parser = new CookieParser();

            var parsed = parser.Parse("cookie1=uno").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(1, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);

            parsed = parser.Parse("cookie1=uno;cookie2=dos").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("dos", parsed[1].Value);

            parsed = parser.Parse("cookie1=uno; cookie2=dos ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("dos", parsed[1].Value);

            parsed = parser.Parse("cookie1=uno; cookie2===dos== ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(2, parsed.Length);
            Assert.AreEqual("cookie1", parsed[0].Name);
            Assert.AreEqual("uno", parsed[0].Value);
            Assert.AreEqual("cookie2", parsed[1].Name);
            Assert.AreEqual("==dos==", parsed[1].Value);

            parsed = parser.Parse("language=ru; _ym_uid=1111111111111; _ym_isad=2; __test; settings=%7B%22market_730_onPage%22%3A24%7D; timezoneOffset=10800").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(6, parsed.Length);
            Assert.AreEqual("language", parsed[0].Name);
            Assert.AreEqual("ru", parsed[0].Value);
            Assert.AreEqual("_ym_uid", parsed[1].Name);
            Assert.AreEqual("1111111111111", parsed[1].Value);
            Assert.AreEqual("_ym_isad", parsed[2].Name);
            Assert.AreEqual("2", parsed[2].Value);
            Assert.AreEqual("__test", parsed[3].Name);
            Assert.AreEqual("", parsed[3].Value);
            Assert.AreEqual("settings", parsed[4].Name);
            Assert.AreEqual("{\"market_730_onPage\":24}", parsed[4].Value);
            Assert.AreEqual("timezoneOffset", parsed[5].Name);
            Assert.AreEqual("10800", parsed[5].Value);

            parsed = parser.Parse(null).ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);
            
            parsed = parser.Parse(String.Empty).ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);

            parsed = parser.Parse("   ").ToArray();
            Assert.IsNotNull(parsed);
            Assert.AreEqual(0, parsed.Length);
        }
예제 #21
0
        private static int Main(string[] args)
        {
            Error error = 0;

            var output          = ".";
            var profile         = "profile.toml";
            var template        = "template.html";
            var showHelpAndExit = false;

            var options = new OptionSet
            {
                { "o|output=", $"Reports output directory\n(default: '{output}')", v => output = v },
                { "p|profile=", $"Profile TOML file with current settings\n(default: '{profile}')", v => profile = v },
                { "t|template=", $"HTML report template file\n(default: '{template}')", v => template = v },
                { "h|help", "Show this message", v => showHelpAndExit = v != null }
            };

            List <string> free = null;

            try
            {
                free = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Error.WriteLine("Option '{0}' value is invalid: {1}", e.OptionName, e.Message);
                Console.Error.WriteLine();
                error = Error.InvalidArgs;
            }

            if (!PathHelper.FileExistsWithOptionalExt(ref profile, ".toml"))
            {
                Console.Error.WriteLine($"Profile '{profile}' not found");
                Console.Error.WriteLine();
                error = Error.InvalidArgs;
            }

            if (!PathHelper.FileExistsWithOptionalExt(ref template, ".html"))
            {
                Console.Error.WriteLine($"Template '{template}' not found");
                Console.Error.WriteLine();
                error = Error.InvalidArgs;
            }

            Settings settings = null;

            try
            {
                settings = Settings.Read(profile.RelativeToBaseDirectory());
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Failed to read profile '{profile}': {e.Message}");
                Console.Error.WriteLine();
                error = Error.InvalidArgs;
            }

            try
            {
                if (free?.Count >= 2)
                {
                    SvcLeft  = new Uri(free[0]);
                    SvcRight = new Uri(free[1]);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine();
                error = Error.InvalidArgs;
            }

            var input = free?.Skip(2).FirstOrDefault();

            if (input != null && !File.Exists(input.RelativeToBaseDirectory()))
            {
                Console.Error.WriteLine("Input file with URLs not found");
                Console.Error.WriteLine();
                error = Error.InvalidArgs;
            }

            if (showHelpAndExit || error != 0 || settings == null || free == null || free.Count < 2)
            {
                Console.WriteLine("Usage: webdiff [OPTIONS] URL1 URL2 [FILE]");
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                Console.WriteLine();
                Console.WriteLine("Examples:");
                Console.WriteLine("  webdiff http://prod.example.com http://test.example.com < URLs.txt");
                Console.WriteLine("  webdiff -p profile.toml -t template.html -o data http://prod.example.com http://test.example.com URLs.txt");
                Console.WriteLine();
                return((int)error);
            }

            Cookie[] cookies = null;
            try
            {
                if (settings.Driver.Cookies != null)
                {
                    cookies = CookieParser.Parse(settings.Driver.Cookies.RelativeToBaseDirectory());
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Failed to parse cookies file: {e.Message}");
                return((int)Error.InvalidArgs);
            }

            var started = DateTime.Now;

            try
            {
                ResultsPath    = Path.Combine(output, started.ToString("yyyyMMdd-HHmmss")).RelativeToBaseDirectory();
                ResultsImgPath = Path.Combine(ResultsPath, ImgRelativePath);

                Directory.CreateDirectory(ResultsPath);
                Directory.CreateDirectory(ResultsImgPath);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Failed to create output directory '{ResultsPath}': {e.Message}");
                return((int)Error.CestLaVie);
            }

            (Uri BaseUri, RemoteWebDriver Driver)[] drivers = null;