/// <summary>
        /// Creates the web request.
        /// </summary>
        /// <param name="uri">The request Uri.</param>
        /// <param name="timeout">The timeout.</param>
        /// <param name="builder">The builder.</param>
        /// <returns>A web request for performing the operation.</returns>
        internal static StorageRequestMessage CreateRequestMessage(HttpMethod method, Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
        {
            if (builder == null)
            {
                builder = new UriQueryBuilder();
            }

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

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

            Uri uriRequest = builder.AddToUri(uri);

            StorageRequestMessage msg = new StorageRequestMessage(method, uriRequest, canonicalizer, credentials, credentials.AccountName);
            msg.Content = content;

            return msg;
        }
        private Task<HttpResponseMessage> GetSharedKeyAuthenticationTask(StorageRequestMessage request, CancellationToken cancellationToken)
        {
            StorageRequestMessage storageRequest = request as StorageRequestMessage;

            ICanonicalizer canonicalizer = storageRequest.Canonicalizer;
            StorageCredentials credentials = storageRequest.Credentials;
            string accountName = storageRequest.AccountName;

            if (!request.Headers.Contains(Constants.HeaderConstants.Date))
            {
                string dateString = HttpWebUtility.ConvertDateTimeToHttpString(DateTimeOffset.UtcNow);
                request.Headers.Add(Constants.HeaderConstants.Date, dateString);
            }

            if (credentials.IsSharedKey)
            {
                StorageAccountKey accountKey = credentials.Key;
                if (!string.IsNullOrEmpty(accountKey.KeyName))
                {
                    request.Headers.Add(Constants.HeaderConstants.KeyNameHeader, accountKey.KeyName);
                }

                string message = canonicalizer.CanonicalizeHttpRequest(request, accountName);
                string signature = CryptoUtility.ComputeHmac256(accountKey.KeyValue, message);

                request.Headers.Authorization = new AuthenticationHeaderValue(
                    canonicalizer.AuthorizationScheme,
                    string.Format(CultureInfo.InvariantCulture, "{0}:{1}", credentials.AccountName, signature));
            }

            return base.SendAsync(request, cancellationToken);
        }
        private Func<StorageRequestMessage, CancellationToken, Task<HttpResponseMessage>> SelectAuthenticationTaskFactory(StorageRequestMessage request)
        {
            Func<StorageRequestMessage, CancellationToken, Task<HttpResponseMessage>> authenticationHandler;

            if (request.Credentials.IsSharedKey)
            {
                authenticationHandler = this.GetSharedKeyAuthenticationTask;
            }
            else
            {
                authenticationHandler = this.GetNoOpAuthenticationTask;
            }

            return authenticationHandler;
        }
        private Func<StorageRequestMessage, CancellationToken, Task<HttpResponseMessage>> SelectAuthenticationTaskFactory(StorageRequestMessage request)
        {
            Func<StorageRequestMessage, CancellationToken, Task<HttpResponseMessage>> authenticationHandler;

            if (request.Credentials.IsSharedKey)
            {
#if PORTABLE
                throw new NotSupportedException(SR.PortableDoesNotSupportSharedKey);
#else
                authenticationHandler = this.GetSharedKeyAuthenticationTask;
#endif
            }
            else
            {
                authenticationHandler = this.GetNoOpAuthenticationTask;
            }

            return authenticationHandler;
        }
        /// <summary>
        /// Adds the metadata.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="name">The metadata name.</param>
        /// <param name="value">The metadata value.</param>
        internal static void AddMetadata(StorageRequestMessage request, string name, string value)
        {
            CommonUtility.AssertNotNull("value", value);
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException(SR.ArgumentEmptyError, value);
            }

            request.Headers.Add("x-ms-meta-" + name, value);
        }
 /// <summary>
 /// Adds the metadata.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="metadata">The metadata.</param>
 internal static void AddMetadata(StorageRequestMessage request, IDictionary<string, string> metadata)
 {
     if (metadata != null)
     {
         foreach (KeyValuePair<string, string> entry in metadata)
         {
             AddMetadata(request, entry.Key, entry.Value);
         }
     }
 }
 /// <summary>
 /// Adds user-defined metadata to the request as a single name-value pair.
 /// </summary>
 /// <param name="request">The web request.</param>
 /// <param name="name">The metadata name.</param>
 /// <param name="value">The metadata value.</param>
 public static void AddMetadata(StorageRequestMessage request, string name, string value)
 {
     HttpRequestMessageFactory.AddMetadata(request, name, value);
 }
 /// <summary>
 /// Adds user-defined metadata to the request as one or more name-value pairs.
 /// </summary>
 /// <param name="request">The web request.</param>
 /// <param name="metadata">The user-defined metadata.</param>
 public static void AddMetadata(StorageRequestMessage request, IDictionary<string, string> metadata)
 {
     HttpRequestMessageFactory.AddMetadata(request, metadata);
 }
 /// <summary>
 /// Initializes a new instance of HttpRequestAdapterMessage.
 /// </summary>
 /// <param name="msg">The message to adapt.</param>
 /// <param name="bufferManager">The <see cref="IBufferManager"/> to use to acquire and return buffers for the stream. May be <c>null</c>.</param>
 /// <param name="bufferSize">The buffer size to use for each block. The default size is 64 KB. Note that this parameter is disregarded when an <see cref="IBufferManager"/> is specified.</param>
 public HttpRequestAdapterMessage(StorageRequestMessage msg, IBufferManager bufferManager, int bufferSize)
 {
     this.msg = msg;
     this.outStr = new MultiBufferMemoryStream(bufferManager, bufferSize);
     this.content = new StreamContent(this.outStr);
 }
 public void Dispose()
 {
     this.msg = null;
     this.outStr = null;
     this.content = null;
 }
        /// <summary>
        /// Adds the Range Header for File Service Operations.
        /// </summary>
        /// <param name="request">Request</param>
        /// <param name="offset">Starting byte of the range</param>
        /// <param name="count">Number of bytes in the range</param>
        private static void AddRange(StorageRequestMessage request, long? offset, long? count)
        {
            if (count.HasValue)
            {
                CommonUtility.AssertNotNull("offset", offset);
                CommonUtility.AssertInBounds("count", count.Value, 1, long.MaxValue);
            }

            if (offset.HasValue)
            {
                string rangeStart = offset.ToString();
                string rangeEnd = string.Empty;
                if (count.HasValue)
                {
                    rangeEnd = (offset + count.Value - 1).ToString();
                }

                string rangeHeaderValue = string.Format(CultureInfo.InvariantCulture, Constants.HeaderConstants.RangeHeaderFormat, rangeStart, rangeEnd);
                request.Headers.Add(Constants.HeaderConstants.RangeHeader, rangeHeaderValue);
            }
        }
 private Task<HttpResponseMessage> GetNoOpAuthenticationTask(StorageRequestMessage request, CancellationToken cancellationToken)
 {
     return base.SendAsync(request, cancellationToken);
 }
 /// <summary>
 /// Adds a lease action to a request.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="leaseAction">The lease action.</param>
 internal static void AddLeaseAction(StorageRequestMessage request, LeaseAction leaseAction)
 {
     request.Headers.Add(Constants.HeaderConstants.LeaseActionHeader, leaseAction.ToString().ToLower());
 }
 /// <summary>
 /// Adds a lease break period to a request.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="leaseBreakPeriod">The lease break period.</param>
 internal static void AddLeaseBreakPeriod(StorageRequestMessage request, int? leaseBreakPeriod)
 {
     request.AddOptionalHeader(Constants.HeaderConstants.LeaseBreakPeriodHeader, leaseBreakPeriod);
 }
 /// <summary>
 /// Adds a lease duration to a request.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="leaseDuration">The lease duration.</param>
 internal static void AddLeaseDuration(StorageRequestMessage request, int? leaseDuration)
 {
     request.AddOptionalHeader(Constants.HeaderConstants.LeaseDurationHeader, leaseDuration);
 }
 /// <summary>
 /// Adds a proposed lease id to a request.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="proposedLeaseId">The proposed lease id.</param>
 internal static void AddProposedLeaseId(StorageRequestMessage request, string proposedLeaseId)
 {
     request.AddOptionalHeader(Constants.HeaderConstants.ProposedLeaseIdHeader, proposedLeaseId);
 }