File: SimpleWebHandlerParser.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (592 lines) | stat: -rw-r--r-- 16,053 bytes parent folder | download | duplicates (10)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//
// System.Web.UI.SimpleWebHandlerParser
//
// Authors:
//	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
// Copyright (C) 2005-2010 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.
//

using System.CodeDom.Compiler;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Text;
using System.Web.Compilation;
using System.Web.Configuration;
using System.Web.Util;

namespace System.Web.UI
{
	// CAS
	[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	[AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public abstract class SimpleWebHandlerParser
	{
		HttpContext context;
		string vPath;
		string physPath;
		string className;
		bool debug;
		string language;
		string program;
		bool gotDefault;
		ArrayList assemblies;
		ArrayList dependencies;
		Hashtable anames;
		string baseDir;
		string baseVDir;
		TextReader reader;
		int appAssemblyIndex = -1;
		Type cachedType;

		protected SimpleWebHandlerParser (HttpContext context, string virtualPath, string physicalPath)
		: this (context, virtualPath, physicalPath, null)
		{}
		
		internal SimpleWebHandlerParser (HttpContext context, string virtualPath, string physicalPath, TextReader reader)
		{
			this.reader = reader;
			cachedType = CachingCompiler.GetTypeFromCache (physicalPath);
			if (cachedType != null)
				return; // We don't need anything else.

			// context is obsolete in 2.0+ - MSDN recommends passing null, so we need to
			// take that into account
			if (context != null)
				this.context = context;
			else
				this.context = HttpContext.Current;
			
			this.vPath = virtualPath;
			AddDependency (virtualPath);
			
			// physicalPath is obsolete in 2.0+ - same note what for context applies here
			if (physicalPath != null && physicalPath.Length > 0)
				this.physPath = physicalPath;
			else {
				HttpRequest req = this.context != null ? context.Request : null;
				if (req != null)
					this.physPath = req.MapPath (virtualPath);
			}

			assemblies = new ArrayList ();
			string location = Context.ApplicationInstance.AssemblyLocation;
			if (location != typeof (TemplateParser).Assembly.Location)
				appAssemblyIndex = assemblies.Add (location);

			bool addAssembliesInBin = false;
			foreach (AssemblyInfo info in CompilationConfig.Assemblies) {
				if (info.Assembly == "*")
					addAssembliesInBin = true;
				else
					AddAssemblyByName (info.Assembly, null);
			}
			if (addAssembliesInBin)
				AddAssembliesInBin ();
			language = CompilationConfig.DefaultLanguage;

			GetDirectivesAndContent ();
		}

		protected Type GetCompiledTypeFromCache ()
		{
			return cachedType;
		}

		void GetDirectivesAndContent ()
		{
			string line;
			bool directiveFound = false;
			bool inDirective = false;
			StringBuilder directive = null;
			StringBuilder content = new StringBuilder ();
			int idxStart, idxEnd, length;
			StreamReader sr;

			if (reader != null)
				sr = reader as StreamReader;
			else
				sr = new StreamReader (File.OpenRead (physPath), WebEncoding.FileEncoding);
			
			using (sr) {
				while ((line = sr.ReadLine ()) != null && cachedType == null) {
					length = line.Length;
					if (length == 0) {
						content.Append ("\n");
						continue;
					}
					
					idxStart = line.IndexOf ("<%");
					if (idxStart > -1) {
						idxEnd = line.IndexOf ("%>");						
						if (idxStart > 0)
							content.Append (line.Substring (0, idxStart));

						if (directive == null)
							directive = new StringBuilder ();
						else
							directive.Length = 0;
						
						if (idxEnd > -1) {
							directiveFound = true;
							inDirective = false;
							directive.Append (line.Substring (idxStart, idxEnd - idxStart + 2));
							if (idxEnd < length - 2)
								content.Append (line.Substring (idxEnd + 2, length - idxEnd - 2));
						} else {
							inDirective = true;
							directiveFound = false;
							directive.Append (line.Substring (idxStart));
							continue;
						}
					}

					if (inDirective) {
						int idx = line.IndexOf ("%>");
						if (idx > -1) {
							directive.Append (line.Substring (0, idx + 2));
							if (idx < length)
								content.Append (line.Substring (idx + 2) + "\n");
							inDirective = false;
							directiveFound = true;
						} else {
							directive.Append (line);
							continue;
						}
					}
					
					if (directiveFound) {
						ParseDirective (directive.ToString ());
						directiveFound = false;
						if (gotDefault) {
							cachedType = CachingCompiler.GetTypeFromCache (physPath);
							if (cachedType != null)
								break;
						}

						continue;
					}

					content.Append (line + "\n");
				}
				directive = null;
			}

			if (!gotDefault)
				throw new ParseException (null, "No @" + DefaultDirectiveName +
							" directive found");

			if (cachedType == null)
				this.program = content.ToString ();
		}

		void TagParsed (ILocation location, System.Web.Compilation.TagType tagtype, string tagid, TagAttributes attributes)
		{
			if (tagtype != System.Web.Compilation.TagType.Directive)
				throw new ParseException (location, "Unexpected tag");

			if (tagid == null || tagid.Length == 0 || String.Compare (tagid, DefaultDirectiveName, true, Helpers.InvariantCulture) == 0) {
				AddDefaultDirective (location, attributes);
			} else if (String.Compare (tagid, "Assembly", true, Helpers.InvariantCulture) == 0) {
				AddAssemblyDirective (location, attributes);
			} else {
				throw new ParseException (location, "Unexpected directive: " + tagid);
			}
		}

		void TextParsed (ILocation location, string text)
		{
			if (text.Trim () != "")
				throw new ParseException (location, "Text not allowed here");
		}

		void ParseError (ILocation location, string message)
		{
			throw new ParseException (location, message);
		}

		static string GetAndRemove (IDictionary table, string key)
		{
			string o = table [key] as string;
			table.Remove (key);
			return o;
		}
		
		void ParseDirective (string line)
		{
			AspParser parser;

			using (StringReader input = new StringReader (line)) {
				parser = new AspParser (physPath, input);
			}
			
			parser.Error += new ParseErrorHandler (ParseError);
			parser.TagParsed += new TagParsedHandler (TagParsed);
			parser.TextParsed += new TextParsedHandler (TextParsed);

			parser.Parse ();
		}

		internal virtual void AddDefaultDirective (ILocation location, TagAttributes attrs)
		{
			CompilationSection compConfig;
			compConfig = CompilationConfig;
			
			if (gotDefault)
				throw new ParseException (location, "duplicate " + DefaultDirectiveName + " directive");

			gotDefault = true;
			IDictionary attributes = attrs.GetDictionary (null);
			className = GetAndRemove (attributes, "class");
			if (className == null)
				throw new ParseException (null, "No Class attribute found.");
			
			string d = GetAndRemove (attributes, "debug");
			if (d != null) {
				debug = (String.Compare (d, "true", true, Helpers.InvariantCulture) == 0);
				if (debug == false && String.Compare (d, "false", true, Helpers.InvariantCulture) != 0)
					throw new ParseException (null, "Invalid value for Debug attribute");
			} else
				debug = compConfig.Debug;

			language = GetAndRemove (attributes, "language");
			if (language == null)
				language = compConfig.DefaultLanguage;

			GetAndRemove (attributes, "codebehind");
			if (attributes.Count > 0)
				throw new ParseException (location, "Unrecognized attribute in " +
							  DefaultDirectiveName + " directive");
		}

		internal virtual void AddAssemblyDirective (ILocation location, TagAttributes attrs)
		{
			IDictionary tbl = attrs.GetDictionary (null);
			string name = GetAndRemove (tbl, "Name");
			string src = GetAndRemove (tbl, "Src");
			if (name == null && src == null)
				throw new ParseException (location, "You gotta specify Src or Name");

			if (name != null && src != null)
				throw new ParseException (location, "Src and Name cannot be used together");

			if (name != null) {
				AddAssemblyByName (name, location);
			} else {
				GetAssemblyFromSource (src, location);
			}

			if (tbl.Count > 0)
				throw new ParseException (location, "Unrecognized attribute in Assembly directive");
		}

		internal virtual void AddAssembly (Assembly assembly, bool fullPath)
		{
			if (assembly == null)
				throw new ArgumentNullException ("assembly");
			
			if (anames == null)
				anames = new Hashtable ();

			string name = assembly.GetName ().Name;
			string loc = assembly.Location;
			if (fullPath) {
				if (!assemblies.Contains (loc)) {
					assemblies.Add (loc);
				}

				anames [name] = loc;
				anames [loc] = assembly;
			} else {
				if (!assemblies.Contains (name)) {
					assemblies.Add (name);
				}

				anames [name] = assembly;
			}
		}

		internal virtual Assembly AddAssemblyByName (string name, ILocation location)
		{
			if (anames == null)
				anames = new Hashtable ();

			if (anames.Contains (name)) {
				object o = anames [name];
				if (o is string)
					o = anames [o];

				return (Assembly) o;
			}

			Assembly assembly = LoadAssemblyFromBin (name);
			if (assembly != null) {
				AddAssembly (assembly, true);
				return assembly;
			}

			Exception ex = null;
			try {
				assembly = Assembly.LoadWithPartialName (name);
			} catch (Exception e) {
				ex = e;
				assembly = null;
			}

			if (assembly == null)
				throw new ParseException (location, String.Format ("Assembly '{0}' not found", name), ex);
			
			AddAssembly (assembly, true);
			return assembly;
		}

		void AddAssembliesInBin ()
		{
			Exception ex;
			foreach (string s in HttpApplication.BinDirectoryAssemblies) {
				ex = null;
				
				try {
					Assembly assembly = Assembly.LoadFrom (s);
					AddAssembly (assembly, true);
				} catch (FileLoadException e) {
					ex = e;
					// ignore
				} catch (BadImageFormatException e) {
					ex = e;
					// ignore
				} catch (Exception e) {
					throw new Exception ("Error while loading " + s, e);
				}
				
				if (ex != null && RuntimeHelpers.DebuggingEnabled) {
					Console.WriteLine ("**** DEBUG MODE *****");
					Console.WriteLine ("Bad assembly found in bin/. Exception (ignored):");
					Console.WriteLine (ex);
				}
			}
		}

		Assembly LoadAssemblyFromBin (string name)
		{
			Assembly assembly = null;
			foreach (string dll in HttpApplication.BinDirectoryAssemblies) {
				string fn = Path.GetFileName (dll);
				fn = Path.ChangeExtension (fn, null);
				if (fn != name)
					continue;

				assembly = Assembly.LoadFrom (dll);
				return assembly;
			}
			
			return null;
		}

		Assembly GetAssemblyFromSource (string vpath, ILocation location)
		{
			vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
			string realPath = context.Request.MapPath (vpath);
			if (!File.Exists (realPath))
				throw new ParseException (location, "File " + vpath + " not found");

			AddDependency (vpath);

			CompilerResults result = CachingCompiler.Compile (language, realPath, realPath, assemblies);
			if (result.NativeCompilerReturnValue != 0) {
				using (StreamReader sr = new StreamReader (realPath)) {
					throw new CompilationException (realPath, result.Errors, sr.ReadToEnd ());
				}
			}

			AddAssembly (result.CompiledAssembly, true);
			return result.CompiledAssembly;
		}
		
		internal Type GetTypeFromBin (string tname)
		{
			if (tname == null || tname.Length == 0)
				throw new ArgumentNullException ("tname");
			
			Type result = null;
			string typeName;
			string assemblyName;
			int comma = tname.IndexOf (',');
			
			if (comma != -1) {
				typeName = tname.Substring (0, comma).Trim ();
				assemblyName = tname.Substring (comma + 1).Trim ();
			} else {
				typeName = tname;
				assemblyName = null;
			}

			Type type = null;
			Assembly assembly = null;
			if (assemblyName != null) {
				assembly = Assembly.Load (assemblyName);
				if (assembly != null)
					type = assembly.GetType (typeName, false);
				if (type != null)
					return type;
			}
			
			IList toplevelAssemblies = BuildManager.TopLevelAssemblies;
			if (toplevelAssemblies != null && toplevelAssemblies.Count > 0) {
				foreach (Assembly asm in toplevelAssemblies) {
					type = asm.GetType (typeName, false);
					if (type != null) {
						if (result != null)
							throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
						result = type;
					}
				}
			}

			foreach (string dll in HttpApplication.BinDirectoryAssemblies) {
				try {
					assembly = Assembly.LoadFrom (dll);
				} catch (FileLoadException) {
					// ignore
					continue;
				} catch (BadImageFormatException) {
					// ignore
					continue;
				}
				
				type = assembly.GetType (typeName, false);
				if (type != null) {
					if (result != null) 
						throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
						
					result = type;
				}
			}

			
			if (result == null)
				throw new HttpException (String.Format ("Type {0} not found.", typeName));

			return result;
		}
		
		internal virtual void AddDependency (string filename)
		{
			if (dependencies == null)
				dependencies = new ArrayList ();

			if (!dependencies.Contains (filename))
				dependencies.Add (filename);
		}
		
		// Properties
		protected abstract string DefaultDirectiveName { get; }

		internal HttpContext Context {
			get { return context; }
		}

		internal string VirtualPath {
			get { return vPath; }
		}

		internal string PhysicalPath {
			get { return physPath; }
		}

		internal string ClassName {
			get { return className; }
		}

		internal bool Debug {
			get { return debug; }
		}

		internal string Language {
			get { return language; }
		}

		internal string Program {
			get {
				if (program != null)
					return program;

				return String.Empty;
			}
		}

		internal ArrayList Assemblies {
			get {
				if (appAssemblyIndex != -1) {
					object o = assemblies [appAssemblyIndex];
					assemblies.RemoveAt (appAssemblyIndex);
					assemblies.Add (o);
					appAssemblyIndex = -1;
				}

				return assemblies;
			}
		}

		internal ArrayList Dependencies {
			get { return dependencies; }
		}

		internal string BaseDir {
			get {
				if (baseDir == null)
					baseDir = context.Request.MapPath (BaseVirtualDir);

				return baseDir;
			}
		}

		internal virtual string BaseVirtualDir {
			get {
				if (baseVDir == null)
					baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);

				return baseVDir;
			}
		}

		CompilationSection CompilationConfig {
			get {
				string vp = VirtualPath;
				if (String.IsNullOrEmpty (vp))
					return WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
				else
					return WebConfigurationManager.GetSection ("system.web/compilation", vp) as CompilationSection;
			}
		}

		internal TextReader Reader {
			get { return reader; }
			set { reader = value; }
		}
	}
}