Beispiel #1
0
        /// <summary>
        /// Method for adding cookies to the HttpResponseInfo object
        /// </summary>
        /// <param name="responseInfo">The HttpResponseInfo object</param>
        /// <param name="headerValue">The "Set-Cookie" header value</param>
        private static void AddCookies(HttpResponseInfo responseInfo, string headerValue)
        {
            /*
             * In case of multiple Set-Cookie headers, .NET collaspses them into a single Set-Cookie header, separated by comma.
             * But cookie attributes themselves can contain commas. Therefore, in order to correctly parse the Set-Cookie header,
             * we need to ensure that the string tokens that we get after splitting are valid cookies. A valid cookie must start
             * with a name-value pair separated by "=" that contains the name of the cookie.
             */

            string[] setCookieHeaders = headerValue.Split(',');

            HTTPHeader previousSetCookieHeader = null;

            foreach (string part in setCookieHeaders)
            {
                if (IsValidSetCookieHeader(part))   //Valid cookie start found, create a new "Set-Cookie"
                {
                    previousSetCookieHeader = new HTTPHeader("Set-Cookie", part);
                    responseInfo.Headers.Add(previousSetCookieHeader);
                }
                else //not a valid cookie start, this should be appended to the last "Set-Cookie"
                {
                    if (previousSetCookieHeader != null)
                    {
                        previousSetCookieHeader.Values[0] += "," + part;
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets the basic authentication header to be added to a HttpRequest
        /// </summary>
        /// <param name="authInfo"></param>
        /// <returns></returns>
        public HTTPHeader GetBasicAuthHeader(HttpAuthenticationInfo authInfo)
        {
            HTTPHeader header = null;
            string     credentialsString;
            string     decryptedPassword = Encryptor.DecryptToString(authInfo.Credentials.Password);

            if (String.IsNullOrEmpty(authInfo.Credentials.Domain))
            {
                credentialsString = String.Format("{0}:{1}", authInfo.Credentials.UserName, decryptedPassword);
            }
            else
            {
                credentialsString = String.Format("{0}\\{1}:{2}", authInfo.Credentials.Domain, authInfo.Credentials.UserName, decryptedPassword);
            }
            //base64 encode the string
            credentialsString = Utils.Base64Encode(credentialsString);
            if (authInfo.IsProxy)
            {
                header = new HTTPHeader("Proxy-Authorization", String.Format("Basic {0}", credentialsString));
            }
            else
            {
                header = new HTTPHeader("Authorization", String.Format("Basic {0}", credentialsString));
            }
            return(header);
        }
Beispiel #3
0
        /// <summary>
        /// Sends the request to the site
        /// </summary>
        /// <param name="requestInfo"></param>
        /// <returns></returns>
        public override HttpResponseInfo SendRequest(HttpRequestInfo requestInfo)
        {
            PrepareConnection(requestInfo);

            HttpClientRequest request = new HttpClientRequest(_connection);

            //check if the request requires credentials
            HttpAuthenticationInfo authInfo;

            if (HttpAuthenticationManager.Instance.RequiresCredentials(requestInfo, out authInfo))
            {
                requestInfo.Headers.Add(HttpAuthenticationManager.Instance.GetBasicAuthHeader(authInfo));
            }

            //delay the request
            ProcessRequestDelay(requestInfo);

            request.SendRequest(requestInfo, requestInfo.IsSecure);

            bool success = request.RequestCompleteEvent.WaitOne(_timeout);

            if (!success || request.Response == null)
            {
                throw new Exception(Resources.RequestTimeout);
            }
            string rawResponse    = Constants.DefaultEncoding.GetString(request.Response);
            string responseStatus = HttpResponseInfo.GetResponseStatus(rawResponse);
            bool   isPlatformAuth = String.Compare(responseStatus, "401") == 0;
            bool   isProxyAuth    = String.Compare(responseStatus, "407") == 0;

            if (isPlatformAuth || isProxyAuth)
            {
                //check the headers
                HttpResponseInfo  respInfo    = new HttpResponseInfo(rawResponse);
                List <HTTPHeader> authHeaders = new List <HTTPHeader>();
                if (isPlatformAuth)
                {
                    authHeaders = respInfo.Headers.GetHeaders("WWW-Authenticate");
                }
                else if (isProxyAuth)
                {
                    authHeaders = respInfo.Headers.GetHeaders("Proxy-Authenticate");
                }

                bool usesBasic = false;

                for (int i = authHeaders.Count - 1; i > -1; i--) //go backwards the basic header is usually last
                {
                    HTTPHeader header = authHeaders[i];
                    if (header.Value.IndexOf("Basic", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        usesBasic = true;
                        break;
                    }
                }

                if (usesBasic)
                {
                    //try to get the credentials from the user
                    authInfo = HttpAuthenticationManager.Instance.GetCredentials(requestInfo, isProxyAuth);
                    if (authInfo != null)
                    {
                        //add the basic auth header
                        requestInfo.Headers.Add(HttpAuthenticationManager.Instance.GetBasicAuthHeader(authInfo));
                        //create a new request
                        PrepareConnection(requestInfo);
                        request = new HttpClientRequest(_connection);
                        //proceed with new request
                        request.SendRequest(requestInfo, requestInfo.IsSecure);
                        success = request.RequestCompleteEvent.WaitOne(_timeout);

                        if (!success)
                        {
                            throw new Exception(Resources.RequestTimeout);
                        }
                    }
                }
            }

            if (request.Response != null)
            {
                return(new HttpResponseInfo(request.Response));
            }

            return(null);
        }