New test.
[mono.git] / mcs / class / System.Web / System.Web / HttpServerUtility.cs
1 //
2 // System.Web.HttpRequest.cs 
3 //
4 // 
5 // Author:
6 //      Miguel de Icaza (miguel@novell.com)
7 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
8 //
9
10 //
11 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.IO;
34 using System.Web.UI;
35 using System.Web.Util;
36 using System.Collections.Specialized;
37 using System.Security.Permissions;
38
39 namespace System.Web {
40
41         //
42         // Methods exposed through HttpContext.Server property
43         //
44         
45         // CAS - no InheritanceDemand here as the class is sealed
46         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47         public sealed class HttpServerUtility {
48                 HttpContext context;
49                 
50                 internal HttpServerUtility (HttpContext context)
51                 {
52                         this.context = context;
53                 }
54
55                 public void ClearError ()
56                 {
57                         context.ClearError ();
58                 }
59
60                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
61                 public object CreateObject (string progID)
62                 {
63                         throw new HttpException (500, "COM is not supported");
64                 }
65
66                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
67                 public object CreateObject (Type type)
68                 {
69                         throw new HttpException (500, "COM is not supported");
70                 }
71
72                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
73                 public object CreateObjectFromClsid (string clsid)
74                 {
75                         throw new HttpException (500, "COM is not supported");
76                 }
77
78                 public void Execute (string path)
79                 {
80                         Execute (path, null, true);
81                 }
82
83                 public void Execute (string path, TextWriter writer)
84                 {
85                         Execute (path, writer, true);
86                 }
87
88 #if NET_2_0
89                 public
90 #else
91                 internal
92 #endif
93                 void Execute (string path, TextWriter writer, bool preserveQuery)
94                 {
95                         if (path == null)
96                                 throw new ArgumentNullException ("path");
97
98                         if (path.IndexOf (':') != -1)
99                                 throw new ArgumentException ("Invalid path.");
100
101                         HttpRequest request = context.Request;
102                         string oldQuery = request.QueryStringRaw;
103                         int qmark = path.IndexOf ('?');
104                         if (qmark != -1) {
105                                 request.QueryStringRaw = path.Substring (qmark + 1);
106                                 path = path.Substring (0, qmark);
107                         } else if (!preserveQuery) {
108                                 request.QueryStringRaw = "";
109                         }
110
111                         HttpResponse response = context.Response;
112                         WebROCollection oldForm = null;
113                         if (!preserveQuery) {
114                                 oldForm = request.Form as WebROCollection;
115                                 request.SetForm (new WebROCollection ());
116                         }
117
118                         TextWriter output = writer;
119                         if (output == null)
120                                 output = response.Output;
121
122                         string oldFilePath = request.FilePath;
123                         request.SetCurrentExePath (UrlUtils.Combine (request.BaseVirtualDir, path));
124                         IHttpHandler handler = context.ApplicationInstance.GetHandler (context);
125                         TextWriter previous = null;
126                         try {
127                                 previous = response.SetTextWriter (output);
128                                 if (!(handler is IHttpAsyncHandler)) {
129                                         handler.ProcessRequest (context);
130                                 } else {
131                                         IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
132                                         IAsyncResult ar = asyncHandler.BeginProcessRequest (context, null, null);
133                                         ar.AsyncWaitHandle.WaitOne ();
134                                         asyncHandler.EndProcessRequest (ar);
135                                 }
136                         } finally {
137                                 request.SetCurrentExePath (oldFilePath);
138                                 if (oldQuery != null && oldQuery != "" && oldQuery != request.QueryStringRaw) {
139                                         oldQuery = oldQuery.Substring (1); // Ignore initial '?'
140                                         request.QueryStringRaw = oldQuery; // which is added here.
141                                 }
142                                 response.SetTextWriter (previous);
143                                 if (!preserveQuery)
144                                         request.SetForm (oldForm);
145                         }
146                 }
147
148                 public Exception GetLastError ()
149                 {
150                         if (context == null)
151                                 return HttpContext.Current.Error;
152                         return context.Error;
153                 }
154
155                 public string HtmlDecode (string s)
156                 {
157                         return HttpUtility.HtmlDecode (s);
158                 }
159
160                 public void HtmlDecode (string s, TextWriter output)
161                 {
162                         HttpUtility.HtmlDecode (s, output);
163                 }
164
165                 public string HtmlEncode (string s)
166                 {
167                         return HttpUtility.HtmlEncode (s);
168                 }
169
170                 public void HtmlEncode (string s, TextWriter output)
171                 {
172                         HttpUtility.HtmlEncode (s, output);
173                 }
174
175                 public string MapPath (string path)
176                 {
177                         return context.Request.MapPath (path);
178                 }
179
180                 public void Transfer (string path)
181                 {
182                         // If it's a page and a postback, don't pass form data
183                         // See bug #65613.
184                         bool preserveForm = true;
185                         if (context.Handler is Page) {
186                                 Page page = (Page) context.Handler;
187                                 preserveForm = !page.IsPostBack;
188                         }
189
190                         Transfer (path, preserveForm);
191                 }
192
193                 public void Transfer (string path, bool preserveForm)
194                 {
195                         Execute (path, null, preserveForm);
196                         context.Response.End ();
197                 }
198 #if NET_2_0
199                 [MonoTODO ("Not implemented")]
200                 public void Transfer (IHttpHandler handler, bool preserveForm)
201                 {
202                         throw new NotImplementedException ();
203                 }
204 #endif
205                 public string UrlDecode (string s)
206                 {
207                         return HttpUtility.UrlDecode (s);
208                 }
209
210                 public void UrlDecode (string s, TextWriter output)
211                 {
212                         if (s != null)
213                                 output.Write (HttpUtility.UrlDecode (s));
214                 }
215
216                 public string UrlEncode (string s)
217                 {
218                         return HttpUtility.UrlEncode (s);
219                 }
220
221                 public void UrlEncode (string s, TextWriter output)
222                 {
223                         if (s != null)
224                                 output.Write (HttpUtility.UrlEncode (s));
225                 }
226
227                 public string UrlPathEncode (string s)
228                 {
229                         if (s == null)
230                                 return null;
231
232                         int idx = s.IndexOf ("?");
233                         string s2 = null;
234                         if (idx != -1) {
235                                 s2 = s.Substring (0, idx-1);
236                                 s2 = HttpUtility.UrlEncode (s2) + s.Substring (idx);
237                         } else {
238                                 s2 = HttpUtility.UrlEncode (s);
239                         }
240
241                         return s2;
242                 }
243
244                 public string MachineName {
245                         [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
246                         // Medium doesn't look heavy enough to replace this... reported as
247                         [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
248                         [EnvironmentPermission (SecurityAction.Assert, Read = "COMPUTERNAME")]
249                         get { return Environment.MachineName; }
250                 }
251
252                 public int ScriptTimeout {
253                         get { return (int) context.ConfigTimeout.TotalSeconds; }
254                         [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
255                         set { context.ConfigTimeout = new TimeSpan (0, 0, value); }
256                 }
257         }
258 }