Beispiel #1
0
        internal static StorageRequestMessage BuildStorageRequestMessageForTableServiceProperties(Uri uri, UriQueryBuilder builder, int?timeout, HttpMethod httpMethod, ServiceProperties properties, ICanonicalizer canonicalizer, StorageCredentials cred, OperationContext ctx, TableRequestOptions options)
        {
            if (builder == null)
            {
                builder = new UriQueryBuilder();
            }
            builder.Add("comp", "properties");
            builder.Add("restype", "service");
            if (timeout.HasValue && timeout.Value > 0)
            {
                builder.Add("timeout", timeout.Value.ToString(CultureInfo.InvariantCulture));
            }
            StorageRequestMessage storageRequestMessage = new StorageRequestMessage(httpMethod, builder.AddToUri(uri), canonicalizer, cred, cred.AccountName);

            if (httpMethod.Equals(HttpMethod.Put))
            {
                MultiBufferMemoryStream multiBufferMemoryStream = new MultiBufferMemoryStream(1024);
                try
                {
                    properties.WriteServiceProperties(multiBufferMemoryStream);
                }
                catch (InvalidOperationException ex)
                {
                    multiBufferMemoryStream.Dispose();
                    throw new ArgumentException(ex.Message, "properties");
                }
                multiBufferMemoryStream.Seek(0L, SeekOrigin.Begin);
                storageRequestMessage.Content = new StreamContent(multiBufferMemoryStream);
            }
            return(storageRequestMessage);
        }
Beispiel #2
0
        internal static StorageRequestMessage BuildStorageRequestMessageForTableOperation(Uri uri, TableOperation operation, ICanonicalizer canonicalizer, StorageCredentials cred, OperationContext ctx, TableRequestOptions options)
        {
            HttpMethod            httpMethod            = RESTCommandGeneratorUtils.ExtractHttpMethod(operation);
            StorageRequestMessage storageRequestMessage = new StorageRequestMessage(httpMethod, uri, canonicalizer, cred, cred.AccountName);

            storageRequestMessage.Headers.AcceptCharset.ParseAdd("UTF-8");
            storageRequestMessage.Headers.Add("MaxDataServiceVersion", "3.0;NetFx");
            TablePayloadFormat value = options.PayloadFormat.Value;

            Logger.LogInformational(ctx, "Setting payload format for the request to '{0}'.", value);
            SetAcceptHeaderValueForStorageRequestMessage(storageRequestMessage, value);
            storageRequestMessage.Headers.Add("DataServiceVersion", "3.0;");
            if (operation.OperationType == TableOperationType.InsertOrMerge || operation.OperationType == TableOperationType.Merge)
            {
                storageRequestMessage.Headers.Add("X-HTTP-Method", "MERGE");
            }
            if ((operation.OperationType == TableOperationType.Delete || operation.OperationType == TableOperationType.Replace || operation.OperationType == TableOperationType.Merge) && operation.ETag != null)
            {
                storageRequestMessage.Headers.TryAddWithoutValidation("If-Match", operation.ETag);
            }
            if (operation.OperationType == TableOperationType.Insert)
            {
                storageRequestMessage.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)
            {
                MultiBufferMemoryStream multiBufferMemoryStream = new MultiBufferMemoryStream();
                using (JsonTextWriter jsonWriter = new JsonTextWriter(new StreamWriter(new NonCloseableStream(multiBufferMemoryStream))))
                {
                    WriteEntityContent(operation, ctx, options, jsonWriter);
                }
                multiBufferMemoryStream.Seek(0L, SeekOrigin.Begin);
                storageRequestMessage.Content = new StreamContent(multiBufferMemoryStream);
            }
            if (httpMethod != HttpMethod.Head && httpMethod != HttpMethod.Get && storageRequestMessage.Content != null)
            {
                storageRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }
            return(storageRequestMessage);
        }
Beispiel #3
0
        internal static StorageRequestMessage BuildStorageRequestMessageForTablePermissions(Uri uri, UriQueryBuilder builder, int?timeout, HttpMethod httpMethod, TablePermissions permissions, ICanonicalizer canonicalizer, StorageCredentials cred, OperationContext ctx, TableRequestOptions options)
        {
            if (builder == null)
            {
                builder = new UriQueryBuilder();
            }
            builder.Add("comp", "acl");
            if (timeout.HasValue && timeout.Value > 0)
            {
                builder.Add("timeout", timeout.Value.ToString(CultureInfo.InvariantCulture));
            }
            StorageRequestMessage storageRequestMessage = new StorageRequestMessage(httpMethod, builder.AddToUri(uri), canonicalizer, cred, cred.AccountName);

            if (httpMethod.Equals(HttpMethod.Put))
            {
                MultiBufferMemoryStream multiBufferMemoryStream = new MultiBufferMemoryStream(1024);
                WriteSharedAccessIdentifiers(permissions.SharedAccessPolicies, multiBufferMemoryStream);
                multiBufferMemoryStream.Seek(0L, SeekOrigin.Begin);
                storageRequestMessage.Content = new StreamContent(multiBufferMemoryStream);
            }
            return(storageRequestMessage);
        }
