PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms 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
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 Microsoft.ApplicationServer.Http
  5. {
  6. using System;
  7. using System.Json;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Net.Http.Headers;
  11. using Microsoft.Server.Common;
  12. /// <summary>
  13. /// A static class that provides HTTP related types and functionality
  14. /// around checking types against the set of HTTP related types.
  15. /// </summary>
  16. internal static class HttpTypeHelper
  17. {
  18. internal static readonly Type JsonValueType = typeof(JsonValue);
  19. internal static readonly Type HttpRequestMessageType = typeof(HttpRequestMessage);
  20. internal static readonly Type HttpRequestHeadersType = typeof(HttpRequestHeaders);
  21. internal static readonly Type UriType = typeof(Uri);
  22. internal static readonly Type HttpMethodType = typeof(HttpMethod);
  23. internal static readonly Type HttpContentType = typeof(HttpContent);
  24. internal static readonly Type HttpResponseMessageType = typeof(HttpResponseMessage);
  25. internal static readonly Type HttpResponseHeadersType = typeof(HttpResponseHeaders);
  26. internal static readonly Type HttpStatusCodeType = typeof(HttpStatusCode);
  27. internal static readonly Type HttpRequestMessageGenericType = typeof(HttpRequestMessage<>);
  28. internal static readonly Type HttpResponseMessageGenericType = typeof(HttpResponseMessage<>);
  29. internal static readonly Type ObjectContentGenericType = typeof(ObjectContent<>);
  30. internal static bool IsHttpContent(Type type)
  31. {
  32. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  33. return HttpContentType.IsAssignableFrom(type);
  34. }
  35. internal static bool IsHttpResponse(Type type)
  36. {
  37. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  38. return HttpResponseMessageType.IsAssignableFrom(type);
  39. }
  40. internal static bool IsHttpRequest(Type type)
  41. {
  42. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  43. return HttpRequestMessageType.IsAssignableFrom(type);
  44. }
  45. internal static bool IsHttpResponseOrContent(Type type)
  46. {
  47. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  48. return HttpContentType.IsAssignableFrom(type) ||
  49. HttpResponseMessageType.IsAssignableFrom(type);
  50. }
  51. internal static bool IsHttpRequestOrContent(Type type)
  52. {
  53. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  54. return HttpContentType.IsAssignableFrom(type) ||
  55. HttpRequestMessageType.IsAssignableFrom(type);
  56. }
  57. internal static bool IsHttp(Type type)
  58. {
  59. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  60. return HttpContentType.IsAssignableFrom(type) ||
  61. HttpRequestMessageType.IsAssignableFrom(type) ||
  62. HttpResponseMessageType.IsAssignableFrom(type);
  63. }
  64. internal static bool IsHttpContentGenericTypeDefinition(Type type)
  65. {
  66. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  67. if (type.IsGenericTypeDefinition &&
  68. ObjectContentGenericType.IsAssignableFrom(type))
  69. {
  70. return true;
  71. }
  72. return false;
  73. }
  74. internal static bool IsHttpRequestGenericTypeDefinition(Type type)
  75. {
  76. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  77. if (type.IsGenericTypeDefinition &&
  78. HttpRequestMessageGenericType.IsAssignableFrom(type))
  79. {
  80. return true;
  81. }
  82. return false;
  83. }
  84. internal static bool IsHttpResponseGenericTypeDefinition(Type type)
  85. {
  86. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  87. if (type.IsGenericTypeDefinition &&
  88. HttpResponseMessageGenericType.IsAssignableFrom(type))
  89. {
  90. return true;
  91. }
  92. return false;
  93. }
  94. internal static bool IsHttpRequestOrContentGenericTypeDefinition(Type type)
  95. {
  96. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  97. if (type.IsGenericTypeDefinition)
  98. {
  99. if (HttpRequestMessageGenericType.IsAssignableFrom(type) ||
  100. ObjectContentGenericType.IsAssignableFrom(type))
  101. {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. internal static bool IsHttpResponseOrContentGenericTypeDefinition(Type type)
  108. {
  109. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  110. if (type.IsGenericTypeDefinition)
  111. {
  112. if (HttpResponseMessageGenericType.IsAssignableFrom(type) ||
  113. ObjectContentGenericType.IsAssignableFrom(type))
  114. {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. internal static bool IsHttpGenericTypeDefinition(Type type)
  121. {
  122. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  123. if (type.IsGenericTypeDefinition)
  124. {
  125. if (HttpRequestMessageGenericType.IsAssignableFrom(type) ||
  126. HttpResponseMessageGenericType.IsAssignableFrom(type) ||
  127. ObjectContentGenericType.IsAssignableFrom(type))
  128. {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. internal static bool IsJsonValue(Type type)
  135. {
  136. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  137. return JsonValueType.IsAssignableFrom(type);
  138. }
  139. internal static Type GetHttpContentInnerTypeOrNull(Type type)
  140. {
  141. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  142. if (type.IsGenericType && !type.IsGenericTypeDefinition)
  143. {
  144. Type genericTypeDefinition = type.GetGenericTypeDefinition();
  145. if (IsHttpContentGenericTypeDefinition(genericTypeDefinition))
  146. {
  147. Type[] typeArgs = type.GetGenericArguments();
  148. if (typeArgs.Length > 1)
  149. {
  150. throw Fx.Exception.AsError(
  151. new InvalidOperationException(
  152. SR.MultipleTypeParametersForHttpContentType(type.Name)));
  153. }
  154. return typeArgs[0];
  155. }
  156. }
  157. return null;
  158. }
  159. internal static Type GetHttpRequestInnerTypeOrNull(Type type)
  160. {
  161. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  162. if (type.IsGenericType && !type.IsGenericTypeDefinition)
  163. {
  164. Type genericTypeDefinition = type.GetGenericTypeDefinition();
  165. if (IsHttpRequestGenericTypeDefinition(genericTypeDefinition))
  166. {
  167. Type[] typeArgs = type.GetGenericArguments();
  168. if (typeArgs.Length > 1)
  169. {
  170. throw Fx.Exception.AsError(
  171. new InvalidOperationException(
  172. SR.MultipleTypeParametersForHttpContentType(type.Name)));
  173. }
  174. return typeArgs[0];
  175. }
  176. }
  177. return null;
  178. }
  179. internal static Type GetHttpResponseInnerTypeOrNull(Type type)
  180. {
  181. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  182. if (type.IsGenericType && !type.IsGenericTypeDefinition)
  183. {
  184. Type genericTypeDefinition = type.GetGenericTypeDefinition();
  185. if (IsHttpResponseGenericTypeDefinition(genericTypeDefinition))
  186. {
  187. Type[] typeArgs = type.GetGenericArguments();
  188. if (typeArgs.Length > 1)
  189. {
  190. throw Fx.Exception.AsError(
  191. new InvalidOperationException(
  192. SR.MultipleTypeParametersForHttpContentType(type.Name)));
  193. }
  194. return typeArgs[0];
  195. }
  196. }
  197. return null;
  198. }
  199. internal static Type GetHttpRequestOrContentInnerTypeOrNull(Type type)
  200. {
  201. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  202. if (type.IsGenericType && !type.IsGenericTypeDefinition)
  203. {
  204. Type genericTypeDefinition = type.GetGenericTypeDefinition();
  205. if (IsHttpRequestOrContentGenericTypeDefinition(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. return typeArgs[0];
  215. }
  216. }
  217. return null;
  218. }
  219. internal static Type GetHttpResponseOrContentInnerTypeOrNull(Type type)
  220. {
  221. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  222. if (type.IsGenericType && !type.IsGenericTypeDefinition)
  223. {
  224. Type genericTypeDefinition = type.GetGenericTypeDefinition();
  225. if (IsHttpResponseOrContentGenericTypeDefinition(genericTypeDefinition))
  226. {
  227. Type[] typeArgs = type.GetGenericArguments();
  228. if (typeArgs.Length > 1)
  229. {
  230. throw Fx.Exception.AsError(
  231. new InvalidOperationException(
  232. SR.MultipleTypeParametersForHttpContentType(type.Name)));
  233. }
  234. return typeArgs[0];
  235. }
  236. }
  237. return null;
  238. }
  239. internal static Type GetHttpInnerTypeOrNull(Type type)
  240. {
  241. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  242. if (type.IsGenericType && !type.IsGenericTypeDefinition)
  243. {
  244. Type genericTypeDefinition = type.GetGenericTypeDefinition();
  245. if (IsHttpGenericTypeDefinition(genericTypeDefinition))
  246. {
  247. Type[] typeArgs = type.GetGenericArguments();
  248. if (typeArgs.Length > 1)
  249. {
  250. // TODO: Throw exception because there is more than one type argument so we
  251. // don't know which argument is the body content.
  252. throw new InvalidOperationException("Exception Message");
  253. }
  254. return typeArgs[0];
  255. }
  256. }
  257. return null;
  258. }
  259. internal static Type MakeHttpRequestMessageOf(Type type)
  260. {
  261. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  262. Type[] typeParams = new Type[] { type };
  263. return HttpTypeHelper.HttpRequestMessageGenericType.MakeGenericType(typeParams);
  264. }
  265. internal static Type MakeHttpResponseMessageOf(Type type)
  266. {
  267. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  268. Type[] typeParams = new Type[] { type };
  269. return HttpTypeHelper.HttpResponseMessageGenericType.MakeGenericType(typeParams);
  270. }
  271. internal static Type MakeObjectContentOf(Type type)
  272. {
  273. Fx.Assert(type != null, "The 'type' parameter should not be null.");
  274. Type[] typeParams = new Type[] { type };
  275. return HttpTypeHelper.ObjectContentGenericType.MakeGenericType(typeParams);
  276. }
  277. }
  278. }