PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/windows-phone-8/templates/standalone/cordovalib/ConfigHandler.cs

https://github.com/casualiswebs/phonegap
C# | 241 lines | 187 code | 30 blank | 24 comment | 20 complexity | 9aa6cba56fcb653e7b423ae30729ee94 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Resources;
  11. using System.Xml.Linq;
  12. namespace WPCordovaClassLib.CordovaLib
  13. {
  14. class ConfigHandler
  15. {
  16. public class PluginConfig
  17. {
  18. public PluginConfig(string name, bool autoLoad = false)
  19. {
  20. Name = name;
  21. isAutoLoad = autoLoad;
  22. }
  23. public string Name;
  24. public bool isAutoLoad;
  25. }
  26. protected Dictionary<string, PluginConfig> AllowedPlugins;
  27. protected List<string> AllowedDomains;
  28. protected Dictionary<string, string> Preferences;
  29. protected bool AllowAllDomains = false;
  30. protected bool AllowAllPlugins = false;
  31. public ConfigHandler()
  32. {
  33. AllowedPlugins = new Dictionary<string, PluginConfig>();
  34. AllowedDomains = new List<string>();
  35. Preferences = new Dictionary<string, string>();
  36. }
  37. public string GetPreference(string key)
  38. {
  39. return Preferences[key];
  40. }
  41. protected static string[] AllowedSchemes = {"http","https","ftp","ftps"};
  42. protected bool SchemeIsAllowed(string scheme)
  43. {
  44. return AllowedSchemes.Contains(scheme);
  45. }
  46. protected void AddWhiteListEntry(string origin, bool allowSubdomains)
  47. {
  48. if (origin == "*")
  49. {
  50. AllowAllDomains = true;
  51. }
  52. if (AllowAllDomains)
  53. {
  54. return;
  55. }
  56. string hostMatchingRegex = "";
  57. string hostName;
  58. try
  59. {
  60. Uri uri = new Uri(origin.Replace("*", "replaced-text"), UriKind.Absolute);
  61. string tempHostName = uri.Host.Replace("replaced-text", "*");
  62. //if (uri.HostNameType == UriHostNameType.Dns){}
  63. // starts with wildcard match - we make the first '.' optional (so '*.org.apache.cordova' will match 'org.apache.cordova')
  64. if (tempHostName.StartsWith("*."))
  65. { //"(\\s{0}|*.)"
  66. hostName = @"\w*.*" + tempHostName.Substring(2).Replace(".", @"\.").Replace("*", @"\w*");
  67. }
  68. else
  69. {
  70. hostName = tempHostName.Replace(".", @"\.").Replace("*", @"\w*");
  71. }
  72. // "^https?://"
  73. hostMatchingRegex = uri.Scheme + "://" + hostName + uri.PathAndQuery;
  74. Debug.WriteLine("Adding regex :: " + hostMatchingRegex);
  75. AllowedDomains.Add(hostMatchingRegex);
  76. }
  77. catch (Exception)
  78. {
  79. Debug.WriteLine("Invalid Whitelist entry (probably missing the protocol):: " + origin);
  80. }
  81. }
  82. /**
  83. An access request is granted for a given URI if there exists an item inside the access-request list such that:
  84. - The URI's scheme component is the same as scheme; and
  85. - if subdomains is false or if the URI's host component is not a domain name (as defined in [RFC1034]), the URI's host component is the same as host; or
  86. - if subdomains is true, the URI's host component is either the same as host, or is a subdomain of host (as defined in [RFC1034]); and
  87. - the URI's port component is the same as port.
  88. **/
  89. public bool URLIsAllowed(string url)
  90. {
  91. // Debug.WriteLine("Testing URLIsAllowed : " + url);
  92. // easy case first
  93. if (this.AllowAllDomains )
  94. {
  95. return true;
  96. }
  97. else
  98. {
  99. // start simple
  100. Uri uri = new Uri(url,UriKind.RelativeOrAbsolute);
  101. if (uri.IsAbsoluteUri)
  102. {
  103. if (this.SchemeIsAllowed(uri.Scheme))
  104. {
  105. // additional test because our pattern will always have a trailing '/'
  106. string matchUrl = url;
  107. if (uri.PathAndQuery == "/")
  108. {
  109. matchUrl = url + "/";
  110. }
  111. foreach (string pattern in AllowedDomains)
  112. {
  113. if (Regex.IsMatch(matchUrl, pattern))
  114. {
  115. // make sure it is at the start, and not part of the query string
  116. // special case :: http://some.other.domain/page.html?x=1&g=http://build.apache.org/
  117. if ( Regex.IsMatch(uri.Scheme + "://" + uri.Host + "/", pattern) ||
  118. (!Regex.IsMatch(uri.PathAndQuery, pattern)))
  119. {
  120. return true;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. else
  127. {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. public bool IsPluginAllowed(string key)
  134. {
  135. return AllowAllPlugins || AllowedPlugins.Keys.Contains(key);
  136. }
  137. public string[] AutoloadPlugins {
  138. get
  139. {
  140. var res = from results in AllowedPlugins.TakeWhile(p => p.Value.isAutoLoad)
  141. select results.Value.Name ;
  142. foreach(var s in res)
  143. {
  144. Debug.WriteLine(s);
  145. }
  146. //string[] res = from results in (AllowedPlugins.Where(p => p.Value.isAutoLoad) )
  147. // select (string)results.Key;
  148. return new string[] { "", "asd" };
  149. }
  150. }
  151. public void LoadAppPackageConfig()
  152. {
  153. StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
  154. if (streamInfo != null)
  155. {
  156. StreamReader sr = new StreamReader(streamInfo.Stream);
  157. //This will Read Keys Collection for the xml file
  158. XDocument document = XDocument.Parse(sr.ReadToEnd());
  159. var plugins = from results in document.Descendants("plugin")
  160. select new
  161. {
  162. name = (string)results.Attribute("name"),
  163. autoLoad = results.Attribute("onload")
  164. };
  165. foreach (var plugin in plugins)
  166. {
  167. Debug.WriteLine("plugin " + plugin.name);
  168. PluginConfig pConfig = new PluginConfig(plugin.name, plugin.autoLoad != null && plugin.autoLoad.Value == "true");
  169. if (pConfig.Name == "*")
  170. {
  171. AllowAllPlugins = true;
  172. // break; wait, don't, some still could be autoload
  173. }
  174. else
  175. {
  176. AllowedPlugins.Add(pConfig.Name, pConfig);
  177. }
  178. }
  179. var preferences = from results in document.Descendants("preference")
  180. select new
  181. {
  182. name = (string)results.Attribute("name"),
  183. value = (string)results.Attribute("value")
  184. };
  185. foreach (var pref in preferences)
  186. {
  187. Debug.WriteLine("pref" + pref.name + ", " + pref.value);
  188. }
  189. var accessList = from results in document.Descendants("access")
  190. select new
  191. {
  192. origin = (string)results.Attribute("origin"),
  193. subdomains = (string)results.Attribute("subdomains") == "true"
  194. };
  195. foreach (var accessElem in accessList)
  196. {
  197. AddWhiteListEntry(accessElem.origin, accessElem.subdomains);
  198. }
  199. }
  200. else
  201. {
  202. // no config.xml, allow all
  203. AllowAllDomains = true;
  204. AllowAllPlugins = true;
  205. }
  206. }
  207. }
  208. }