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