File: HttpHandlerAction.cs

package info (click to toggle)
mono 1.9.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 173,128 kB
  • ctags: 310,695
  • sloc: cs: 1,855,117; ansic: 276,741; sh: 21,695; xml: 15,360; makefile: 6,139; perl: 1,508; asm: 689; yacc: 288; sql: 81
file content (283 lines) | stat: -rw-r--r-- 8,217 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// System.Web.Configuration.HttpHandlerAction
//
// Authors:
//	Chris Toshok (toshok@ximian.com)
//	Daniel Nauck    (dna(at)mono-project(dot)de)
//
// (C) 2005 Novell, Inc (http://www.novell.com)
// (C) 2008 Daniel Nauck
//

//
// 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.
//

#if NET_2_0

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Util;

namespace System.Web.Configuration
{
	public sealed class HttpHandlerAction: ConfigurationElement
	{
		static ConfigurationPropertyCollection _properties;
		static ConfigurationProperty pathProp;
		static ConfigurationProperty typeProp;
		static ConfigurationProperty validateProp;
		static ConfigurationProperty verbProp;

		static HttpHandlerAction ()
		{
			pathProp = new ConfigurationProperty ("path", typeof (string), null,
							      TypeDescriptor.GetConverter (typeof (string)),
							      PropertyHelper.NonEmptyStringValidator,
							      ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
			typeProp = new ConfigurationProperty ("type", typeof (string), null,
							      TypeDescriptor.GetConverter (typeof (string)),
							      PropertyHelper.NonEmptyStringValidator,
							      ConfigurationPropertyOptions.IsRequired);
			validateProp = new ConfigurationProperty ("validate", typeof (bool), true);
			verbProp = new ConfigurationProperty ("verb", typeof (string), null,
							      TypeDescriptor.GetConverter (typeof (string)),
							      PropertyHelper.NonEmptyStringValidator,
							      ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);

			_properties = new ConfigurationPropertyCollection ();
			_properties.Add (pathProp);
			_properties.Add (typeProp);
			_properties.Add (validateProp);
			_properties.Add (verbProp);
		}

		internal HttpHandlerAction ()
		{ }

		public HttpHandlerAction (string path, string type, string verb)
			: this (path, type, verb, true)
		{ }

		public HttpHandlerAction (string path, string type, string verb, bool validate)
		{
			Path = path;
			Type = type;
			Verb = verb;
			Validate = validate;
		}

		[ConfigurationProperty ("path", Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
		// LAMESPEC: MS lists no validator here but provides one in Properties.
		public string Path {
			get { return (string) base[pathProp]; }
			set { base[pathProp] = value; }
		}

		[ConfigurationProperty ("type", Options = ConfigurationPropertyOptions.IsRequired)]
		// LAMESPEC: MS lists no validator here but provides one in Properties.
		public string Type {
			get { return (string) base[typeProp]; }
			set { base[typeProp] = value; }
		}

		[ConfigurationProperty ("validate", DefaultValue = true)]
		public bool Validate {
			get { return (bool) base[validateProp]; }
			set { base[validateProp] = value; }
		}

		[ConfigurationProperty ("verb", Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
		// LAMESPEC: MS lists no validator here but provides one in Properties.
		public string Verb {
			get { return (string) base[verbProp]; }
			set { base[verbProp] = value; }
		}

		protected override ConfigurationPropertyCollection Properties {
			get { return _properties; }
		}

#region CompatabilityCode
		object instance;
		Type type;

		string cached_verb = null;
		string[] cached_verbs;
		Dictionary<string, bool> cachedMatches = new Dictionary<string, bool> ();
		Object pathMatchesLock = new Object ();

		string[] SplitVerbs ()
		{
			if (Verb == "*")
				cached_verbs = null;
			else
				cached_verbs = Verb.Split (',');

			return cached_verbs;
		}

		internal string[] Verbs {
			get {
				if (cached_verb != Verb) {
					cached_verbs = SplitVerbs();
					cached_verb = Verb;
				}

				return cached_verbs;
			}
		}

		//
		// Loads the a type by name and verifies that it implements
		// IHttpHandler or IHttpHandlerFactory
		//
		internal static Type LoadType (string type_name)
		{
			Type t = null;
			
			t = HttpApplication.LoadType (type_name, false);

			if (t == null)
				throw new HttpException (String.Format ("Failed to load httpHandler type `{0}'", type_name));

			if (typeof (IHttpHandler).IsAssignableFrom (t) ||
			    typeof (IHttpHandlerFactory).IsAssignableFrom (t))
				return t;
			
			throw new HttpException (String.Format ("Type {0} does not implement IHttpHandler or IHttpHandlerFactory", type_name));
		}

		internal bool PathMatches (string pathToMatch)
		{
			if (cachedMatches.ContainsKey (pathToMatch))
				return cachedMatches [pathToMatch];

			lock (pathMatchesLock)
			{
				if (cachedMatches.ContainsKey (pathToMatch))
					return cachedMatches [pathToMatch];

				bool result = false;
				string[] handlerPaths = Path.Split (',');
				int slash = pathToMatch.LastIndexOf ('/');
				string origPathToMatch = pathToMatch;
				if (slash != -1)
					pathToMatch = pathToMatch.Substring (slash);

				foreach (string handlerPath in handlerPaths)
				{
					if (handlerPath == "*")
					{
						result = true;
						break;
					}

					string matchExact = null;
					string endsWith = null;
					Regex regEx = null;

					if (handlerPath.Length > 0)
					{
						if (handlerPath [0] == '*' && (handlerPath.IndexOf ('*', 1) == -1))
							endsWith = handlerPath.Substring (1);

						if (handlerPath.IndexOf ('*') == -1)
							if (handlerPath [0] != '/')
							{
								HttpContext ctx = HttpContext.Current;
								HttpRequest req = ctx != null ? ctx.Request : null;
								string vpath = req != null ? req.BaseVirtualDir : HttpRuntime.AppDomainAppVirtualPath;

								if (vpath == "/")
									vpath = String.Empty;

								matchExact = String.Concat (vpath, "/", handlerPath);
							}
					}

					if (matchExact != null)
					{
						result = matchExact.Length == origPathToMatch.Length && StrUtils.EndsWith (origPathToMatch, matchExact, true);
						if (result == true)
							break;
						else
							continue;
					}
					else if (endsWith != null)
					{
						result = StrUtils.EndsWith (pathToMatch, endsWith, true);
						if (result == true)
							break;
						else
							continue;
					}

					if (handlerPath != "*")
					{
						string expr = handlerPath.Replace (".", "\\.").Replace ("?", "\\?").Replace ("*", ".*");
						if (expr.Length > 0 && expr [0] == '/')
							expr = expr.Substring (1);

						expr += "\\z";
						regEx = new Regex (expr, RegexOptions.IgnoreCase);

						if (regEx.IsMatch (origPathToMatch))
						{
							result = true;
							break;
						}
					}
				}

				if (!cachedMatches.ContainsKey (origPathToMatch))
					cachedMatches.Add (origPathToMatch, result);

				return result;
			}
		}

		// Loads the handler, possibly delay-loaded.
		internal object GetHandlerInstance ()
		{
			IHttpHandler ihh = instance as IHttpHandler;
			
			if (instance == null || (ihh != null && !ihh.IsReusable)){
				if (type == null)
					type = LoadType (Type);

				instance = Activator.CreateInstance (type);
			} 
			
			return instance;
		}
#endregion

	}

}

#endif