private static string GetCorrectRegion(AmazonS3Uri requestedBucketUri, AmazonServiceException serviceException)
{
string regionFromExceptionBody = null;
string regionFromExceptionHeader = null;
var s3Exception = serviceException as AmazonS3Exception;
if (s3Exception != null)
{
if (string.Equals(s3Exception.ErrorCode, AuthorizationHeaderMalformedErrorCode, StringComparison.Ordinal))
{
regionFromExceptionBody = CheckRegionAndUpdateCache(requestedBucketUri, s3Exception.Region);
}
if (regionFromExceptionBody == null)
{
var innerException = s3Exception.InnerException as HttpErrorResponseException;
if (innerException != null && innerException.Response != null && innerException.Response.IsHeaderPresent(HeaderKeys.XAmzBucketRegion))
{
regionFromExceptionHeader = CheckRegionAndUpdateCache(requestedBucketUri, innerException.Response.GetHeaderValue(HeaderKeys.XAmzBucketRegion));
}
}
}
return regionFromExceptionBody ?? regionFromExceptionHeader;
}