コード例 #1
0
        public void CookieWithPath_UsesAssignedPath()
        {
            const string     testDomain   = "test.com";
            const string     cookieHeader = "sessionToken=abc123; Path=/test/path";
            CookieCollection actual       = SetCookieHeaderParser.GetAllCookiesFromHeader(testDomain, cookieHeader);

            Assert.That(actual[0].Path, Is.EqualTo("/test/path"));
        }
コード例 #2
0
        public void CookieWithDomain_UsesAssignedDomain()
        {
            const string     testDomain   = "test.com";
            const string     cookieHeader = "sessionToken=abc123; Domain=.cookie-specified.com";
            CookieCollection actual       = SetCookieHeaderParser.GetAllCookiesFromHeader(testDomain, cookieHeader);

            Assert.That(actual[0].Domain, Is.EqualTo(".cookie-specified.com"));
        }
コード例 #3
0
        public void CookieWithNoPath_PathIsForwardSlash()
        {
            const string     testDomain   = "test.com";
            const string     cookieHeader = "sessionToken=abc123";
            CookieCollection actual       = SetCookieHeaderParser.GetAllCookiesFromHeader(testDomain, cookieHeader);

            Assert.That(actual[0].Path, Is.EqualTo("/"));
        }
コード例 #4
0
        public void KeyOnlyCookie_ParsesNameValueCorrectly()
        {
            CookieCollection actual = SetCookieHeaderParser.GetAllCookiesFromHeader("test.com", "theme");

            Assert.That(actual.Count, Is.EqualTo(1));
            Assert.That(actual[0].Name, Is.EqualTo("theme"));
            Assert.That(actual[0].Value, Is.EqualTo(""));
        }
コード例 #5
0
        public void CookieWithNoDomain_UsesDefaultDomain()
        {
            const string     testDomain   = "test.com";
            const string     cookieHeader = "theme";
            CookieCollection actual       = SetCookieHeaderParser.GetAllCookiesFromHeader(testDomain, cookieHeader);

            Assert.That(actual[0].Domain, Is.EqualTo(testDomain));
        }
コード例 #6
0
        public void KeyValueCookie_StripsCarriageReturns()
        {
            CookieCollection actual = SetCookieHeaderParser.GetAllCookiesFromHeader("test.com", "theme=light\r\n");

            Assert.That(actual.Count, Is.EqualTo(1));
            Assert.That(actual[0].Name, Is.EqualTo("theme"));
            Assert.That(actual[0].Value, Is.EqualTo("light"));
        }
コード例 #7
0
        public void KeyValueCookie_ParsesNameValueCorrectly()
        {
            var actual = SetCookieHeaderParser.GetAllCookiesFromHeader("test.com", "theme=light");

            Assert.That(actual.Count, Is.EqualTo(1));
            Assert.That(actual[0].Name, Is.EqualTo("theme"));
            Assert.That(actual[0].Value, Is.EqualTo("light"));
        }
コード例 #8
0
        public void CookieContainingCommaValue_IsReadAsASingleCookie()
        {
            const string     cookieHeader = "key=\"value1, value2\"";
            CookieCollection actual       = SetCookieHeaderParser.GetAllCookiesFromHeader("test.com", cookieHeader);

            Assert.That(actual.Count, Is.EqualTo(1));
            Assert.That(actual[0].Name, Is.EqualTo("key"));
            Assert.That(actual[0].Value, Is.EqualTo("value1, value2"));
        }
コード例 #9
0
        public void KeyValueCookieWithExpiresAttribute_DisregardsExpiresValue()
        {
            const string     cookieHeader = "sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT";
            CookieCollection actual       = SetCookieHeaderParser.GetAllCookiesFromHeader("test.com", cookieHeader);

            Assert.That(actual.Count, Is.EqualTo(1));
            Assert.That(actual[0].Name, Is.EqualTo("sessionToken"));
            Assert.That(actual[0].Value, Is.EqualTo("abc123"));
            Assert.That(actual[0].Expires, Is.EqualTo(DateTime.MinValue));
        }
コード例 #10
0
        public void CommaSeparatedCookies_AreReadAsMultipleCookies()
        {
            const string     cookieHeader = "key1=value1; Expires=Test, TestDate, key2=value2";
            CookieCollection actual       = SetCookieHeaderParser.GetAllCookiesFromHeader("test.com", cookieHeader);

            Assert.That(actual.Count, Is.EqualTo(2));
            Assert.That(actual[0].Name, Is.EqualTo("key1"));
            Assert.That(actual[0].Value, Is.EqualTo("value1"));
            Assert.That(actual[1].Name, Is.EqualTo("key2"));
            Assert.That(actual[1].Value, Is.EqualTo("value2"));
        }
