PageRenderTime 38ms CodeModel.GetById 26ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/WCFWebApi/src/Microsoft.ApplicationServer.Http/Microsoft/ApplicationServer/Http/HttpTypeHelper.cs

#
C# | 342 lines | 269 code | 64 blank | 9 comment | 59 complexity | a4ede4b06bdbe7ef7566bbaa8194681e MD5 | raw file
  1// <copyright>
  2//   Copyright (c) Microsoft Corporation.  All rights reserved.
  3// </copyright>
  4
  5namespace Microsoft.ApplicationServer.Http
  6{
  7    using System;
  8    using System.Json;
  9    using System.Net;
 10    using System.Net.Http;
 11    using System.Net.Http.Headers;
 12    using Microsoft.Server.Common;
 13
 14    /// <summary>
 15    /// A static class that provides HTTP related types and functionality
 16    /// around checking types against the set of HTTP related types.
 17    /// </summary>
 18    internal static class HttpTypeHelper 
 19    {
 20        internal static readonly Type JsonValueType = typeof(JsonValue);
 21        internal static readonly Type HttpRequestMessageType = typeof(HttpRequestMessage);
 22        internal static readonly Type HttpRequestHeadersType = typeof(HttpRequestHeaders);
 23        internal static readonly Type UriType = typeof(Uri);
 24        internal static readonly Type HttpMethodType = typeof(HttpMethod);
 25        internal static readonly Type HttpContentType = typeof(HttpContent);
 26        internal static readonly Type HttpResponseMessageType = typeof(HttpResponseMessage);
 27        internal static readonly Type HttpResponseHeadersType = typeof(HttpResponseHeaders);
 28        internal static readonly Type HttpStatusCodeType = typeof(HttpStatusCode);
 29        internal static readonly Type HttpRequestMessageGenericType = typeof(HttpRequestMessage<>);
 30        internal static readonly Type HttpResponseMessageGenericType = typeof(HttpResponseMessage<>);
 31        internal static readonly Type ObjectContentGenericType = typeof(ObjectContent<>);
 32
 33        internal static bool IsHttpContent(Type type)
 34        {
 35            Fx.Assert(type != null, "The 'type' parameter should not be null.");
 36
 37            return HttpContentType.IsAssignableFrom(type);
 38        }
 39
 40        internal static bool IsHttpResponse(Type type)
 41        {
 42            Fx.Assert(type != null, "The 'type' parameter should not be null.");
 43
 44            return HttpResponseMessageType.IsAssignableFrom(type);
 45        }
 46
 47        internal static bool IsHttpRequest(Type type)
 48        {
 49            Fx.Assert(type != null, "The 'type' parameter should not be null.");
 50
 51            return HttpRequestMessageType.IsAssignableFrom(type);
 52        }
 53
 54        internal static bool IsHttpResponseOrContent(Type type)
 55        {
 56            Fx.Assert(type != null, "The 'type' parameter should not be null.");
 57
 58            return HttpContentType.IsAssignableFrom(type) || 
 59                   HttpResponseMessageType.IsAssignableFrom(type);
 60        }
 61
 62        internal static bool IsHttpRequestOrContent(Type type)
 63        {
 64            Fx.Assert(type != null, "The 'type' parameter should not be null.");
 65
 66            return HttpContentType.IsAssignableFrom(type) || 
 67                   HttpRequestMessageType.IsAssignableFrom(type);
 68        }
 69
 70        internal static bool IsHttp(Type type)
 71        {
 72            Fx.Assert(type != null, "The 'type' parameter should not be null.");
 73
 74            return HttpContentType.IsAssignableFrom(type) || 
 75                   HttpRequestMessageType.IsAssignableFrom(type) || 
 76                   HttpResponseMessageType.IsAssignableFrom(type);
 77        }
 78
 79        internal static bool IsHttpContentGenericTypeDefinition(Type type)
 80        {
 81            Fx.Assert(type != null, "The 'type' parameter should not be null.");
 82
 83            if (type.IsGenericTypeDefinition &&
 84                ObjectContentGenericType.IsAssignableFrom(type))
 85            {
 86                return true;
 87            }
 88
 89            return false;
 90        }
 91
 92        internal static bool IsHttpRequestGenericTypeDefinition(Type type)
 93        {
 94            Fx.Assert(type != null, "The 'type' parameter should not be null.");
 95
 96            if (type.IsGenericTypeDefinition &&
 97                HttpRequestMessageGenericType.IsAssignableFrom(type))
 98            {
 99                return true;
100            }
101
102            return false;
103        }
104
105        internal static bool IsHttpResponseGenericTypeDefinition(Type type)
106        {
107            Fx.Assert(type != null, "The 'type' parameter should not be null.");
108
109            if (type.IsGenericTypeDefinition &&
110                HttpResponseMessageGenericType.IsAssignableFrom(type))
111            {
112                return true;
113            }
114
115            return false;
116        }
117
118        internal static bool IsHttpRequestOrContentGenericTypeDefinition(Type type)
119        {
120            Fx.Assert(type != null, "The 'type' parameter should not be null.");
121
122            if (type.IsGenericTypeDefinition)
123            {
124                if (HttpRequestMessageGenericType.IsAssignableFrom(type) ||
125                    ObjectContentGenericType.IsAssignableFrom(type))
126                {
127                    return true;
128                }
129            }
130
131            return false;
132        }
133
134        internal static bool IsHttpResponseOrContentGenericTypeDefinition(Type type)
135        {
136            Fx.Assert(type != null, "The 'type' parameter should not be null.");
137
138            if (type.IsGenericTypeDefinition)
139            {
140                if (HttpResponseMessageGenericType.IsAssignableFrom(type) ||
141                    ObjectContentGenericType.IsAssignableFrom(type))
142                {
143                    return true;
144                }
145            }
146
147            return false;
148        }
149
150        internal static bool IsHttpGenericTypeDefinition(Type type)
151        {
152            Fx.Assert(type != null, "The 'type' parameter should not be null.");
153
154            if (type.IsGenericTypeDefinition)
155            {
156                if (HttpRequestMessageGenericType.IsAssignableFrom(type) ||
157                    HttpResponseMessageGenericType.IsAssignableFrom(type) ||
158                    ObjectContentGenericType.IsAssignableFrom(type))
159                {
160                    return true;
161                }
162            }
163
164            return false;
165        }
166
167        internal static bool IsJsonValue(Type type)
168        {
169            Fx.Assert(type != null, "The 'type' parameter should not be null.");
170
171            return JsonValueType.IsAssignableFrom(type);
172        }
173
174        internal static Type GetHttpContentInnerTypeOrNull(Type type)
175        {
176            Fx.Assert(type != null, "The 'type' parameter should not be null.");
177
178            if (type.IsGenericType && !type.IsGenericTypeDefinition)
179            {
180                Type genericTypeDefinition = type.GetGenericTypeDefinition();
181                if (IsHttpContentGenericTypeDefinition(genericTypeDefinition))
182                {
183                    Type[] typeArgs = type.GetGenericArguments();
184                    if (typeArgs.Length > 1)
185                    {
186                        throw Fx.Exception.AsError(
187                            new InvalidOperationException(
188                                SR.MultipleTypeParametersForHttpContentType(type.Name)));
189                    }
190
191                    return typeArgs[0];
192                }
193            }
194
195            return null;
196        }
197
198        internal static Type GetHttpRequestInnerTypeOrNull(Type type)
199        {
200            Fx.Assert(type != null, "The 'type' parameter should not be null.");
201
202            if (type.IsGenericType && !type.IsGenericTypeDefinition)
203            {
204                Type genericTypeDefinition = type.GetGenericTypeDefinition();
205                if (IsHttpRequestGenericTypeDefinition(genericTypeDefinition))
206                {
207                    Type[] typeArgs = type.GetGenericArguments();
208                    if (typeArgs.Length > 1)
209                    {
210                        throw Fx.Exception.AsError(
211                            new InvalidOperationException(
212                                SR.MultipleTypeParametersForHttpContentType(type.Name)));
213                    }
214
215                    return typeArgs[0];
216                }
217            }
218
219            return null;
220        }
221
222        internal static Type GetHttpResponseInnerTypeOrNull(Type type)
223        {
224            Fx.Assert(type != null, "The 'type' parameter should not be null.");
225
226            if (type.IsGenericType && !type.IsGenericTypeDefinition)
227            {
228                Type genericTypeDefinition = type.GetGenericTypeDefinition();
229                if (IsHttpResponseGenericTypeDefinition(genericTypeDefinition))
230                {
231                    Type[] typeArgs = type.GetGenericArguments();
232                    if (typeArgs.Length > 1)
233                    {
234                        throw Fx.Exception.AsError(
235                            new InvalidOperationException(
236                                SR.MultipleTypeParametersForHttpContentType(type.Name)));
237                    }
238
239                    return typeArgs[0];
240                }
241            }
242
243            return null;
244        }
245
246        internal static Type GetHttpRequestOrContentInnerTypeOrNull(Type type)
247        {
248            Fx.Assert(type != null, "The 'type' parameter should not be null.");
249
250            if (type.IsGenericType && !type.IsGenericTypeDefinition)
251            {
252                Type genericTypeDefinition = type.GetGenericTypeDefinition();
253                if (IsHttpRequestOrContentGenericTypeDefinition(genericTypeDefinition))
254                {
255                    Type[] typeArgs = type.GetGenericArguments();
256                    if (typeArgs.Length > 1)
257                    {
258                        throw Fx.Exception.AsError(
259                            new InvalidOperationException(
260                                SR.MultipleTypeParametersForHttpContentType(type.Name)));
261                    }
262
263                    return typeArgs[0];
264                }
265            }
266
267            return null;
268        }
269
270        internal static Type GetHttpResponseOrContentInnerTypeOrNull(Type type)
271        {
272            Fx.Assert(type != null, "The 'type' parameter should not be null.");
273
274            if (type.IsGenericType && !type.IsGenericTypeDefinition)
275            {
276                Type genericTypeDefinition = type.GetGenericTypeDefinition();
277                if (IsHttpResponseOrContentGenericTypeDefinition(genericTypeDefinition))
278                {
279                    Type[] typeArgs = type.GetGenericArguments();
280                    if (typeArgs.Length > 1)
281                    {
282                        throw Fx.Exception.AsError(
283                            new InvalidOperationException(
284                                SR.MultipleTypeParametersForHttpContentType(type.Name)));
285                    }
286
287                    return typeArgs[0];
288                }
289            }
290
291            return null;
292        }
293
294        internal static Type GetHttpInnerTypeOrNull(Type type)
295        {
296            Fx.Assert(type != null, "The 'type' parameter should not be null.");
297
298            if (type.IsGenericType && !type.IsGenericTypeDefinition)
299            {
300                Type genericTypeDefinition = type.GetGenericTypeDefinition();
301                if (IsHttpGenericTypeDefinition(genericTypeDefinition))
302                {
303                    Type[] typeArgs = type.GetGenericArguments();
304                    if (typeArgs.Length > 1)
305                    {
306                        // TODO: Throw exception because there is more than one type argument so we
307                        //  don't know which argument is the body content.
308                        throw new InvalidOperationException("Exception Message");
309                    }
310
311                    return typeArgs[0];
312                }
313            }
314
315            return null;
316        }
317
318        internal static Type MakeHttpRequestMessageOf(Type type)
319        {
320            Fx.Assert(type != null, "The 'type' parameter should not be null.");
321
322            Type[] typeParams = new Type[] { type };
323            return HttpTypeHelper.HttpRequestMessageGenericType.MakeGenericType(typeParams);
324        }
325
326        internal static Type MakeHttpResponseMessageOf(Type type)
327        {
328            Fx.Assert(type != null, "The 'type' parameter should not be null.");
329
330            Type[] typeParams = new Type[] { type };
331            return HttpTypeHelper.HttpResponseMessageGenericType.MakeGenericType(typeParams);
332        }
333
334        internal static Type MakeObjectContentOf(Type type)
335        {
336            Fx.Assert(type != null, "The 'type' parameter should not be null.");
337
338            Type[] typeParams = new Type[] { type };
339            return HttpTypeHelper.ObjectContentGenericType.MakeGenericType(typeParams);
340        }
341    }
342}