-
Notifications
You must be signed in to change notification settings - Fork 371
/
RequestResult.cs
353 lines (310 loc) · 14.8 KB
/
RequestResult.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// -----------------------------------------------------------------------------------------
// <copyright file="RequestResult.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
{
using Microsoft.Azure.Storage.Core;
using Microsoft.Azure.Storage.Core.Util;
using Microsoft.Azure.Storage.Shared.Protocol;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
/// <summary>
/// Represents the result of a physical request.
/// </summary>
#if WINDOWS_DESKTOP && !WINDOWS_PHONE
[Serializable]
#endif
public class RequestResult
{
private volatile Exception exception = null;
/// <summary>
/// Gets or sets the HTTP status code for the request.
/// </summary>
/// <value>The HTTP status code for the request.</value>
public int HttpStatusCode { get; set; }
/// <summary>
/// Gets the HTTP status message for the request.
/// </summary>
/// <value>The HTTP status message for the request.</value>
public string HttpStatusMessage { get; internal set; }
/// <summary>
/// Gets the service request ID for this request.
/// </summary>
/// <value>The service request ID for this request.</value>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "ID", Justification = "Back compatibility.")]
public string ServiceRequestID { get; internal set; }
/// <summary>
/// Gets the content-MD5 value for the request.
/// </summary>
/// <value>The content-MD5 value for the request.</value>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Md", Justification = "Back compatibility.")]
public string ContentMd5 { get; internal set; }
/// <summary>
/// Gets the content-CRC64 value for the request.
/// </summary>
/// <value>The content-CRC645 value for the request.</value>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Crc", Justification = "Back compatibility.")]
public string ContentCrc64 { get; internal set; }
/// <summary>
/// Gets the ETag value of the request.
/// </summary>
/// <value>The ETag value of the request.</value>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Etag", Justification = "Reviewed: Etag can be used for identifier names.")]
public string Etag { get; internal set; }
#if !(WINDOWS_RT || NETCORE)
/// <summary>
/// The number of bytes read from the response body for the given request
/// </summary>
public long IngressBytes { get; set; }
/// <summary>
/// The number of bytes written to the request body for a given request
/// </summary>
public long EgressBytes { get; set; }
#endif
/// <summary>
/// Gets the request date.
/// </summary>
/// <value>The request date.</value>
public string RequestDate { get; internal set; }
/// <summary>
/// Gets the location to which the request was sent.
/// </summary>
/// <value>A <see cref="StorageLocation"/> enumeration value.</value>
public StorageLocation TargetLocation { get; internal set; }
/// <summary>
/// Gets the extended error information.
/// </summary>
/// <value>A <see cref="StorageExtendedErrorInformation"/> object.</value>
public StorageExtendedErrorInformation ExtendedErrorInformation { get; internal set; }
/// <summary>
/// Gets the storage service error code.
/// </summary>
/// <value>A string containing the storage service error code.</value>
public string ErrorCode { get; internal set; }
/// <summary>
/// Gets whether or not the data for a write operation is encrypted server-side.
/// </summary>
public bool IsRequestServerEncrypted { get; internal set; }
/// <summary>
/// Represents whether or not the data for a read operation is encrypted on the server-side.
/// </summary>
public bool IsServiceEncrypted { get; internal set; }
/// <summary>
/// Represents the hash for the key used to server-side encrypt with client-provided keys.
/// </summary>
public string EncryptionKeySHA256 { get; internal set; }
/// <summary>
/// Represents encryption scope.
/// </summary>
public string EncryptionScope { get; internal set; }
/// <summary>
/// Gets or sets the exception.
/// </summary>
/// <value>An <see cref="System.Exception"/> object.</value>
public Exception Exception
{
get
{
return this.exception;
}
set
{
#if WINDOWS_RT || NETCORE
this.ExceptionInfo = (value != null) ? new ExceptionInfo(value) : null;
#endif
this.exception = value;
}
}
#if WINDOWS_RT || NETCORE
public DateTimeOffset StartTime { get; internal set; }
public DateTimeOffset EndTime { get; internal set; }
public ExceptionInfo ExceptionInfo { get; internal set; }
#else
/// <summary>
/// Gets the start time of the operation.
/// </summary>
/// <value>A <see cref="DateTime"/> value indicating the start time of the operation.</value>
public DateTime StartTime { get; internal set; }
/// <summary>
/// Gets the end time of the operation.
/// </summary>
/// <value>A <see cref="DateTime"/> value indicating the end time of the operation.</value>
public DateTime EndTime { get; internal set; }
#endif
/// <summary>
/// Translates the specified message into a <see cref="RequestResult"/> object.
/// </summary>
/// <param name="message">The message to translate.</param>
/// <returns>The translated <see cref="RequestResult"/>.</returns>
#if WINDOWS_DESKTOP
[Obsolete("This should be available only in Microsoft.Azure.Storage.WinMD and not in Microsoft.Azure.Storage.dll. Please use ReadXML to deserialize RequestResult when Microsoft.Azure.Storage.dll is used.")]
#endif
public static RequestResult TranslateFromExceptionMessage(string message)
{
RequestResult res = new RequestResult();
using (XmlReader reader = XmlReader.Create(
new StringReader(message),
new XmlReaderSettings
{
IgnoreWhitespace = true,
Async = true
}
))
{
res.ReadXmlAsync(reader).GetAwaiter().GetResult();
}
return res;
}
internal string WriteAsXml()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
try
{
using (XmlWriter writer = XmlWriter.Create(sb, settings))
{
this.WriteXml(writer);
}
return sb.ToString();
}
catch (XmlException)
{
return null;
}
}
#region XML Serializable
/// <summary>
/// Generates a serializable RequestResult from its XML representation.
/// </summary>
/// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the RequestResult is deserialized.</param>
#if WINDOWS_RT
internal
#else
public
#endif
async Task ReadXmlAsync(XmlReader reader)
{
CommonUtility.AssertNotNull("reader", reader);
await reader.ReadAsync().ConfigureAwait(false);
if (reader.NodeType == XmlNodeType.Comment)
{
await reader.ReadAsync().ConfigureAwait(false);
}
await reader.ReadStartElementAsync().ConfigureAwait(false);
this.HttpStatusCode = int.Parse(await CommonUtility.ReadElementAsStringAsync("HTTPStatusCode", reader).ConfigureAwait(false), CultureInfo.InvariantCulture);
this.HttpStatusMessage = await CommonUtility.ReadElementAsStringAsync("HttpStatusMessage", reader).ConfigureAwait(false);
StorageLocation targetLocation;
if (Enum.TryParse<StorageLocation>(await CommonUtility.ReadElementAsStringAsync("TargetLocation", reader).ConfigureAwait(false), out targetLocation))
{
this.TargetLocation = targetLocation;
}
this.ServiceRequestID = await CommonUtility.ReadElementAsStringAsync("ServiceRequestID", reader).ConfigureAwait(false);
this.ContentMd5 = await CommonUtility.ReadElementAsStringAsync("ContentMd5", reader).ConfigureAwait(false);
this.ContentCrc64 = await CommonUtility.ReadElementAsStringAsync("ContentCrc64", reader).ConfigureAwait(false);
this.Etag = await CommonUtility.ReadElementAsStringAsync("Etag", reader).ConfigureAwait(false);
this.RequestDate = await CommonUtility.ReadElementAsStringAsync("RequestDate", reader).ConfigureAwait(false);
try
{
this.ErrorCode = await CommonUtility.ReadElementAsStringAsync("ErrorCode", reader).ConfigureAwait(false);
}
catch (XmlException)
{
/* The ErrorCode property only exists after service version 07-17.
* If it is not present, we are reading an old version and can ignore this property.
*/
}
#if WINDOWS_RT || NETCORE
this.StartTime = DateTimeOffset.Parse(await CommonUtility.ReadElementAsStringAsync("StartTime", reader).ConfigureAwait(false), CultureInfo.InvariantCulture);
this.EndTime = DateTimeOffset.Parse(await CommonUtility.ReadElementAsStringAsync("EndTime", reader).ConfigureAwait(false), CultureInfo.InvariantCulture);
#else
this.StartTime = DateTime.Parse(await CommonUtility.ReadElementAsStringAsync("StartTime", reader).ConfigureAwait(false), CultureInfo.InvariantCulture);
this.EndTime = DateTime.Parse(await CommonUtility.ReadElementAsStringAsync("EndTime", reader).ConfigureAwait(false), CultureInfo.InvariantCulture);
#endif
this.ExtendedErrorInformation = new StorageExtendedErrorInformation();
await this.ExtendedErrorInformation.ReadXmlAsync(reader, CancellationToken.None).ConfigureAwait(false);
#if WINDOWS_RT || NETCORE
this.ExceptionInfo = await ExceptionInfo.ReadFromXMLReaderAsync(reader).ConfigureAwait(false);
#endif
// End request Result
await reader.ReadEndElementAsync().ConfigureAwait(false);
}
/// <summary>
/// Converts a serializable RequestResult into its XML representation.
/// </summary>
/// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the RequestResult is serialized.</param>
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "TranslateFromExceptionMessage", Justification = "TranslateFromException is a field name that when split could confuse the users")]
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "RequestResult", Justification = "RequestResult is a variable name which when split could confuse the users")]
[SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Xml.XmlWriter.WriteComment(System.String)", Justification = "Reviewed. Literals can be used in an internal method")]
#if WINDOWS_RT
internal
#else
public
#endif
void WriteXml(XmlWriter writer)
{
CommonUtility.AssertNotNull("writer", writer);
writer.WriteComment(SR.ExceptionOccurred);
writer.WriteStartElement("RequestResult");
writer.WriteElementString("HTTPStatusCode", Convert.ToString(this.HttpStatusCode, CultureInfo.InvariantCulture));
writer.WriteElementString("HttpStatusMessage", this.HttpStatusMessage);
writer.WriteElementString("TargetLocation", this.TargetLocation.ToString());
// Headers
writer.WriteElementString("ServiceRequestID", this.ServiceRequestID);
writer.WriteElementString("ContentMd5", this.ContentMd5);
writer.WriteElementString("ContentCrc64", this.ContentCrc64);
writer.WriteElementString("Etag", this.Etag);
writer.WriteElementString("RequestDate", this.RequestDate);
writer.WriteElementString("ErrorCode", this.ErrorCode);
// Dates - using RFC 1123 pattern
writer.WriteElementString("StartTime", this.StartTime.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture));
writer.WriteElementString("EndTime", this.EndTime.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture));
// Extended info
if (this.ExtendedErrorInformation != null)
{
this.ExtendedErrorInformation.WriteXml(writer);
}
else
{
// Write empty
writer.WriteStartElement(Constants.ErrorRootElement);
writer.WriteFullEndElement();
}
#if WINDOWS_RT || NETCORE
// Exception
if (this.ExceptionInfo != null)
{
this.ExceptionInfo.WriteXml(writer);
}
else
{
// Write empty
writer.WriteStartElement("ExceptionInfo");
writer.WriteFullEndElement();
}
#endif
// End RequestResult
writer.WriteEndElement();
}
#endregion
}
}