/// <summary>
        /// Get the complete query builder for creating the Shared Access Signature query.
        /// </summary>
        /// <param name="policy">The shared access policy to hash.</param>
        /// <param name="headers">The optional header values to set for a blob returned with this SAS.</param>
        /// <param name="accessPolicyIdentifier">An optional identifier for the policy.</param>
        /// <param name="resourceType">Either "b" for blobs or "c" for containers.</param>
        /// <param name="signature">The signature to use.</param>
        /// <param name="accountKeyName">The name of the key used to create the signature, or <c>null</c> if the key is implicit.</param>
        /// <param name="sasVersion">A string indicating the desired SAS version to use, in storage service version format.</param>
        /// <param name="protocols">The HTTP/HTTPS protocols for Account SAS.</param>
        /// <param name="ipAddressOrRange">The IP range for IPSAS.</param>
        /// <returns>The finished query builder.</returns>
        internal static UriQueryBuilder GetSignature(
            SharedAccessBlobPolicy policy,
            SharedAccessBlobHeaders headers,
            string accessPolicyIdentifier,
            string resourceType,
            string signature,
            string accountKeyName,
            string sasVersion,
            SharedAccessProtocol? protocols,
            IPAddressOrRange ipAddressOrRange
            )
        {
            CommonUtility.AssertNotNullOrEmpty("resourceType", resourceType);

            UriQueryBuilder builder = new UriQueryBuilder();

            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedVersion, sasVersion);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedResource, resourceType);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIdentifier, accessPolicyIdentifier);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedKey, accountKeyName);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedProtocols, GetProtocolString(protocols));
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIP, ipAddressOrRange == null ? null : ipAddressOrRange.ToString());

            if (policy != null)
            {
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, GetDateTimeOrNull(policy.SharedAccessStartTime));
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, GetDateTimeOrNull(policy.SharedAccessExpiryTime));

                string permissions = SharedAccessBlobPolicy.PermissionsToString(policy.Permissions);
                if (!string.IsNullOrEmpty(permissions))
                {
                    AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, permissions);
                }
            }

            if (headers != null)
            {
                AddEscapedIfNotNull(builder, Constants.QueryConstants.CacheControl, headers.CacheControl);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentType, headers.ContentType);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentEncoding, headers.ContentEncoding);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentLanguage, headers.ContentLanguage);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentDisposition, headers.ContentDisposition);
            }

            return builder;
        }
Esempio n. 2
0
        public XDocument BrowseCategory(ECategoryName categoryName, EExtendedInfoType extInfoType = EExtendedInfoType.None) {
            var url = new UriQueryBuilder(string.Format("http://api.xrel.to/api/release/browse_category.{0}", ResultFormatValue));
            var client = new WebClient();
            url.QueryString.Add("category_name", categoryName.GetAttribute<ValueAttribute>().Value);
            if (extInfoType != EExtendedInfoType.None) {
                url.QueryString.Add("ext_info_type", extInfoType.GetAttribute<ValueAttribute>().Value);
            }

            var resultString = client.DownloadString(url.ToString());
            if (string.IsNullOrEmpty(resultString)) {
                return null;
            }

            using (var memStream = new MemoryStream()) {
                var buf = Encoding.UTF8.GetBytes(resultString);
                memStream.Write(buf, 0, buf.Length);
                memStream.Position = 0;
                return XDocument.Load(memStream);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Generates a web request to return a listing of all blobs in the container.
        /// </summary>
        /// <param name="uri">The absolute URI to the container.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="listingContext">A set of parameters for the listing operation.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static StorageRequestMessage ListBlobs(Uri uri, int? timeout, BlobListingContext listingContext, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
        {
            UriQueryBuilder builder = ContainerHttpRequestMessageFactory.GetContainerUriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "list");

            if (listingContext != null)
            {
                if (listingContext.Prefix != null)
                {
                    builder.Add("prefix", listingContext.Prefix);
                }

                if (listingContext.Delimiter != null)
                {
                    builder.Add("delimiter", listingContext.Delimiter);
                }

                if (listingContext.Marker != null)
                {
                    builder.Add("marker", listingContext.Marker);
                }

                if (listingContext.MaxResults.HasValue)
                {
                    builder.Add("maxresults", listingContext.MaxResults.ToString());
                }

                if (listingContext.Details != BlobListingDetails.None)
                {
                    StringBuilder sb = new StringBuilder();

                    bool started = false;

                    if ((listingContext.Details & BlobListingDetails.Snapshots) == BlobListingDetails.Snapshots)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("snapshots");
                    }

                    if ((listingContext.Details & BlobListingDetails.UncommittedBlobs) == BlobListingDetails.UncommittedBlobs)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("uncommittedblobs");
                    }

                    if ((listingContext.Details & BlobListingDetails.Metadata) == BlobListingDetails.Metadata)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("metadata");
                    }

                    if ((listingContext.Details & BlobListingDetails.Copy) == BlobListingDetails.Copy)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("copy");
                    }

                    if ((listingContext.Details & BlobListingDetails.Deleted) == BlobListingDetails.Deleted)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("deleted");
                    }

                    builder.Add("include", sb.ToString());
                }
            }

            StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Get, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
            return request;
        }
Esempio n. 4
0
 /// <summary>
 /// Creates a web request to get the stats of the Blob service.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the Blob service endpoint.</param>
 /// <param name="builder">A <see cref="UriQueryBuilder"/> object specifying additional parameters to add to the URI query string.</param>
 /// <param name="timeout">The server timeout interval, in seconds.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest GetServiceStats(Uri uri, UriQueryBuilder builder, int?timeout, OperationContext operationContext)
 {
     return(HttpWebRequestFactory.GetServiceStats(uri, builder, timeout, operationContext));
 }
