/// <summary>
        /// Gets the container's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The container's attributes.</returns>
        public static BlobContainerProperties GetProperties(HttpWebResponse response)
        {
            CommonUtility.AssertNotNull("response", response);

            // Set the container properties
            BlobContainerProperties containerProperties = new BlobContainerProperties();

            containerProperties.ETag = HttpResponseParsers.GetETag(response);

#if WINDOWS_PHONE
            containerProperties.LastModified = HttpResponseParsers.GetLastModified(response);
#else
            containerProperties.LastModified = response.LastModified.ToUniversalTime();
#endif

            // Get lease properties
            containerProperties.LeaseStatus   = BlobHttpResponseParsers.GetLeaseStatus(response);
            containerProperties.LeaseState    = BlobHttpResponseParsers.GetLeaseState(response);
            containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response);

            // Reading public access
            containerProperties.PublicAccess = GetAcl(response);

            // WORM policies
            string hasImmutability = response.Headers[Constants.HeaderConstants.HasImmutabilityPolicyHeader];
            containerProperties.HasImmutabilityPolicy = string.IsNullOrEmpty(hasImmutability) ? (bool?)null : bool.Parse(hasImmutability);

            string hasLegalHold = response.Headers[Constants.HeaderConstants.HasLegalHoldHeader];
            containerProperties.HasLegalHold = string.IsNullOrEmpty(hasLegalHold) ? (bool?)null : bool.Parse(hasLegalHold);

            return(containerProperties);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the container's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The container's attributes.</returns>
        public static BlobContainerProperties GetProperties(HttpResponseMessage response)
        {
            // Set the container properties
            BlobContainerProperties containerProperties = new BlobContainerProperties();

            containerProperties.ETag = (response.Headers.ETag == null) ? null :
                                       response.Headers.ETag.ToString();

            if (response.Content != null)
            {
                containerProperties.LastModified = response.Content.Headers.LastModified;
            }
            else
            {
                containerProperties.LastModified = null;
            }

            // Get lease properties
            containerProperties.LeaseStatus   = BlobHttpResponseParsers.GetLeaseStatus(response);
            containerProperties.LeaseState    = BlobHttpResponseParsers.GetLeaseState(response);
            containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response);

            // Reading public access
            containerProperties.PublicAccess = GetAcl(response);
            return(containerProperties);
        }
        /// <summary>
        /// Validates a lease ID header in a response.
        /// </summary>
        /// <param name="response">The response.</param>
        public static void LeaseIdHeader(HttpResponseMessage response)
        {
            string leaseId = BlobHttpResponseParsers.GetLeaseId(response);

            Assert.IsNotNull(leaseId);
            Assert.IsTrue(BlobTests.LeaseIdValidator(AccessCondition.GenerateLeaseCondition(leaseId)));
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the container's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The container's attributes.</returns>
        public static BlobContainerProperties GetProperties(HttpResponseMessage response)
        {
            CommonUtility.AssertNotNull("response", response);

            // Set the container properties
            BlobContainerProperties containerProperties = new BlobContainerProperties();

            containerProperties.ETag = (response.Headers.ETag == null) ? null :
                                       response.Headers.ETag.ToString();

            containerProperties.LastModified = response?.Content?.Headers?.LastModified;

            // Get lease properties
            containerProperties.LeaseStatus   = BlobHttpResponseParsers.GetLeaseStatus(response);
            containerProperties.LeaseState    = BlobHttpResponseParsers.GetLeaseState(response);
            containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response);

            // Reading public access
            containerProperties.PublicAccess = GetAcl(response);

            // WORM policies
            string hasImmutability = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.HasImmutabilityPolicyHeader);

            containerProperties.HasImmutabilityPolicy = string.IsNullOrEmpty(hasImmutability) ? (bool?)null : bool.Parse(hasImmutability);

            string hasLegalHold = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.HasLegalHoldHeader);

            containerProperties.HasLegalHold = string.IsNullOrEmpty(hasLegalHold) ? (bool?)null : bool.Parse(hasLegalHold);

            return(containerProperties);
        }
        /// <summary>
        /// Validates a lease time header in a response.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="expectedValue">The expected value.</param>
        /// <param name="errorMargin">The margin of error in the value.</param>
        public static void LeaseTimeHeader(HttpResponseMessage response, int?expectedValue, int?errorMargin)
        {
            int?leaseTime = BlobHttpResponseParsers.GetRemainingLeaseTime(response);

            Assert.IsFalse((expectedValue != null) && (leaseTime == null));
            if (leaseTime != null)
            {
                int error = Math.Abs(expectedValue.Value - leaseTime.Value);
                Assert.IsTrue(error < errorMargin, "Lease Time header is not within expected range.");
            }
        }
        /// <summary>
        /// Validates a lease ID header in a response.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="expectedValue">The expected value.</param>
        public static void LeaseIdHeader(HttpResponseMessage response, string expectedValue)
        {
            string leaseId = BlobHttpResponseParsers.GetLeaseId(response);

            Assert.IsFalse((expectedValue != null) && (leaseId == null));
            if (leaseId != null)
            {
                LeaseIdHeader(response);
                Assert.AreEqual(expectedValue, leaseId);
            }
        }
        /// <summary>
        /// Scenario test for acquiring a lease.
        /// </summary>
        /// <param name="containerName">The name of the container.</param>
        /// <param name="blobName">The name of the blob, if any.</param>
        /// <param name="leaseDuration">The lease duration.</param>
        /// <param name="proposedLeaseId">The proposed lease ID.</param>
        /// <param name="expectedError">The error status code to expect.</param>
        /// <returns>The lease ID.</returns>
        public async Task <string> AcquireLeaseScenarioTest(string containerName, string blobName, int leaseDuration, string proposedLeaseId, HttpStatusCode?expectedError)
        {
            // Create and validate the web request
            HttpRequestMessage request = BlobTests.AcquireLeaseRequest(BlobContext, containerName, blobName, leaseDuration, proposedLeaseId, null);

            using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext))
            {
                BlobTests.AcquireLeaseResponse(response, proposedLeaseId, expectedError);

                return(BlobHttpResponseParsers.GetLeaseId(response));
            }
        }
        /// <summary>
        /// Scenario test for changing a lease.
        /// </summary>
        /// <param name="containerName">The name of the container.</param>
        /// <param name="blobName">The name of the blob, if any.</param>
        /// <param name="leaseId">The lease ID.</param>
        /// <param name="proposedLeaseId">The proposed lease ID.</param>
        /// <param name="expectedError">The error status code to expect.</param>
        /// <returns>The lease ID.</returns>
        public async Task <string> ChangeLeaseScenarioTest(string containerName, string blobName, string leaseId, string proposedLeaseId, HttpStatusCode?expectedError)
        {
            // Create and validate the web request
            HttpRequestMessage request = BlobTests.ChangeLeaseRequest(BlobContext, containerName, blobName, proposedLeaseId, AccessCondition.GenerateLeaseCondition(leaseId));

            using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext))
            {
                BlobTests.ChangeLeaseResponse(response, proposedLeaseId, expectedError);

                return(BlobHttpResponseParsers.GetLeaseId(response));
            }
        }
        /// <summary>
        /// Scenario test for breaking a lease.
        /// </summary>
        /// <param name="containerName">The name of the container.</param>
        /// <param name="blobName">The name of the blob, if any.</param>
        /// <param name="breakPeriod">The break period.</param>
        /// <param name="expectedRemainingTime">The expected remaining time.</param>
        /// <param name="expectedError">The error status code to expect.</param>
        /// <returns>The remaining lease time.</returns>
        public async Task <int> BreakLeaseScenarioTest(string containerName, string blobName, int?breakPeriod, int?expectedRemainingTime, HttpStatusCode?expectedError)
        {
            // Create and validate the web request
            HttpRequestMessage request = BlobTests.BreakLeaseRequest(BlobContext, containerName, blobName, breakPeriod, null);

            using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext))
            {
                int expectedTime = expectedRemainingTime ?? breakPeriod.Value;
                int errorMargin  = 10;
                BlobTests.BreakLeaseResponse(response, expectedTime, errorMargin, expectedError);

                return(expectedError.HasValue ? 0 : BlobHttpResponseParsers.GetRemainingLeaseTime(response).Value);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Gets the container's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The container's attributes.</returns>
        public static BlobContainerProperties GetProperties(HttpWebResponse response)
        {
            // Set the container properties
            BlobContainerProperties containerProperties = new BlobContainerProperties();

            containerProperties.ETag         = HttpResponseParsers.GetETag(response);
            containerProperties.LastModified = response.LastModified.ToUniversalTime();

            // Get lease properties
            containerProperties.LeaseStatus   = BlobHttpResponseParsers.GetLeaseStatus(response);
            containerProperties.LeaseState    = BlobHttpResponseParsers.GetLeaseState(response);
            containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response);

            return(containerProperties);
        }