コード例 #11
0
ファイル: Browser.cs プロジェクト: yuvalhaimov/SimpleBrowser
        internal bool DoRequest(Uri uri, string method, NameValueCollection userVariables, string postData, string contentType, string encodingType, int timeoutMilliseconds)
        {
            string html;
            string referer = null;

            if (uri.IsFile)
            {
                StreamReader reader = new StreamReader(uri.AbsolutePath);
                html = reader.ReadToEnd();
                reader.Close();
            }
            else
            {
                bool   handle3xxRedirect = false;
                int    maxRedirects      = 5;         // Per RFC2068, Section 10.3
                string postBody          = string.Empty;
                do
                {
                    Debug.WriteLine(uri.ToString());
                    if (maxRedirects-- == 0)
                    {
                        Log("Too many 3xx redirects", LogMessageType.Error);
                        return(false);
                    }

                    handle3xxRedirect = false;
                    IHttpWebRequest req = null;

                    try
                    {
                        req = PrepareRequestObject(uri, method, contentType, timeoutMilliseconds);
                    }
                    catch (NotSupportedException)
                    {
                        // Happens when the URL cannot be parsed (example: 'javascript:')
                        return(false);
                    }

                    foreach (var header in _extraHeaders)
                    {
                        if (header.StartsWith("host:", StringComparison.OrdinalIgnoreCase))
                        {
                            req.Host = header.Split(':')[1];
                        }
                        else
                        {
                            req.Headers.Add(header);
                        }
                    }

                    if (!string.IsNullOrEmpty(encodingType))
                    {
                        req.Headers.Add(HttpRequestHeader.ContentEncoding, encodingType);
                    }

                    // Remove all expired basic authentication tokens
                    List <BasicAuthenticationToken> expired =
                        _basicAuthenticationTokens.Values.Where(t => DateTime.Now > t.Expiration).ToList();
                    foreach (var expiredToken in expired)
                    {
                        _basicAuthenticationTokens.Remove(expiredToken.Domain);
                    }

                    // If an authentication token exists for the domain, add the authorization header.
                    foreach (var token in _basicAuthenticationTokens.Values)
                    {
                        if (req.Host.Contains(token.Domain))
                        {
                            // Extend the expiration.
                            token.UpdateExpiration();

                            // Add the authentication header.
                            req.Headers.Add(string.Format(
                                                "Authorization: Basic {0}",
                                                token.Token));
                        }
                    }

                    if (_includeFormValues != null)
                    {
                        if (userVariables == null)
                        {
                            userVariables = _includeFormValues;
                        }
                        else
                        {
                            userVariables.Add(_includeFormValues);
                        }
                    }

                    if (userVariables != null)
                    {
                        if (method == "POST")
                        {
                            postBody = StringUtil.MakeQueryString(userVariables);
                            byte[] data = Encoding.GetEncoding(28591).GetBytes(postBody);
                            req.ContentLength = data.Length;
                            using (Stream stream = req.GetRequestStream())
                            {
                                stream.Write(data, 0, data.Length);
                            }
                        }
                        else
                        {
                            uri = new Uri(
                                uri.Scheme + "://" + uri.Host + ":" + uri.Port + uri.AbsolutePath
                                + ((userVariables.Count > 0) ? "?" + StringUtil.MakeQueryString(userVariables) : "")
                                );
                            req = PrepareRequestObject(uri, method, contentType, timeoutMilliseconds);
                        }
                    }
                    else if (postData != null)
                    {
                        if (method == "GET")
                        {
                            throw new InvalidOperationException("Cannot call DoRequest with method GET and non-null postData");
                        }
                        postBody = postData;
                        byte[] data = Encoding.GetEncoding(28591).GetBytes(postData);
                        req.ContentLength = data.Length;
                        using (Stream stream = req.GetRequestStream())
                        {
                            stream.Write(data, 0, data.Length);
                        }
                    }

                    referer = req.Referer;

                    if (contentType != null)
                    {
                        req.ContentType = contentType;
                    }

                    _lastRequestLog = new HttpRequestLog
                    {
                        Method         = method,
                        PostData       = userVariables,
                        PostBody       = postBody,
                        RequestHeaders = req.Headers,
                        Url            = uri
                    };
                    try
                    {
                        using (IHttpWebResponse response = req.GetResponse())
                        {
                            Encoding responseEncoding = Encoding.UTF8;                             //default
                            string   charSet          = response.CharacterSet;
                            if (!String.IsNullOrEmpty(charSet))
                            {
                                try
                                {
                                    responseEncoding = Encoding.GetEncoding(charSet);
                                }
                                catch (ArgumentException)
                                {
                                    responseEncoding = Encoding.UTF8;                                     // try using utf8
                                }
                            }

                            //ensure the stream is disposed
                            using (Stream rs = response.GetResponseStream())
                            {
                                using (StreamReader reader = new StreamReader(rs, responseEncoding))
                                {
                                    html = reader.ReadToEnd();
                                }
                            }

                            _doc = null;
                            _includeFormValues = null;

                            _lastRequestLog.Text            = html;
                            _lastRequestLog.ResponseHeaders = response.Headers;
                            _lastRequestLog.ResponseCode    = (int)response.StatusCode;

                            if (method == "GET" && uri.Query.Length > 0 && uri.Query != "?")
                            {
                                _lastRequestLog.QueryStringData = HttpUtility.ParseQueryString(uri.Query);
                            }

                            if (AutoRedirect == true &&
                                (((int)response.StatusCode == 300 ||                                 // Not entirely supported. If provided, the server's preference from the Location header is honored.
                                  (int)response.StatusCode == 301 ||
                                  (int)response.StatusCode == 302 ||
                                  (int)response.StatusCode == 303 ||
                                  // 304 - Unsupported, conditional Get requests are not supported (mostly because SimpleBrowser does not cache content)
                                  // 305 - Unsupported, possible security threat
                                  // 306 - No longer used, per RFC2616, Section 10.3.7
                                  (int)response.StatusCode == 307 ||
                                  (int)response.StatusCode == 308) &&
                                 response.Headers.AllKeys.Contains("Location")))
                            {
                                uri = new Uri(uri, response.Headers["Location"]);
                                handle3xxRedirect = true;
                                Debug.WriteLine("Redirecting to: " + uri);
                                method        = "GET";
                                postData      = null;
                                userVariables = null;
                            }

                            if (response.Headers.AllKeys.Contains("Set-Cookie"))
                            {
                                var cookies = SetCookieHeaderParser.GetAllCookiesFromHeader(uri.Host, response.Headers["Set-Cookie"]);
                                Cookies.Add(cookies);
                            }
                        }
                    }
                    catch (WebException ex)
                    {
                        if (ex.Response != null)
                        {
                            _lastRequestLog.ResponseHeaders = ex.Response.Headers;
                            //ensure the stream is disposed
                            using (Stream rs = ex.Response.GetResponseStream())
                            {
                                using (StreamReader reader = new StreamReader(rs))
                                {
                                    html = reader.ReadToEnd();
                                }
                            }
                            _lastRequestLog.Text = html;
                        }

                        LastWebException = ex;

                        switch (ex.Status)
                        {
                        case WebExceptionStatus.Timeout:
                            Log("A timeout occurred while trying to load the web page", LogMessageType.Error);
                            break;

                        case WebExceptionStatus.ReceiveFailure:
                            Log("The response was cut short prematurely", LogMessageType.Error);
                            break;

                        default:
                            Log("An exception was thrown while trying to load the page: " + ex.Message, LogMessageType.Error);
                            break;
                        }
                        return(false);
                    }
                    finally
                    {
                        LogRequestData();
                    }
                }while (handle3xxRedirect);
            }

            this._navigationAttributes = null;
            this.RemoveChildBrowsers();             //Any frames contained in the previous state should be removed. They will be recreated if we ever navigate back
            this.AddNavigationState(
                new NavigationState()
            {
                Html        = html,
                Url         = uri,
                ContentType = contentType,
                Referer     = string.IsNullOrEmpty(referer) ? null : new Uri(Uri.UnescapeDataString(referer))
            });

            return(true);
        }
コード例 #12
0
 public void ComplexFacebookCookie_DoesNotThrowException()
 {
     const string cookieHeader =
         "datr=80gTVQMaYfPPoOakoxDXhrmQ; expires=Fri, 24-Mar-2017 23:46:59 GMT; Max-Age=63072000; path=/; domain=.facebook.com; httponly,reg_ext_ref=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; domain=.facebook.com,reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com; httponly,reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com; httponly";
     CookieCollection actual = SetCookieHeaderParser.GetAllCookiesFromHeader("test.com", cookieHeader);
 }