Esempio n. 5
0
        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.ContentChecksum.MD5 = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);

                                    break;

                                case Constants.ContentCRC64Element:
                                    blob.Properties.ContentChecksum.CRC64 = 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. 6
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;

            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;

                                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);
            }

            return(new ListBlobEntry(name, blob));
        }
        internal static Tuple <HttpWebRequest, Stream> BuildRequestForTableBatchOperation(Uri uri, UriQueryBuilder builder, IBufferManager bufferManager, int?timeout, string tableName, TableBatchOperation batch, bool useVersionHeader, OperationContext ctx, TableRequestOptions options, string accountName)
        {
            HttpWebRequest     msg           = BuildRequestCore(NavigationHelper.AppendPathToSingleUri(uri, "$batch"), builder, "POST", timeout, useVersionHeader, ctx);
            TablePayloadFormat payloadFormat = options.PayloadFormat.Value;

            Logger.LogInformational(ctx, SR.PayloadFormat, payloadFormat);

            // create the writer, indent for readability of the examples.
            ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings()
            {
                CheckCharacters = false,                              // sets this flag on the XmlWriter for ATOM
                Version         = TableConstants.ODataProtocolVersion // set the Odata version to use when writing the entry
            };

            HttpWebRequestAdapterMessage adapterMsg = new HttpWebRequestAdapterMessage(msg, bufferManager);

            // Start Batch
            ODataMessageWriter odataWriter = new ODataMessageWriter(adapterMsg, writerSettings);
            ODataBatchWriter   batchWriter = odataWriter.CreateODataBatchWriter();

            batchWriter.WriteStartBatch();

            bool isQuery = batch.Count == 1 && batch[0].OperationType == TableOperationType.Retrieve;

            // Query operations should not be inside changeset in payload
            if (!isQuery)
            {
                // Start Operation
                batchWriter.WriteStartChangeset();
                batchWriter.Flush();
            }

            foreach (TableOperation operation in batch)
            {
                string httpMethod = operation.HttpMethod;
                if (operation.OperationType == TableOperationType.Merge || operation.OperationType == TableOperationType.InsertOrMerge)
                {
                    options.AssertNoEncryptionPolicyOrStrictMode();
                    httpMethod = "MERGE";
                }

                ODataBatchOperationRequestMessage mimePartMsg = batchWriter.CreateOperationRequestMessage(httpMethod, operation.GenerateRequestURI(uri, tableName));
                SetAcceptAndContentTypeForODataBatchMessage(mimePartMsg, payloadFormat);

                // etag
                if (operation.OperationType == TableOperationType.Delete ||
                    operation.OperationType == TableOperationType.Replace ||
                    operation.OperationType == TableOperationType.Merge)
                {
                    mimePartMsg.SetHeader("If-Match", operation.Entity.ETag);
                }

                // Prefer header
                if (operation.OperationType == TableOperationType.Insert)
                {
                    mimePartMsg.SetHeader("Prefer", operation.EchoContent ? "return-content" : "return-no-content");
                }

                if (operation.OperationType != TableOperationType.Delete && operation.OperationType != TableOperationType.Retrieve)
                {
                    using (ODataMessageWriter batchEntryWriter = new ODataMessageWriter(mimePartMsg, writerSettings, new TableStorageModel(accountName)))
                    {
                        // Write entity
                        ODataWriter entryWriter = batchEntryWriter.CreateODataEntryWriter();
                        WriteOdataEntity(operation.Entity, operation.OperationType, ctx, entryWriter, options);
                    }
                }
            }

            if (!isQuery)
            {
                // End Operation
                batchWriter.WriteEndChangeset();
            }

            // End Batch
            batchWriter.WriteEndBatch();
            batchWriter.Flush();

            return(new Tuple <HttpWebRequest, Stream>(adapterMsg.GetPopulatedMessage(), adapterMsg.GetStream()));
        }
        /// <summary>
        /// Constructs a web request to create a new directory.
        /// </summary>
        /// <param name="uri">The absolute URI to the directory.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
        /// <param name="operationContext">An <see cref="OperationContext" /> object for tracking the current operation.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest Create(Uri uri, int?timeout, bool useVersionHeader, OperationContext operationContext)
        {
            UriQueryBuilder directoryBuilder = GetDirectoryUriQueryBuilder();

            return(HttpWebRequestFactory.Create(uri, timeout, directoryBuilder, useVersionHeader, operationContext));
        }
 /// <summary>
 /// Creates a web request to get Queue service stats.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the Queue service endpoint.</param>
 /// <param name="builder">A <see cref="UriQueryBuilder"/> object specifying additional parameters to add to the URI query string.</param>
 /// <param name="timeout">The server timeout interval, in seconds.</param>
 /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 internal static HttpWebRequest GetServiceStats(Uri uri, UriQueryBuilder builder, int?timeout, bool useVersionHeader, OperationContext operationContext)
 {
     return(HttpWebRequestFactory.GetServiceStats(uri, builder, timeout, useVersionHeader, operationContext));
 }
        /// <summary>
        /// Creates the specified URI.
        /// </summary>
        /// <param name="uri">The URI to create.</param>
        /// <param name="timeout">The timeout.</param>
        /// <param name="builder">The builder.</param>
        /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A web request for performing the operation.</returns>
        internal static HttpWebRequest Create(Uri uri, int?timeout, UriQueryBuilder builder, bool useVersionHeader, OperationContext operationContext)
        {
            HttpWebRequest request = CreateWebRequest(WebRequestMethods.Http.Put, uri, timeout, builder, useVersionHeader, operationContext);

            return(request);
        }
        internal static HttpWebRequest CreateWebRequest(string method, Uri uri, int?timeout, UriQueryBuilder builder, bool useVersionHeader, OperationContext operationContext)
        {
            if (builder == null)
            {
                builder = new UriQueryBuilder();
            }

            if (timeout.HasValue && timeout.Value > 0)
            {
                builder.Add("timeout", timeout.Value.ToString(CultureInfo.InvariantCulture));
            }

#if WINDOWS_PHONE || !WINDOWS_DESKTOP
            // Windows Phone does not allow the caller to disable caching, so a random GUID
            // is added to every URI to make it look a different request.
            builder.Add("randomguid", Guid.NewGuid().ToString("N"));
#endif

            Uri uriRequest = builder.AddToUri(uri);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriRequest);
            request.Method = method;

            // Set the Content-Length of requests to 0 by default for all put requests.
            if (method.Equals(WebRequestMethods.Http.Put, StringComparison.OrdinalIgnoreCase))
            {
                request.ContentLength = 0;
            }

            request.UserAgent = Constants.HeaderConstants.UserAgent;

            if (useVersionHeader)
            {
                request.Headers[Constants.HeaderConstants.StorageVersionHeader] = Constants.HeaderConstants.TargetStorageVersion;
            }

#if WINDOWS_DESKTOP && !WINDOWS_PHONE
            request.KeepAlive = true;

            // Disable the Expect 100-Continue
            request.ServicePoint.Expect100Continue = false;
#endif

            return(request);
        }
        /// <summary>
        /// Deletes the specified URI.
        /// </summary>
        /// <param name="uri">The URI of the resource to delete.</param>
        /// <param name="timeout">The timeout.</param>
        /// <param name="builder">The builder.</param>
        /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A web request for performing the operation.</returns>
        internal static HttpWebRequest Delete(Uri uri, UriQueryBuilder builder, int?timeout, bool useVersionHeader, OperationContext operationContext)
        {
            HttpWebRequest request = CreateWebRequest("DELETE", uri, timeout, builder, useVersionHeader, operationContext);

            return(request);
        }
 internal static void AddEscapedIfNotNull(UriQueryBuilder builder, string name, string value)
 {
     throw new System.NotImplementedException();
 }
Esempio n. 14
0
        internal static HttpRequestMessage BuildRequestForTableOperation <T>(RESTCommand <T> cmd, Uri uri, UriQueryBuilder builder, int?timeout, TableOperation operation, CloudTableClient client, HttpContent content, OperationContext ctx, TablePayloadFormat payloadFormat)
        {
            HttpRequestMessage msg = BuildRequestCore(uri, builder, operation.HttpMethod, timeout, content, ctx);

            // Set Accept and Content-Type based on the payload format.
            SetAcceptHeaderForHttpWebRequest(msg, payloadFormat);
            Logger.LogInformational(ctx, SR.PayloadFormat, payloadFormat);

            if (!operation.HttpMethod.Equals("HEAD") && !operation.HttpMethod.Equals("GET"))
            {
                SetContentTypeForHttpWebRequest(msg, payloadFormat);
            }

            if (operation.OperationType == TableOperationType.InsertOrMerge || operation.OperationType == TableOperationType.Merge)
            {
                // post tunnelling
                msg.Headers.Add("X-HTTP-Method", "MERGE");
            }

            // etag
            if (operation.OperationType == TableOperationType.Delete ||
                operation.OperationType == TableOperationType.Replace ||
                operation.OperationType == TableOperationType.Merge)
            {
                msg.Headers.Add("If-Match", operation.Entity.ETag);
            }

            // Prefer header
            if (operation.OperationType == TableOperationType.Insert)
            {
                msg.Headers.Add("Prefer", operation.EchoContent ? "return-content" : "return-no-content");
            }

            if (operation.OperationType == TableOperationType.Insert ||
                operation.OperationType == TableOperationType.Merge ||
                operation.OperationType == TableOperationType.InsertOrMerge ||
                operation.OperationType == TableOperationType.InsertOrReplace ||
                operation.OperationType == TableOperationType.Replace)
            {
                // create the writer, indent for readability of the examples.
                ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings()
                {
                    CheckCharacters = false,                              // sets this flag on the XmlWriter for ATOM
                    Version         = TableConstants.ODataProtocolVersion // set the Odata version to use when writing the entry
                };

                HttpRequestAdapterMessage adapterMsg = new HttpRequestAdapterMessage(msg, client.BufferManager, (int)Constants.KB);
                if (!operation.HttpMethod.Equals("HEAD") && !operation.HttpMethod.Equals("GET"))
                {
                    SetContentTypeForAdapterMessage(adapterMsg, payloadFormat);
                }

                cmd.StreamToDispose = adapterMsg.GetStream();

                ODataMessageWriter odataWriter = new ODataMessageWriter(adapterMsg, writerSettings, new TableStorageModel(client.AccountName));
                ODataWriter        writer      = odataWriter.CreateODataEntryWriter();
                WriteOdataEntity(operation.Entity, operation.OperationType, ctx, writer);

                return(adapterMsg.GetPopulatedMessage());
            }

            return(msg);
        }
