Beispiel #1
0
        internal IWebResponseData CreateMockResponse(HttpRequestInfo requestInfo)
        {
            HttpWebResponse mockResponse = CreateHttpWebResponse(requestInfo);
            var             responseData = new HttpWebRequestResponseData(mockResponse);

            var isError = (int)mockResponse.StatusCode >= 300;

            if (isError)
            {
                throw new HttpErrorResponseException(responseData);
            }

            return(responseData);
        }
Beispiel #2
0
        private bool HandleHttpWebErrorResponse(AsyncResult asyncResult, WebException we)
        {
            asyncResult.Metrics.AddProperty(Metric.Exception, we);

            HttpStatusCode         statusCode;
            AmazonServiceException errorResponseException = null;

            using (HttpWebResponse httpErrorResponse = we.Response as HttpWebResponse)
            {
                if (we != null && WebExceptionStatusesToThrowOn.Contains(we.Status))
                {
                    throw new AmazonServiceException("Encountered a non retryable WebException : " + we.Status, we);
                }

                if (httpErrorResponse == null ||
                    (we != null && WebExceptionStatusesToRetryOn.Contains(we.Status)))
                {
                    // Abort the unsuccessful request
                    asyncResult.RequestState.WebRequest.Abort();

                    if (CanRetry(asyncResult) && asyncResult.RetriesAttempt < Config.MaxErrorRetry)
                    {
                        pauseExponentially(asyncResult);
                        return(true);
                    }
                    var errorMessage = string.Format(CultureInfo.InvariantCulture,
                                                     "Encountered a WebException ({0}), the request cannot be retried. Either the maximum number of retries has been exceeded ({1}/{2}) or the request is using a non-seekable stream.",
                                                     we.Status, asyncResult.RetriesAttempt, Config.MaxErrorRetry);
                    throw new AmazonServiceException(errorMessage, we);
                }

                statusCode = httpErrorResponse.StatusCode;
                asyncResult.Metrics.AddProperty(Metric.StatusCode, statusCode);
                string redirectedLocation = httpErrorResponse.Headers[HeaderKeys.LocationHeader];
                asyncResult.Metrics.AddProperty(Metric.RedirectLocation, redirectedLocation);

                using (httpErrorResponse)
                {
                    var unmarshaller                 = asyncResult.Unmarshaller;
                    var httpResponseData             = new HttpWebRequestResponseData(httpErrorResponse);
                    UnmarshallerContext errorContext = unmarshaller.CreateContext(httpResponseData,
                                                                                  Config.LogResponse || Config.ReadEntireResponse || AWSConfigs.LoggingConfig.LogResponses != ResponseLoggingOption.Never,
                                                                                  httpResponseData.ResponseBody.OpenResponse(),
                                                                                  asyncResult.Metrics);

                    errorResponseException = unmarshaller.UnmarshallException(errorContext, we, statusCode);
                    if (Config.LogResponse || AWSConfigs.LoggingConfig.LogResponses != ResponseLoggingOption.Never)
                    {
                        this.logger.Error(errorResponseException, "Received error response: [{0}]", errorContext.ResponseBody);
                    }
                    asyncResult.Metrics.AddProperty(Metric.AWSRequestID, errorResponseException.RequestId);
                    asyncResult.Metrics.AddProperty(Metric.AWSErrorCode, errorResponseException.ErrorCode);
                }
                asyncResult.RequestState.WebRequest.Abort();

                if (CanRetry(asyncResult))
                {
                    if (isTemporaryRedirect(statusCode, redirectedLocation))
                    {
                        this.logger.DebugFormat("Request {0} is being redirected to {1}.", asyncResult.RequestName, redirectedLocation);
                        asyncResult.Request.Endpoint = new Uri(redirectedLocation);
                        return(true);
                    }
                    else if (ShouldRetry(statusCode, this.Config, errorResponseException, asyncResult.RetriesAttempt))
                    {
                        this.logger.DebugFormat("Retry number {0} for request {1}.", asyncResult.RetriesAttempt, asyncResult.RequestName);
                        pauseExponentially(asyncResult);
                        return(true);
                    }
                }
            }

            if (errorResponseException != null)
            {
                this.logger.Error(errorResponseException, "Error making request {0}.", asyncResult.RequestName);
                throw errorResponseException;
            }

            AmazonServiceException excep = new AmazonServiceException("Unable to make request", we, statusCode);

            this.logger.Error(excep, "Error making request {0}.", asyncResult.RequestName);
            asyncResult.Metrics.AddProperty(Metric.Exception, excep);
            throw excep;
        }
