5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Moonlight List (moonlight-list@lists.ximian.com)
8 // Copyright (C) 2009-2010 Novell, Inc. http://www.novell.com
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections.Generic;
37 // Base class for shared stuff between the Silverlight and Flash policies
38 // e.g. Headers and Domain comparison
40 namespace System.Net.Policy {
42 abstract class BaseDomainPolicy : ICrossDomainPolicy {
47 static BaseDomainPolicy ()
49 ApplicationUri = new Uri (AppDomain.CurrentDomain.GetData ("xap_uri") as string);
52 static public Uri ApplicationUri {
60 static public string ApplicationRoot {
63 root = CrossDomainPolicyManager.GetRoot (ApplicationUri);
68 public class Headers {
70 sealed class PrefixComparer : IEqualityComparer<string> {
72 public bool Equals (string x, string y)
74 int check_length = x.Length - 1;
75 if ((x.Length > 0) && (x [check_length] == '*'))
76 return (String.Compare (x, 0, y, 0, check_length, StringComparison.OrdinalIgnoreCase) == 0);
78 return (String.Compare (x, y, StringComparison.OrdinalIgnoreCase) == 0);
81 public int GetHashCode (string obj)
83 return (obj == null) ? 0 : obj.GetHashCode ();
87 static PrefixComparer pc = new PrefixComparer ();
89 private List<string> list;
95 public bool AllowAllHeaders { get; private set; }
97 public bool IsAllowed (string[] headers)
102 if (headers == null || headers.Length == 0)
105 return headers.All (s => list.Contains (s, pc));
108 public void SetHeaders (string raw)
111 AllowAllHeaders = true;
113 } else if (raw != null) {
114 string [] headers = raw.Split (',');
115 list = new List<string> (headers.Length + 1);
116 list.Add ("Content-Type");
117 for (int i = 0; i < headers.Length; i++) {
118 string s = headers [i].Trim ();
119 if (!String.IsNullOrEmpty (s))
123 // without a specified 'http-request-headers' no header, expect Content-Type, is allowed
124 AllowAllHeaders = false;
125 list = new List<string> (1);
126 list.Add ("Content-Type");
131 abstract public bool IsAllowed (WebRequest request);
133 public Exception Exception {