Esempio n. 15
0
        internal static HttpRequestMessage BuildRequestForTableBatchOperation <T>(RESTCommand <T> cmd, Uri uri, UriQueryBuilder builder, int?timeout, string tableName, TableBatchOperation batch, CloudTableClient client, HttpContent content, OperationContext ctx, TablePayloadFormat payloadFormat)
        {
            HttpRequestMessage msg = BuildRequestCore(NavigationHelper.AppendPathToSingleUri(uri, "$batch"), builder, HttpMethod.Post, timeout, content, ctx);

            Logger.LogInformational(ctx, SR.PayloadFormat, payloadFormat);

            // create the writer, indent for readability of the examples.
            ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings()
            {
                CheckCharacters = false,                              // sets this flag on the XmlWriter for ATOM
                Version         = TableConstants.ODataProtocolVersion // set the Odata version to use when writing the entry
            };

            HttpRequestAdapterMessage adapterMsg = new HttpRequestAdapterMessage(msg, client.BufferManager, 64 * (int)Constants.KB);

            cmd.StreamToDispose = adapterMsg.GetStream();

            // Start Batch
            ODataMessageWriter odataWriter = new ODataMessageWriter(adapterMsg, writerSettings);
            ODataBatchWriter   batchWriter = odataWriter.CreateODataBatchWriter();

            batchWriter.WriteStartBatch();

            bool isQuery = batch.Count == 1 && batch[0].OperationType == TableOperationType.Retrieve;

            // Query operations should not be inside changeset in payload
            if (!isQuery)
            {
                // Start Operation
                batchWriter.WriteStartChangeset();
                batchWriter.Flush();
            }

            foreach (TableOperation operation in batch)
            {
                string httpMethod = operation.OperationType == TableOperationType.Merge || operation.OperationType == TableOperationType.InsertOrMerge ? "MERGE" : operation.HttpMethod.Method;

                ODataBatchOperationRequestMessage mimePartMsg = batchWriter.CreateOperationRequestMessage(httpMethod, operation.GenerateRequestURI(uri, tableName));
                SetAcceptAndContentTypeForODataBatchMessage(mimePartMsg, payloadFormat);

                // etag
                if (operation.OperationType == TableOperationType.Delete ||
                    operation.OperationType == TableOperationType.Replace ||
                    operation.OperationType == TableOperationType.Merge)
                {
                    mimePartMsg.SetHeader("If-Match", operation.Entity.ETag);
                }

                // Prefer header
                if (operation.OperationType == TableOperationType.Insert)
                {
                    mimePartMsg.SetHeader("Prefer", operation.EchoContent ? "return-content" : "return-no-content");
                }

                if (operation.OperationType != TableOperationType.Delete && operation.OperationType != TableOperationType.Retrieve)
                {
                    using (ODataMessageWriter batchEntryWriter = new ODataMessageWriter(mimePartMsg, writerSettings, new TableStorageModel(client.AccountName)))
                    {
                        // Write entity
                        ODataWriter entryWriter = batchEntryWriter.CreateODataEntryWriter();
                        WriteOdataEntity(operation.Entity, operation.OperationType, ctx, entryWriter);
                    }
                }
            }

            if (!isQuery)
            {
                // End Operation
                batchWriter.WriteEndChangeset();
            }

            // End Batch
            batchWriter.WriteEndBatch();
            batchWriter.Flush();

            return(adapterMsg.GetPopulatedMessage());
        }
        /// <summary>
        /// Get the complete query builder for creating the Shared Access Signature query.
        /// </summary>
        /// <param name="permissions">The permissions string for the resource, or null.</param>
        /// <param name="startTime">The start time, or null.</param>
        /// <param name="expiryTime">The expiration time, or null.</param>
        /// <param name="startPartitionKey">The start partition key, or null.</param>
        /// <param name="startRowKey">The start row key, or null.</param>
        /// <param name="endPartitionKey">The end partition key, or null.</param>
        /// <param name="endRowKey">The end row key, or null.</param>
        /// <param name="accessPolicyIdentifier">An optional identifier for the policy.</param>
        /// <param name="resourceType">Either "b" for blobs or "c" for containers, or null if neither.</param>
        /// <param name="tableName">The name of the table this signature is associated with,
        ///     or null if not using table SAS.</param>
        /// <param name="signature">The signature to use.</param>
        /// <param name="accountKeyName">The name of the key used to create the signature, or null if the key is implicit.</param>
        /// <returns>The finished query builder.</returns>
        private static UriQueryBuilder GetSharedAccessSignatureImpl(
            string permissions,
            DateTimeOffset? startTime,
            DateTimeOffset? expiryTime,
            string startPartitionKey,
            string startRowKey,
            string endPartitionKey,
            string endRowKey,
            string accessPolicyIdentifier,
            string resourceType,
            string tableName,
            string signature,
            string accountKeyName)
        {
            UriQueryBuilder builder = new UriQueryBuilder();

            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedVersion, Constants.HeaderConstants.TargetStorageVersion);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, GetDateTimeOrNull(startTime));
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, GetDateTimeOrNull(expiryTime));
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedResource, resourceType);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SasTableName, tableName);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, permissions);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.StartPartitionKey, startPartitionKey);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.StartRowKey, startRowKey);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.EndPartitionKey, endPartitionKey);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.EndRowKey, endRowKey);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIdentifier, accessPolicyIdentifier);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedKey, accountKeyName);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature);

            return builder;
        }
        internal static UriQueryBuilder GetSignature(
            SharedAccessAccountPolicy policy,
            string signature,
            string accountKeyName,
            string sasVersion)
        {
            CommonUtility.AssertNotNull("signature", signature);
            CommonUtility.AssertNotNull("policy", policy);

            UriQueryBuilder builder = new UriQueryBuilder();
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedVersion, sasVersion);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedKey, accountKeyName);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedProtocols, policy.Protocols == null ? null : GetProtocolString(policy.Protocols.Value));
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIP, policy.IPAddressOrRange == null ? null : policy.IPAddressOrRange.ToString());
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, GetDateTimeOrNull(policy.SharedAccessStartTime));
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, GetDateTimeOrNull(policy.SharedAccessExpiryTime));

            string resourceTypes = SharedAccessAccountPolicy.ResourceTypesToString(policy.ResourceTypes);
            if (!string.IsNullOrEmpty(resourceTypes))
            {
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedResourceTypes, resourceTypes);
            }

            string services = SharedAccessAccountPolicy.ServicesToString(policy.Services);
            if (!string.IsNullOrEmpty(services))
            {
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedServices, services);
            }

            string permissions = SharedAccessAccountPolicy.PermissionsToString(policy.Permissions);
            if (!string.IsNullOrEmpty(permissions))
            {
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, permissions);
            }

            return builder;
        }
 /// <summary>
 /// Creates a web request to get Queue service stats.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the Queue service endpoint.</param>
 /// <param name="builder">A <see cref="UriQueryBuilder"/> object specifying additional parameters to add to the URI query string.</param>
 /// <param name="timeout">The server timeout interval, in seconds.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest GetServiceStats(Uri uri, UriQueryBuilder builder, int?timeout, OperationContext operationContext)
 {
     return(QueueHttpWebRequestFactory.GetServiceStats(uri, builder, timeout, true /* useVersionHeader */, operationContext));
 }
        internal static StorageCredentials ParseQuery(IDictionary<string, string> queryParameters, bool mandatorySignedResource)
        {
            string signature = null;
            string signedStart = null;
            string signedExpiry = null;
            string signedResource = null;
            string sigendPermissions = null;
            string signedIdentifier = null;
            string signedVersion = null;
            string cacheControl = null;
            string contentType = null;
            string contentEncoding = null;
            string contentLanguage = null;
            string contentDisposition = null;
            string tableName = null;

            bool sasParameterFound = false;

            foreach (KeyValuePair<string, string> parameter in queryParameters)
            {
                switch (parameter.Key.ToLower())
                {
                    case Constants.QueryConstants.SignedStart:
                        signedStart = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedExpiry:
                        signedExpiry = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedPermissions:
                        sigendPermissions = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedResource:
                        signedResource = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedIdentifier:
                        signedIdentifier = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.Signature:
                        signature = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedVersion:
                        signedVersion = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.CacheControl:
                        cacheControl = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.ContentType:
                        contentType = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.ContentEncoding:
                        contentEncoding = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.ContentLanguage:
                        contentLanguage = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.ContentDisposition:
                        contentDisposition = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SasTableName:
                        tableName = parameter.Value;
                        sasParameterFound = true;
                        break;

                    default:
                        break;
                }
            }

            if (sasParameterFound)
            {
                if (signature == null || (mandatorySignedResource && signedResource == null))
                {
                    string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.MissingMandatoryParametersForSAS);
                    throw new ArgumentException(errorMessage);
                }

                UriQueryBuilder builder = new UriQueryBuilder();
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, signedStart);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, signedExpiry);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, sigendPermissions);
                if (signedResource != null)
                {
                    builder.Add(Constants.QueryConstants.SignedResource, signedResource);
                }

                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIdentifier, signedIdentifier);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedVersion, signedVersion);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.CacheControl, cacheControl);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentType, contentType);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentEncoding, contentEncoding);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentLanguage, contentLanguage);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentDisposition, contentDisposition);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SasTableName, tableName);
               
                return new StorageCredentials(builder.ToString());
            }

            return null;
        }
