private void OnCallback(HttpRequestQueueResultCode resultCode, 
            AsyncHttpRequest result, object userState, Action<AsyncHttpResponse, ClientAuthenticationQueue> userCallback)
        {
            try
            {
                var url = result.WebRequest.RequestUri;
                byte[] responseData = null;
                var status = result.Status;
                var exception = result.Exception;

                this.RequestTimeCounter.AddValue((int)result.Elapsedtime.TotalMilliseconds);
                if (result.Response != null)
                {
                    responseData = result.Response;
                }

                byte[] resultResponseData = null;
                switch (resultCode)
                {
                    case HttpRequestQueueResultCode.Success:
                    {
                        if (log.IsDebugEnabled)
                        {
                            var responseString = string.Empty;
                            if (responseData != null)
                            {
                                responseString = Encoding.UTF8.GetString(responseData);
                            }

                            log.DebugFormat(
                                "Custom authentication result: uri={0}, status={1}, msg={2}, data={3}",
                                url,
                                status,
                                exception != null ? exception.Message : string.Empty,
                                responseString);
                        }

                        this.timeoutCounter.AddValue(0);
                        resultResponseData = responseData;
                    }
                        break;
                    case HttpRequestQueueResultCode.RequestTimeout:
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.DebugFormat("Custom authentication timed out: uri={0}, status={1}, msg={2}",
                                url, status, exception != null ? exception.Message : string.Empty);
                        }
                        this.timeoutCounter.AddValue(1);
                    }
                        break;
                    case HttpRequestQueueResultCode.QueueFull:
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.DebugFormat(
                                "Custom authentication error: queue is full. Requests count {0}, url:{1}, msg:{2}",
                                this.httpRequestQueue.QueuedRequestCount, url,
                                exception != null ? exception.Message : string.Empty);
                        }
                    }
                        break;
                    case HttpRequestQueueResultCode.QueueTimeout:
                        if (log.IsDebugEnabled)
                        {
                            log.DebugFormat("Custom authentication error: Queue timedout. uri={0}, status={1}, msg={2}",
                                url, status, exception != null ? exception.Message : string.Empty);
                        }
                        break;
                    case HttpRequestQueueResultCode.Error:
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.DebugFormat("Custom authentication error: uri={0}, status={1}, msg={2}",
                                url, status, exception != null ? exception.Message : string.Empty);
                        }
                    }
                        break;
                    case HttpRequestQueueResultCode.Offline:
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.DebugFormat("Custom auth error. Queue is offline. url:{0}, status{1}, msg:{2}",
                                url, status, exception != null ? exception.Message : string.Empty);
                        }
                    }
                        break;
                }

                var response = new AsyncHttpResponse(resultCode, this.RejectIfUnavailable, userState)
                    {
                        ResponseData = resultResponseData
                    };

                ThreadPool.QueueUserWorkItem(delegate { userCallback(response, this); });
                return;
            }
            catch (Exception e)
            {
                log.ErrorFormat("Exception happened:{0}", e);
            }

            ThreadPool.QueueUserWorkItem(delegate { userCallback(new AsyncHttpResponse(HttpRequestQueueResultCode.Error, this.RejectIfUnavailable, userState), this); });
        }
