PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/casualiswebs/phonegap
C# | 249 lines | 168 code | 29 blank | 52 comment | 18 complexity | 4d8935b41fc79a0550aa67925c6598f0 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.Windows;
  9. using System.Windows.Resources;
  10. using System.Xml.Linq;
  11. namespace WPCordovaClassLib.CordovaLib
  12. {
  13. class ConfigHandler
  14. {
  15. protected List<string> AllowedPlugins;
  16. protected List<string> AllowedDomains;
  17. protected Dictionary<string, string> Preferences;
  18. protected bool AllowAllDomains = false;
  19. protected bool AllowAllPlugins = false;
  20. public ConfigHandler()
  21. {
  22. AllowedPlugins = new List<string>();
  23. AllowedDomains = new List<string>();
  24. Preferences = new Dictionary<string, string>();
  25. }
  26. public string GetPreference(string key)
  27. {
  28. return Preferences[key];
  29. }
  30. /*
  31. - (BOOL)URLIsAllowed:(NSURL*)url
  32. {
  33. if (self.expandedWhitelist == nil) {
  34. return NO;
  35. }
  36. if (self.allowAll) {
  37. return YES;
  38. }
  39. // iterate through settings ExternalHosts, check for equality
  40. NSEnumerator* enumerator = [self.expandedWhitelist objectEnumerator];
  41. id regex = nil;
  42. NSString* urlHost = [url host];
  43. // if the url host IS found in the whitelist, load it in the app (however UIWebViewNavigationTypeOther kicks it out to Safari)
  44. // if the url host IS NOT found in the whitelist, we do nothing
  45. while (regex = [enumerator nextObject]) {
  46. NSPredicate* regex_test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  47. if ([regex_test evaluateWithObject:urlHost] == YES) {
  48. // if it matches at least one rule, return
  49. return YES;
  50. }
  51. }
  52. NSLog(@"%@", [self errorStringForURL:url]);
  53. // if we got here, the url host is not in the white-list, do nothing
  54. return NO;
  55. }*/
  56. protected static string[] AllowedSchemes = {"http","https","ftp","ftps"};
  57. protected bool SchemeIsAllowed(string scheme)
  58. {
  59. return AllowedSchemes.Contains(scheme);
  60. }
  61. protected string PathAndQuery(Uri uri)
  62. {
  63. string result = uri.LocalPath;
  64. if (uri.Query.Length > 0)
  65. {
  66. result += uri.Query;
  67. }
  68. return result;
  69. }
  70. protected void AddWhiteListEntry(string origin, bool allowSubdomains)
  71. {
  72. if (origin == "*")
  73. {
  74. AllowAllDomains = true;
  75. }
  76. if (AllowAllDomains)
  77. {
  78. return;
  79. }
  80. string hostMatchingRegex = "";
  81. string hostName;
  82. try
  83. {
  84. Uri uri = new Uri(origin.Replace("*", "replaced-text"), UriKind.Absolute);
  85. string tempHostName = uri.Host.Replace("replaced-text", "*");
  86. //if (uri.HostNameType == UriHostNameType.Dns){}
  87. // starts with wildcard match - we make the first '.' optional (so '*.org.apache.cordova' will match 'org.apache.cordova')
  88. if (tempHostName.StartsWith("*."))
  89. { //"(\\s{0}|*.)"
  90. hostName = @"\w*.*" + tempHostName.Substring(2).Replace(".", @"\.").Replace("*", @"\w*");
  91. }
  92. else
  93. {
  94. hostName = tempHostName.Replace(".", @"\.").Replace("*", @"\w*");
  95. }
  96. // "^https?://"
  97. hostMatchingRegex = uri.Scheme + "://" + hostName + PathAndQuery(uri);
  98. //Debug.WriteLine("Adding regex :: " + hostMatchingRegex);
  99. AllowedDomains.Add(hostMatchingRegex);
  100. }
  101. catch (Exception)
  102. {
  103. Debug.WriteLine("Invalid Whitelist entry (probably missing the protocol):: " + origin);
  104. }
  105. }
  106. /**
  107. An access request is granted for a given URI if there exists an item inside the access-request list such that:
  108. - The URI's scheme component is the same as scheme; and
  109. - 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
  110. - 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
  111. - the URI's port component is the same as port.
  112. **/
  113. public bool URLIsAllowed(string url)
  114. {
  115. // easy case first
  116. if (AllowAllDomains )
  117. {
  118. return true;
  119. }
  120. else
  121. {
  122. // start simple
  123. Uri uri = new Uri(url,UriKind.RelativeOrAbsolute);
  124. if (uri.IsAbsoluteUri)
  125. {
  126. if (this.SchemeIsAllowed(uri.Scheme))
  127. {
  128. // additional test because our pattern will always have a trailing '/'
  129. string matchUrl = url;
  130. if (PathAndQuery(uri) == "/")
  131. {
  132. matchUrl = url + "/";
  133. }
  134. foreach (string pattern in AllowedDomains)
  135. {
  136. if (Regex.IsMatch(matchUrl, pattern))
  137. {
  138. // make sure it is at the start, and not part of the query string
  139. // special case :: http://some.other.domain/page.html?x=1&g=http://build.apache.org/
  140. if ( Regex.IsMatch(uri.Scheme + "://" + uri.Host + "/", pattern) ||
  141. (!Regex.IsMatch(PathAndQuery(uri), pattern)))
  142. {
  143. return true;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. else
  150. {
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. public bool IsPluginAllowed(string key)
  157. {
  158. return AllowAllPlugins || AllowedPlugins.Contains(key);
  159. }
  160. public void LoadAppPackageConfig()
  161. {
  162. StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));
  163. if (streamInfo != null)
  164. {
  165. StreamReader sr = new StreamReader(streamInfo.Stream);
  166. //This will Read Keys Collection for the xml file
  167. XDocument document = XDocument.Parse(sr.ReadToEnd());
  168. var plugins = from results in document.Descendants("plugin")
  169. select new { name = (string)results.Attribute("name") };
  170. foreach (var plugin in plugins)
  171. {
  172. Debug.WriteLine("plugin " + plugin.name);
  173. if (plugin.name == "*")
  174. {
  175. AllowAllPlugins = true;
  176. break;
  177. }
  178. else
  179. {
  180. AllowedPlugins.Add(plugin.name);
  181. }
  182. }
  183. var preferences = from results in document.Descendants("preference")
  184. select new
  185. {
  186. name = (string)results.Attribute("name"),
  187. value = (string)results.Attribute("value")
  188. };
  189. foreach (var pref in preferences)
  190. {
  191. Debug.WriteLine("pref" + pref.name + ", " + pref.value);
  192. }
  193. var accessList = from results in document.Descendants("access")
  194. select new
  195. {
  196. origin = (string)results.Attribute("origin"),
  197. subdomains = (string)results.Attribute("subdomains") == "true"
  198. };
  199. foreach (var accessElem in accessList)
  200. {
  201. AddWhiteListEntry(accessElem.origin, accessElem.subdomains);
  202. }
  203. }
  204. else
  205. {
  206. // no config.xml, allow all
  207. AllowAllDomains = true;
  208. AllowAllPlugins = true;
  209. }
  210. }
  211. }
  212. }