Esempio n. 20
0
        /// <summary>
        /// Constructs a web request to create a new directory.
        /// </summary>
        /// <param name="uri">The absolute URI to the directory.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static StorageRequestMessage Create(Uri uri, int?timeout, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
        {
            UriQueryBuilder directoryBuilder = GetDirectoryUriQueryBuilder();

            return(HttpRequestMessageFactory.Create(uri, timeout, directoryBuilder, content, operationContext, canonicalizer, credentials));
        }
        public void UriQueryBuilder_A9()
        {
            var uri = UriQueryBuilder.AppendParameter("https://example.com/?key=abc", "say", "hello");

            Assert.AreEqual("https://example.com/?key=abc&say=hello", uri);
        }
 /// <summary>
 /// Creates a web request to get the properties of the Blob service account.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the Blob service endpoint.</param>
 /// <param name="builder">A <see cref="UriQueryBuilder"/> object specifying additional parameters to add to the URI query string.</param>
 /// <param name="timeout">The server timeout interval, in seconds.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A web request to use to perform the operation.</returns>
 public static StorageRequestMessage GetAccountProperties(Uri uri, UriQueryBuilder builder, int?timeout, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
 {
     return(HttpRequestMessageFactory.GetAccountProperties(uri, builder, timeout, content, operationContext, canonicalizer, credentials));
 }
        internal static Tuple <HttpWebRequest, Stream> BuildRequestForTableOperation(Uri uri, UriQueryBuilder builder, IBufferManager bufferManager, int?timeout, TableOperation operation, bool useVersionHeader, OperationContext ctx, TableRequestOptions options, string accountName)
        {
            HttpWebRequest msg = BuildRequestCore(uri, builder, operation.HttpMethod, timeout, useVersionHeader, ctx);

            TablePayloadFormat payloadFormat = options.PayloadFormat.Value;

            // Set Accept and Content-Type based on the payload format.
            SetAcceptHeaderForHttpWebRequest(msg, payloadFormat);
            Logger.LogInformational(ctx, SR.PayloadFormat, payloadFormat);

            if (operation.HttpMethod != "HEAD" && operation.HttpMethod != "GET")
            {
                msg.ContentType = Constants.JsonContentTypeHeaderValue;
            }

            if (operation.OperationType == TableOperationType.InsertOrMerge || operation.OperationType == TableOperationType.Merge)
            {
                options.AssertNoEncryptionPolicyOrStrictMode();

                // post tunnelling
                msg.Headers.Add("X-HTTP-Method", "MERGE");
            }

            // etag
            if (operation.OperationType == TableOperationType.Delete ||
                operation.OperationType == TableOperationType.Replace ||
                operation.OperationType == TableOperationType.Merge)
            {
                if (operation.Entity.ETag != null)
                {
                    msg.Headers.Add("If-Match", operation.Entity.ETag);
                }
            }

            // Prefer header
            if (operation.OperationType == TableOperationType.Insert)
            {
                msg.Headers.Add("Prefer", operation.EchoContent ? "return-content" : "return-no-content");
            }

            if (operation.OperationType == TableOperationType.Insert ||
                operation.OperationType == TableOperationType.Merge ||
                operation.OperationType == TableOperationType.InsertOrMerge ||
                operation.OperationType == TableOperationType.InsertOrReplace ||
                operation.OperationType == TableOperationType.Replace)
            {
                // create the writer, indent for readability of the examples.
                ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings()
                {
                    CheckCharacters = false,                              // sets this flag on the XmlWriter for ATOM
                    Version         = TableConstants.ODataProtocolVersion // set the Odata version to use when writing the entry
                };

                HttpWebRequestAdapterMessage adapterMsg = new HttpWebRequestAdapterMessage(msg, bufferManager);
                if (operation.HttpMethod != "HEAD" && operation.HttpMethod != "GET")
                {
                    adapterMsg.SetHeader(Constants.HeaderConstants.PayloadContentTypeHeader, Constants.JsonContentTypeHeaderValue);
                }

                ODataMessageWriter odataWriter = new ODataMessageWriter(adapterMsg, writerSettings, new TableStorageModel(accountName));
                ODataWriter        writer      = odataWriter.CreateODataEntryWriter();
                WriteOdataEntity(operation.Entity, operation.OperationType, ctx, writer, options);

                return(new Tuple <HttpWebRequest, Stream>(adapterMsg.GetPopulatedMessage(), adapterMsg.GetStream()));
            }

            return(new Tuple <HttpWebRequest, Stream>(msg, null));
        }
Esempio n. 24
0
 /// <summary>
 /// Creates a web request to set the properties of the File service.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the File service endpoint.</param>
 /// <param name="builder">A <see cref="UriQueryBuilder"/> object specifying additional parameters to add to the URI query string.</param>
 /// <param name="timeout">The server timeout interval, in seconds.</param>
 /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest SetServiceProperties(Uri uri, UriQueryBuilder builder, int?timeout, bool useVersionHeader, OperationContext operationContext)
 {
     return(HttpWebRequestFactory.SetServiceProperties(uri, builder, timeout, useVersionHeader, operationContext));
 }
        /// <summary>
        /// Generates a web request to return a listing of all blobs in the container.
        /// </summary>
        /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the container.</param>
        /// <param name="timeout">An integer specifying the server timeout interval.</param>
        /// <param name="listingContext">A <see cref="ListingContext"/> object.</param>
        /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
        public static HttpWebRequest ListBlobs(Uri uri, int?timeout, BlobListingContext listingContext, bool useVersionHeader, OperationContext operationContext)
        {
            UriQueryBuilder builder = ContainerHttpWebRequestFactory.GetContainerUriQueryBuilder();

            builder.Add(Constants.QueryConstants.Component, "list");

            if (listingContext != null)
            {
                if (listingContext.Prefix != null)
                {
                    builder.Add("prefix", listingContext.Prefix);
                }

                if (listingContext.Delimiter != null)
                {
                    builder.Add("delimiter", listingContext.Delimiter);
                }

                if (listingContext.Marker != null)
                {
                    builder.Add("marker", listingContext.Marker);
                }

                if (listingContext.MaxResults.HasValue)
                {
                    builder.Add("maxresults", listingContext.MaxResults.ToString());
                }

                if (listingContext.Details != BlobListingDetails.None)
                {
                    StringBuilder sb = new StringBuilder();

                    bool started = false;

                    if ((listingContext.Details & BlobListingDetails.Snapshots) == BlobListingDetails.Snapshots)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("snapshots");
                    }

                    if ((listingContext.Details & BlobListingDetails.UncommittedBlobs) == BlobListingDetails.UncommittedBlobs)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("uncommittedblobs");
                    }

                    if ((listingContext.Details & BlobListingDetails.Metadata) == BlobListingDetails.Metadata)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("metadata");
                    }

                    if ((listingContext.Details & BlobListingDetails.Copy) == BlobListingDetails.Copy)
                    {
                        if (!started)
                        {
                            started = true;
                        }
                        else
                        {
                            sb.Append(",");
                        }

                        sb.Append("copy");
                    }

                    builder.Add("include", sb.ToString());
                }
            }

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Get, uri, timeout, builder, useVersionHeader, operationContext);

            return(request);
        }
        /// <summary>
        /// Parses the query.
        /// </summary>
        /// <param name="queryParameters">The query parameters.</param>
        internal static StorageCredentials ParseQuery(IDictionary<string, string> queryParameters)
        {
            string signature = null;
            string signedStart = null;
            string signedExpiry = null;
            string signedResource = null;
            string sigendPermissions = null;
            string signedIdentifier = null;
            string signedVersion = null;

            bool sasParameterFound = false;

            foreach (KeyValuePair<string, string> parameter in queryParameters)
            {
                switch (parameter.Key.ToLower())
                {
                    case Constants.QueryConstants.SignedStart:
                        signedStart = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedExpiry:
                        signedExpiry = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedPermissions:
                        sigendPermissions = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedResource:
                        signedResource = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedIdentifier:
                        signedIdentifier = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.Signature:
                        signature = parameter.Value;
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.SignedVersion:
                        signedVersion = parameter.Value;
                        sasParameterFound = true;
                        break;

                    default:
                        break;
                    //// string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.InvalidQueryParametersInsideBlobAddress, key.ToLower());
                    //// throw new ArgumentException(errorMessage);
                }
            }

            if (sasParameterFound)
            {
                if (signature == null || signedResource == null)
                {
                    string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.MissingMandatoryParamtersForSAS);
                    throw new ArgumentException(errorMessage);
                }

                UriQueryBuilder builder = new UriQueryBuilder();
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, signedStart);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, signedExpiry);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, sigendPermissions);
                builder.Add(Constants.QueryConstants.SignedResource, signedResource);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIdentifier, signedIdentifier);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedVersion, signedVersion);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature);

                return new StorageCredentials(builder.ToString());
            }

            return null;
        }