Esempio n. 11
0
        /// <summary>
        /// Scenario test for changing a lease.
        /// </summary>
        /// <param name="containerName">The name of the container.</param>
        /// <param name="blobName">The name of the blob, if any.</param>
        /// <param name="leaseId">The lease ID.</param>
        /// <param name="proposedLeaseId">The proposed lease ID.</param>
        /// <param name="expectedError">The error status code to expect.</param>
        /// <returns>The lease ID.</returns>
        public string ChangeLeaseScenarioTest(string containerName, string blobName, string leaseId, string proposedLeaseId, HttpStatusCode?expectedError)
        {
            // Create and validate the web request
            HttpWebRequest request = BlobTests.ChangeLeaseRequest(BlobContext, containerName, blobName, proposedLeaseId, AccessCondition.GenerateLeaseCondition(leaseId));

            if (BlobContext.Credentials != null)
            {
                BlobTests.SignRequest(request, BlobContext);
            }

            using (HttpWebResponse response = BlobTestUtils.GetResponse(request, BlobContext))
            {
                BlobTests.ChangeLeaseResponse(response, proposedLeaseId, expectedError);

                return(BlobHttpResponseParsers.GetLeaseId(response));
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Scenario test for breaking a lease.
        /// </summary>
        /// <param name="containerName">The name of the container.</param>
        /// <param name="blobName">The name of the blob, if any.</param>
        /// <param name="breakPeriod">The break period.</param>
        /// <param name="expectedRemainingTime">The expected remaining time.</param>
        /// <param name="expectedError">The error status code to expect.</param>
        /// <returns>The remaining lease time.</returns>
        public int BreakLeaseScenarioTest(string containerName, string blobName, int?breakPeriod, int?expectedRemainingTime, HttpStatusCode?expectedError)
        {
            // Create and validate the web request
            HttpWebRequest request = BlobTests.BreakLeaseRequest(BlobContext, containerName, blobName, breakPeriod, null);

            if (BlobContext.Credentials != null)
            {
                BlobTests.SignRequest(request, BlobContext);
            }

            using (HttpWebResponse response = BlobTestUtils.GetResponse(request, BlobContext))
            {
                int expectedTime = expectedRemainingTime ?? breakPeriod.Value;
                int errorMargin  = 10;
                BlobTests.BreakLeaseResponse(response, expectedTime, errorMargin, expectedError);

                return(expectedError.HasValue ? 0 : BlobHttpResponseParsers.GetRemainingLeaseTime(response).Value);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Gets the container's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The container's attributes.</returns>
        public static BlobContainerProperties GetProperties(HttpWebResponse response)
        {
            CommonUtility.AssertNotNull("response", response);

            // Set the container properties
            BlobContainerProperties containerProperties = new BlobContainerProperties();

            containerProperties.ETag = HttpResponseParsers.GetETag(response);

#if WINDOWS_PHONE
            containerProperties.LastModified = HttpResponseParsers.GetLastModified(response);
#else
            containerProperties.LastModified = response.LastModified.ToUniversalTime();
#endif

            // Get lease properties
            containerProperties.LeaseStatus   = BlobHttpResponseParsers.GetLeaseStatus(response);
            containerProperties.LeaseState    = BlobHttpResponseParsers.GetLeaseState(response);
            containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response);

            return(containerProperties);
        }
        /// <summary>
        /// Gets the blob's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The blob's properties.</returns>
        public static BlobProperties GetProperties(HttpWebResponse response)
        {
            CommonUtility.AssertNotNull("response", response);

            BlobProperties properties = new BlobProperties();

            properties.ETag = HttpResponseParsers.GetETag(response);

#if WINDOWS_PHONE
            properties.Created         = HttpResponseParsers.GetCreated(response);
            properties.LastModified    = HttpResponseParsers.GetLastModified(response);
            properties.ContentLanguage = response.Headers[Constants.HeaderConstants.ContentLanguageHeader];
#else
            string created = response.Headers[Constants.HeaderConstants.CreationTimeHeader];
            properties.Created = string.IsNullOrEmpty(created) ? (DateTimeOffset?)null : DateTimeOffset.Parse(created).ToUniversalTime();

            properties.LastModified    = response.LastModified.ToUniversalTime();
            properties.ContentLanguage = response.Headers[HttpResponseHeader.ContentLanguage];
#endif

            properties.ContentDisposition = response.Headers[Constants.HeaderConstants.ContentDispositionResponseHeader];
            properties.ContentEncoding    = response.Headers[HttpResponseHeader.ContentEncoding];

            // For range gets, only look at 'x-ms-blob-content-md5' for overall MD5
            if (response.Headers[HttpResponseHeader.ContentRange] != null)
            {
                properties.ContentMD5 = response.Headers[Constants.HeaderConstants.BlobContentMD5Header];
            }
            else
            {
                properties.ContentMD5 = response.Headers[HttpResponseHeader.ContentMd5];
            }

            properties.ContentType  = response.Headers[HttpResponseHeader.ContentType];
            properties.CacheControl = response.Headers[HttpResponseHeader.CacheControl];

            string blobEncryption = response.Headers[Constants.HeaderConstants.ServerEncrypted];
            properties.IsServerEncrypted = string.Equals(blobEncryption, Constants.HeaderConstants.TrueHeader, StringComparison.OrdinalIgnoreCase);

            string incrementalCopy = response.Headers[Constants.HeaderConstants.IncrementalCopyHeader];
            properties.IsIncrementalCopy = string.Equals(incrementalCopy, Constants.HeaderConstants.TrueHeader, StringComparison.OrdinalIgnoreCase);

            // Get blob type
            string blobType = response.Headers[Constants.HeaderConstants.BlobType];
            if (!string.IsNullOrEmpty(blobType))
            {
                properties.BlobType = (BlobType)Enum.Parse(typeof(BlobType), blobType, true);
            }

            // Get lease properties
            properties.LeaseStatus   = GetLeaseStatus(response);
            properties.LeaseState    = GetLeaseState(response);
            properties.LeaseDuration = GetLeaseDuration(response);

            // Get the content length. Prioritize range and x-ms over content length for the special cases.
            string rangeHeader             = response.Headers[HttpResponseHeader.ContentRange];
            string contentLengthHeader     = response.Headers[Constants.HeaderConstants.ContentLengthHeader];
            string blobContentLengthHeader = response.Headers[Constants.HeaderConstants.BlobContentLengthHeader];
            if (!string.IsNullOrEmpty(rangeHeader))
            {
                properties.Length = long.Parse(rangeHeader.Split('/')[1], CultureInfo.InvariantCulture);
            }
            else if (!string.IsNullOrEmpty(blobContentLengthHeader))
            {
                properties.Length = long.Parse(blobContentLengthHeader, CultureInfo.InvariantCulture);
            }
            else if (!string.IsNullOrEmpty(contentLengthHeader))
            {
                // On Windows Phone, ContentLength property is not always same as Content-Length header,
                // so we try to parse the header first.
                properties.Length = long.Parse(contentLengthHeader, CultureInfo.InvariantCulture);
            }
            else
            {
                properties.Length = response.ContentLength;
            }

            // Get sequence number
            string sequenceNumber = response.Headers[Constants.HeaderConstants.BlobSequenceNumber];
            if (!string.IsNullOrEmpty(sequenceNumber))
            {
                properties.PageBlobSequenceNumber = long.Parse(sequenceNumber, CultureInfo.InvariantCulture);
            }

            // Get committed block count
            string comittedBlockCount = response.Headers[Constants.HeaderConstants.BlobCommittedBlockCount];
            if (!string.IsNullOrEmpty(comittedBlockCount))
            {
                properties.AppendBlobCommittedBlockCount = int.Parse(comittedBlockCount, CultureInfo.InvariantCulture);
            }

            // Get the tier of the blob
            string premiumPageBlobTierInferredString = response.Headers[Constants.HeaderConstants.AccessTierInferredHeader];
            if (!string.IsNullOrEmpty(premiumPageBlobTierInferredString))
            {
                properties.BlobTierInferred = Convert.ToBoolean(premiumPageBlobTierInferredString);
            }

            string blobTierString = response.Headers[Constants.HeaderConstants.AccessTierHeader];

            StandardBlobTier?   standardBlobTier;
            PremiumPageBlobTier?premiumPageBlobTier;
            BlobHttpResponseParsers.GetBlobTier(properties.BlobType, blobTierString, out standardBlobTier, out premiumPageBlobTier);
            properties.StandardBlobTier    = standardBlobTier;
            properties.PremiumPageBlobTier = premiumPageBlobTier;

            if ((properties.PremiumPageBlobTier.HasValue || properties.StandardBlobTier.HasValue) && !properties.BlobTierInferred.HasValue)
            {
                properties.BlobTierInferred = false;
            }

            // Get the rehydration status
            string rehydrationStatusString = response.Headers[Constants.HeaderConstants.ArchiveStatusHeader];
            properties.RehydrationStatus = BlobHttpResponseParsers.GetRehydrationStatus(rehydrationStatusString);

            // Get the time the tier of the blob was last modified
            string accessTierChangeTimeString = response.Headers[Constants.HeaderConstants.AccessTierChangeTimeHeader];
            if (!string.IsNullOrEmpty(accessTierChangeTimeString))
            {
                properties.BlobTierLastModifiedTime = DateTimeOffset.Parse(accessTierChangeTimeString, CultureInfo.InvariantCulture);
            }

            return(properties);
        }
        private IListBlobEntry ParseBlobEntry(Uri baseUri)
        {
            BlobAttributes blob = new BlobAttributes();
            string         name = null;

            // copy blob attribute strings
            string copyId                = null;
            string copyStatus            = null;
            string copyCompletionTime    = null;
            string copyProgress          = null;
            string copySource            = null;
            string copyStatusDescription = null;

            this.reader.ReadStartElement();
            while (this.reader.IsStartElement())
            {
                if (this.reader.IsEmptyElement)
                {
                    this.reader.Skip();
                }
                else
                {
                    switch (this.reader.Name)
                    {
                    case Constants.NameElement:
                        name = reader.ReadElementContentAsString();
                        break;

                    case Constants.SnapshotElement:
                        blob.SnapshotTime = reader.ReadElementContentAsString().ToUTCTime();
                        break;

                    case Constants.PropertiesElement:
                        this.reader.ReadStartElement();
                        while (this.reader.IsStartElement())
                        {
                            if (this.reader.IsEmptyElement)
                            {
                                this.reader.Skip();
                            }
                            else
                            {
                                switch (this.reader.Name)
                                {
                                case Constants.LastModifiedElement:
                                    blob.Properties.LastModified = reader.ReadElementContentAsString().ToUTCTime();
                                    break;

                                case Constants.EtagElement:
                                    blob.Properties.ETag = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", reader.ReadElementContentAsString());
                                    break;

                                case Constants.ContentLengthElement:
                                    blob.Properties.Length = reader.ReadElementContentAsLong();
                                    break;

                                case Constants.CacheControlElement:
                                    blob.Properties.CacheControl = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentTypeElement:
                                    blob.Properties.ContentType = reader.ReadElementContentAsString();
                                    break;

                                case Constants.HeaderConstants.ContentDispositionResponseHeader:
                                    blob.Properties.ContentDisposition = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentEncodingElement:
                                    blob.Properties.ContentEncoding = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentLanguageElement:
                                    blob.Properties.ContentLanguage = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentMD5Element:
                                    blob.Properties.ContentMD5 = reader.ReadElementContentAsString();
                                    break;

                                case Constants.BlobTypeElement:
                                    string blobTypeString = reader.ReadElementContentAsString();
                                    switch (blobTypeString)
                                    {
                                    case Constants.BlockBlobValue:
                                        blob.Properties.BlobType = BlobType.BlockBlob;
                                        break;

                                    case Constants.PageBlobValue:
                                        blob.Properties.BlobType = BlobType.PageBlob;
                                        break;
                                    }

                                    break;

                                case Constants.LeaseStatusElement:
                                    blob.Properties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseStateElement:
                                    blob.Properties.LeaseState = BlobHttpResponseParsers.GetLeaseState(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseDurationElement:
                                    blob.Properties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(reader.ReadElementContentAsString());
                                    break;

                                case Constants.CopyIdElement:
                                    copyId = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyCompletionTimeElement:
                                    copyCompletionTime = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyStatusElement:
                                    copyStatus = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyProgressElement:
                                    copyProgress = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopySourceElement:
                                    copySource = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyStatusDescriptionElement:
                                    copyStatusDescription = reader.ReadElementContentAsString();
                                    break;

                                default:
                                    reader.Skip();
                                    break;
                                }
                            }
                        }

                        this.reader.ReadEndElement();
                        break;

                    case Constants.MetadataElement:
                        blob.Metadata = Response.ParseMetadata(this.reader);
                        break;

                    default:
                        this.reader.Skip();
                        break;
                    }
                }
            }

            this.reader.ReadEndElement();

            Uri uri = NavigationHelper.AppendPathToSingleUri(baseUri, name);

            if (blob.SnapshotTime.HasValue)
            {
                UriQueryBuilder builder = new UriQueryBuilder();
                builder.Add("snapshot", BlobRequest.ConvertDateTimeToSnapshotString(blob.SnapshotTime.Value));
                uri = builder.AddToUri(uri);
            }

            blob.StorageUri = new StorageUri(uri);

            if (!string.IsNullOrEmpty(copyStatus))
            {
                blob.CopyState = BlobHttpResponseParsers.GetCopyAttributes(
                    copyStatus,
                    copyId,
                    copySource,
                    copyProgress,
                    copyCompletionTime,
                    copyStatusDescription);
            }

            return(new ListBlobEntry(name, blob));
        }
Esempio n. 16
0
        /// <summary>
        /// Gets the blob's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The blob's properties.</returns>
        public static BlobProperties GetProperties(HttpResponseMessage response)
        {
            CommonUtility.AssertNotNull("response", response);

            BlobProperties properties = new BlobProperties();

            if (response.Content != null)
            {
                properties.LastModified = response.Content.Headers.LastModified;
                HttpContentHeaders contentHeaders = response.Content.Headers;
                properties.ContentEncoding    = HttpWebUtility.GetHeaderValues("Content-Encoding", contentHeaders);
                properties.ContentLanguage    = HttpWebUtility.GetHeaderValues("Content-Language", contentHeaders);
                properties.ContentDisposition = HttpWebUtility.GetHeaderValues("Content-Disposition", contentHeaders);
                properties.ContentType        = HttpWebUtility.GetHeaderValues("Content-Type", contentHeaders);


                if (response.Content.Headers.ContentMD5 != null && response.Content.Headers.ContentRange == null)
                {
                    properties.ContentMD5 = HttpResponseParsers.GetContentMD5(response);
                }
                else if (!string.IsNullOrEmpty(response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobContentMD5Header)))
                {
                    properties.ContentMD5 = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobContentMD5Header);
                }

                string created = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.CreationTimeHeader);
                properties.Created = string.IsNullOrEmpty(created) ? (DateTimeOffset?)null : DateTimeOffset.Parse(created, CultureInfo.InvariantCulture).ToUniversalTime();

                string blobEncryption = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.ServerEncrypted);
                properties.IsServerEncrypted = string.Equals(blobEncryption, Constants.HeaderConstants.TrueHeader, StringComparison.OrdinalIgnoreCase);

                string incrementalCopy = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.IncrementalCopyHeader);
                properties.IsIncrementalCopy = string.Equals(incrementalCopy, Constants.HeaderConstants.TrueHeader, StringComparison.OrdinalIgnoreCase);

                // Get the content length. Prioritize range and x-ms over content length for the special cases.
                string contentLengthHeader = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobContentLengthHeader);
                if ((response.Content.Headers.ContentRange != null) &&
                    response.Content.Headers.ContentRange.HasLength)
                {
                    properties.Length = response.Content.Headers.ContentRange.Length.Value;
                }
                else if (!string.IsNullOrEmpty(contentLengthHeader))
                {
                    properties.Length = long.Parse(contentLengthHeader);
                }
                else if (response.Content.Headers.ContentLength.HasValue)
                {
                    properties.Length = response.Content.Headers.ContentLength.Value;
                }
            }
            properties.CacheControl = HttpWebUtility.GetHeaderValues("Cache-Control", response.Headers);

            if (response.Headers.ETag != null)
            {
                properties.ETag = response.Headers.ETag.ToString();
            }

            // Get blob type
            string blobType = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobType);

            if (!string.IsNullOrEmpty(blobType))
            {
                properties.BlobType = (BlobType)Enum.Parse(typeof(BlobType), blobType, true);
            }

            // Get lease properties
            properties.LeaseStatus   = GetLeaseStatus(response);
            properties.LeaseState    = GetLeaseState(response);
            properties.LeaseDuration = GetLeaseDuration(response);

            // Get sequence number
            string sequenceNumber = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobSequenceNumber);

            if (!string.IsNullOrEmpty(sequenceNumber))
            {
                properties.PageBlobSequenceNumber = long.Parse(sequenceNumber, CultureInfo.InvariantCulture);
            }

            // Get committed block count
            string comittedBlockCount = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobCommittedBlockCount);

            if (!string.IsNullOrEmpty(comittedBlockCount))
            {
                properties.AppendBlobCommittedBlockCount = int.Parse(comittedBlockCount, CultureInfo.InvariantCulture);
            }

            // Get the tier of the blob
            string premiumBlobTierInferredString = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.AccessTierInferredHeader);

            if (!string.IsNullOrEmpty(premiumBlobTierInferredString))
            {
                properties.BlobTierInferred = Convert.ToBoolean(premiumBlobTierInferredString);
            }

            string              blobTierString = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.AccessTierHeader);
            StandardBlobTier?   standardBlobTier;
            PremiumPageBlobTier?premiumPageBlobTier;

            BlobHttpResponseParsers.GetBlobTier(properties.BlobType, blobTierString, out standardBlobTier, out premiumPageBlobTier);
            properties.StandardBlobTier    = standardBlobTier;
            properties.PremiumPageBlobTier = premiumPageBlobTier;

            // Get the rehydration status
            string rehydrationStatusString = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.ArchiveStatusHeader);

            if (!string.IsNullOrEmpty(rehydrationStatusString))
            {
                if (Constants.RehydratePendingToHot.Equals(rehydrationStatusString))
                {
                    properties.RehydrationStatus = RehydrationStatus.PendingToHot;
                }
                else if (Constants.RehydratePendingToCool.Equals(rehydrationStatusString))
                {
                    properties.RehydrationStatus = RehydrationStatus.PendingToCool;
                }
                else
                {
                    properties.RehydrationStatus = RehydrationStatus.Unknown;
                }
            }

            if ((properties.PremiumPageBlobTier.HasValue || properties.StandardBlobTier.HasValue) && !properties.BlobTierInferred.HasValue)
            {
                properties.BlobTierInferred = false;
            }

            // Get the time the tier of the blob was last modified
            string accessTierChangeTimeString = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.AccessTierChangeTimeHeader);

            if (!string.IsNullOrEmpty(accessTierChangeTimeString))
            {
                properties.BlobTierLastModifiedTime = DateTimeOffset.Parse(accessTierChangeTimeString, CultureInfo.InvariantCulture);
            }

            return(properties);
        }
        private static async Task <IListBlobEntry> ParseBlobEntryAsync(XmlReader reader, Uri baseUri, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            BlobAttributes blob = new BlobAttributes();
            string         name = null;

            // copy blob attribute strings
            string copyId                      = null;
            string copyStatus                  = null;
            string copyCompletionTime          = null;
            string copyProgress                = null;
            string copySource                  = null;
            string copyStatusDescription       = null;
            string copyDestinationSnapshotTime = null;

            string         blobTierString           = null;
            bool?          blobTierInferred         = null;
            string         rehydrationStatusString  = null;
            DateTimeOffset?blobTierLastModifiedTime = null;

            await reader.ReadStartElementAsync().ConfigureAwait(false);

            while (await reader.IsStartElementAsync().ConfigureAwait(false))
            {
                token.ThrowIfCancellationRequested();

                if (reader.IsEmptyElement)
                {
                    await reader.SkipAsync().ConfigureAwait(false);
                }
                else
                {
                    switch (reader.Name)
                    {
                    case Constants.NameElement:
                        name = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                        break;

                    case Constants.SnapshotElement:
                        blob.SnapshotTime = (await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)).ToUTCTime();
                        break;

                    case Constants.DeletedElement:
                        blob.IsDeleted = BlobHttpResponseParsers.GetDeletionStatus(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                        break;

                    case Constants.PropertiesElement:
                        await reader.ReadStartElementAsync().ConfigureAwait(false);

                        while (await reader.IsStartElementAsync().ConfigureAwait(false))
                        {
                            token.ThrowIfCancellationRequested();

                            if (reader.IsEmptyElement)
                            {
                                await reader.SkipAsync().ConfigureAwait(false);
                            }
                            else
                            {
                                switch (reader.Name)
                                {
                                case Constants.CreationTimeElement:
                                    blob.Properties.Created = (await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)).ToUTCTime();
                                    break;

                                case Constants.LastModifiedElement:
                                    blob.Properties.LastModified = (await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)).ToUTCTime();
                                    break;

                                case Constants.EtagElement:
                                    blob.Properties.ETag = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.ContentLengthElement:
                                    blob.Properties.Length = await reader.ReadElementContentAsInt64Async().ConfigureAwait(false);

                                    break;

                                case Constants.CacheControlElement:
                                    blob.Properties.CacheControl = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.ContentTypeElement:
                                    blob.Properties.ContentType = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.HeaderConstants.ContentDispositionResponseHeader:
                                    blob.Properties.ContentDisposition = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.ContentEncodingElement:
                                    blob.Properties.ContentEncoding = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.ContentLanguageElement:
                                    blob.Properties.ContentLanguage = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.ContentMD5Element:
                                    blob.Properties.ContentMD5 = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.BlobTypeElement:
                                    string blobTypeString = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    switch (blobTypeString)
                                    {
                                    case Constants.BlockBlobValue:
                                        blob.Properties.BlobType = BlobType.BlockBlob;
                                        break;

                                    case Constants.PageBlobValue:
                                        blob.Properties.BlobType = BlobType.PageBlob;
                                        break;

                                    case Constants.AppendBlobValue:
                                        blob.Properties.BlobType = BlobType.AppendBlob;
                                        break;
                                    }

                                    break;

                                case Constants.LeaseStatusElement:
                                    blob.Properties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.LeaseStateElement:
                                    blob.Properties.LeaseState = BlobHttpResponseParsers.GetLeaseState(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.LeaseDurationElement:
                                    blob.Properties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.CopyIdElement:
                                    copyId = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.CopyCompletionTimeElement:
                                    copyCompletionTime = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.CopyStatusElement:
                                    copyStatus = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.CopyProgressElement:
                                    copyProgress = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.CopySourceElement:
                                    copySource = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.CopyStatusDescriptionElement:
                                    copyStatusDescription = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.ServerEncryptionElement:
                                    blob.Properties.IsServerEncrypted = BlobHttpResponseParsers.GetServerEncrypted(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.IncrementalCopy:
                                    blob.Properties.IsIncrementalCopy = BlobHttpResponseParsers.GetIncrementalCopyStatus(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.CopyDestinationSnapshotElement:
                                    copyDestinationSnapshotTime = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.AccessTierElement:
                                    blobTierString = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.ArchiveStatusElement:
                                    rehydrationStatusString = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.AccessTierInferred:
                                    blobTierInferred = await reader.ReadElementContentAsBooleanAsync().ConfigureAwait(false);

                                    break;

                                case Constants.AccessTierChangeTimeElement:
                                    string t = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    blobTierLastModifiedTime = DateTimeOffset.Parse(t, CultureInfo.InvariantCulture);
                                    break;

                                case Constants.DeletedTimeElement:
                                    blob.Properties.DeletedTime = (await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)).ToUTCTime();
                                    break;

                                case Constants.RemainingRetentionDaysElement:
                                    blob.Properties.RemainingDaysBeforePermanentDelete = int.Parse(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                default:
                                    await reader.SkipAsync().ConfigureAwait(false);

                                    break;
                                }
                            }
                        }

                        await reader.ReadEndElementAsync().ConfigureAwait(false);

                        break;

                    case Constants.MetadataElement:
                        blob.Metadata = await Response.ParseMetadataAsync(reader).ConfigureAwait(false);

                        break;

                    default:
                        await reader.SkipAsync().ConfigureAwait(false);

                        break;
                    }
                }
            }

            await reader.ReadEndElementAsync().ConfigureAwait(false);

            Uri uri = NavigationHelper.AppendPathToSingleUri(baseUri, name);

            if (blob.SnapshotTime.HasValue)
            {
                UriQueryBuilder builder = new UriQueryBuilder();
                builder.Add("snapshot", Request.ConvertDateTimeToSnapshotString(blob.SnapshotTime.Value));
                uri = builder.AddToUri(uri);
            }

            blob.StorageUri = new StorageUri(uri);

            if (!string.IsNullOrEmpty(copyStatus))
            {
                blob.CopyState = BlobHttpResponseParsers.GetCopyAttributes(
                    copyStatus,
                    copyId,
                    copySource,
                    copyProgress,
                    copyCompletionTime,
                    copyStatusDescription,
                    copyDestinationSnapshotTime);
            }

            if (!string.IsNullOrEmpty(blobTierString))
            {
                StandardBlobTier?   standardBlobTier;
                PremiumPageBlobTier?premiumPageBlobTier;
                BlobHttpResponseParsers.GetBlobTier(blob.Properties.BlobType, blobTierString, out standardBlobTier, out premiumPageBlobTier);
                blob.Properties.StandardBlobTier    = standardBlobTier;
                blob.Properties.PremiumPageBlobTier = premiumPageBlobTier;
            }

            blob.Properties.RehydrationStatus        = BlobHttpResponseParsers.GetRehydrationStatus(rehydrationStatusString);
            blob.Properties.BlobTierLastModifiedTime = blobTierLastModifiedTime;
            blob.Properties.BlobTierInferred         = blobTierInferred;

            return(new ListBlobEntry(name, blob));
        }
Esempio n. 18
0
        /// <summary>
        /// Reads a container entry completely including its properties and metadata.
        /// </summary>
        /// <returns>Container listing entry</returns>
        private static async Task <BlobContainerEntry> ParseContainerEntryAsync(XmlReader reader, Uri baseUri, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            string name = null;
            IDictionary <string, string> metadata            = null;
            BlobContainerProperties      containerProperties = new BlobContainerProperties();

            containerProperties.PublicAccess = BlobContainerPublicAccessType.Off;

            await reader.ReadStartElementAsync().ConfigureAwait(false);

            while (await reader.IsStartElementAsync().ConfigureAwait(false))
            {
                token.ThrowIfCancellationRequested();

                if (reader.IsEmptyElement)
                {
                    await reader.SkipAsync().ConfigureAwait(false);
                }
                else
                {
                    switch (reader.Name)
                    {
                    case Constants.NameElement:
                        name = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                        break;

                    case Constants.PropertiesElement:
                        await reader.ReadStartElementAsync().ConfigureAwait(false);

                        while (await reader.IsStartElementAsync().ConfigureAwait(false))
                        {
                            token.ThrowIfCancellationRequested();

                            if (reader.IsEmptyElement)
                            {
                                await reader.SkipAsync().ConfigureAwait(false);
                            }
                            else
                            {
                                switch (reader.Name)
                                {
                                case Constants.LastModifiedElement:
                                    containerProperties.LastModified = (await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)).ToUTCTime();
                                    break;

                                case Constants.EtagElement:
                                    containerProperties.ETag = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.HasImmutabilityPolicyElement:
                                    containerProperties.HasImmutabilityPolicy = await reader.ReadElementContentAsBooleanAsync().ConfigureAwait(false);

                                    break;

                                case Constants.HasLegalHoldElement:
                                    containerProperties.HasLegalHold = await reader.ReadElementContentAsBooleanAsync().ConfigureAwait(false);

                                    break;

                                case Constants.LeaseStatusElement:
                                    containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.LeaseStateElement:
                                    containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.LeaseDurationElement:
                                    containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                case Constants.PublicAccessElement:
                                    containerProperties.PublicAccess = ContainerHttpResponseParsers.GetContainerAcl(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false));
                                    break;

                                default:
                                    await reader.SkipAsync().ConfigureAwait(false);

                                    break;
                                }
                            }
                        }

                        await reader.ReadEndElementAsync().ConfigureAwait(false);

                        break;

                    case Constants.MetadataElement:
                        metadata = await Response.ParseMetadataAsync(reader).ConfigureAwait(false);

                        break;

                    default:
                        await reader.SkipAsync().ConfigureAwait(false);

                        break;
                    }
                }
            }

            await reader.ReadEndElementAsync().ConfigureAwait(false);

            if (metadata == null)
            {
                metadata = new Dictionary <string, string>();
            }

            return(new BlobContainerEntry
            {
                Properties = containerProperties,
                Name = name,
                Uri = NavigationHelper.AppendPathToSingleUri(baseUri, name),
                Metadata = metadata,
            });
        }
        /// <summary>
        /// Reads a container entry completely including its properties and metadata.
        /// </summary>
        /// <returns>Container listing entry</returns>
        private BlobContainerEntry ParseContainerEntry(Uri baseUri)
        {
            string name = null;
            IDictionary <string, string> metadata            = null;
            BlobContainerProperties      containerProperties = new BlobContainerProperties();

            containerProperties.PublicAccess = BlobContainerPublicAccessType.Off;

            this.reader.ReadStartElement();
            while (this.reader.IsStartElement())
            {
                if (this.reader.IsEmptyElement)
                {
                    this.reader.Skip();
                }
                else
                {
                    switch (this.reader.Name)
                    {
                    case Constants.NameElement:
                        name = this.reader.ReadElementContentAsString();
                        break;

                    case Constants.PropertiesElement:
                        this.reader.ReadStartElement();
                        while (this.reader.IsStartElement())
                        {
                            if (this.reader.IsEmptyElement)
                            {
                                this.reader.Skip();
                            }
                            else
                            {
                                switch (this.reader.Name)
                                {
                                case Constants.LastModifiedElement:
                                    containerProperties.LastModified = reader.ReadElementContentAsString().ToUTCTime();
                                    break;

                                case Constants.EtagElement:
                                    containerProperties.ETag = reader.ReadElementContentAsString();
                                    break;

                                case Constants.HasImmutabilityPolicyElement:
                                    containerProperties.HasImmutabilityPolicy = reader.ReadElementContentAsBoolean();
                                    break;

                                case Constants.HasLegalHoldElement:
                                    containerProperties.HasLegalHold = reader.ReadElementContentAsBoolean();
                                    break;

                                case Constants.LeaseStatusElement:
                                    containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseStateElement:
                                    containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseDurationElement:
                                    containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(reader.ReadElementContentAsString());
                                    break;

                                case Constants.PublicAccessElement:
                                    containerProperties.PublicAccess = ContainerHttpResponseParsers.GetContainerAcl(reader.ReadElementContentAsString());
                                    break;

                                default:
                                    reader.Skip();
                                    break;
                                }
                            }
                        }

                        this.reader.ReadEndElement();
                        break;

                    case Constants.MetadataElement:
                        metadata = Response.ParseMetadata(this.reader);
                        break;

                    default:
                        reader.Skip();
                        break;
                    }
                }
            }

            this.reader.ReadEndElement();

            if (metadata == null)
            {
                metadata = new Dictionary <string, string>();
            }

            return(new BlobContainerEntry
            {
                Properties = containerProperties,
                Name = name,
                Uri = NavigationHelper.AppendPathToSingleUri(baseUri, name),
                Metadata = metadata,
            });
        }
