New test.
[mono.git] / mcs / class / System.Web / System.Web.UI / PageParser.cs
1 //
2 // System.Web.UI.PageParser
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 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 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Web.Compilation;
36 using System.Web.Configuration;
37 using System.Web.Util;
38 using System.IO;
39
40 namespace System.Web.UI
41 {
42         // CAS - no InheritanceDemand here as the class is sealed
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public sealed class PageParser : TemplateControlParser
45         {
46                 bool enableSessionState = true;
47                 bool haveTrace;
48                 bool trace;
49                 bool notBuffer;
50                 TraceMode tracemode;
51                 bool readonlySessionState;
52                 string responseEncoding;
53                 string contentType;
54                 int codepage = -1;
55                 int lcid = -1;
56                 string culture;
57                 string uiculture;
58                 string errorPage;
59                 bool validateRequest;
60                 string clientTarget;
61                 Type baseType = typeof (Page);
62
63 #if NET_2_0
64                 string masterPage;
65                 Type masterType;
66                 string title;
67                 string theme;
68                 string styleSheetTheme;
69                 bool enable_event_validation;
70 #endif
71
72                 public PageParser ()
73                 {
74                 }
75                 
76                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
77                 {
78                         Context = context;
79                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
80                         InputFile = inputFile;
81                         SetBaseType (PagesConfig.PageBaseType);
82                         AddApplicationAssembly ();
83                 }
84
85 #if NET_2_0
86                 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
87                 {
88                         Context = context;
89                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
90                         Reader = reader;
91                         SetBaseType (PagesConfig.PageBaseType);
92                         AddApplicationAssembly ();
93                 }
94 #endif
95
96                 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
97                                                                     string inputFile, 
98                                                                     HttpContext context)
99                 {
100                         PageParser pp = new PageParser (virtualPath, inputFile, context);
101                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
102                         return h;
103                 }
104
105                 internal override void ProcessMainAttributes (Hashtable atts)
106                 {
107                         string enabless = GetString (atts, "EnableSessionState",
108 #if NET_2_0
109                                                      PagesConfig.EnableSessionState.ToString()
110 #else
111                                                      PagesConfig.EnableSessionState
112 #endif
113                                                      );
114                         if (enabless != null) {
115                                 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
116                                 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
117                                         enableSessionState = true;
118                                 } else if (String.Compare (enabless, "false", true) == 0) {
119                                         enableSessionState = false;
120                                 } else {
121                                         ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
122                                 }
123                         }
124
125                         string cp = GetString (atts, "CodePage", null);
126                         if (cp != null) {
127                                 if (responseEncoding != null)
128                                         ThrowParseException ("CodePage and ResponseEncoding are " +
129                                                              "mutually exclusive.");
130
131                                 int codepage = 0;
132                                 try {
133                                         codepage = (int) UInt32.Parse (cp);
134                                 } catch {
135                                         ThrowParseException ("Invalid value for CodePage: " + cp);
136                                 }
137
138                                 try {
139                                         Encoding.GetEncoding (codepage);
140                                 } catch {
141                                         ThrowParseException ("Unsupported codepage: " + cp);
142                                 }
143                         }
144                         
145                         responseEncoding = GetString (atts, "ResponseEncoding", null);
146                         if (responseEncoding != null) {
147                                 if (codepage != -1)
148                                         ThrowParseException ("CodePage and ResponseEncoding are " +
149                                                              "mutually exclusive.");
150
151                                 try {
152                                         Encoding.GetEncoding (responseEncoding);
153                                 } catch {
154                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
155                                 }
156                         }
157                         
158                         contentType = GetString (atts, "ContentType", null);
159
160                         string lcidStr = GetString (atts, "LCID", null);
161                         if (lcidStr != null) {
162                                 try {
163                                         lcid = (int) UInt32.Parse (lcidStr);
164                                 } catch {
165                                         ThrowParseException ("Invalid value for LCID: " + lcid);
166                                 }
167
168                                 CultureInfo ci = null;
169                                 try {
170                                         ci = new CultureInfo (lcid);
171                                 } catch {
172                                         ThrowParseException ("Unsupported LCID: " + lcid);
173                                 }
174
175                                 if (ci.IsNeutralCulture) {
176                                         string suggestedCulture = SuggestCulture (ci.Name);
177                                         string fmt = "LCID attribute must be set to a non-neutral Culture.";
178                                         if (suggestedCulture != null) {
179                                                 ThrowParseException (fmt + " Please try one of these: " +
180                                                                      suggestedCulture);
181                                         } else {
182                                                 ThrowParseException (fmt);
183                                         }
184                                 }
185                         }
186
187                         culture = GetString (atts, "Culture", null);
188                         if (culture != null) {
189                                 if (lcidStr != null) 
190                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
191                                 
192                                 CultureInfo ci = null;
193                                 try {
194                                         ci = new CultureInfo (culture);                                 
195                                 } catch {
196                                         ThrowParseException ("Unsupported Culture: " + culture);
197                                 }
198
199                                 if (ci.IsNeutralCulture) {
200                                         string suggestedCulture = SuggestCulture (culture);
201                                         string fmt = "Culture attribute must be set to a non-neutral Culture.";
202                                         if (suggestedCulture != null)
203                                                 ThrowParseException (fmt +
204                                                                 " Please try one of these: " + suggestedCulture);
205                                         else
206                                                 ThrowParseException (fmt);
207                                 }
208                         }
209
210                         uiculture = GetString (atts, "UICulture", null);
211                         if (uiculture != null) {
212                                 CultureInfo ci = null;
213                                 try {
214                                         ci = new CultureInfo (uiculture);                                       
215                                 } catch {
216                                         ThrowParseException ("Unsupported Culture: " + uiculture);
217                                 }
218
219                                 if (ci.IsNeutralCulture) {
220                                         string suggestedCulture = SuggestCulture (uiculture);
221                                         string fmt = "UICulture attribute must be set to a non-neutral Culture.";
222                                         if (suggestedCulture != null)
223                                                 ThrowParseException (fmt +
224                                                                 " Please try one of these: " + suggestedCulture);
225                                         else
226                                                 ThrowParseException (fmt);
227                                 }
228                         }
229
230                         string tracestr = GetString (atts, "Trace", null);
231                         if (tracestr != null) {
232                                 haveTrace = true;
233                                 atts ["Trace"] = tracestr;
234                                 trace = GetBool (atts, "Trace", false);
235                         }
236
237                         string tracemodes = GetString (atts, "TraceMode", null);
238                         if (tracemodes != null) {
239                                 bool valid = true;
240                                 try {
241                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
242                                 } catch {
243                                         valid = false;
244                                 }
245
246                                 if (!valid || tracemode == TraceMode.Default)
247                                         ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
248                                                         "one of the following values: SortByTime, SortByCategory.");
249                         }
250
251                         errorPage = GetString (atts, "ErrorPage", null);
252                         validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
253                         clientTarget = GetString (atts, "ClientTarget", null);
254                         if (clientTarget != null) {
255 #if NET_2_0
256                                 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
257                                 if (sec.ClientTargets[clientTarget] == null) {
258                                         ThrowParseException (String.Format (
259                                                         "ClientTarget '{0}' is an invalid alias. See the " +
260                                                         "documentation for <clientTarget> config. section.",
261                                                         clientTarget));
262                                 }
263                                 clientTarget = sec.ClientTargets[clientTarget].UserAgent;
264 #else
265                                 NameValueCollection coll;
266                                 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
267                                 if (coll == null || coll [clientTarget] == null) {
268                                         ThrowParseException (String.Format (
269                                                         "ClientTarget '{0}' is an invalid alias. See the " +
270                                                         "documentation for <clientTarget> config. section.",
271                                                         clientTarget));
272                                 }
273                                 clientTarget = (string) coll [clientTarget];
274 #endif
275                         }
276
277                         notBuffer = !GetBool (atts, "Buffer", true);
278                         
279 #if NET_2_0
280                         masterPage = GetString (atts, "MasterPageFile", null);
281                         
282                         // Make sure the page exists
283                         if (masterPage != null)
284                                 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
285
286                         title = GetString(atts, "Title", null);
287
288                         theme = GetString (atts, "Theme", null);
289                         styleSheetTheme = GetString (atts, "StyleSheetTheme", null);
290                         enable_event_validation = GetBool (atts, "EnableEventValidation", true);
291 #endif
292                         // Ignored by now
293                         GetString (atts, "EnableViewStateMac", null);
294                         GetString (atts, "SmartNavigation", null);
295
296                         base.ProcessMainAttributes (atts);
297                 }
298                 
299 #if NET_2_0
300                 internal override void AddDirective (string directive, Hashtable atts)
301                 {
302                         if (String.Compare ("MasterType", directive, true) == 0) {
303                                 string type = GetString (atts, "TypeName", null);
304                                 if (type != null) {
305                                         masterType = LoadType (type);
306                                         if (masterType == null)
307                                                 ThrowParseException ("Could not load type '" + type + "'.");
308                                 } else {
309                                         string path = GetString (atts, "VirtualPath", null);
310                                         if (path != null)
311                                                 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
312                                         else
313                                                 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
314                                 }
315                                 AddAssembly (masterType.Assembly, true);
316                         }
317                         else
318                                 base.AddDirective (directive, atts);
319                 }
320 #endif
321                 
322                 static string SuggestCulture (string culture)
323                 {
324                         string retval = null;
325                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
326                                 if (ci.Name.StartsWith (culture))
327                                         retval += ci.Name + " ";
328                         }
329                         return retval;
330                 }
331
332                 protected override Type CompileIntoType ()
333                 {
334                         AspGenerator generator = new AspGenerator (this);
335                         return generator.GetCompiledType ();
336                 }
337
338                 internal bool EnableSessionState {
339                         get { return enableSessionState; }
340                 }
341                 
342                 internal bool ReadOnlySessionState {
343                         get { return readonlySessionState; }
344                 }
345
346                 internal bool HaveTrace {
347                         get { return haveTrace; }
348                 }
349
350                 internal bool Trace {
351                         get { return trace; }
352                 }
353
354                 internal TraceMode TraceMode {
355                         get { return tracemode; }
356                 }
357                 
358                 internal override Type DefaultBaseType {
359                         get { return baseType; }
360                 }
361
362                 internal override string DefaultBaseTypeName {
363                         get { return "System.Web.UI.Page"; }
364                 }
365
366                 internal override string DefaultDirectiveName {
367                         get { return "page"; }
368                 }
369
370                 internal string ResponseEncoding {
371                         get { return responseEncoding; }
372                 }
373
374                 internal string ContentType {
375                         get { return contentType; }
376                 }
377
378                 internal int CodePage {
379                         get { return codepage; }
380                 }
381
382                 internal string Culture {
383                         get { return culture; }
384                 }
385
386                 internal string UICulture {
387                         get { return uiculture; }
388                 }
389
390                 internal int LCID {
391                         get { return lcid; }
392                 }
393
394                 internal string ErrorPage {
395                         get { return errorPage; }
396                 }
397
398                 internal bool ValidateRequest {
399                         get { return validateRequest; }
400                 }
401
402                 internal string ClientTarget {
403                         get { return clientTarget; }
404                 }
405
406                 internal bool NotBuffer {
407                         get { return notBuffer; }
408                 }
409
410 #if NET_2_0
411                 internal string Theme {
412                         get { return theme; }
413                 }
414
415                 internal string StyleSheetTheme {
416                         get { return styleSheetTheme; }
417                 }
418
419                 internal string MasterPageFile {
420                         get { return masterPage; }
421                 }
422                 
423                 internal Type MasterType {
424                         get { return masterType; }
425                 }
426
427                 internal string Title {
428                         get { return title; }
429                 }
430
431                 internal bool EnableEventValidation {
432                         get { return enable_event_validation; }
433                 }
434 #endif
435         }
436 }
437