This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
/
HttpRequestMessageFactory.cs
325 lines (286 loc) · 15.8 KB
/
HttpRequestMessageFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// -----------------------------------------------------------------------------------------
// <copyright file="HttpRequestMessageFactory.cs" company="Microsoft">
// Copyright 2013 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// -----------------------------------------------------------------------------------------
namespace Microsoft.Azure.Storage.Shared.Protocol
{
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Core;
using Microsoft.Azure.Storage.Core.Auth;
using Microsoft.Azure.Storage.Core.Util;
using System;
using System.Collections.Generic;
using System.Net.Http;
internal static class HttpRequestMessageFactory
{
/// <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;
}
/// <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>
/// <returns>A web request for performing the operation.</returns>
internal static StorageRequestMessage Create(Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Constructs a web request to return the ACL for a cloud resource.
/// </summary>
/// <param name="uri">The absolute URI to the resource.</param>
/// <param name="timeout">The server timeout interval.</param>
/// <param name="builder">An optional query builder to use.</param>
/// <returns><returns>A web request to use to perform the operation.</returns></returns>
internal static StorageRequestMessage GetAcl(Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
if (builder == null)
{
builder = new UriQueryBuilder();
}
builder.Add(Constants.QueryConstants.Component, "acl");
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Get, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
#if WINDOWS_PHONE
// Windows phone adds */* as the Accept type when we don't set one explicitly.
request.Headers.Add(Constants.HeaderConstants.PayloadAcceptHeader, Constants.XMLAcceptHeaderValue);
#endif
return request;
}
/// <summary>
/// Constructs a web request to set the ACL for a cloud resource.
/// </summary>
/// <param name="uri">The absolute URI to the resource.</param>
/// <param name="timeout">The server timeout interval.</param>
/// <param name="builder">An optional query builder to use.</param>
/// <returns><returns>A web request to use to perform the operation.</returns></returns>
internal static StorageRequestMessage SetAcl(Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
if (builder == null)
{
builder = new UriQueryBuilder();
}
builder.Add(Constants.QueryConstants.Component, "acl");
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
#if WINDOWS_PHONE
// Windows phone adds */* as the Accept type when we don't set one explicitly.
request.Headers.Add(Constants.HeaderConstants.PayloadAcceptHeader, Constants.XMLAcceptHeaderValue);
#endif
return request;
}
/// <summary>
/// Gets the properties.
/// </summary>
/// <param name="uri">The Uri to query.</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 GetProperties(Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Head, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Gets the metadata.
/// </summary>
/// <param name="uri">The blob 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 GetMetadata(Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
if (builder == null)
{
builder = new UriQueryBuilder();
}
builder.Add(Constants.QueryConstants.Component, "metadata");
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Head, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Sets the metadata.
/// </summary>
/// <param name="uri">The blob 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 SetMetadata(Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
if (builder == null)
{
builder = new UriQueryBuilder();
}
builder.Add(Constants.QueryConstants.Component, "metadata");
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
return request;
}
/// <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 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>
/// Deletes the specified Uri.
/// </summary>
/// <param name="uri">The Uri to delete.</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 Delete(Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Delete, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Undeletes 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>
/// <returns>A web request for performing the operation.</returns>
internal static StorageRequestMessage Undelete(Uri uri, int? timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
if (builder == null)
{
builder = new UriQueryBuilder();
}
builder.Add(Constants.QueryConstants.Component, "undelete");
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Creates a web request to get the properties of the account.
/// </summary>
/// <param name="uri">The absolute URI to the service.</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.</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 to get the service properties.
/// </returns>
internal static StorageRequestMessage GetAccountProperties(Uri uri, UriQueryBuilder builder, int? timeout, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
if (builder == null)
{
builder = new UriQueryBuilder();
}
builder.Add(Constants.QueryConstants.Component, "properties");
builder.Add(Constants.QueryConstants.ResourceType, "account");
StorageRequestMessage request = CreateRequestMessage(HttpMethod.Head, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Creates a web request to get the properties of the service.
/// </summary>
/// <param name="uri">The absolute URI to the service.</param>
/// <param name="timeout">The server timeout interval.</param>
/// <returns>A web request to get the service properties.</returns>
internal static StorageRequestMessage GetServiceProperties(Uri uri, int? timeout, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
UriQueryBuilder builder = GetServiceUriQueryBuilder();
builder.Add(Constants.QueryConstants.Component, "properties");
StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Get, uri, timeout, builder, null /* content */, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Creates a web request to set the properties of the service.
/// </summary>
/// <param name="uri">The absolute URI to the service.</param>
/// <param name="timeout">The server timeout interval.</param>
/// <returns>A web request to set the service properties.</returns>
internal static StorageRequestMessage SetServiceProperties(Uri uri, int? timeout, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
UriQueryBuilder builder = GetServiceUriQueryBuilder();
builder.Add(Constants.QueryConstants.Component, "properties");
StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Creates a web request to get the stats of the service.
/// </summary>
/// <param name="uri">The absolute URI to the service.</param>
/// <param name="timeout">The server timeout interval.</param>
/// <returns>A web request to get the service stats.</returns>
internal static StorageRequestMessage GetServiceStats(Uri uri, int? timeout, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
{
UriQueryBuilder builder = GetServiceUriQueryBuilder();
builder.Add(Constants.QueryConstants.Component, "stats");
StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Get, uri, timeout, builder, null /* content */, operationContext, canonicalizer, credentials);
return request;
}
/// <summary>
/// Generates a query builder for building service requests.
/// </summary>
/// <returns>A <see cref="UriQueryBuilder"/> for building service requests.</returns>
internal static UriQueryBuilder GetServiceUriQueryBuilder()
{
UriQueryBuilder uriBuilder = new UriQueryBuilder();
uriBuilder.Add(Constants.QueryConstants.ResourceType, "service");
return uriBuilder;
}
}
}