PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceStack.Client/PclExportClient.cs

http://github.com/ServiceStack/ServiceStack
C# | 237 lines | 171 code | 35 blank | 31 comment | 16 complexity | cf160c39e2ff35f19f3f3aeab063b46d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. //
  2. // System.Collections.Specialized.NameObjectCollectionBase.cs
  3. //
  4. // Author:
  5. // Gleb Novodran
  6. // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. /*** REMINDER: Keep this file in sync with ServiceStack.Text/Pcl.NameValueCollection.cs ***/
  31. using System.IO;
  32. using System.Net;
  33. using System.Reflection;
  34. using System.Threading;
  35. using System.Threading.Tasks;
  36. using ServiceStack.Text;
  37. using System.Globalization;
  38. //Dummy namespaces
  39. namespace System.Collections.Specialized {}
  40. namespace System.Web {}
  41. namespace ServiceStack.Pcl {}
  42. namespace ServiceStack
  43. {
  44. using System;
  45. using System.Collections.Generic;
  46. using System.Web;
  47. using ServiceStack.Web;
  48. using ServiceStack.Pcl;
  49. using System.Collections.Specialized;
  50. public class PclExportClient
  51. {
  52. public static PclExportClient Instance
  53. #if NETSTANDARD2_0
  54. = NetStandardPclExportClient.Configure()
  55. #else
  56. = Net45PclExportClient.Configure()
  57. #endif
  58. ;
  59. public static readonly Task<object> EmptyTask = TypeConstants.EmptyTask;
  60. static PclExportClient()
  61. {
  62. if (Instance != null)
  63. return;
  64. try
  65. {
  66. if (ConfigureProvider("ServiceStack.IosPclExportClient, ServiceStack.Pcl.iOS"))
  67. return;
  68. if (ConfigureProvider("ServiceStack.AndroidPclExportClient, ServiceStack.Pcl.Android"))
  69. return;
  70. if (ConfigureProvider("ServiceStack.WinStorePclExportClient, ServiceStack.Pcl.WinStore"))
  71. return;
  72. if (ConfigureProvider("ServiceStack.Net40PclExportClient, ServiceStack.Pcl.Net45"))
  73. return;
  74. }
  75. catch (Exception /*ignore*/) {}
  76. }
  77. public static bool ConfigureProvider(string typeName)
  78. {
  79. var type = Type.GetType(typeName);
  80. if (type == null)
  81. return false;
  82. var mi = type.GetMethod("Configure");
  83. if (mi != null)
  84. {
  85. mi.Invoke(null, TypeConstants.EmptyObjectArray);
  86. }
  87. return true;
  88. }
  89. [Obsolete("Use new NameValueCollection()")]
  90. public NameValueCollection NewNameValueCollection() => new NameValueCollection();
  91. public virtual NameValueCollection ParseQueryString(string query)
  92. {
  93. #if NETSTANDARD2_0
  94. return ServiceStack.Pcl.HttpUtility.ParseQueryString(query);
  95. #else
  96. return System.Web.HttpUtility.ParseQueryString(query);
  97. #endif
  98. }
  99. public virtual string UrlEncode(string url)
  100. {
  101. return WebUtility.UrlEncode(url);
  102. }
  103. public virtual string UrlDecode(string url)
  104. {
  105. return WebUtility.UrlDecode(url);
  106. }
  107. public virtual string HtmlAttributeEncode(string html)
  108. {
  109. #if NETSTANDARD2_0
  110. return HtmlEncode(html).Replace("\"","&quot;").Replace("'","&#x27;");
  111. #else
  112. return System.Web.HttpUtility.HtmlAttributeEncode(html);
  113. #endif
  114. }
  115. public virtual string HtmlEncode(string html)
  116. {
  117. return WebUtility.HtmlEncode(html);
  118. }
  119. public virtual string HtmlDecode(string html)
  120. {
  121. return WebUtility.HtmlDecode(html);
  122. }
  123. public virtual void AddHeader(WebRequest webReq, NameValueCollection headers)
  124. {
  125. if (headers == null)
  126. return;
  127. foreach (var name in headers.AllKeys)
  128. {
  129. webReq.Headers[name] = headers[name];
  130. }
  131. }
  132. public virtual string GetHeader(WebHeaderCollection headers, string name, Func<string, bool> valuePredicate)
  133. {
  134. return null;
  135. }
  136. public virtual void SetCookieContainer(HttpWebRequest webRequest, ServiceClientBase client)
  137. {
  138. webRequest.CookieContainer = client.CookieContainer;
  139. }
  140. public virtual void SetCookieContainer(HttpWebRequest webRequest, AsyncServiceClient client)
  141. {
  142. webRequest.CookieContainer = client.CookieContainer;
  143. }
  144. public virtual void SynchronizeCookies(AsyncServiceClient client)
  145. {
  146. }
  147. public virtual ITimer CreateTimer(TimerCallback cb, TimeSpan timeOut, object state)
  148. {
  149. return new AsyncTimer(new
  150. System.Threading.Timer(cb, state, (int)timeOut.TotalMilliseconds, Timeout.Infinite));
  151. }
  152. public virtual Task WaitAsync(int waitForMs)
  153. {
  154. if (waitForMs <= 0)
  155. throw new ArgumentOutOfRangeException(nameof(waitForMs));
  156. var tcs = new TaskCompletionSource<bool>();
  157. Timer timer = null;
  158. timer = new Timer(self => {
  159. tcs.TrySetResult(true);
  160. timer.Dispose();
  161. }, null, waitForMs, Timeout.Infinite);
  162. return tcs.Task;
  163. }
  164. public virtual void RunOnUiThread(Action fn)
  165. {
  166. if (UiContext == null)
  167. {
  168. fn();
  169. }
  170. else
  171. {
  172. UiContext.Post(_ => fn(), null);
  173. }
  174. }
  175. public SynchronizationContext UiContext;
  176. public static void Configure(PclExportClient instance)
  177. {
  178. Instance = instance;
  179. Instance.UiContext = SynchronizationContext.Current;
  180. }
  181. public virtual Exception CreateTimeoutException(Exception ex, string errorMsg)
  182. {
  183. return new WebException("The request timed out", ex, WebExceptionStatus.RequestCanceled, null);
  184. }
  185. public virtual void CloseReadStream(Stream stream)
  186. {
  187. stream.Close();
  188. }
  189. public virtual void CloseWriteStream(Stream stream)
  190. {
  191. stream.Close();
  192. }
  193. public virtual bool IsWebException(WebException webEx)
  194. {
  195. return webEx?.Response != null;
  196. }
  197. public virtual void SetIfModifiedSince(HttpWebRequest webReq, DateTime lastModified)
  198. {
  199. webReq.IfModifiedSince = lastModified;
  200. }
  201. }
  202. }