Esempio n. 27
0
        private async static Task <T> ExecuteAsyncInternal <T>(RESTCommand <T> cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token)
        {
            // Note all code below will reference state, not params directly, this will allow common code with multiple executors (APM, Sync, Async)
            using (ExecutionState <T> executionState = new ExecutionState <T>(cmd, policy, operationContext))
                using (CancellationTokenSource timeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token))
                {
                    bool     shouldRetry = false;
                    TimeSpan delay       = TimeSpan.Zero;

                    // Note - The service accepts both api-version and x-ms-version and therefore it is ok to add x-ms-version to all requests.

                    // Create a new client
                    HttpClient client = cmd.HttpClient ?? HttpClientFactory.Instance;

                    do
                    {
                        try
                        {
                            executionState.Init();

                            // 0. Begin Request
                            Executor.StartRequestAttempt(executionState);

                            // 1. Build request and content
                            executionState.CurrentOperation = ExecutorOperation.BeginOperation;

                            // Content is re-created every retry, as HttpClient disposes it after a successful request
                            HttpContent content = cmd.BuildContent != null?cmd.BuildContent(cmd, executionState.OperationContext) : null;

                            // This is so the old auth header etc is cleared out, the content is where serialization occurs which is the major perf hit
                            Uri uri            = cmd.StorageUri.GetUri(executionState.CurrentLocation);
                            Uri transformedUri = cmd.Credentials.TransformUri(uri);
                            Logger.LogInformational(executionState.OperationContext, SR.TraceStartRequestAsync, transformedUri);
                            UriQueryBuilder builder = new UriQueryBuilder(executionState.RestCMD.Builder);
                            executionState.Req = cmd.BuildRequest(cmd, transformedUri, builder, content, cmd.ServerTimeoutInSeconds, executionState.OperationContext);

                            // 2. Set Headers
                            Executor.ApplyUserHeaders(executionState);

                            // Let the user know we are ready to send
                            Executor.FireSendingRequest(executionState);

                            // 3. Sign Request is not needed, as HttpClient will call us

                            // 4. Set timeout
                            if (executionState.OperationExpiryTime.HasValue)
                            {
                                // set the token to cancel after timing out, if the higher token hasn't already been cancelled
                                timeoutTokenSource.CancelAfter(executionState.RemainingTimeout);
                            }
                            else
                            {
                                // effectively prevent timeout
                                timeoutTokenSource.CancelAfter(int.MaxValue);
                            }

                            Executor.CheckTimeout <T>(executionState, true);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(executionState.OperationContext, SR.TraceInitRequestError, ex.Message);

                            // Store exception and throw here. All operations in this try would be non-retryable by default
                            StorageException storageEx = await StorageException.TranslateExceptionAsync(ex, executionState.Cmd.CurrentResult, null, timeoutTokenSource.Token, executionState.Resp).ConfigureAwait(false);

                            storageEx.IsRetryable       = false;
                            executionState.ExceptionRef = storageEx;

                            throw executionState.ExceptionRef;
                        }

                        // Enter Retryable Section of execution
                        try
                        {
                            // Send Request
                            executionState.CurrentOperation = ExecutorOperation.BeginGetResponse;
                            Logger.LogInformational(executionState.OperationContext, SR.TraceGetResponse);
                            executionState.Resp = await client.SendAsync(executionState.Req, HttpCompletionOption.ResponseHeadersRead, timeoutTokenSource.Token).ConfigureAwait(false);

                            executionState.CurrentOperation = ExecutorOperation.EndGetResponse;

                            // Check that echoed client ID matches the one we sent
                            var clientRequestId       = HttpRequestParsers.GetHeader(executionState.Req, Constants.HeaderConstants.ClientRequestIdHeader);
                            var echoedClientRequestId = HttpResponseParsers.GetHeader(executionState.Resp, Constants.HeaderConstants.ClientRequestIdHeader);

                            if (echoedClientRequestId != null && echoedClientRequestId != clientRequestId)
                            {
                                var requestId = HttpResponseParsers.GetHeader(executionState.Resp, Constants.HeaderConstants.RequestIdHeader);
                                var storageEx = new StorageException($"Echoed client request ID: {echoedClientRequestId} does not match sent client request ID: {clientRequestId}.  Service request ID: {requestId}")
                                {
                                    IsRetryable = false
                                };
                                executionState.ExceptionRef = storageEx;
                                throw storageEx;
                            }

                            // Since HttpClient wont throw for non success, manually check and populate an exception
                            if (!executionState.Resp.IsSuccessStatusCode)
                            {
                                // At this point, don't try to read the stream to parse the error
                                executionState.ExceptionRef = await Exceptions.PopulateStorageExceptionFromHttpResponseMessage(executionState.Resp, executionState.Cmd.CurrentResult, executionState.Cmd.ParseError).ConfigureAwait(false);
                            }

                            Logger.LogInformational(executionState.OperationContext, SR.TraceResponse, executionState.Cmd.CurrentResult.HttpStatusCode, executionState.Cmd.CurrentResult.ServiceRequestID, executionState.Cmd.CurrentResult.ContentMd5, executionState.Cmd.CurrentResult.ContentCrc64, executionState.Cmd.CurrentResult.Etag);
                            Executor.FireResponseReceived(executionState);

                            // 7. Do Response parsing (headers etc, no stream available here)
                            if (cmd.PreProcessResponse != null)
                            {
                                executionState.CurrentOperation = ExecutorOperation.PreProcess;

                                try
                                {
                                    executionState.Result = cmd.PreProcessResponse(cmd, executionState.Resp, executionState.ExceptionRef, executionState.OperationContext);

                                    // clear exception
                                    executionState.ExceptionRef = null;
                                }
                                catch (Exception ex)
                                {
                                    executionState.ExceptionRef = ex;
                                }

                                Logger.LogInformational(executionState.OperationContext, SR.TracePreProcessDone);
                            }

                            // 8. (Potentially reads stream from server)
                            executionState.CurrentOperation = ExecutorOperation.GetResponseStream;
                            cmd.ResponseStream = await executionState.Resp.Content.ReadAsStreamAsync().ConfigureAwait(false);

                            // The stream is now available in ResponseStream. Use the stream to parse out the response or error
                            if (executionState.ExceptionRef != null)
                            {
                                executionState.CurrentOperation = ExecutorOperation.BeginDownloadResponse;
                                Logger.LogInformational(executionState.OperationContext, SR.TraceDownloadError);

                                try
                                {
                                    cmd.ErrorStream = new MemoryStream();
                                    await cmd.ResponseStream.WriteToAsync(cmd.ErrorStream, default(IBufferManager), null /* copyLength */, null /* maxLength */, ChecksumRequested.None, executionState, new StreamDescriptor(), timeoutTokenSource.Token).ConfigureAwait(false);

                                    cmd.ErrorStream.Seek(0, SeekOrigin.Begin);
                                    executionState.ExceptionRef = StorageException.TranslateExceptionWithPreBufferedStream(executionState.ExceptionRef, executionState.Cmd.CurrentResult, stream => executionState.Cmd.ParseError(stream, executionState.Resp, null), cmd.ErrorStream, executionState.Resp);
                                    throw executionState.ExceptionRef;
                                }
                                finally
                                {
                                    cmd.ResponseStream.Dispose();
                                    cmd.ResponseStream = null;

                                    cmd.ErrorStream.Dispose();
                                    cmd.ErrorStream = null;
                                }
                            }
                            else
                            {
                                if (!cmd.RetrieveResponseStream)
                                {
                                    cmd.DestinationStream = Stream.Null;
                                }

                                if (cmd.DestinationStream != null)
                                {
                                    if (cmd.StreamCopyState == null)
                                    {
                                        cmd.StreamCopyState = new StreamDescriptor();
                                    }

                                    try
                                    {
                                        executionState.CurrentOperation = ExecutorOperation.BeginDownloadResponse;
                                        Logger.LogInformational(executionState.OperationContext, SR.TraceDownload);
                                        await cmd.ResponseStream.WriteToAsync(cmd.DestinationStream, default(IBufferManager), null /* copyLength */, null /* maxLength */, cmd.ChecksumRequestedForResponseStream, executionState, cmd.StreamCopyState, timeoutTokenSource.Token).ConfigureAwait(false);
                                    }
                                    finally
                                    {
                                        cmd.ResponseStream.Dispose();
                                        cmd.ResponseStream = null;
                                    }
                                }
                            }

                            // 9. Evaluate Response & Parse Results, (Stream potentially available here)
                            if (cmd.PostProcessResponseAsync != null)
                            {
                                executionState.CurrentOperation = ExecutorOperation.PostProcess;
                                Logger.LogInformational(executionState.OperationContext, SR.TracePostProcess);
                                executionState.Result = await cmd.PostProcessResponseAsync(cmd, executionState.Resp, executionState.OperationContext, timeoutTokenSource.Token).ConfigureAwait(false);
                            }

                            executionState.CurrentOperation = ExecutorOperation.EndOperation;
                            Logger.LogInformational(executionState.OperationContext, SR.TraceSuccess);
                            Executor.FinishRequestAttempt(executionState);

                            return(executionState.Result);
                        }
                        catch (Exception e)
                        {
                            Logger.LogWarning(executionState.OperationContext, SR.TraceGenericError, e.Message);
                            Executor.FinishRequestAttempt(executionState);

                            if (e is TaskCanceledException && (executionState.OperationExpiryTime.HasValue && DateTime.Now.CompareTo(executionState.OperationExpiryTime.Value) > 0))
                            {
                                e = new TimeoutException(SR.TimeoutExceptionMessage, e);
                            }
#if NETCORE || WINDOWS_RT
                            StorageException translatedException = StorageException.TranslateException(e, executionState.Cmd.CurrentResult, stream => executionState.Cmd.ParseError(stream, executionState.Resp, null), executionState.Resp);
#else
                            StorageException translatedException = StorageException.TranslateException(e, executionState.Cmd.CurrentResult, stream => executionState.Cmd.ParseError(stream, executionState.Resp, null));
#endif
                            executionState.ExceptionRef = translatedException;
                            Logger.LogInformational(executionState.OperationContext, SR.TraceRetryCheck, executionState.RetryCount, executionState.Cmd.CurrentResult.HttpStatusCode, translatedException.IsRetryable ? "yes" : "no", translatedException.Message);

                            shouldRetry = false;
                            if (translatedException.IsRetryable && (executionState.RetryPolicy != null))
                            {
                                executionState.CurrentLocation = Executor.GetNextLocation(executionState.CurrentLocation, cmd.LocationMode);
                                Logger.LogInformational(executionState.OperationContext, SR.TraceNextLocation, executionState.CurrentLocation);

                                IExtendedRetryPolicy extendedRetryPolicy = executionState.RetryPolicy as IExtendedRetryPolicy;
                                if (extendedRetryPolicy != null)
                                {
                                    RetryContext retryContext = new RetryContext(
                                        executionState.RetryCount++,
                                        cmd.CurrentResult,
                                        executionState.CurrentLocation,
                                        cmd.LocationMode);

                                    RetryInfo retryInfo = extendedRetryPolicy.Evaluate(retryContext, executionState.OperationContext);
                                    if (retryInfo != null)
                                    {
                                        Logger.LogInformational(executionState.OperationContext, SR.TraceRetryInfo, retryInfo.TargetLocation, retryInfo.UpdatedLocationMode);
                                        shouldRetry = true;
                                        executionState.CurrentLocation = retryInfo.TargetLocation;
                                        cmd.LocationMode = retryInfo.UpdatedLocationMode;
                                        delay            = retryInfo.RetryInterval;
                                    }
                                }
                                else
                                {
                                    shouldRetry = executionState.RetryPolicy.ShouldRetry(
                                        executionState.RetryCount++,
                                        cmd.CurrentResult.HttpStatusCode,
                                        executionState.ExceptionRef,
                                        out delay,
                                        executionState.OperationContext);
                                }

                                if ((delay < TimeSpan.Zero) || (delay > Constants.MaximumRetryBackoff))
                                {
                                    delay = Constants.MaximumRetryBackoff;
                                }
                            }
                        }
                        finally
                        {
                            if (executionState.Resp != null)
                            {
                                executionState.Resp.Dispose();
                                executionState.Resp = null;
                            }
                        }

                        // potentially backoff
                        if (!shouldRetry || (executionState.OperationExpiryTime.HasValue && (DateTime.Now + delay).CompareTo(executionState.OperationExpiryTime.Value) > 0))
                        {
                            Logger.LogError(executionState.OperationContext, shouldRetry ? SR.TraceRetryDecisionTimeout : SR.TraceRetryDecisionPolicy, executionState.ExceptionRef.Message);
                            throw executionState.ExceptionRef;
                        }
                        else
                        {
                            if (cmd.RecoveryAction != null)
                            {
                                // I.E. Rewind stream etc.
                                cmd.RecoveryAction(cmd, executionState.Cmd.CurrentResult.Exception, executionState.OperationContext);
                            }

                            if (delay > TimeSpan.Zero)
                            {
                                await Task.Delay(delay, token).ConfigureAwait(false);
                            }

                            Logger.LogInformational(executionState.OperationContext, SR.TraceRetry);
                        }

                        Executor.FireRetrying(executionState);
                    }while (shouldRetry);

                    // should never get here
                    throw new NotImplementedException(SR.InternalStorageError);
                }
        }
