// // 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. // /** * Namespace: System.Web * Class: HttpServerUtility * * Authosr: * Wictor Wilén (wictor@ibizkit.se) * Patrik Torstensson (patrik.torstensson@labs2.com) * Gonzalo Paniagua Javier (gonzalo@ximian.com) * * (c) Wictor Wilén (2002) * (c) 2002 Patrik Torstensson * (c) 2003 Ximian, Inc. (http://www.ximian.com) * * (This log is no longer maintained) * --------------------------------------- * 2002-03-27 Wictor Started implementation * 2002-04-09 Patrik Added HttpContext constructor * 2002-04-10 Patrik Moved encoding to HttpUtility and * fixed all functions that used * HttpContext * */ using System; using System.Collections.Specialized; using System.IO; using System.Text; using System.Web.Hosting; using System.Web.UI; using System.Web.Util; namespace System.Web { public sealed class HttpServerUtility { private static string _name; private HttpContext _Context; private HttpApplication _Application; internal HttpServerUtility (HttpContext Context) { _Context = Context; } internal HttpServerUtility (HttpApplication app) { _Application = app; } // Properties /// /// Gets the server's computer name. /// public string MachineName { get { if(_name == null) _name = Environment.MachineName; return _name; } } public int ScriptTimeout { get { return (int) _Context.ConfigTimeout.TotalSeconds; } set { if (value <= 0) throw new ArgumentOutOfRangeException ("value"); _Context.ConfigTimeout = new TimeSpan (0, 0, value); } } // Methods /// /// Clears the previous exception. /// public void ClearError () { if (null != _Context) { _Context.ClearError (); return; } if (null != _Application) { _Application.ClearError (); } } /// /// Creates a server instance of a COM object identified by the object's Programmatic Identifier (ProgID). /// /// The class or type of object to be instantiated. /// The new object. public object CreateObject (string progID) { return CreateObject (Type.GetTypeFromProgID (progID)); } /// /// Creates a server instance of a COM object identified by the object's type. /// /// A Type representing the object to create. /// The new object. public object CreateObject (Type type) { return Activator.CreateInstance (type); } /// /// Creates a server instance of a COM object identified by the object's class identifier (CLSID). /// /// The class identifier of the object to be instantiated. /// The new object. public object CreateObjectFromClsid (string clsid) { Guid guid = new Guid (clsid); return CreateObject (Type.GetTypeFromCLSID (guid)); } /// /// Executes a request to another page using the specified URL path to the page. /// /// The URL path of the new request. public void Execute (string path) { Execute (path, null); } /// /// Executes a request to another page using the specified URL path to the page. /// A TextWriter captures output from the page. /// /// The URL path of the new request. /// The TextWriter to capture the output. public void Execute (string path, TextWriter writer) { Execute (path, writer, false); } #if NET_2_0 public #else internal #endif void Execute (string path, TextWriter writer, bool preserveQuery) { if (path == null) throw new ArgumentNullException ("path"); if (_Context == null) throw new HttpException ("No context available."); if (path.IndexOf (':') != -1) throw new ArgumentException ("Invalid path."); int qmark = path.IndexOf ('?'); string query; if (qmark != -1) { query = path.Substring (qmark + 1); path = path.Substring (0, qmark); } else { query = ""; } HttpRequest request = _Context.Request; HttpResponse response = _Context.Response; string oldQuery = request.QueryStringRaw; request.QueryStringRaw = query; HttpValueCollection oldForm = null; if (!preserveQuery) { oldForm = _Context.Request.Form as HttpValueCollection; _Context.Request.SetForm (new HttpValueCollection ()); } string filePath = _Context.Request.MapPath (path); TextWriter output = writer; if (output == null) output = response.Output; string oldFilePath = request.FilePath; request.SetCurrentExePath (UrlUtils.Combine (_Context.Request.BaseVirtualDir, path)); IHttpHandler handler = _Context.ApplicationInstance.CreateHttpHandler (_Context, request.RequestType, path, filePath); TextWriter previous = null; try { previous = response.SetTextWriter (output); if (!(handler is IHttpAsyncHandler)) { handler.ProcessRequest (_Context); } else { IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler; IAsyncResult ar = asyncHandler.BeginProcessRequest (_Context, null, null); ar.AsyncWaitHandle.WaitOne (); asyncHandler.EndProcessRequest (ar); } } finally { request.SetCurrentExePath (oldFilePath); request.QueryStringRaw = oldQuery; response.SetTextWriter (previous); if (!preserveQuery) _Context.Request.SetForm (oldForm); } } /// /// Returns the previous exception. /// /// The previous exception that was thrown. public Exception GetLastError () { if (_Context == null) return null; return _Context.Error; } /// /// Decodes an HTML-encoded string and returns the decoded string. /// /// The HTML string to decode. /// The decoded text. public string HtmlDecode (string s) { return HttpUtility.HtmlDecode (s); } /// /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream. /// /// The HTML string to decode /// The TextWriter output stream containing the decoded string. public void HtmlDecode (string s, TextWriter output) { output.Write (HttpUtility.HtmlDecode (s)); } /// /// HTML-encodes a string and returns the encoded string. /// /// The text string to encode. /// The HTML-encoded text. public string HtmlEncode (string s) { return HttpUtility.HtmlEncode (s); } /// /// HTML-encodes a string and sends the resulting output to a TextWriter output stream. /// /// The string to encode. /// The TextWriter output stream containing the encoded string. public void HtmlEncode (string s, TextWriter output) { output.Write (HtmlEncode (s)); } /// /// Returns the physical file path that corresponds to the specified virtual path on the Web server. /// /// The virtual path on the Web server. /// The physical file path that corresponds to path. public string MapPath (string path) { if (null == _Context) throw new HttpException ("MapPath is not available"); return _Context.Request.MapPath (path); } /// /// Terminates execution of the current page and begins execution of a new page using the specified /// URL path to the page. /// /// The URL path of the new page on the server to execute. public void Transfer (string path) { // If it's a page and a postback, don't pass form data // See bug #65613. bool preserveForm = true; if (_Context.Handler is Page) { Page page = (Page) _Context.Handler; preserveForm = !page.IsPostBack; } Transfer (path, preserveForm); } /// /// Terminates execution of the current page and begins execution of a new page using the specified /// URL path to the page. Specifies whether to clear the QueryString and Form collections. /// /// The URL path of the new page on the server to execute. /// If true, the QueryString and Form collections are preserved. If false, /// they are cleared. The default is false. public void Transfer (string path, bool preserveForm) { Execute (path, null, preserveForm); _Context.Response.End (); } /// /// URL-decodes a string and returns the decoded string. /// /// The text string to decode. /// The decoded text. /// /// Post/html encoding @ ftp://ftp.isi.edu/in-notes/rfc1866.txt /// Uncomment the line marked with RFC1738 to get pure RFC1738 /// and it will also consider the RFC1866 (ftp://ftp.isi.edu/in-notes/rfc1866.txt) /// `application/x-www-form-urlencoded' format /// public string UrlDecode (string s) { return HttpUtility.UrlDecode (s); } /// /// Decodes an HTML string received in a URL and sends the resulting output to a TextWriter output stream. /// /// /// public void UrlDecode (string s, TextWriter output) { if (s != null) output.Write (UrlDecode (s)); } /// /// URL-encodes a string and returns the encoded string. /// /// The text to URL-encode. /// The URL encoded text. public string UrlEncode (string s) { return HttpUtility.UrlEncode (s); } /// /// URL encodes a string and sends the resulting output to a TextWriter output stream. /// /// The text string to encode. /// The TextWriter output stream containing the encoded string. public void UrlEncode (string s, TextWriter output) { if (s != null) output.Write (UrlEncode (s)); } /// /// URL-encodes the path portion of a URL string and returns the encoded string. /// /// The text to URL-encode. /// The URL encoded text. /// Does not do any browser specific adjustments, just encode everything public string UrlPathEncode (string s) { if (s == null) return null; int idx = s.IndexOf ("?"); string s2 = null; if (idx != -1) { s2 = s.Substring (0, idx-1); s2 = UrlEncode (s2) + s.Substring (idx); } else { s2 = UrlEncode (s); } return s2; } } }