/**
* 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;
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.
[MonoTODO()]
public object CreateObject (Type type)
{
Object o;
o = Activator.CreateInstance (type);
// TODO: Call OnStartPage()
return o;
}
///
/// 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_1_2
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 = "";
}
string filePath = _Context.Request.MapPath (path);
HttpResponse response = _Context.Response;
TextWriter output = writer;
if (output == null)
output = response.Output;
HttpRequest request = _Context.Request;
string oldFilePath = request.FilePath;
request.SetFilePath (path);
string oldQuery = request.QueryStringRaw;
if (!preserveQuery) request.QueryStringRaw = query;
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.SetFilePath (oldFilePath);
request.QueryStringRaw = oldQuery;
response.SetTextWriter (previous);
}
}
///
/// 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)
{
Transfer (path, true);
}
///
/// 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)
{
HttpValueCollection oldForm = null;
if (!preserveForm) {
oldForm = _Context.Request.Form as HttpValueCollection;
_Context.Request.SetForm (new HttpValueCollection ());
}
Execute (path, null, preserveForm);
if (!preserveForm)
_Context.Request.SetForm (oldForm);
_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;
}
}
}