Esempio n. 28
0
 /// <summary>
 /// Constructs a web request to set the ACL for a table.
 /// </summary>
 /// <param name="uri">The absolute URI to the table.</param>
 /// <param name="builder">An object of type <see cref="UriQueryBuilder"/>, containing additional parameters to add to the URI query string.</param>
 /// <param name="timeout">The server timeout interval.</param>
 /// <param name="operationContext">An <see cref="OperationContext" /> object for tracking the current operation.</param>
 /// <returns>
 /// A web request to use to perform the operation.
 /// </returns>
 public static HttpWebRequest SetAcl(Uri uri, UriQueryBuilder builder, int?timeout, OperationContext operationContext)
 {
     return(HttpWebRequestFactory.SetAcl(uri, builder, timeout, operationContext));
 }
        internal UriQueryBuilder GenerateQueryBuilder()
        {
            UriQueryBuilder builder = new UriQueryBuilder();

            // filter
            if (!string.IsNullOrEmpty(this.FilterString))
            {
                builder.Add(TableConstants.Filter, this.FilterString);
            }

            // take
            if (this.takeCount.HasValue)
            {
                builder.Add(TableConstants.Top, Convert.ToString(Math.Min(this.takeCount.Value, TableConstants.TableServiceMaxResults), CultureInfo.InvariantCulture));
            }

            // select
            if (this.SelectColumns != null && this.SelectColumns.Count > 0)
            {
                StringBuilder colBuilder = new StringBuilder();
                bool          foundRk    = false;
                bool          foundPk    = false;
                bool          foundTs    = false;

                for (int m = 0; m < this.SelectColumns.Count; m++)
                {
                    if (this.SelectColumns[m] == TableConstants.PartitionKey)
                    {
                        foundPk = true;
                    }
                    else if (this.SelectColumns[m] == TableConstants.RowKey)
                    {
                        foundRk = true;
                    }
                    else if (this.SelectColumns[m] == TableConstants.Timestamp)
                    {
                        foundTs = true;
                    }

                    colBuilder.Append(this.SelectColumns[m]);
                    if (m < this.SelectColumns.Count - 1)
                    {
                        colBuilder.Append(",");
                    }
                }

                if (!foundPk)
                {
                    colBuilder.Append(",");
                    colBuilder.Append(TableConstants.PartitionKey);
                }

                if (!foundRk)
                {
                    colBuilder.Append(",");
                    colBuilder.Append(TableConstants.RowKey);
                }

                if (!foundTs)
                {
                    colBuilder.Append(",");
                    colBuilder.Append(TableConstants.Timestamp);
                }

                builder.Add(TableConstants.Select, colBuilder.ToString());
            }

            return(builder);
        }
