2 // System.Web.HttpRequest.cs
6 // Miguel de Icaza (miguel@novell.com)
7 // Gonzalo Paniagua Javier (gonzalo@novell.com)
11 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
35 using System.Web.Util;
36 using System.Collections.Specialized;
37 using System.Security.Permissions;
39 namespace System.Web {
42 // Methods exposed through HttpContext.Server property
45 // CAS - no InheritanceDemand here as the class is sealed
46 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 public sealed class HttpServerUtility {
50 internal HttpServerUtility (HttpContext context)
52 this.context = context;
55 public void ClearError ()
57 context.ClearError ();
60 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
61 public object CreateObject (string progID)
63 throw new HttpException (500, "COM is not supported");
66 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
67 public object CreateObject (Type type)
69 throw new HttpException (500, "COM is not supported");
72 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
73 public object CreateObjectFromClsid (string clsid)
75 throw new HttpException (500, "COM is not supported");
78 public void Execute (string path)
83 public void Execute (string path, TextWriter writer)
85 Execute (path, writer, false);
93 void Execute (string path, TextWriter writer, bool preserveQuery)
96 throw new ArgumentNullException ("path");
98 if (path.IndexOf (':') != -1)
99 throw new ArgumentException ("Invalid path.");
101 int qmark = path.IndexOf ('?');
104 query = path.Substring (qmark + 1);
105 path = path.Substring (0, qmark);
110 HttpRequest request = context.Request;
111 HttpResponse response = context.Response;
113 string oldQuery = request.QueryStringRaw;
114 request.QueryStringRaw = query;
116 WebROCollection oldForm = null;
117 if (!preserveQuery) {
118 oldForm = request.Form as WebROCollection;
119 request.SetForm (new WebROCollection ());
122 TextWriter output = writer;
124 output = response.Output;
126 string oldFilePath = request.FilePath;
127 request.SetCurrentExePath (UrlUtils.Combine (request.BaseVirtualDir, path));
128 IHttpHandler handler = context.ApplicationInstance.GetHandler (context);
129 TextWriter previous = null;
131 previous = response.SetTextWriter (output);
132 if (!(handler is IHttpAsyncHandler)) {
133 handler.ProcessRequest (context);
135 IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
136 IAsyncResult ar = asyncHandler.BeginProcessRequest (context, null, null);
137 ar.AsyncWaitHandle.WaitOne ();
138 asyncHandler.EndProcessRequest (ar);
141 request.SetCurrentExePath (oldFilePath);
142 request.QueryStringRaw = oldQuery;
143 response.SetTextWriter (previous);
145 request.SetForm (oldForm);
149 public Exception GetLastError ()
151 return context.Error;
154 public string HtmlDecode (string s)
156 return HttpUtility.HtmlDecode (s);
159 public void HtmlDecode (string s, TextWriter output)
161 HttpUtility.HtmlDecode (s, output);
164 public string HtmlEncode (string s)
166 return HttpUtility.HtmlEncode (s);
169 public void HtmlEncode (string s, TextWriter output)
171 HttpUtility.HtmlEncode (s, output);
174 public string MapPath (string path)
176 return context.Request.MapPath (path);
179 public void Transfer (string path)
181 // If it's a page and a postback, don't pass form data
183 bool preserveForm = true;
184 if (context.Handler is Page) {
185 Page page = (Page) context.Handler;
186 preserveForm = !page.IsPostBack;
189 Transfer (path, preserveForm);
192 public void Transfer (string path, bool preserveForm)
194 Execute (path, null, preserveForm);
195 context.Response.End ();
199 public void Transfer (IHttpHandler handler, bool preserveForm)
201 throw new NotImplementedException ();
204 public string UrlDecode (string s)
206 return HttpUtility.UrlDecode (s);
209 public void UrlDecode (string s, TextWriter output)
212 output.Write (HttpUtility.UrlDecode (s));
215 public string UrlEncode (string s)
217 return HttpUtility.UrlEncode (s);
220 public void UrlEncode (string s, TextWriter output)
223 output.Write (HttpUtility.UrlEncode (s));
226 public string UrlPathEncode (string s)
231 int idx = s.IndexOf ("?");
234 s2 = s.Substring (0, idx-1);
235 s2 = HttpUtility.UrlEncode (s2) + s.Substring (idx);
237 s2 = HttpUtility.UrlEncode (s);
243 public string MachineName {
244 [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
245 // Medium doesn't look heavy enough to replace this... reported as
246 [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
247 [EnvironmentPermission (SecurityAction.Assert, Read = "COMPUTERNAME")]
248 get { return Environment.MachineName; }
251 public int ScriptTimeout {
252 get { return (int) context.ConfigTimeout.TotalSeconds; }
253 [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
254 set { context.ConfigTimeout = new TimeSpan (0, 0, value); }