Beispiel #4
0
        internal static StorageRequestMessage BuildStorageRequestMessageForTableBatchOperation(Uri uri, TableBatchOperation batch, ICanonicalizer canonicalizer, string tableName, StorageCredentials cred, OperationContext ctx, TableRequestOptions options)
        {
            StorageRequestMessage storageRequestMessage = new StorageRequestMessage(HttpMethod.Post, NavigationHelper.AppendPathToSingleUri(uri, "$batch"), canonicalizer, cred, cred.AccountName);

            storageRequestMessage.Headers.AcceptCharset.ParseAdd("UTF-8");
            storageRequestMessage.Headers.Add("MaxDataServiceVersion", "3.0;NetFx");
            TablePayloadFormat value = options.PayloadFormat.Value;

            Logger.LogInformational(ctx, "Setting payload format for the request to '{0}'.", value);
            SetAcceptHeaderValueForStorageRequestMessage(storageRequestMessage, value);
            storageRequestMessage.Headers.Add("DataServiceVersion", "3.0;");
            MultiBufferMemoryStream multiBufferMemoryStream = new MultiBufferMemoryStream();
            string str = Guid.NewGuid().ToString();

            using (StreamWriter streamWriter = new StreamWriter(new NonCloseableStream(multiBufferMemoryStream)))
            {
                string str2  = Guid.NewGuid().ToString();
                string text  = "--batch_" + str;
                string text2 = "--changeset_" + str2;
                string text3 = "Accept: ";
                switch (value)
                {
                case TablePayloadFormat.Json:
                    text3 += "application/json;odata=minimalmetadata";
                    break;

                case TablePayloadFormat.JsonFullMetadata:
                    text3 += "application/json;odata=fullmetadata";
                    break;

                case TablePayloadFormat.JsonNoMetadata:
                    text3 += "application/json;odata=nometadata";
                    break;
                }
                streamWriter.WriteLine(text);
                bool flag = batch.Count == 1 && batch[0].OperationType == TableOperationType.Retrieve;
                if (!flag)
                {
                    streamWriter.WriteLine("Content-Type: multipart/mixed; boundary=changeset_" + str2);
                    streamWriter.WriteLine();
                }
                foreach (TableOperation item in batch)
                {
                    HttpMethod httpMethod = RESTCommandGeneratorUtils.ExtractHttpMethod(item);
                    if (item.OperationType == TableOperationType.Merge || item.OperationType == TableOperationType.InsertOrMerge)
                    {
                        httpMethod = new HttpMethod("MERGE");
                    }
                    if (!flag)
                    {
                        streamWriter.WriteLine(text2);
                    }
                    streamWriter.WriteLine("Content-Type: application/http");
                    streamWriter.WriteLine("Content-Transfer-Encoding: binary");
                    streamWriter.WriteLine();
                    string text4 = Uri.EscapeUriString(RESTCommandGeneratorUtils.GenerateRequestURI(item, uri, tableName).ToString());
                    text4 = text4.Replace("%25", "%");
                    streamWriter.WriteLine(httpMethod + " " + text4 + " HTTP/1.1");
                    streamWriter.WriteLine(text3);
                    streamWriter.WriteLine("Content-Type: application/json");
                    if (item.OperationType == TableOperationType.Insert)
                    {
                        streamWriter.WriteLine("Prefer: " + (item.EchoContent ? "return-content" : "return-no-content"));
                    }
                    streamWriter.WriteLine("DataServiceVersion: 3.0;");
                    if (item.OperationType == TableOperationType.Delete || item.OperationType == TableOperationType.Replace || item.OperationType == TableOperationType.Merge)
                    {
                        streamWriter.WriteLine("If-Match: " + item.ETag);
                    }
                    streamWriter.WriteLine();
                    if (item.OperationType != TableOperationType.Delete && item.OperationType != TableOperationType.Retrieve)
                    {
                        using (JsonTextWriter jsonTextWriter = new JsonTextWriter(streamWriter))
                        {
                            jsonTextWriter.CloseOutput = false;
                            WriteEntityContent(item, ctx, options, jsonTextWriter);
                        }
                        streamWriter.WriteLine();
                    }
                }
                if (!flag)
                {
                    streamWriter.WriteLine(text2 + "--");
                }
                streamWriter.WriteLine(text + "--");
            }
            multiBufferMemoryStream.Seek(0L, SeekOrigin.Begin);
            storageRequestMessage.Content = new StreamContent(multiBufferMemoryStream);
            storageRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("multipart/mixed");
            storageRequestMessage.Content.Headers.ContentType.Parameters.Add(NameValueHeaderValue.Parse("boundary=batch_" + str));
            return(storageRequestMessage);
        }