コード例 #1
0
        private void ProcessS3PostResponse(object state)
        {
            AsyncResult asyncResult = null;

            try
            {
                asyncResult = state as AsyncResult;
                asyncResult.ServiceResult.HttpResponseData = asyncResult.ResponseData;

                S3PostUploadResponse response = new S3PostUploadResponse();

                WWWResponseData responseData = asyncResult.ResponseData;

                if (!String.IsNullOrEmpty(responseData.Error) || !String.IsNullOrEmpty(System.Text.Encoding.UTF8.GetString(responseData.GetBytes())))
                {
                    AmazonLogging.LogError(AmazonLogging.AmazonLoggingLevel.Critical, "S3", responseData.Error);
                    foreach (string key in responseData.GetHeaderNames())
                    {
                        AmazonLogging.Log(AmazonLogging.AmazonLoggingLevel.Verbose, "S3", key + " : " + responseData.GetHeaderValue(key));
                    }

                    if (asyncResult.Exception == null)
                    {
                        asyncResult.Exception = new AmazonServiceException("Bad Request");
                    }

                    asyncResult.IsCompleted            = true;
                    response.StatusCode                = HttpStatusCode.BadRequest;
                    asyncResult.ServiceResult.Response = response;
                    AmazonLogging.LogError(AmazonLogging.AmazonLoggingLevel.Warnings, "S3", "RetriesAttempt exceeded");
                    asyncResult.HandleException(asyncResult.Exception);

                    return;
                }
                else
                {
                    asyncResult.IsCompleted            = true;
                    response.StatusCode                = HttpStatusCode.NoContent;
                    asyncResult.ServiceResult.Response = response;
                    asyncResult.InvokeCallback();
                    return;
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                asyncResult.HandleException(e);
                return;
            }
        }
        /// <summary>
        /// The HandleWWWErrorResponse differs significantly from the error handling doing in .NET sdk
        /// since the www.error string message is incomplete
        /// so this requires rewriting all responseunmarshallers.HandleErrorContext which is not part of this version
        /// hence exception thrown will always be of base type AmazonServiceException
        /// </summary>
        /// <returns>True if the error needs retry</returns>
        private bool HandleWWWErrorResponse(AsyncResult asyncResult)
        {
            WWWResponseData errorResponse = asyncResult.ResponseData;

            asyncResult.Metrics.AddProperty(Metric.Exception, errorResponse.Error);

            AmazonServiceException errorResponseException = null;

            errorResponseException = new AmazonServiceException(errorResponse.Error,
                                                                new WebException(errorResponse.Error));

            errorResponseException.UnityStatusCode = errorResponse.Error;
            try
            {
                errorResponseException.StatusCode = errorResponse.ErrorStatusCode;
            }
            catch (Exception e)
            {
                // Parsing exception
                AmazonLogging.LogException(AmazonLogging.AmazonLoggingLevel.Errors, asyncResult.Request.RequestName, e);
            }

            string curl = "curl " + (asyncResult.Request.HttpMethod == "GET" &&
                                     !HttpOverrideSupportedServices.Contains(asyncResult.Request.ServiceName) ?
                                     "-G " :  "-X POST ");

            foreach (string key in asyncResult.RequestData.Headers.Keys)
            {
                curl += " -H \"" + key + ": " + asyncResult.RequestData.Headers[key] + "\" ";
            }
            if (asyncResult.RequestData.Data != null)
            {
                curl += " -d '" + System.Text.Encoding.Default.GetString(asyncResult.RequestData.Data) + "' ";
            }

            curl += " " + asyncResult.RequestData.Url;
            Debug.LogError(curl);

            if (errorResponse.IsHeaderPresent(HeaderKeys.XAmzRequestIdHeader.ToUpper()))
            {
                errorResponseException.RequestId = errorResponse.GetHeaderValue(HeaderKeys.XAmzRequestIdHeader);
            }

            asyncResult.Exception = errorResponseException;

            // currently no retries are done
            return(false);
        }