Exemplo n.º 1
0
        public static HttpResponseImpl CreateHttpResponse(HttpRequest request, ICallInfo info,
                                                          HttpRequestQueueResultCode result, AsyncHttpRequest asyncHttpRequest)
        {
            var    statusCode = -1;
            string statusDescription;

            byte[] responseData         = null;
            NameValueCollection headers = null;

            try
            {
                switch (result)
                {
                case HttpRequestQueueResultCode.Success:
                    statusCode        = (int)asyncHttpRequest.WebResponse.StatusCode;
                    statusDescription = asyncHttpRequest.WebResponse.StatusDescription;
                    responseData      = asyncHttpRequest.Response;
                    headers           = asyncHttpRequest.WebResponse.Headers;
                    break;

                case HttpRequestQueueResultCode.Error:
                    if (asyncHttpRequest.WebResponse != null)
                    {
                        statusCode        = (int)asyncHttpRequest.WebResponse.StatusCode;
                        statusDescription = asyncHttpRequest.WebResponse.StatusDescription;
                        headers           = asyncHttpRequest.WebResponse.Headers;
                    }
                    else
                    {
                        statusDescription = asyncHttpRequest.Exception.Message;
                    }

                    break;

                default:
                    statusCode        = -1;
                    statusDescription = String.Empty;
                    break;
                }
            }
            catch (Exception ex)
            {
                // we should never get her
                statusDescription = ex.Message;

                log.Warn("Exception during http response creation", ex);
            }

            var webStatus = asyncHttpRequest == null ? -1 : (int)asyncHttpRequest.WebStatus;

            if (asyncHttpRequest != null)
            {
                asyncHttpRequest.Dispose();
            }

            return(new HttpResponseImpl(request, info, responseData, result, statusCode, statusDescription, webStatus, headers));
        }
Exemplo n.º 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); });
        }
Exemplo n.º 3
0
        private static void HttpRequestQueueCallbackAsync(HttpRequestQueueResultCode result, AsyncHttpRequest request, object userState)
        {
            var state = (AsyncRequestState)userState;

            state.Stopwatch.Stop();
            var appId     = state.AppId;
            var elapsedMS = state.Stopwatch.ElapsedMilliseconds;
            var queue     = state.HttpQueue;

            state.ResultCode = MonitoringServiceResult.Error;
            state.HttpRequestQueueResultCode = result;

            switch (result)
            {
            case HttpRequestQueueResultCode.Success:
            {
                GetTmpMonitoringFromResponse(state, request.WebRequest.RequestUri.ToString(), Encoding.UTF8.GetString(request.Response));
                break;
            }

            case HttpRequestQueueResultCode.RequestTimeout:
            {
                if (log.IsWarnEnabled)
                {
                    log.WarnFormat(
                        "Monitoring service: Request failed. Timeout. appId={0}, url={1}, duration: {2}ms, duration2: {4}, request timeout set to: {3}ms, ConnectionLimit:{5}",
                        appId,
                        request.WebRequest.RequestUri,
                        elapsedMS,
                        request.WebRequest.Timeout,
                        request.Elapsedtime,
                        System.Net.ServicePointManager.DefaultConnectionLimit);
                }


                state.ResultCode   = MonitoringServiceResult.Timeout;
                state.ErrorMessage = ErrorMessages.RequestTimedout;
                break;
            }

            case HttpRequestQueueResultCode.Error:
            {
                var response = request.WebResponse;
                if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                {
//                            if (state.IsBlobStoreQueue && log.IsWarnEnabled)
//                            {
//                                log.WarnFormat(state.LogGuard,
//                                    "Account service: Application account does not exist or Url is wrong - did user submit an invalid App ID? appId={0}, url={1}, duration: {2}ms, request timeout set to: {3}ms",
//                                    appId,
//                                    request.WebRequest.RequestUri,
//                                    elapsedMS,
//                                    request.WebRequest.Timeout);
//                            }

                    state.ResultCode   = MonitoringServiceResult.NotFound;
                    state.ErrorMessage = ErrorMessages.AppIdNotFound;
                }
                else
                {
                    var msg = string.Format("Monitoring service: Error getting application account: appId={0}, url={1}", appId, request.WebRequest.RequestUri);
                    log.Error(state.LogGuard, msg, request.Exception);
                    state.ErrorMessage = string.Format("Error during getting monitoring: WebStatus:{0}, Request Status:{1}", request.WebStatus, request.Status);
                }
                break;
            }

            case HttpRequestQueueResultCode.QueueTimeout:
            {
                if (log.IsWarnEnabled)
                {
                    log.WarnFormat(state.LogGuard,
                                   "Monitoring service: Request failed. Http Queue Timeout. appId:'{0}', url:'{1}', timeout:{2}, max concurent request:{3}",
                                   appId,
                                   request.WebRequest.RequestUri,
                                   queue.QueueTimeout,
                                   queue.MaxConcurrentRequests
                                   );
                }

                state.ErrorMessage = ErrorMessages.QueueTimeout;
                break;
            }

            case HttpRequestQueueResultCode.QueueFull:
            {
                if (log.IsWarnEnabled)
                {
                    log.WarnFormat(state.LogGuard,
                                   "Monitoring service: Request failed. Http Queue Full. appId:'{0}', url:'{1}', max queue size:{2}",
                                   appId,
                                   request.WebRequest.RequestUri,
                                   queue.MaxQueuedRequests);
                }
                state.ErrorMessage = ErrorMessages.QueueFull;
                break;
            }

            case HttpRequestQueueResultCode.Offline:
            {
                if (log.IsWarnEnabled)
                {
                    log.WarnFormat(state.LogGuard,
                                   "Monitoring service: Request failed. Http Queue is offline. appId:'{0}', url:'{1}', queue reconnect interval:{2}",
                                   appId,
                                   request.WebRequest.RequestUri,
                                   queue.ReconnectInterval);
                }
                state.ErrorMessage = ErrorMessages.QueueOffline;
                break;
            }

            default:
                log.ErrorFormat("Monitoring service: Unknown result code: {0}, a", result);
                break;
            }

            request.Dispose();

            try
            {
                state.Callback(state);
            }
            catch (Exception e)
            {
                log.Warn(state.LogGuard, string.Format("Monitoring service: Got exception during callback call. appId:{0}. Exception Msg:{1}", appId, e.Message), e);
            }
        }