2 // FlashCrossDomainPolicy.cs
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 namespace System.Net.Policy {
39 partial class FlashCrossDomainPolicy : BaseDomainPolicy {
41 private string site_control;
43 public FlashCrossDomainPolicy ()
45 AllowedAccesses = new List<AllowAccessFrom> ();
46 AllowedHttpRequestHeaders = new List<AllowHttpRequestHeadersFrom> ();
49 public List<AllowAccessFrom> AllowedAccesses { get; private set; }
50 public List<AllowHttpRequestHeadersFrom> AllowedHttpRequestHeaders { get; private set; }
52 public string SiteControl {
53 get { return String.IsNullOrEmpty (site_control) ? "all" : site_control; }
54 set { site_control = value; }
57 public override bool IsAllowed (WebRequest request)
59 return IsAllowed (request.RequestUri, request.Headers.AllKeys);
62 public bool IsAllowed (Uri uri, string [] headerKeys)
64 switch (SiteControl) {
67 case "by-ftp-filename":
70 // others, e.g. 'none', are not supported/accepted
74 if (AllowedAccesses.Count > 0 &&
75 !AllowedAccesses.Any (a => a.IsAllowed (uri, headerKeys)))
77 if (AllowedHttpRequestHeaders.Count > 0 &&
78 AllowedHttpRequestHeaders.Any (h => h.IsRejected (uri, headerKeys)))
84 public class AllowAccessFrom {
86 public AllowAccessFrom ()
88 Secure = true; // true by default
91 public string Domain { get; set; }
92 public bool AllowAnyPort { get; set; }
93 public int [] ToPorts { get; set; }
94 public bool Secure { get; set; }
96 public bool IsAllowed (Uri uri, string [] headerKeys)
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
102 if (!AllowAnyPort && ToPorts != null && Array.IndexOf (ToPorts, uri.Port) < 0)
105 // if Secure is false then it allows applications from HTTP to download data from HTTPS servers
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
116 public class AllowHttpRequestHeadersFrom {
118 public AllowHttpRequestHeadersFrom ()
120 Headers = new Headers ();
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; }
128 public bool IsRejected (Uri uri, string [] headerKeys)
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
135 if (Headers.IsAllowed (headerKeys))
138 // if Secure is false then it allows applications from HTTP to download data from HTTPS servers
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