protected virtual void HandleException(IExecutionContext executionContext, Exception exception)
        {
            PutObjectRequest putObjectRequest = executionContext.get_RequestContext().get_OriginalRequest() as PutObjectRequest;

            if (putObjectRequest != null)
            {
                HashStream val = putObjectRequest.InputStream as HashStream;
                if (val != null)
                {
                    putObjectRequest.InputStream = val.GetNonWrapperBaseStream();
                }
            }
            UploadPartRequest uploadPartRequest = executionContext.get_RequestContext().get_OriginalRequest() as UploadPartRequest;

            if (uploadPartRequest != null)
            {
                HashStream val2 = uploadPartRequest.InputStream as HashStream;
                if (val2 != null)
                {
                    uploadPartRequest.InputStream = val2.GetNonWrapperBaseStream();
                }
            }
            if (executionContext.get_RequestContext().get_Request() != null)
            {
                AmazonS3Client.CleanupRequest(executionContext.get_RequestContext().get_Request());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Catch exceptions and clean up any open streams.
        /// </summary>
        /// <param name="executionContext"></param>
        /// <param name="exception"></param>
        protected virtual void HandleException(IExecutionContext executionContext, Exception exception)
        {
            var putObjectRequest = executionContext.RequestContext.OriginalRequest as PutObjectRequest;

            if (putObjectRequest != null)
            {
                // If InputStream was a HashStream, compare calculated hash to returned etag
                HashStream hashStream = putObjectRequest.InputStream as HashStream;
                if (hashStream != null)
                {
                    // Set InputStream to its original value
                    putObjectRequest.InputStream = hashStream.GetNonWrapperBaseStream();
                }
            }

            var uploadPartRequest = executionContext.RequestContext.OriginalRequest as UploadPartRequest;

            if (uploadPartRequest != null)
            {
                // If InputStream was a HashStream, compare calculated hash to returned etag
                HashStream hashStream = uploadPartRequest.InputStream as HashStream;
                if (hashStream != null)
                {
                    // Set InputStream to its original value
                    uploadPartRequest.InputStream = hashStream.GetNonWrapperBaseStream();
                }
            }

            AmazonS3Client.CleanupRequest(executionContext.RequestContext.OriginalRequest);
        }
        private static void ProcessResponseHandlers(IExecutionContext executionContext)
        {
            AmazonWebServiceResponse response = executionContext.ResponseContext.Response;
            IRequest         request          = executionContext.RequestContext.Request;
            IWebResponseData webResponseData  = executionContext.ResponseContext.HttpResponse;
            bool             isSse            = HasSSEHeaders(webResponseData);

            var getObjectResponse = response as GetObjectResponse;

            if (getObjectResponse != null)
            {
                GetObjectRequest getObjectRequest = request.OriginalRequest as GetObjectRequest;
                getObjectResponse.BucketName = getObjectRequest.BucketName;
                getObjectResponse.Key        = getObjectRequest.Key;

                // If ETag is present and is an MD5 hash (not a multi-part upload ETag), and no byte range is specified,
                // wrap the response stream in an MD5Stream.
                // If there is a customer encryption algorithm the etag is not an MD5.
                if (!string.IsNullOrEmpty(getObjectResponse.ETag) &&
                    !getObjectResponse.ETag.Contains("-") &&
                    !isSse &&
                    getObjectRequest.ByteRange == null)
                {
                    string     etag         = getObjectResponse.ETag.Trim(etagTrimChars);
                    byte[]     expectedHash = AWSSDKUtils.HexStringToBytes(etag);
                    HashStream hashStream   = new MD5Stream(getObjectResponse.ResponseStream, expectedHash, getObjectResponse.ContentLength);
                    getObjectResponse.ResponseStream = hashStream;
                }
            }

            var deleteObjectsResponse = response as DeleteObjectsResponse;

            if (deleteObjectsResponse != null)
            {
                if (deleteObjectsResponse.DeleteErrors != null && deleteObjectsResponse.DeleteErrors.Count > 0)
                {
                    throw new DeleteObjectsException(deleteObjectsResponse as DeleteObjectsResponse);
                }
            }

            var putObjectResponse = response as PutObjectResponse;
            var putObjectRequest  = request.OriginalRequest as PutObjectRequest;

            if (putObjectRequest != null)
            {
                // If InputStream was a HashStream, compare calculated hash to returned etag
                HashStream hashStream = putObjectRequest.InputStream as HashStream;
                if (hashStream != null)
                {
                    if (putObjectResponse != null && !isSse)
                    {
                        // Stream may not have been closed, so force calculation of hash
                        hashStream.CalculateHash();
                        CompareHashes(putObjectResponse.ETag, hashStream.CalculatedHash);
                    }

                    // Set InputStream to its original value
                    putObjectRequest.InputStream = hashStream.GetNonWrapperBaseStream();
                }
            }

            var listObjectsResponse = response as ListObjectsResponse;

            if (listObjectsResponse != null)
            {
                if (listObjectsResponse.IsTruncated &&
                    string.IsNullOrEmpty(listObjectsResponse.NextMarker) &&
                    listObjectsResponse.S3Objects.Count > 0)
                {
                    listObjectsResponse.NextMarker = listObjectsResponse.S3Objects.Last().Key;
                }
            }

            var uploadPartRequest  = request.OriginalRequest as UploadPartRequest;
            var uploadPartResponse = response as UploadPartResponse;

            if (uploadPartRequest != null)
            {
                if (uploadPartResponse != null)
                {
                    uploadPartResponse.PartNumber = uploadPartRequest.PartNumber;
                }

                // If InputStream was a HashStream, compare calculated hash to returned etag
                HashStream hashStream = uploadPartRequest.InputStream as HashStream;
                if (hashStream != null)
                {
                    if (uploadPartResponse != null && !isSse)
                    {
                        // Stream may not have been closed, so force calculation of hash
                        hashStream.CalculateHash();
                        CompareHashes(uploadPartResponse.ETag, hashStream.CalculatedHash);
                    }

                    // Set InputStream to its original value
                    uploadPartRequest.InputStream = hashStream.GetNonWrapperBaseStream();
                }
            }

            var copyPartResponse = response as CopyPartResponse;

            if (copyPartResponse != null)
            {
                copyPartResponse.PartNumber = ((CopyPartRequest)request.OriginalRequest).PartNumber;
            }

            AmazonS3Client.CleanupRequest(request);
        }
        private static void ProcessResponseHandlers(IExecutionContext executionContext)
        {
            //IL_00ac: Unknown result type (might be due to invalid IL or missing references)
            //IL_00b3: Expected O, but got Unknown
            AmazonWebServiceResponse response   = executionContext.get_ResponseContext().get_Response();
            IRequest          request           = executionContext.get_RequestContext().get_Request();
            bool              flag              = HasSSEHeaders(executionContext.get_ResponseContext().get_HttpResponse());
            GetObjectResponse getObjectResponse = response as GetObjectResponse;

            if (getObjectResponse != null)
            {
                GetObjectRequest getObjectRequest = request.get_OriginalRequest() as GetObjectRequest;
                getObjectResponse.BucketName = getObjectRequest.BucketName;
                getObjectResponse.Key        = getObjectRequest.Key;
                if (!string.IsNullOrEmpty(getObjectResponse.ETag) && !getObjectResponse.ETag.Contains("-") && !flag && getObjectRequest.ByteRange == null)
                {
                    byte[]     array = AWSSDKUtils.HexStringToBytes(getObjectResponse.ETag.Trim(etagTrimChars));
                    HashStream val   = getObjectResponse.ResponseStream = (Stream) new MD5Stream(getObjectResponse.ResponseStream, array, getObjectResponse.get_ContentLength());
                }
            }
            DeleteObjectsResponse deleteObjectsResponse = response as DeleteObjectsResponse;

            if (deleteObjectsResponse != null && deleteObjectsResponse.DeleteErrors != null && deleteObjectsResponse.DeleteErrors.Count > 0)
            {
                throw new DeleteObjectsException(deleteObjectsResponse);
            }
            PutObjectResponse putObjectResponse = response as PutObjectResponse;
            PutObjectRequest  putObjectRequest  = request.get_OriginalRequest() as PutObjectRequest;

            if (putObjectRequest != null)
            {
                HashStream val2 = putObjectRequest.InputStream as HashStream;
                if (val2 != null)
                {
                    if (putObjectResponse != null && !flag)
                    {
                        val2.CalculateHash();
                        CompareHashes(putObjectResponse.ETag, val2.get_CalculatedHash());
                    }
                    putObjectRequest.InputStream = val2.GetNonWrapperBaseStream();
                }
            }
            ListObjectsResponse listObjectsResponse = response as ListObjectsResponse;

            if (listObjectsResponse != null && listObjectsResponse.IsTruncated && string.IsNullOrEmpty(listObjectsResponse.NextMarker) && listObjectsResponse.S3Objects.Count > 0)
            {
                listObjectsResponse.NextMarker = listObjectsResponse.S3Objects.Last().Key;
            }
            UploadPartRequest  uploadPartRequest  = request.get_OriginalRequest() as UploadPartRequest;
            UploadPartResponse uploadPartResponse = response as UploadPartResponse;

            if (uploadPartRequest != null)
            {
                if (uploadPartResponse != null)
                {
                    uploadPartResponse.PartNumber = uploadPartRequest.PartNumber;
                }
                HashStream val3 = uploadPartRequest.InputStream as HashStream;
                if (val3 != null)
                {
                    if (uploadPartResponse != null && !flag)
                    {
                        val3.CalculateHash();
                        CompareHashes(uploadPartResponse.ETag, val3.get_CalculatedHash());
                    }
                    uploadPartRequest.InputStream = val3.GetNonWrapperBaseStream();
                }
            }
            CopyPartResponse copyPartResponse = response as CopyPartResponse;

            if (copyPartResponse != null)
            {
                copyPartResponse.PartNumber = ((CopyPartRequest)request.get_OriginalRequest()).PartNumber;
            }
            AmazonS3Client.CleanupRequest(request);
        }