示例#2
0
        //added for Viveport, they use 4XX status codes when token expires
        private void OnCallbackReturnExpectedStatusCodes(HttpRequestQueueResultCode resultCode, AsyncHttpRequest result, object userState, Action <AsyncHttpResponse, ClientAuthenticationQueue> userCallback, List <HttpStatusCode> expectedStatusCodes)
        {
            try
            {
                var    url          = result.WebRequest.RequestUri;
                byte[] responseData = null;
                var    status       = result.Status;
                var    exception    = result.Exception;

                this.RequestTimeCounter.AddValue((int)result.Elapsedtime.TotalMilliseconds);
                if (result.Response != null)
                {
                    responseData = result.Response;
                }

                if (resultCode == HttpRequestQueueResultCode.Error && expectedStatusCodes != null && expectedStatusCodes.Any(expectedStatusCode => expectedStatusCode == result.WebResponse.StatusCode))
                {
                    resultCode   = HttpRequestQueueResultCode.Success;
                    responseData = Encoding.UTF8.GetBytes(string.Format("{0}/{1}", (int)result.WebResponse.StatusCode, result.WebResponse.StatusDescription));
//                    responseData = Encoding.UTF8.GetBytes(((int)result.WebResponse.StatusCode).ToString());
                }

                result.Dispose();

                byte[] resultResponseData = null;
                switch (resultCode)
                {
                case HttpRequestQueueResultCode.Success:
                {
                    if (log.IsDebugEnabled)
                    {
                        var responseString = string.Empty;
                        if (responseData != null)
                        {
                            responseString = Encoding.UTF8.GetString(responseData);
                        }

                        log.DebugFormat(
                            "Custom authentication result: uri={0}, status={1}, msg={2}, data={3}",
                            url,
                            status,
                            exception != null ? exception.Message : string.Empty,
                            responseString);
                    }

                    this.timeoutCounter.AddValue(0);
                    resultResponseData = responseData;
                }
                break;

                case HttpRequestQueueResultCode.RequestTimeout:
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Custom authentication timed out: uri={0}, status={1}, msg={2}",
                                        url, status, exception != null ? exception.Message : string.Empty);
                    }
                    this.timeoutCounter.AddValue(1);
                }
                break;

                case HttpRequestQueueResultCode.QueueFull:
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat(
                            "Custom authentication error: queue is full. Requests count {0}, url:{1}, msg:{2}",
                            this.httpRequestQueue.QueuedRequestCount, url,
                            exception != null ? exception.Message : string.Empty);
                    }
                }
                break;

                case HttpRequestQueueResultCode.QueueTimeout:
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Custom authentication error: Queue timedout. uri={0}, status={1}, msg={2}",
                                        url, status, exception != null ? exception.Message : string.Empty);
                    }
                    break;

                case HttpRequestQueueResultCode.Error:
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Custom authentication error: uri={0}, status={1}, msg={2}",
                                        url, status, exception != null ? exception.Message : string.Empty);
                    }
                }
                break;

                case HttpRequestQueueResultCode.Offline:
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Custom auth error. Queue is offline. url:{0}, status{1}, msg:{2}",
                                        url, status, exception != null ? exception.Message : string.Empty);
                    }
                }
                break;
                }

                var response = new AsyncHttpResponse(resultCode, this.RejectIfUnavailable, userState)
                {
                    ResponseData = resultResponseData,
                    ElapsedTicks = result.ElapsedTicks,
                };

                ThreadPool.QueueUserWorkItem(delegate { userCallback(response, this); });
                return;
            }
            catch (Exception e)
            {
                log.Error(e);
            }


            ThreadPool.QueueUserWorkItem(delegate { userCallback(new AsyncHttpResponse(HttpRequestQueueResultCode.Error, this.RejectIfUnavailable, userState), this); });
        }
        private void AuthQueueResponseCallback(AsyncHttpResponse response, ClientAuthenticationQueue queue)
        {
            var queueState = (AuthQueueState)response.State;
            var peer = queueState.Peer;
            var authRequest = queueState.AuthenticateRequest;
            var sendParameters = queueState.SendParameters;

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Authenticate client finished: conId={0}, result={1}", peer.ConnectionId, response.Status);
            }

            switch (response.Status)
            {
                case HttpRequestQueueResultCode.Success:
                    {
                        if (response.ResponseData == null)
                        {
                            log.ErrorFormat("Custom authentication: failed. ResponseData is empty. AppId={0}/{1}",
                                authRequest.ApplicationId, authRequest.ApplicationId);

                            return;
                        }

                        // deserialize
                        var responseString = Encoding.UTF8.GetString(response.ResponseData).Trim(new char[] { '\uFEFF', '\u200B' });
                        CustomAuthenticationResult customAuthResult;
                        try
                        {
                            customAuthResult = Newtonsoft.Json.JsonConvert.DeserializeObject<CustomAuthenticationResult>(responseString);
                            //TBD: handle backward compatibility incustomAuthResult class
                            if (customAuthResult.AuthCookie == null)
                            {
                                customAuthResult.AuthCookie = customAuthResult.Secure;
                            }
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Custom authentication: failed to deserialize response. AppId={0}/{1}, Response={2}, Uri={3}",
                                authRequest.ApplicationId, authRequest.ApplicationVersion, responseString, queue.Uri);

                            peer.OnCustomAuthenticationError(
                                ErrorCode.CustomAuthenticationFailed, "Custom authentication deserialization failed: " + ex.Message, authRequest, sendParameters, queueState.State);
                            this.IncrementFailedCustomAuth();
                            return;
                        }

                        this.IncrementResultCounters(customAuthResult, (CustomAuthResultCounters.Instance)queue.CustomData);
                        peer.OnCustomAuthenticationResult(customAuthResult, authRequest, sendParameters, queueState.State);
                        return;
                    }

                case HttpRequestQueueResultCode.Offline:
                case HttpRequestQueueResultCode.QueueFull:
                case HttpRequestQueueResultCode.QueueTimeout:
                case HttpRequestQueueResultCode.RequestTimeout:
                case HttpRequestQueueResultCode.Error:
                    {
                        if (response.RejectIfUnavailable)
                        {
                            peer.OnCustomAuthenticationError(
                                ErrorCode.CustomAuthenticationFailed, "Custom authentication service error: " + response.Status, authRequest, sendParameters, queueState.State);
                            this.IncrementFailedCustomAuth();
                        }
                        else
                        {
                            var result = new CustomAuthenticationResult { ResultCode = CustomAuthenticationResultCode.Ok };
                            peer.OnCustomAuthenticationResult(result, authRequest, sendParameters, queueState.State);
                        }
                        return;
                    }
            }
        }