Esempio n. 1
0
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method);

            // Add the Content-* HTTP headers. Empty values are allowed.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement(HttpUtility.CombineHttpHeaderValues(request.Content.Headers.ContentEncoding));
                canonicalizedString.AppendCanonicalizedElement(HttpUtility.CombineHttpHeaderValues(request.Content.Headers.ContentLanguage));
                AuthenticationUtility.AppendCanonicalizedContentLengthHeader(canonicalizedString, request);
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? null :
                                                               Convert.ToBase64String(request.Content.Headers.ContentMD5));
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? null :
                                                               request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
                if (request.Method == HttpMethod.Put || request.Method == HttpMethod.Delete)
                {
                    canonicalizedString.AppendCanonicalizedElement("0");
                }
                else
                {
                    canonicalizedString.AppendCanonicalizedElement(null);
                }

                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
            }

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request);

            // Add If-* headers and Range header
            canonicalizedString.AppendCanonicalizedElement(AuthenticationUtility.GetCanonicalizedHeaderValue(request.Headers.IfModifiedSince));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetFirstHeaderValue(request.Headers.IfMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetFirstHeaderValue(request.Headers.IfNoneMatch));
            canonicalizedString.AppendCanonicalizedElement(AuthenticationUtility.GetCanonicalizedHeaderValue(request.Headers.IfUnmodifiedSince));
            canonicalizedString.AppendCanonicalizedElement((request.Headers.Range == null) ? null :
                                                           CommonUtils.GetFirstHeaderValue(request.Headers.Range.Ranges));

            // Add any custom headers
            AuthenticationUtility.AppendCanonicalizedCustomHeaders(canonicalizedString, request);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

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

            if (response.Content != null)
            {
                properties.LastModified = response.Content.Headers.LastModified;

                properties.ContentEncoding = HttpUtility.CombineHttpHeaderValues(response.Content.Headers.ContentEncoding);
                properties.ContentLanguage = HttpUtility.CombineHttpHeaderValues(response.Content.Headers.ContentLanguage);
                properties.ContentMD5      = (response.Content.Headers.ContentMD5 == null) ? null :
                                             Convert.ToBase64String(response.Content.Headers.ContentMD5);
                properties.ContentType = (response.Content.Headers.ContentType == null) ? null :
                                         response.Content.Headers.ContentType.ToString();
            }
            else
            {
                properties.LastModified    = null;
                properties.ContentEncoding = null;
                properties.ContentLanguage = null;
                properties.ContentMD5      = null;
                properties.ContentType     = null;
            }

            properties.CacheControl = (response.Headers.CacheControl == null) ? null :
                                      response.Headers.CacheControl.ToString();

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

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

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

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

            // Get the content length. Prioritize range and x-ms over content length for the special cases.
            string contentLengthHeader = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.ContentLengthHeader);

            if ((response.Content.Headers.ContentRange != null) &&
                response.Content.Headers.ContentRange.HasLength)
            {
                properties.Length = response.Content.Headers.ContentRange.Length.Value;
            }
            else if (!string.IsNullOrEmpty(contentLengthHeader))
            {
                properties.Length = long.Parse(contentLengthHeader);
            }
            else if (response.Content.Headers.ContentLength.HasValue)
            {
                properties.Length = response.Content.Headers.ContentLength.Value;
            }
            else
            {
                properties.Length = -1;
            }

            return(properties);
        }
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // The first element should be the Method of the request.
            // I.e. GET, POST, PUT, or HEAD.
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method);

            // The next elements are Content*
            // If any element is missing it may be empty.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement(HttpUtility.CombineHttpHeaderValues(request.Content.Headers.ContentEncoding));
                canonicalizedString.AppendCanonicalizedElement(HttpUtility.CombineHttpHeaderValues(request.Content.Headers.ContentLanguage));

                long contentLength = request.Content.Headers.ContentLength.HasValue ? request.Content.Headers.ContentLength.Value : -1;
                canonicalizedString.AppendCanonicalizedElement(contentLength == -1 ? string.Empty : contentLength.ToString());

                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? string.Empty :
                                                               Convert.ToBase64String(request.Content.Headers.ContentMD5));
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? string.Empty :
                                                               request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                if ((request.Method == HttpMethod.Put) ||
                    (request.Method == HttpMethod.Delete))
                {
                    canonicalizedString.AppendCanonicalizedElement("0");
                }
                else
                {
                    canonicalizedString.AppendCanonicalizedElement(string.Empty);
                }

                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
            }

            // If x-ms-date header exists, Date should be empty string
            string dateValue = HttpResponseMessageUtils.GetHeaderSingleValueOrDefault(request.Headers, Constants.HeaderConstants.Date);

            if (!string.IsNullOrEmpty(dateValue))
            {
                dateValue = string.Empty;
            }
            else
            {
                dateValue = CommonUtils.GetDateTimeValueOrDefault(request.Headers.Date);
            }

            canonicalizedString.AppendCanonicalizedElement(dateValue);

            // Conditionals and range
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetDateTimeValueOrDefault(request.Headers.IfModifiedSince));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetSingleValueOrDefault(request.Headers.IfMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetSingleValueOrDefault(request.Headers.IfNoneMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetDateTimeValueOrDefault(request.Headers.IfUnmodifiedSince));
            canonicalizedString.AppendCanonicalizedElement((request.Headers.Range == null) ? string.Empty :
                                                           CommonUtils.GetSingleValueOrDefault(request.Headers.Range.Ranges));

            // Rest of the headers
            CanonicalizationHelper.AddCanonicalizedHeaders(request, canonicalizedString);

            // Now we append the canonicalized resource element
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKey(request.RequestUri, accountName));

            return(canonicalizedString.Value);
        }