16089b5c933a83871d210cca6aadf82cdba03b3b
[mono.git] / mcs / class / System.Net / System.Net.Policy / BaseDomainPolicy.cs
1 //
2 // BaseDomainPolicy.cs
3 //
4 // Authors:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Moonlight List (moonlight-list@lists.ximian.com)
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
30 #if MOBILE
31
32 using System;
33 using System.Collections.Generic;
34 using System.IO;
35 using System.Linq;
36
37 // Base class for shared stuff between the Silverlight and Flash policies
38 // e.g. Headers and Domain comparison
39
40 namespace System.Net.Policy {
41
42         abstract class BaseDomainPolicy : ICrossDomainPolicy {
43
44                 static string root;
45                 static Uri app;
46 #if !TEST
47                 static BaseDomainPolicy ()
48                 {
49                         ApplicationUri = new Uri (AppDomain.CurrentDomain.GetData ("xap_uri") as string);
50                 }
51 #endif
52                 static public Uri ApplicationUri { 
53                         get { return app; }
54                         set {
55                                 app = value;
56                                 root = null;
57                         }
58                 }
59
60                 static public string ApplicationRoot {
61                         get {
62                                 if (root == null)
63                                         root = CrossDomainPolicyManager.GetRoot (ApplicationUri);
64                                 return root;
65                         }
66                 }
67
68                 public class Headers {
69
70                         sealed class PrefixComparer : IEqualityComparer<string> {
71
72                                 public bool Equals (string x, string y)
73                                 {
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);
77
78                                         return (String.Compare (x, y, StringComparison.OrdinalIgnoreCase) == 0);
79                                 }
80
81                                 public int GetHashCode (string obj)
82                                 {
83                                         return (obj == null) ? 0 : obj.GetHashCode ();
84                                 }
85                         }
86
87                         static PrefixComparer pc = new PrefixComparer ();
88
89                         private List<string> list;
90
91                         public Headers ()
92                         {
93                         }
94
95                         public bool AllowAllHeaders { get; private set; }
96
97                         public bool IsAllowed (string[] headers)
98                         {
99                                 if (AllowAllHeaders)
100                                         return true;
101
102                                 if (headers == null || headers.Length == 0)
103                                         return true;
104
105                                 return headers.All (s => list.Contains (s, pc));
106                         }
107
108                         public void SetHeaders (string raw)
109                         {
110                                 if (raw == "*") {
111                                         AllowAllHeaders = true;
112                                         list = null;
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))
120                                                         list.Add (s);
121                                         }
122                                 } else {
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");
127                                 }
128                         }
129                 }
130
131                 abstract public bool IsAllowed (WebRequest request);
132
133                 public Exception Exception {
134                         get; internal set;
135                 }
136         }
137 }
138
139 #endif
140