Esempio n. 20
0
        private IListBlobEntry ParseBlobEntry(Uri baseUri)
        {
            BlobAttributes blob = new BlobAttributes();
            string         name = null;

            // copy blob attribute strings
            string copyId                      = null;
            string copyStatus                  = null;
            string copyCompletionTime          = null;
            string copyProgress                = null;
            string copySource                  = null;
            string copyStatusDescription       = null;
            string copyDestinationSnapshotTime = null;

            string         blobTierString           = null;
            bool?          blobTierInferred         = null;
            string         rehydrationStatusString  = null;
            DateTimeOffset?blobTierLastModifiedTime = null;

            this.reader.ReadStartElement();
            while (this.reader.IsStartElement())
            {
                if (this.reader.IsEmptyElement)
                {
                    this.reader.Skip();
                }
                else
                {
                    switch (this.reader.Name)
                    {
                    case Constants.NameElement:
                        name = reader.ReadElementContentAsString();
                        break;

                    case Constants.SnapshotElement:
                        blob.SnapshotTime = reader.ReadElementContentAsString().ToUTCTime();
                        break;

                    case Constants.PropertiesElement:
                        this.reader.ReadStartElement();
                        while (this.reader.IsStartElement())
                        {
                            if (this.reader.IsEmptyElement)
                            {
                                this.reader.Skip();
                            }
                            else
                            {
                                switch (this.reader.Name)
                                {
                                case Constants.LastModifiedElement:
                                    blob.Properties.LastModified = reader.ReadElementContentAsString().ToUTCTime();
                                    break;

                                case Constants.EtagElement:
                                    blob.Properties.ETag = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", reader.ReadElementContentAsString());
                                    break;

                                case Constants.ContentLengthElement:
                                    blob.Properties.Length = reader.ReadElementContentAsLong();
                                    break;

                                case Constants.CacheControlElement:
                                    blob.Properties.CacheControl = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentTypeElement:
                                    blob.Properties.ContentType = reader.ReadElementContentAsString();
                                    break;

                                case Constants.HeaderConstants.ContentDispositionResponseHeader:
                                    blob.Properties.ContentDisposition = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentEncodingElement:
                                    blob.Properties.ContentEncoding = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentLanguageElement:
                                    blob.Properties.ContentLanguage = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentMD5Element:
                                    blob.Properties.ContentMD5 = reader.ReadElementContentAsString();
                                    break;

                                case Constants.BlobTypeElement:
                                    string blobTypeString = reader.ReadElementContentAsString();
                                    switch (blobTypeString)
                                    {
                                    case Constants.BlockBlobValue:
                                        blob.Properties.BlobType = BlobType.BlockBlob;
                                        break;

                                    case Constants.PageBlobValue:
                                        blob.Properties.BlobType = BlobType.PageBlob;
                                        break;

                                    case Constants.AppendBlobValue:
                                        blob.Properties.BlobType = BlobType.AppendBlob;
                                        break;
                                    }

                                    break;

                                case Constants.LeaseStatusElement:
                                    blob.Properties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseStateElement:
                                    blob.Properties.LeaseState = BlobHttpResponseParsers.GetLeaseState(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseDurationElement:
                                    blob.Properties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(reader.ReadElementContentAsString());
                                    break;

                                case Constants.CopyIdElement:
                                    copyId = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyCompletionTimeElement:
                                    copyCompletionTime = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyStatusElement:
                                    copyStatus = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyProgressElement:
                                    copyProgress = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopySourceElement:
                                    copySource = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyStatusDescriptionElement:
                                    copyStatusDescription = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ServerEncryptionElement:
                                    blob.Properties.IsServerEncrypted = BlobHttpResponseParsers.GetServerEncrypted(reader.ReadElementContentAsString());
                                    break;

                                case Constants.IncrementalCopy:
                                    blob.Properties.IsIncrementalCopy = BlobHttpResponseParsers.GetIncrementalCopyStatus(reader.ReadElementContentAsString());
                                    break;

                                case Constants.CopyDestinationSnapshotElement:
                                    copyDestinationSnapshotTime = reader.ReadElementContentAsString();
                                    break;

                                case Constants.AccessTierElement:
                                    blobTierString = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ArchiveStatusElement:
                                    rehydrationStatusString = reader.ReadElementContentAsString();
                                    break;

                                case Constants.AccessTierInferred:
                                    blobTierInferred = reader.ReadElementContentAsBoolean();
                                    break;

                                case Constants.AccessTierChangeTimeElement:
                                    string t = reader.ReadElementContentAsString();
                                    blobTierLastModifiedTime = DateTimeOffset.Parse(t, CultureInfo.InvariantCulture);
                                    break;

                                default:
                                    reader.Skip();
                                    break;
                                }
                            }
                        }

                        this.reader.ReadEndElement();
                        break;

                    case Constants.MetadataElement:
                        blob.Metadata = Response.ParseMetadata(this.reader);
                        break;

                    default:
                        this.reader.Skip();
                        break;
                    }
                }
            }

            this.reader.ReadEndElement();

            Uri uri = NavigationHelper.AppendPathToSingleUri(baseUri, name);

            if (blob.SnapshotTime.HasValue)
            {
                UriQueryBuilder builder = new UriQueryBuilder();
                builder.Add("snapshot", Request.ConvertDateTimeToSnapshotString(blob.SnapshotTime.Value));
                uri = builder.AddToUri(uri);
            }

            blob.StorageUri = new StorageUri(uri);

            if (!string.IsNullOrEmpty(copyStatus))
            {
                blob.CopyState = BlobHttpResponseParsers.GetCopyAttributes(
                    copyStatus,
                    copyId,
                    copySource,
                    copyProgress,
                    copyCompletionTime,
                    copyStatusDescription,
                    copyDestinationSnapshotTime);
            }

            if (!string.IsNullOrEmpty(blobTierString))
            {
                StandardBlobTier?   standardBlobTier;
                PremiumPageBlobTier?premiumPageBlobTier;
                BlobHttpResponseParsers.GetBlobTier(blob.Properties.BlobType, blobTierString, out standardBlobTier, out premiumPageBlobTier);
                blob.Properties.StandardBlobTier    = standardBlobTier;
                blob.Properties.PremiumPageBlobTier = premiumPageBlobTier;
            }

            blob.Properties.RehydrationStatus        = BlobHttpResponseParsers.GetRehydrationStatus(rehydrationStatusString);
            blob.Properties.BlobTierLastModifiedTime = blobTierLastModifiedTime;
            blob.Properties.BlobTierInferred         = blobTierInferred;

            return(new ListBlobEntry(name, blob));
        }
Esempio n. 21
0
        /// <summary>
        /// Reads a container entry completely including its properties and metadata.
        /// </summary>
        /// <returns>Container listing entry</returns>
        private BlobContainerEntry ParseContainerEntry()
        {
            Uri    uri  = null;
            string name = null;
            IDictionary <string, string> metadata            = null;
            BlobContainerProperties      containerProperties = new BlobContainerProperties();

            this.reader.ReadStartElement();
            while (this.reader.IsStartElement())
            {
                if (this.reader.IsEmptyElement)
                {
                    this.reader.Skip();
                }
                else
                {
                    switch (this.reader.Name)
                    {
                    case Constants.UrlElement:
                        string url = this.reader.ReadElementContentAsString();
                        Uri.TryCreate(url, UriKind.Absolute, out uri);
                        break;

                    case Constants.NameElement:
                        name = this.reader.ReadElementContentAsString();
                        break;

                    case Constants.PropertiesElement:
                        this.reader.ReadStartElement();
                        while (this.reader.IsStartElement())
                        {
                            if (this.reader.IsEmptyElement)
                            {
                                this.reader.Skip();
                            }
                            else
                            {
                                switch (this.reader.Name)
                                {
                                case Constants.LastModifiedElement:
                                    containerProperties.LastModified = reader.ReadElementContentAsString().ToUTCTime();
                                    break;

                                case Constants.EtagElement:
                                    containerProperties.ETag = reader.ReadElementContentAsString();
                                    break;

                                case Constants.LeaseStatusElement:
                                    containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseStateElement:
                                    containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseDurationElement:
                                    containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(reader.ReadElementContentAsString());
                                    break;
                                }
                            }
                        }

                        this.reader.ReadEndElement();
                        break;

                    case Constants.MetadataElement:
                        metadata = Response.ParseMetadata(this.reader);
                        break;

                    default:
                        reader.Skip();
                        break;
                    }
                }
            }

            this.reader.ReadEndElement();

            if (metadata == null)
            {
                metadata = new Dictionary <string, string>();
            }

            return(new BlobContainerEntry
            {
                Properties = containerProperties,
                Name = name,
                Uri = uri,
                Metadata = metadata,
            });
        }
Esempio n. 22
0
        /// <summary>
        /// Gets the blob's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The blob's properties.</returns>
        public static BlobProperties GetProperties(HttpResponseMessage response)
        {
            BlobProperties properties = new BlobProperties();

            if (response.Content != null)
            {
                properties.LastModified    = response.Content.Headers.LastModified;
                properties.ContentEncoding = HttpWebUtility.CombineHttpHeaderValues(response.Content.Headers.ContentEncoding);
                properties.ContentLanguage = HttpWebUtility.CombineHttpHeaderValues(response.Content.Headers.ContentLanguage);

                if (response.Content.Headers.ContentDisposition != null)
                {
                    properties.ContentDisposition = response.Content.Headers.ContentDisposition.ToString();
                }

                if (response.Content.Headers.ContentMD5 != null && response.Content.Headers.ContentRange == null)
                {
                    properties.ContentMD5 = Convert.ToBase64String(response.Content.Headers.ContentMD5);
                }
                else if (!string.IsNullOrEmpty(response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobContentMD5Header)))
                {
                    properties.ContentMD5 = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobContentMD5Header);
                }

                if (response.Content.Headers.ContentType != null)
                {
                    properties.ContentType = response.Content.Headers.ContentType.ToString();
                }

                string blobEncryption = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.ServerEncrypted);
                properties.IsServerEncrypted = string.Equals(blobEncryption, Constants.HeaderConstants.TrueHeader, StringComparison.OrdinalIgnoreCase);

                string incrementalCopy = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.IncrementalCopyHeader);
                properties.IsIncrementalCopy = string.Equals(incrementalCopy, Constants.HeaderConstants.TrueHeader, StringComparison.OrdinalIgnoreCase);

                // Get the content length. Prioritize range and x-ms over content length for the special cases.
                string contentLengthHeader = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobContentLengthHeader);
                if ((response.Content.Headers.ContentRange != null) &&
                    response.Content.Headers.ContentRange.HasLength)
                {
                    properties.Length = response.Content.Headers.ContentRange.Length.Value;
                }
                else if (!string.IsNullOrEmpty(contentLengthHeader))
                {
                    properties.Length = long.Parse(contentLengthHeader);
                }
                else if (response.Content.Headers.ContentLength.HasValue)
                {
                    properties.Length = response.Content.Headers.ContentLength.Value;
                }
            }

            if (response.Headers.CacheControl != null)
            {
                properties.CacheControl = response.Headers.CacheControl.ToString();
            }

            if (response.Headers.ETag != null)
            {
                properties.ETag = response.Headers.ETag.ToString();
            }

            // Get blob type
            string blobType = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobType);

            if (!string.IsNullOrEmpty(blobType))
            {
                properties.BlobType = (BlobType)Enum.Parse(typeof(BlobType), blobType, true);
            }

            // Get lease properties
            properties.LeaseStatus   = GetLeaseStatus(response);
            properties.LeaseState    = GetLeaseState(response);
            properties.LeaseDuration = GetLeaseDuration(response);

            // Get sequence number
            string sequenceNumber = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobSequenceNumber);

            if (!string.IsNullOrEmpty(sequenceNumber))
            {
                properties.PageBlobSequenceNumber = long.Parse(sequenceNumber, CultureInfo.InvariantCulture);
            }

            // Get committed block count
            string comittedBlockCount = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobCommittedBlockCount);

            if (!string.IsNullOrEmpty(comittedBlockCount))
            {
                properties.AppendBlobCommittedBlockCount = int.Parse(comittedBlockCount, CultureInfo.InvariantCulture);
            }

            // Get the tier of the blob
            string premiumBlobTierInferredString = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.AccessTierInferredHeader);

            if (!string.IsNullOrEmpty(premiumBlobTierInferredString))
            {
                properties.BlobTierInferred = Convert.ToBoolean(premiumBlobTierInferredString);
            }

            string premiumBlobTierString = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.AccessTierHeader);
            PremiumPageBlobTier?premiumPageBlobTier;

            BlobHttpResponseParsers.GetBlobTier(properties.BlobType, premiumBlobTierString, out premiumPageBlobTier);
            properties.PremiumPageBlobTier = premiumPageBlobTier;
            if (properties.PremiumPageBlobTier.HasValue && !properties.BlobTierInferred.HasValue)
            {
                properties.BlobTierInferred = false;
            }

            return(properties);
        }
        /// <summary>
        /// Parses a blob entry in a blob listing response.
        /// </summary>
        /// <returns>Blob listing entry</returns>
        private IListBlobEntry ParseBlobEntry()
        {
            BlobAttributes blob = new BlobAttributes();
            string         url  = null;
            string         name = null;

            // copy blob attribute strings
            string copyId                = null;
            string copyStatus            = null;
            string copyCompletionTime    = null;
            string copyProgress          = null;
            string copySource            = null;
            string copyStatusDescription = null;

            this.reader.ReadStartElement();
            while (this.reader.IsStartElement())
            {
                if (this.reader.IsEmptyElement)
                {
                    this.reader.Skip();
                }
                else
                {
                    switch (this.reader.Name)
                    {
                    case Constants.UrlElement:
                        url = reader.ReadElementContentAsString();
                        break;

                    case Constants.NameElement:
                        name = reader.ReadElementContentAsString();
                        break;

                    case Constants.SnapshotElement:
                        blob.SnapshotTime = reader.ReadElementContentAsString().ToUTCTime();
                        break;

                    case Constants.PropertiesElement:
                        this.reader.ReadStartElement();
                        while (this.reader.IsStartElement())
                        {
                            if (this.reader.IsEmptyElement)
                            {
                                this.reader.Skip();
                            }
                            else
                            {
                                switch (this.reader.Name)
                                {
                                case Constants.LastModifiedElement:
                                    blob.Properties.LastModified = reader.ReadElementContentAsString().ToUTCTime();
                                    break;

                                case Constants.EtagElement:
                                    blob.Properties.ETag = string.Format("\"{0}\"", reader.ReadElementContentAsString());
                                    break;

                                case Constants.ContentLengthElement:
                                    blob.Properties.Length = reader.ReadElementContentAsLong();
                                    break;

                                case Constants.CacheControlElement:
                                    blob.Properties.CacheControl = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentTypeElement:
                                    blob.Properties.ContentType = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentEncodingElement:
                                    blob.Properties.ContentEncoding = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentLanguageElement:
                                    blob.Properties.ContentLanguage = reader.ReadElementContentAsString();
                                    break;

                                case Constants.ContentMD5Element:
                                    blob.Properties.ContentMD5 = reader.ReadElementContentAsString();
                                    break;

                                case Constants.BlobTypeElement:
                                    string blobTypeString = reader.ReadElementContentAsString();
                                    switch (blobTypeString)
                                    {
                                    case Constants.BlockBlobValue:
                                        blob.Properties.BlobType = BlobType.BlockBlob;
                                        break;

                                    case Constants.PageBlobValue:
                                        blob.Properties.BlobType = BlobType.PageBlob;
                                        break;
                                    }

                                    break;

                                case Constants.LeaseStatusElement:
                                    blob.Properties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseStateElement:
                                    blob.Properties.LeaseState = BlobHttpResponseParsers.GetLeaseState(reader.ReadElementContentAsString());
                                    break;

                                case Constants.LeaseDurationElement:
                                    blob.Properties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(reader.ReadElementContentAsString());
                                    break;

                                case Constants.CopyIdElement:
                                    copyId = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyCompletionTimeElement:
                                    copyCompletionTime = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyStatusElement:
                                    copyStatus = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyProgressElement:
                                    copyProgress = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopySourceElement:
                                    copySource = reader.ReadElementContentAsString();
                                    break;

                                case Constants.CopyStatusDescriptionElement:
                                    copyStatusDescription = reader.ReadElementContentAsString();
                                    break;

                                default:
                                    reader.Skip();
                                    break;
                                }
                            }
                        }

                        this.reader.ReadEndElement();
                        break;

                    case Constants.MetadataElement:
                        blob.Metadata = Response.ParseMetadata(this.reader);
                        break;

                    default:
                        this.reader.Skip();
                        break;
                    }
                }
            }

            this.reader.ReadEndElement();

            var    blobNameSectionIndex = url.LastIndexOf(NavigationHelper.Slash + name);
            string baseUri = url.Substring(0, blobNameSectionIndex + 1);
            var    ub      = new UriBuilder(baseUri);

            ub.Path += Uri.EscapeUriString(name);
            if (baseUri.Length + name.Length < url.Length)
            {
                // it's a url for snapshot.
                // Snapshot blob URI example:http://<yourstorageaccount>.blob.core.windows.net/<yourcontainer>/<yourblobname>?snapshot=2009-12-03T15%3a26%3a19.4466877Z
                ub.Query = url.Substring(baseUri.Length + name.Length + 1);
            }

            blob.Uri = ub.Uri;

            if (!string.IsNullOrEmpty(copyStatus))
            {
                blob.CopyState = BlobHttpResponseParsers.GetCopyAttributes(
                    copyStatus,
                    copyId,
                    copySource,
                    copyProgress,
                    copyCompletionTime,
                    copyStatusDescription);
            }

            return(new ListBlobEntry(name, blob));
        }