Beispiel #3
0
        void getResponseCallback(IAsyncResult result)
        {
            AsyncResult asyncResult = result as AsyncResult;

            if (asyncResult == null)
            {
                asyncResult = result.AsyncState as AsyncResult;
            }

            UnmarshallerContext context = null;

            asyncResult.RequestState.GetResponseCallbackCalled = true;
            bool shouldRetry = false;
            AmazonWebServiceResponse response = null;

            try
            {
                AsyncResult.AsyncRequestState state = asyncResult.RequestState;
                HttpWebResponse httpResponse        = null;
                try
                {
                    if (asyncResult.CompletedSynchronously)
                    {
                        httpResponse = state.WebRequest.GetResponse() as HttpWebResponse;
                    }
                    else
                    {
                        httpResponse = state.WebRequest.EndGetResponse(result) as HttpWebResponse;
                    }

                    using (asyncResult.Metrics.StartEvent(Metric.ResponseProcessingTime))
                    {
                        var unmarshaller = asyncResult.Unmarshaller;
                        LogResponse(asyncResult.Metrics, asyncResult.Request, httpResponse.StatusCode);

                        try
                        {
                            var httpResponseData = new HttpWebRequestResponseData(httpResponse);
                            context = unmarshaller.CreateContext(httpResponseData,
                                                                 this.SupportResponseLogging &&
                                                                 (Config.LogResponse || Config.ReadEntireResponse || AWSConfigs.LoggingConfig.LogResponses != ResponseLoggingOption.Never),
                                                                 httpResponseData.ResponseBody.OpenResponse(),
                                                                 asyncResult.Metrics);

                            using (asyncResult.Metrics.StartEvent(Metric.ResponseUnmarshallTime))
                            {
                                response = unmarshaller.Unmarshall(context);
                            }
                            context.ValidateCRC32IfAvailable();

                            response.ContentLength    = httpResponse.ContentLength;
                            response.HttpStatusCode   = httpResponse.StatusCode;
                            asyncResult.FinalResponse = response;
                            if (response.ResponseMetadata != null)
                            {
                                asyncResult.Metrics.AddProperty(Metric.AWSRequestID, response.ResponseMetadata.RequestId);
                            }

                            LogFinishedResponse(asyncResult.Metrics, context, httpResponse.ContentLength);
                        }
                        finally
                        {
                            if (!unmarshaller.HasStreamingProperty)
                            {
                                httpResponse.Close();
                            }
                        }
                    }
                }
                catch (WebException we)
                {
                    HttpWebResponse exceptionHttpResponse = we.Response as HttpWebResponse;

                    // If the error is a 404 and the request is configured to supress it. Then unmarshall as much as we can.
                    if (exceptionHttpResponse != null &&
                        exceptionHttpResponse.StatusCode == HttpStatusCode.NotFound &&
                        asyncResult.Request.Suppress404Exceptions)
                    {
                        var unmarshaller                 = asyncResult.Unmarshaller;
                        var httpResponseData             = new HttpWebRequestResponseData(exceptionHttpResponse);
                        UnmarshallerContext errorContext = unmarshaller.CreateContext(
                            httpResponseData,
                            Config.LogResponse || Config.ReadEntireResponse || AWSConfigs.LoggingConfig.LogResponses != ResponseLoggingOption.Never,
                            httpResponseData.ResponseBody.OpenResponse(),
                            asyncResult.Metrics);

                        try
                        {
                            response = unmarshaller.Unmarshall(errorContext);
                            response.ContentLength  = exceptionHttpResponse.ContentLength;
                            response.HttpStatusCode = exceptionHttpResponse.StatusCode;
                        }
                        catch (Exception e)
                        {
                            logger.Debug(e, "Failed to unmarshall 404 response when it was being supressed");
                        }

                        asyncResult.FinalResponse = response;
                    }
                    else
                    {
                        if (exceptionHttpResponse != null)
                        {
                            LogResponse(asyncResult.Metrics, asyncResult.Request, exceptionHttpResponse.StatusCode);
                        }
                        else
                        {
                            asyncResult.Metrics.StopEvent(Metric.HttpRequestTime);
                        }
                        shouldRetry = HandleHttpWebErrorResponse(asyncResult, we);
                    }
                }
                catch (IOException ioe)
                {
                    LogResponse(asyncResult.Metrics, asyncResult.Request, HttpStatusCode.Unused);
                    shouldRetry = HandleIOException(asyncResult, httpResponse, ioe);
                    if (!shouldRetry)
                    {
                        asyncResult.Exception = ioe;
                    }
                }

                if (shouldRetry)
                {
                    asyncResult.RequestState.WebRequest.Abort();
                    asyncResult.RetriesAttempt++;
                    InvokeHelper(asyncResult);
                }
                else
                {
                    LogFinalMetrics(asyncResult.Metrics);
                }
            }
            catch (Exception e)
            {
                if (context != null && AWSConfigs.LoggingConfig.LogResponses == ResponseLoggingOption.OnError)
                {
                    this.logger.Error(e, "Received response: [{0}]", context.ResponseBody);
                }

                asyncResult.RequestState.WebRequest.Abort();
                asyncResult.Exception = e;
                asyncResult.Metrics.AddProperty(Metric.Exception, e);
                asyncResult.Metrics.StopEvent(Metric.ClientExecuteTime);
                shouldRetry = false;
                if (Config.LogMetrics)
                {
                    logger.InfoFormat("Request metrics: {0}", asyncResult.Metrics);
                }

                ProcessExceptionHandlers(e, asyncResult.Request);
                logger.Error(e, "Error configuring web request {0} to {1}.", asyncResult.RequestName, asyncResult.Request.Endpoint.ToString());
            }
            finally
            {
                if (!shouldRetry)
                {
                    var responseData = context == null ? null : context.ResponseData;
                    ProcessResponseHandlers(response, asyncResult.Request, responseData);
                    asyncResult.InvokeCallback();
                }
            }
        }