Esempio n. 30
0
 public RESTCommand(StorageCredentials credentials, StorageUri storageUri, UriQueryBuilder builder)
 {
     this.Credentials = credentials;
     this.StorageUri  = storageUri;
     this.Builder     = builder;
 }
Esempio n. 31
0
        internal static StorageCredentials ParseQuery(IDictionary <string, string> queryParameters, bool mandatorySignedResource)
        {
            string signature          = null;
            string signedStart        = null;
            string signedExpiry       = null;
            string signedResource     = null;
            string sigendPermissions  = null;
            string signedIdentifier   = null;
            string signedVersion      = null;
            string signedKey          = null;
            string cacheControl       = null;
            string contentType        = null;
            string contentEncoding    = null;
            string contentLanguage    = null;
            string contentDisposition = null;
            string tableName          = null;
            string startPk            = null;
            string startRk            = null;
            string endPk = null;
            string endRk = null;

            bool sasParameterFound = false;

            foreach (KeyValuePair <string, string> parameter in queryParameters)
            {
                switch (parameter.Key.ToLower())
                {
                case Constants.QueryConstants.SignedStart:
                    signedStart       = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.SignedExpiry:
                    signedExpiry      = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.SignedPermissions:
                    sigendPermissions = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.SignedResource:
                    signedResource    = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.SignedIdentifier:
                    signedIdentifier  = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.SignedKey:
                    signedKey         = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.Signature:
                    signature         = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.SignedVersion:
                    signedVersion     = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.CacheControl:
                    cacheControl      = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.ContentType:
                    contentType       = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.ContentEncoding:
                    contentEncoding   = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.ContentLanguage:
                    contentLanguage   = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.ContentDisposition:
                    contentDisposition = parameter.Value;
                    sasParameterFound  = true;
                    break;

                case Constants.QueryConstants.SasTableName:
                    tableName         = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.StartPartitionKey:
                    startPk           = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.StartRowKey:
                    startRk           = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.EndPartitionKey:
                    endPk             = parameter.Value;
                    sasParameterFound = true;
                    break;

                case Constants.QueryConstants.EndRowKey:
                    endRk             = parameter.Value;
                    sasParameterFound = true;
                    break;

                default:
                    break;
                }
            }

            if (sasParameterFound)
            {
                if (signature == null || (mandatorySignedResource && signedResource == null))
                {
                    string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.MissingMandatoryParametersForSAS);
                    throw new ArgumentException(errorMessage);
                }

                UriQueryBuilder builder = new UriQueryBuilder();
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, signedStart);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, signedExpiry);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, sigendPermissions);
                if (signedResource != null)
                {
                    builder.Add(Constants.QueryConstants.SignedResource, signedResource);
                }

                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIdentifier, signedIdentifier);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedVersion, signedVersion);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedKey, signedKey);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.CacheControl, cacheControl);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentType, contentType);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentEncoding, contentEncoding);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentLanguage, contentLanguage);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.ContentDisposition, contentDisposition);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SasTableName, tableName);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.StartPartitionKey, startPk);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.StartRowKey, startRk);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.EndPartitionKey, endPk);
                AddEscapedIfNotNull(builder, Constants.QueryConstants.EndRowKey, endRk);

                return(new StorageCredentials(builder.ToString()));
            }

            return(null);
        }
        public void UriQueryBuilder_B1()
        {
            var uri = UriQueryBuilder.AppendParameter("https://example.com/?p=1#test", "p", "2");

            Assert.AreEqual("https://example.com/?p=1&p=2#test", uri);
        }
        /// <summary>
        /// Get the complete query builder for creating the Shared Access Signature query.
        /// </summary>
        /// <param name="policy">The shared access policy to hash.</param>
        /// <param name="tableName">The name of the table associated with this shared access signature.</param>
        /// <param name="accessPolicyIdentifier">An optional identifier for the policy.</param>
        /// <param name="startPartitionKey">The start partition key, or <c>null</c>.</param>
        /// <param name="startRowKey">The start row key, or <c>null</c>.</param>
        /// <param name="endPartitionKey">The end partition key, or <c>null</c>.</param>
        /// <param name="endRowKey">The end row key, or <c>null</c>.</param>
        /// <param name="signature">The signature to use.</param>
        /// <param name="accountKeyName">The name of the key used to create the signature, or <c>null</c> if the key is implicit.</param>
        /// <param name="sasVersion">A string indicating the desired SAS version to use, in storage service version format.</param>
        /// <param name="protocols">The HTTP/HTTPS protocols for Account SAS.</param>
        /// <param name="ipAddressOrRange">The IP range for IPSAS.</param>
        /// <returns>The finished query builder.</returns>
        internal static UriQueryBuilder GetSignature(
            SharedAccessTablePolicy policy,
            string tableName,
            string accessPolicyIdentifier,
            string startPartitionKey,
            string startRowKey,
            string endPartitionKey,
            string endRowKey,
            string signature,
            string accountKeyName,
            string sasVersion,
            SharedAccessProtocol? protocols,
            IPAddressOrRange ipAddressOrRange)
        {          
            CommonUtility.AssertNotNull("signature", signature);

            UriQueryBuilder builder = new UriQueryBuilder();
 
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedVersion, sasVersion);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SasTableName, tableName);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.StartPartitionKey, startPartitionKey);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.StartRowKey, startRowKey);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.EndPartitionKey, endPartitionKey);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.EndRowKey, endRowKey);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIdentifier, accessPolicyIdentifier);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedKey, accountKeyName);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedProtocols, GetProtocolString(protocols));
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIP, ipAddressOrRange == null ? null : ipAddressOrRange.ToString());

            if (policy != null)
            {
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, GetDateTimeOrNull(policy.SharedAccessStartTime));
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, GetDateTimeOrNull(policy.SharedAccessExpiryTime));

                string permissions = SharedAccessTablePolicy.PermissionsToString(policy.Permissions);
                if (!string.IsNullOrEmpty(permissions))
                {
                    AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, permissions);
                }
            }

            return builder;
        }
 public void UriQueryBuilder_C1()
 {
     Assert.AreEqual("p=1", UriQueryBuilder.CombineWithUri("", "p=1"));
     Assert.AreEqual("p=1", UriQueryBuilder.CombineWithUri((string)null, "p=1"));
 }
        /// <summary>
        /// Parses the query.
        /// </summary>
        /// <param name="queryParameters">The query parameters.</param>
        internal static StorageCredentials ParseQuery(IDictionary<string, string> queryParameters)
        {
            bool sasParameterFound = false;
            List<string> removeList = new List<string>();

            foreach (KeyValuePair<string, string> parameter in queryParameters)
            {
                switch (parameter.Key.ToLower())
                {
                    case Constants.QueryConstants.Signature:
                        sasParameterFound = true;
                        break;

                    case Constants.QueryConstants.ResourceType:
                    case Constants.QueryConstants.Component:
                    case Constants.QueryConstants.Snapshot:
                    case Constants.QueryConstants.ApiVersion:
                        removeList.Add(parameter.Key);
                        break;

                    default:
                        break;
                }
            }

            foreach (string removeParam in removeList)
            {
                queryParameters.Remove(removeParam);
            }

            if (sasParameterFound)
            {
                UriQueryBuilder builder = new UriQueryBuilder();
                foreach (KeyValuePair<string, string> parameter in queryParameters)
                {
                    AddEscapedIfNotNull(builder, parameter.Key.ToLower(), parameter.Value);
                }

                return new StorageCredentials(builder.ToString());
            }

            return null;
        }
 public void UriQueryBuilder_C3()
 {
     Assert.AreEqual("", UriQueryBuilder.CombineWithUri("", null));
 }
 /// <summary>
 /// Escapes and adds the specified name/value pair to the query builder if it is not null.
 /// </summary>
 /// <param name="builder">The builder to add the value to.</param>
 /// <param name="name">The name of the pair.</param>
 /// <param name="value">The value to be escaped.</param>
 internal static void AddEscapedIfNotNull(UriQueryBuilder builder, string name, string value)
 {
     if (value != null)
     {
         builder.Add(name, value);
     }
 }
        public void UriQueryBuilder_A6()
        {
            var uqb = new UriQueryBuilder("?");

            Assert.AreEqual("", uqb.ToString());
        }
        /// <summary>
        /// Get the complete query builder for creating the Shared Access Signature query.
        /// </summary>
        /// <param name="policy">The shared access policy to hash.</param>
        /// <param name="accessPolicyIdentifier">An optional identifier for the policy.</param>
        /// <param name="signature">The signature to use.</param>
        /// <param name="accountKeyName">The name of the key used to create the signature, or <c>null</c> if the key is implicit.</param>
        /// <param name="sasVersion">A string indicating the desired SAS version to use, in storage service version format. Value must be <c>2012-02-12</c> or later.</param>
        /// <returns>The finished query builder.</returns>
        internal static UriQueryBuilder GetSignature(
            SharedAccessQueuePolicy policy,
            string accessPolicyIdentifier,
            string signature,
            string accountKeyName,
            string sasVersion)
        {
            CommonUtility.AssertNotNull("signature", signature);

            UriQueryBuilder builder = new UriQueryBuilder();

            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedVersion, sasVersion);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIdentifier, accessPolicyIdentifier);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedKey, accountKeyName);
            AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature);

            if (policy != null)
            {
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, GetDateTimeOrNull(policy.SharedAccessStartTime));
                AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, GetDateTimeOrNull(policy.SharedAccessExpiryTime));

                string permissions = SharedAccessQueuePolicy.PermissionsToString(policy.Permissions);
                if (!string.IsNullOrEmpty(permissions))
                {
                    AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, permissions);
                }
            }

            return builder;
        }
Esempio n. 40
0
 /// <summary>
 /// Constructs a web request to set the ACL for a table.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI for the table.</param>
 /// <param name="builder">A <see cref="UriQueryBuilder"/> object specifying additional parameters to add to the URI query string.</param>
 /// <param name="timeout">An integer specifying the server timeout interval.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest SetAcl(Uri uri, UriQueryBuilder builder, int?timeout, OperationContext operationContext)
 {
     return(TableHttpWebRequestFactory.SetAcl(uri, builder, timeout, true /* useVersionHeader */, operationContext));
 }