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