0ed9107e46756d4e6d67cdc0247596d5d6ffa3f1
[mono.git] / mcs / class / System.Net / System.Net.Policy / FlashCrossDomainPolicy.cs
1 //
2 // FlashCrossDomainPolicy.cs
3 //
4 // Author:
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 NET_2_1
31
32 using System;
33 using System.Collections.Generic;
34 using System.IO;
35 using System.Linq;
36
37 namespace System.Net.Policy {
38
39         partial class FlashCrossDomainPolicy : BaseDomainPolicy {
40
41                 private string site_control;
42
43                 public FlashCrossDomainPolicy ()
44                 {
45                         AllowedAccesses = new List<AllowAccessFrom> ();
46                         AllowedHttpRequestHeaders = new List<AllowHttpRequestHeadersFrom> ();
47                 }
48
49                 public List<AllowAccessFrom> AllowedAccesses { get; private set; }
50                 public List<AllowHttpRequestHeadersFrom> AllowedHttpRequestHeaders { get; private set; }
51
52                 public string SiteControl {
53                         get { return String.IsNullOrEmpty (site_control) ? "all" : site_control; }
54                         set { site_control = value; }
55                 }
56
57                 public override bool IsAllowed (WebRequest request)
58                 {
59                         return IsAllowed (request.RequestUri, request.Headers.AllKeys);
60                 }
61
62                 public bool IsAllowed (Uri uri, string [] headerKeys)
63                 {
64                         switch (SiteControl) {
65                         case "all":
66                         case "master-only":
67                         case "by-ftp-filename":
68                                 break;
69                         default:
70                                 // others, e.g. 'none', are not supported/accepted
71                                 return false;
72                         }
73
74                         if (AllowedAccesses.Count > 0 &&
75                             !AllowedAccesses.Any (a => a.IsAllowed (uri, headerKeys)))
76                                 return false;
77                         if (AllowedHttpRequestHeaders.Count > 0 && 
78                             AllowedHttpRequestHeaders.Any (h => h.IsRejected (uri, headerKeys)))
79                                 return false;
80
81                         return true;
82                 }
83
84                 public class AllowAccessFrom {
85
86                         public AllowAccessFrom ()
87                         {
88                                 Secure = true;  // true by default
89                         }
90
91                         public string Domain { get; set; }
92                         public bool AllowAnyPort { get; set; }
93                         public int [] ToPorts { get; set; }
94                         public bool Secure { get; set; }
95
96                         public bool IsAllowed (Uri uri, string [] headerKeys)
97                         {
98                                 // "A Flash policy file must allow access to all domains to be used by the Silverlight runtime."
99                                 // http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx
100                                 if (Domain != "*")
101                                         return false;
102                                 if (!AllowAnyPort && ToPorts != null && Array.IndexOf (ToPorts, uri.Port) < 0)
103                                         return false;
104
105                                 // if Secure is false then it allows applications from HTTP to download data from HTTPS servers
106                                 if (!Secure)
107                                         return true;
108                                 // if Secure is true then data on HTTPS servers can only be accessed by application on HTTPS servers
109                                 if (uri.Scheme == Uri.UriSchemeHttps)
110                                         return (ApplicationUri.Scheme == Uri.UriSchemeHttps);
111                                 // otherwise FILE/HTTP applications can access HTTP uris
112                                 return true;
113                         }
114                 }
115
116                 public class AllowHttpRequestHeadersFrom {
117
118                         public AllowHttpRequestHeadersFrom ()
119                         {
120                                 Headers = new Headers ();
121                         }
122
123                         public string Domain { get; set; }
124                         public bool AllowAllHeaders { get; set; }
125                         public Headers Headers { get; private set; }
126                         public bool Secure { get; set; }
127
128                         public bool IsRejected (Uri uri, string [] headerKeys)
129                         {
130                                 // "A Flash policy file must allow access to all domains to be used by the Silverlight runtime."
131                                 // http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx
132                                 if (Domain != "*")
133                                         return false;
134
135                                 if (Headers.IsAllowed (headerKeys))
136                                         return false;
137
138                                 // if Secure is false then it allows applications from HTTP to download data from HTTPS servers
139                                 if (!Secure)
140                                         return true;
141                                 // if Secure is true then only application on HTTPS servers can access data on HTTPS servers
142                                 if (ApplicationUri.Scheme == Uri.UriSchemeHttps)
143                                         return (uri.Scheme == Uri.UriSchemeHttps);
144                                 // otherwise FILE/HTTP applications can access HTTP uris
145                                 return true;
146                         }
147                 }
148         }
149 }
150
151 #endif
152