2 // System.Web.Hosting.SimpleWorkerRequest.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
10 // Copyright (C) 2005,2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
34 using System.Runtime.InteropServices;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Web.Configuration;
39 using System.Web.Util;
41 namespace System.Web.Hosting {
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 public class SimpleWorkerRequest : HttpWorkerRequest {
51 string app_virtual_dir;
52 string app_physical_dir;
62 // Constructor used when the target application domain
63 // was created with ApplicationHost.CreateApplicationHost
65 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
66 public SimpleWorkerRequest (string page, string query, TextWriter output)
72 app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
73 app_physical_dir = HttpRuntime.AppDomainAppPath;
79 // Creates a SimpleWorkerRequest that can be used from any AppDomain
81 // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
83 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
84 public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
89 app_virtual_dir = appVirtualDir;
90 app_physical_dir = appPhysicalDir;
94 void InitializePaths ()
96 int idx = page.IndexOf ('/');
98 path_info = page.Substring (idx);
99 page = page.Substring (0, idx);
105 public override string MachineConfigPath {
108 string path = ICalls.GetMachineConfigPath ();
109 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
110 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
118 public override string MachineInstallDirectory {
121 string path = ICalls.GetMachineInstallDirectory ();
122 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
123 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
131 public override string RootWebConfigPath {
132 get { return WebConfigurationManager.OpenWebConfiguration ("~").FilePath; }
136 public override void EndOfRequest ()
140 public override void FlushResponse (bool finalFlush)
144 public override string GetAppPath ()
146 return app_virtual_dir;
149 public override string GetAppPathTranslated ()
151 if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
152 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
154 return app_physical_dir;
157 public override string GetFilePath ()
159 string result = UrlUtils.Combine (app_virtual_dir, page);
161 return app_virtual_dir == "/" ? app_virtual_dir : app_virtual_dir + "/";
166 public override string GetFilePathTranslated ()
170 if (Path.DirectorySeparatorChar == '\\')
171 local_page = page.Replace ('/', '\\');
175 string path = Path.Combine (app_physical_dir, local_page);
176 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
177 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
182 public override string GetHttpVerbName ()
187 public override string GetHttpVersion ()
192 public override string GetLocalAddress ()
197 public override int GetLocalPort ()
202 public override string GetPathInfo ()
207 public override string GetQueryString ()
212 public override string GetRawUrl ()
214 if (raw_url == null){
215 string q = ((query == null || query == "") ? "" : "?" + query);
216 raw_url = UrlUtils.Combine (app_virtual_dir, page);
217 if (path_info != "") {
218 raw_url += "/" + path_info + q;
226 public override string GetRemoteAddress ()
231 public override int GetRemotePort ()
236 public override string GetServerVariable (string name)
241 public override string GetUriPath ()
243 if (app_virtual_dir == "/")
244 return app_virtual_dir + page + path_info;
246 return app_virtual_dir + "/" + page + path_info;
249 public override IntPtr GetUserToken ()
254 public override string MapPath (string path)
258 if (path != null && path.Length == 0)
259 return app_physical_dir;
261 if (!path.StartsWith (app_virtual_dir))
262 throw new ArgumentNullException ("path is not rooted in the virtual directory");
264 string rest = path.Substring (app_virtual_dir.Length);
265 if (rest.Length > 0 && rest [0] == '/')
266 rest = rest.Substring (1);
267 if (Path.DirectorySeparatorChar != '/') // for windows suport
268 rest = rest.Replace ('/', Path.DirectorySeparatorChar);
269 return Path.Combine (app_physical_dir, rest);
272 public override void SendKnownResponseHeader (int index, string value)
276 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
280 public override void SendResponseFromFile (string filename, long offset, long length)
284 public override void SendResponseFromMemory (byte [] data, int length)
286 output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
290 public override void SendStatus (int statusCode, string statusDescription)
294 public override void SendUnknownResponseHeader (string name, string value)