svn path=/branches/mono-1-1-9/mcs/; revision=50438
[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;
34 using System.IO;
35 using System.Web.UI;
36 using System.Web.Util;
37 using System.Collections.Specialized;
38
39 namespace System.Web {
40
41         //
42         // Methods exposed through HttpContext.Server property
43         //
44         
45         public sealed class HttpServerUtility {
46                 HttpContext context;
47                 
48                 internal HttpServerUtility (HttpContext context)
49                 {
50                         this.context = context;
51                 }
52
53                 public void ClearError ()
54                 {
55                         context.ClearError ();
56                 }
57
58                 public object CreateObject (string progID)
59                 {
60                         throw new HttpException (500, "COM is not supported");
61                 }
62
63                 public object CreateObject (Type type)
64                 {
65                         throw new HttpException (500, "COM is not supported");
66                 }
67
68                 public object CreateObjectFromClsid (string clsid)
69                 {
70                         throw new HttpException (500, "COM is not supported");
71                 }
72
73                 public void Execute (string path)
74                 {
75                         Execute (path, null);
76                 }
77
78                 public void Execute (string path, TextWriter writer)
79                 {
80                         Execute (path, writer, false);
81                 }
82
83 #if NET_2_0
84                 public
85 #else
86                 internal
87 #endif
88                 void Execute (string path, TextWriter writer, bool preserveQuery)
89                 {
90                         if (path == null)
91                                 throw new ArgumentNullException ("path");
92
93                         if (path.IndexOf (':') != -1)
94                                 throw new ArgumentException ("Invalid path.");
95
96                         int qmark = path.IndexOf ('?');
97                         string query;
98                         if (qmark != -1) {
99                                 query = path.Substring (qmark + 1);
100                                 path = path.Substring (0, qmark);
101                         } else {
102                                 query = "";
103                         }
104
105                         HttpRequest request = context.Request;
106                         HttpResponse response = context.Response;
107
108                         string oldQuery = request.QueryStringRaw;
109                         request.QueryStringRaw = query;
110
111                         WebROCollection oldForm = null;
112                         if (!preserveQuery) {
113                                 oldForm = request.Form as WebROCollection;
114                                 request.SetForm (new WebROCollection ());
115                         }
116
117                         TextWriter output = writer;
118                         if (output == null)
119                                 output = response.Output;
120
121                         string oldFilePath = request.FilePath;
122                         request.SetCurrentExePath (UrlUtils.Combine (request.BaseVirtualDir, path));
123                         IHttpHandler handler = context.ApplicationInstance.GetHandler (context);
124                         TextWriter previous = null;
125                         try {
126                                 previous = response.SetTextWriter (output);
127                                 if (!(handler is IHttpAsyncHandler)) {
128                                         handler.ProcessRequest (context);
129                                 } else {
130                                         IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
131                                         IAsyncResult ar = asyncHandler.BeginProcessRequest (context, null, null);
132                                         ar.AsyncWaitHandle.WaitOne ();
133                                         asyncHandler.EndProcessRequest (ar);
134                                 }
135                         } finally {
136                                 request.SetCurrentExePath (oldFilePath);
137                                 request.QueryStringRaw = oldQuery;
138                                 response.SetTextWriter (previous);
139                                 if (!preserveQuery)
140                                         request.SetForm (oldForm);
141                         }
142                 }
143
144                 public Exception GetLastError ()
145                 {
146                         return context.Error;
147                 }
148
149                 public string HtmlDecode (string s)
150                 {
151                         return HttpUtility.HtmlDecode (s);
152                 }
153
154                 public void HtmlDecode (string s, TextWriter output)
155                 {
156                         HttpUtility.HtmlDecode (s, output);
157                 }
158
159                 public string HtmlEncode (string s)
160                 {
161                         return HttpUtility.HtmlEncode (s);
162                 }
163
164                 public void HtmlEncode (string s, TextWriter output)
165                 {
166                         HttpUtility.HtmlEncode (s, output);
167                 }
168
169                 public string MapPath (string path)
170                 {
171                         return context.Request.MapPath (path);
172                 }
173
174                 public void Transfer (string path)
175                 {
176                         // If it's a page and a postback, don't pass form data
177                         // See bug #65613.
178                         bool preserveForm = true;
179                         if (context.Handler is Page) {
180                                 Page page = (Page) context.Handler;
181                                 preserveForm = !page.IsPostBack;
182                         }
183
184                         Transfer (path, preserveForm);
185                 }
186
187                 public void Transfer (string path, bool preserveForm)
188                 {
189                         Execute (path, null, preserveForm);
190                         context.Response.End ();
191                 }
192
193                 public string UrlDecode (string s)
194                 {
195                         return HttpUtility.UrlDecode (s);
196                 }
197
198                 public void UrlDecode (string s, TextWriter output)
199                 {
200                         if (s != null)
201                                 output.Write (HttpUtility.UrlDecode (s));
202                 }
203
204                 public string UrlEncode (string s)
205                 {
206                         return HttpUtility.UrlEncode (s);
207                 }
208
209                 public void UrlEncode (string s, TextWriter output)
210                 {
211                         if (s != null)
212                                 output.Write (HttpUtility.UrlEncode (s));
213                 }
214
215                 public string UrlPathEncode (string s)
216                 {
217                         if (s == null)
218                                 return null;
219
220                         int idx = s.IndexOf ("?");
221                         string s2 = null;
222                         if (idx != -1) {
223                                 s2 = s.Substring (0, idx-1);
224                                 s2 = HttpUtility.UrlEncode (s2) + s.Substring (idx);
225                         } else {
226                                 s2 = HttpUtility.UrlEncode (s);
227                         }
228
229                         return s2;
230                 }
231
232                 public string MachineName {
233                         get { return Environment.MachineName; }
234                 }
235
236                 public int ScriptTimeout {
237                         get { return (int) context.ConfigTimeout.TotalSeconds; }
238                         set { context.ConfigTimeout = new TimeSpan (0, 0, value); }
239                 }
240         }
241 }
242