3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Namespace: System.Web
24 * Class: HttpServerUtility
27 * Wictor Wilén (wictor@ibizkit.se)
28 * Patrik Torstensson (patrik.torstensson@labs2.com)
29 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
31 * (c) Wictor Wilén (2002)
32 * (c) 2002 Patrik Torstensson
33 * (c) 2003 Ximian, Inc. (http://www.ximian.com)
35 * (This log is no longer maintained)
36 * ---------------------------------------
37 * 2002-03-27 Wictor Started implementation
38 * 2002-04-09 Patrik Added HttpContext constructor
39 * 2002-04-10 Patrik Moved encoding to HttpUtility and
40 * fixed all functions that used
45 using System.Collections.Specialized;
48 using System.Web.Hosting;
50 using System.Web.Util;
54 public sealed class HttpServerUtility
56 private static string _name;
58 private HttpContext _Context;
59 private HttpApplication _Application;
61 internal HttpServerUtility (HttpContext Context)
66 internal HttpServerUtility (HttpApplication app)
75 /// Gets the server's computer name.
77 public string MachineName {
80 _name = Environment.MachineName;
86 public int ScriptTimeout {
88 return (int) _Context.ConfigTimeout.TotalSeconds;
92 throw new ArgumentOutOfRangeException ("value");
94 _Context.ConfigTimeout = new TimeSpan (0, 0, value);
101 /// Clears the previous exception.
103 public void ClearError ()
105 if (null != _Context) {
106 _Context.ClearError ();
110 if (null != _Application) {
111 _Application.ClearError ();
117 /// Creates a server instance of a COM object identified by the object's Programmatic Identifier (ProgID).
119 /// <param name="progID">The class or type of object to be instantiated. </param>
120 /// <returns>The new object.</returns>
121 public object CreateObject (string progID)
123 return CreateObject (Type.GetTypeFromProgID (progID));
128 /// Creates a server instance of a COM object identified by the object's type.
130 /// <param name="type">A Type representing the object to create. </param>
131 /// <returns>The new object.</returns>
132 public object CreateObject (Type type)
134 return Activator.CreateInstance (type);
139 /// Creates a server instance of a COM object identified by the object's class identifier (CLSID).
141 /// <param name="clsid">The class identifier of the object to be instantiated. </param>
142 /// <returns>The new object.</returns>
143 public object CreateObjectFromClsid (string clsid)
145 Guid guid = new Guid (clsid);
146 return CreateObject (Type.GetTypeFromCLSID (guid));
151 /// Executes a request to another page using the specified URL path to the page.
153 /// <param name="path">The URL path of the new request. </param>
154 public void Execute (string path)
156 Execute (path, null);
161 /// Executes a request to another page using the specified URL path to the page.
162 /// A TextWriter captures output from the page.
164 /// <param name="path">The URL path of the new request. </param>
165 /// <param name="writer">The TextWriter to capture the output. </param>
166 public void Execute (string path, TextWriter writer)
168 Execute (path, writer, false);
176 void Execute (string path, TextWriter writer, bool preserveQuery)
179 throw new ArgumentNullException ("path");
181 if (_Context == null)
182 throw new HttpException ("No context available.");
184 if (path.IndexOf (':') != -1)
185 throw new ArgumentException ("Invalid path.");
187 int qmark = path.IndexOf ('?');
190 query = path.Substring (qmark + 1);
191 path = path.Substring (0, qmark);
196 string filePath = _Context.Request.MapPath (path);
197 HttpResponse response = _Context.Response;
198 TextWriter output = writer;
200 output = response.Output;
202 HttpRequest request = _Context.Request;
203 string oldFilePath = request.FilePath;
204 request.SetFilePath (UrlUtils.Combine (_Context.Request.BaseVirtualDir, path));
205 string oldQuery = request.QueryStringRaw;
207 if (!preserveQuery) request.QueryStringRaw = query;
208 IHttpHandler handler = _Context.ApplicationInstance.CreateHttpHandler (_Context,
212 TextWriter previous = null;
214 previous = response.SetTextWriter (output);
215 if (!(handler is IHttpAsyncHandler)) {
216 handler.ProcessRequest (_Context);
218 IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
219 IAsyncResult ar = asyncHandler.BeginProcessRequest (_Context, null, null);
220 ar.AsyncWaitHandle.WaitOne ();
221 asyncHandler.EndProcessRequest (ar);
224 request.SetFilePath (oldFilePath);
225 request.QueryStringRaw = oldQuery;
226 response.SetTextWriter (previous);
232 /// Returns the previous exception.
234 /// <returns>The previous exception that was thrown.</returns>
235 public Exception GetLastError ()
237 if (_Context == null)
240 return _Context.Error;
246 /// Decodes an HTML-encoded string and returns the decoded string.
248 /// <param name="s">The HTML string to decode. </param>
249 /// <returns>The decoded text.</returns>
250 public string HtmlDecode (string s)
252 return HttpUtility.HtmlDecode (s);
257 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
259 /// <param name="s">The HTML string to decode</param>
260 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
261 public void HtmlDecode (string s, TextWriter output)
263 output.Write (HttpUtility.HtmlDecode (s));
267 /// HTML-encodes a string and returns the encoded string.
269 /// <param name="s">The text string to encode. </param>
270 /// <returns>The HTML-encoded text.</returns>
271 public string HtmlEncode (string s)
273 return HttpUtility.HtmlEncode (s);
277 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
279 /// <param name="s">The string to encode. </param>
280 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
281 public void HtmlEncode (string s, TextWriter output)
283 output.Write (HtmlEncode (s));
288 /// Returns the physical file path that corresponds to the specified virtual path on the Web server.
290 /// <param name="path">The virtual path on the Web server. </param>
291 /// <returns>The physical file path that corresponds to path.</returns>
292 public string MapPath (string path)
294 if (null == _Context)
295 throw new HttpException ("MapPath is not available");
297 return _Context.Request.MapPath (path);
301 /// Terminates execution of the current page and begins execution of a new page using the specified
302 /// URL path to the page.
304 /// <param name="path">The URL path of the new page on the server to execute. </param>
305 public void Transfer (string path)
307 // If it's a page and a postback, don't pass form data
309 bool preserveForm = true;
310 if (_Context.Handler is Page) {
311 Page page = (Page) _Context.Handler;
312 preserveForm = !page.IsPostBack;
315 Transfer (path, preserveForm);
319 /// Terminates execution of the current page and begins execution of a new page using the specified
320 /// URL path to the page. Specifies whether to clear the QueryString and Form collections.
322 /// <param name="path">The URL path of the new page on the server to execute. </param>
323 /// <param name="preserveForm">If true, the QueryString and Form collections are preserved. If false,
324 /// they are cleared. The default is false. </param>
325 public void Transfer (string path, bool preserveForm)
327 HttpValueCollection oldForm = null;
329 oldForm = _Context.Request.Form as HttpValueCollection;
330 _Context.Request.SetForm (new HttpValueCollection ());
333 Execute (path, null, preserveForm);
335 _Context.Request.SetForm (oldForm);
337 _Context.Response.End ();
341 /// URL-decodes a string and returns the decoded string.
343 /// <param name="s">The text string to decode. </param>
344 /// <returns>The decoded text.</returns>
346 /// Post/html encoding @ ftp://ftp.isi.edu/in-notes/rfc1866.txt
347 /// Uncomment the line marked with RFC1738 to get pure RFC1738
348 /// and it will also consider the RFC1866 (ftp://ftp.isi.edu/in-notes/rfc1866.txt)
349 /// `application/x-www-form-urlencoded' format
351 public string UrlDecode (string s)
353 return HttpUtility.UrlDecode (s);
357 /// Decodes an HTML string received in a URL and sends the resulting output to a TextWriter output stream.
359 /// <param name="s"></param>
360 /// <param name="output"></param>
361 public void UrlDecode (string s, TextWriter output)
364 output.Write (UrlDecode (s));
368 /// URL-encodes a string and returns the encoded string.
370 /// <param name="s">The text to URL-encode. </param>
371 /// <returns>The URL encoded text.</returns>
372 public string UrlEncode (string s)
374 return HttpUtility.UrlEncode (s);
378 /// URL encodes a string and sends the resulting output to a TextWriter output stream.
380 /// <param name="s">The text string to encode. </param>
381 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
382 public void UrlEncode (string s, TextWriter output)
385 output.Write (UrlEncode (s));
389 /// URL-encodes the path portion of a URL string and returns the encoded string.
391 /// <param name="s">The text to URL-encode.</param>
392 /// <returns>The URL encoded text.</returns>
393 /// <remarks>Does not do any browser specific adjustments, just encode everything</remarks>
394 public string UrlPathEncode (string s)
399 int idx = s.IndexOf ("?");
402 s2 = s.Substring (0, idx-1);
403 s2 = UrlEncode (s2) + s.Substring (idx);