BaseDomainPolicy.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // BaseDomainPolicy.cs
  3. //
  4. // Authors:
  5. // Atsushi Enomoto <[email protected]>
  6. // Moonlight List ([email protected])
  7. //
  8. // Copyright (C) 2009-2010 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if MOBILE
  30. using System;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using System.Linq;
  34. // Base class for shared stuff between the Silverlight and Flash policies
  35. // e.g. Headers and Domain comparison
  36. namespace System.Net.Policy {
  37. abstract class BaseDomainPolicy : ICrossDomainPolicy {
  38. static string root;
  39. static Uri app;
  40. #if !TEST
  41. static BaseDomainPolicy ()
  42. {
  43. ApplicationUri = new Uri (AppDomain.CurrentDomain.GetData ("xap_uri") as string);
  44. }
  45. #endif
  46. static public Uri ApplicationUri {
  47. get { return app; }
  48. set {
  49. app = value;
  50. root = null;
  51. }
  52. }
  53. static public string ApplicationRoot {
  54. get {
  55. if (root == null)
  56. root = CrossDomainPolicyManager.GetRoot (ApplicationUri);
  57. return root;
  58. }
  59. }
  60. public class Headers {
  61. sealed class PrefixComparer : IEqualityComparer<string> {
  62. public bool Equals (string x, string y)
  63. {
  64. int check_length = x.Length - 1;
  65. if ((x.Length > 0) && (x [check_length] == '*'))
  66. return (String.Compare (x, 0, y, 0, check_length, StringComparison.OrdinalIgnoreCase) == 0);
  67. return (String.Compare (x, y, StringComparison.OrdinalIgnoreCase) == 0);
  68. }
  69. public int GetHashCode (string obj)
  70. {
  71. return (obj == null) ? 0 : obj.GetHashCode ();
  72. }
  73. }
  74. static PrefixComparer pc = new PrefixComparer ();
  75. private List<string> list;
  76. public Headers ()
  77. {
  78. }
  79. public bool AllowAllHeaders { get; private set; }
  80. public bool IsAllowed (string[] headers)
  81. {
  82. if (AllowAllHeaders)
  83. return true;
  84. if (headers == null || headers.Length == 0)
  85. return true;
  86. return headers.All (s => list.Contains (s, pc));
  87. }
  88. public void SetHeaders (string raw)
  89. {
  90. if (raw == "*") {
  91. AllowAllHeaders = true;
  92. list = null;
  93. } else if (raw != null) {
  94. string [] headers = raw.Split (',');
  95. list = new List<string> (headers.Length + 1);
  96. list.Add ("Content-Type");
  97. for (int i = 0; i < headers.Length; i++) {
  98. string s = headers [i].Trim ();
  99. if (!String.IsNullOrEmpty (s))
  100. list.Add (s);
  101. }
  102. } else {
  103. // without a specified 'http-request-headers' no header, expect Content-Type, is allowed
  104. AllowAllHeaders = false;
  105. list = new List<string> (1);
  106. list.Add ("Content-Type");
  107. }
  108. }
  109. }
  110. abstract public bool IsAllowed (WebRequest request);
  111. public Exception Exception {
  112. get; internal set;
  113. }
  114. }
  115. }
  116. #endif