PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/WCFWebApi/src/Microsoft.Net.Http.Formatting/System/Net/Http/FormattingUtilities.cs

#
C# | 201 lines | 98 code | 26 blank | 77 comment | 9 complexity | 5e6d702b3eb0066f87277342cdfc81da MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace System.Net.Http
  5. {
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Json;
  9. using System.Linq;
  10. using System.Net.Http.Formatting;
  11. using System.Net.Http.Headers;
  12. using System.Text;
  13. /// <summary>
  14. /// Provides various internal utility functions
  15. /// </summary>
  16. internal static class FormattingUtilities
  17. {
  18. /// <summary>
  19. /// A <see cref="Type"/> representing <see cref="UTF8Encoding"/>.
  20. /// </summary>
  21. public static readonly Type Utf8EncodingType = typeof(UTF8Encoding);
  22. /// <summary>
  23. /// A <see cref="Type"/> representing <see cref="UnicodeEncoding"/>.
  24. /// </summary>
  25. public static readonly Type Utf16EncodingType = typeof(UnicodeEncoding);
  26. /// <summary>
  27. /// A <see cref="Type"/> representing <see cref="HttpRequestMessage"/>.
  28. /// </summary>
  29. public static readonly Type HttpRequestMessageType = typeof(HttpRequestMessage);
  30. /// <summary>
  31. /// A <see cref="Type"/> representing <see cref="HttpResponseMessage"/>.
  32. /// </summary>
  33. public static readonly Type HttpResponseMessageType = typeof(HttpResponseMessage);
  34. /// <summary>
  35. /// A <see cref="Type"/> representing <see cref="HttpContent"/>.
  36. /// </summary>
  37. public static readonly Type HttpContentType = typeof(HttpContent);
  38. /// <summary>
  39. /// A <see cref="Type"/> representing <see cref="DelegatingEnumerable{T}"/>.
  40. /// </summary>
  41. public static readonly Type DelegatingEnumerableGenericType = typeof(DelegatingEnumerable<>);
  42. /// <summary>
  43. /// A <see cref="Type"/> representing <see cref="IEnumerable{T}"/>.
  44. /// </summary>
  45. public static readonly Type EnumerableInterfaceGenericType = typeof(IEnumerable<>);
  46. /// <summary>
  47. /// A <see cref="Type"/> representing <see cref="IQueryable{T}"/>.
  48. /// </summary>
  49. public static readonly Type QueryableInterfaceGenericType = typeof(IQueryable<>);
  50. /// <summary>
  51. /// A <see cref="Type"/> representing <see cref="JsonValue"/>.
  52. /// </summary>
  53. public static readonly Type JsonValueType = typeof(JsonValue);
  54. /// <summary>
  55. /// HTTP X-Requested-With header field name
  56. /// </summary>
  57. public const string HttpRequestedWithHeader = @"x-requested-with";
  58. /// <summary>
  59. /// HTTP X-Requested-With header field value
  60. /// </summary>
  61. public const string HttpRequestedWithHeaderValue = @"xmlhttprequest";
  62. /// <summary>
  63. /// JSON literal for 'null'
  64. /// </summary>
  65. public const string JsonNullLiteral = "null";
  66. /// <summary>
  67. /// HTTP Host header field name
  68. /// </summary>
  69. public const string HttpHostHeader = "Host";
  70. /// <summary>
  71. /// HTTP Version token
  72. /// </summary>
  73. public const string HttpVersionToken = "HTTP";
  74. // This list should be kept in sync with the list of headers supported by HttpContentHeaders
  75. // TODO: CSDMAIN 231195 -- Change hard-coded list of HttpContentHeaders to dynamic list provided by DCR #225156
  76. private static readonly HashSet<string> httpContentHeaders = new HashSet<string>()
  77. {
  78. "Allow",
  79. "Content-Disposition",
  80. "Content-Encoding",
  81. "Content-Language",
  82. "Content-Length",
  83. "Content-Location",
  84. "Content-MD5",
  85. "Content-Range",
  86. "Content-Type",
  87. "Expires",
  88. "Last-Modified",
  89. };
  90. /// <summary>
  91. /// Gets the HTTP headers that are associated with <see cref="HttpContentHeaders"/>.
  92. /// </summary>
  93. public static HashSet<string> HttpContentHeaders
  94. {
  95. get
  96. {
  97. return FormattingUtilities.httpContentHeaders;
  98. }
  99. }
  100. /// <summary>
  101. /// Determines whether <paramref name="type"/> is a <see cref="JsonValue"/> type.
  102. /// </summary>
  103. /// <param name="type">The type to test.</param>
  104. /// <returns>
  105. /// <c>true</c> if <paramref name="type"/> is a <see cref="JsonValue"/> type; otherwise, <c>false</c>.
  106. /// </returns>
  107. public static bool IsJsonValueType(Type type)
  108. {
  109. return JsonValueType.IsAssignableFrom(type);
  110. }
  111. /// <summary>
  112. /// Creates an empty <see cref="HttpContentHeaders"/> instance. The only way is to get it from a dummy
  113. /// <see cref="HttpContent"/> instance.
  114. /// </summary>
  115. /// <returns>The created instance.</returns>
  116. public static HttpContentHeaders CreateEmptyContentHeaders()
  117. {
  118. HttpContent tempContent = null;
  119. HttpContentHeaders contentHeaders = null;
  120. try
  121. {
  122. tempContent = new StringContent(string.Empty);
  123. contentHeaders = tempContent.Headers;
  124. contentHeaders.Clear();
  125. }
  126. finally
  127. {
  128. // We can dispose the content without touching the headers
  129. if (tempContent != null)
  130. {
  131. tempContent.Dispose();
  132. }
  133. }
  134. return contentHeaders;
  135. }
  136. /// <summary>
  137. /// Ensure the actual collection is identical to the expected one
  138. /// </summary>
  139. /// <param name="actual">The actual collection of the instance</param>
  140. /// <param name="expected">The expected collection of the instance</param>
  141. /// <returns>Returns true if they are identical</returns>
  142. public static bool ValidateCollection(Collection<MediaTypeHeaderValue> actual, MediaTypeHeaderValue[] expected)
  143. {
  144. if (actual.Count != expected.Length)
  145. {
  146. return false;
  147. }
  148. foreach (MediaTypeHeaderValue value in expected)
  149. {
  150. if (!actual.Contains(value))
  151. {
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. /// <summary>
  158. /// Remove bounding quotes on a token if present
  159. /// </summary>
  160. /// <param name="token">Token to unquote.</param>
  161. /// <returns>Unquoted token.</returns>
  162. public static string UnquoteToken(string token)
  163. {
  164. if (string.IsNullOrWhiteSpace(token))
  165. {
  166. return token;
  167. }
  168. if (token.StartsWith("\"", StringComparison.Ordinal) && token.EndsWith("\"", StringComparison.Ordinal) && token.Length > 1)
  169. {
  170. return token.Substring(1, token.Length - 2);
  171. }
  172. return token;
  173. }
  174. }
  175. }