2 // System.Web.Hosting.SimpleWorkerRequest.cs
5 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
33 using System.Runtime.InteropServices;
34 using System.Security;
35 using System.Security.Permissions;
37 using System.Web.Util;
39 namespace System.Web.Hosting {
42 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 public class SimpleWorkerRequest : HttpWorkerRequest {
49 string app_virtual_dir;
50 string app_physical_dir;
60 // Constructor used when the target application domain
61 // was created with ApplicationHost.CreateApplicationHost
63 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
64 public SimpleWorkerRequest (string page, string query, TextWriter output)
70 app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
71 app_physical_dir = HttpRuntime.AppDomainAppPath;
76 // Creates a SimpleWorkerRequest that can be used from any AppDomain
78 // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
80 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
81 public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
86 app_virtual_dir = appVirtualDir;
87 app_physical_dir = appPhysicalDir;
91 public override string MachineConfigPath {
94 string path = ICalls.GetMachineConfigPath ();
95 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
96 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
104 public override string MachineInstallDirectory {
107 string path = ICalls.GetMachineInstallDirectory ();
108 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
109 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
117 public override void EndOfRequest ()
121 public override void FlushResponse (bool finalFlush)
125 public override string GetAppPath ()
127 return app_virtual_dir;
130 public override string GetAppPathTranslated ()
132 if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
133 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
135 return app_physical_dir;
138 public override string GetFilePath ()
140 return Path.Combine (app_virtual_dir, page);
143 public override string GetFilePathTranslated ()
147 if (Path.DirectorySeparatorChar == '\\')
148 local_page = page.Replace ('/', '\\');
152 string path = Path.Combine (app_physical_dir, local_page);
153 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
154 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
159 public override string GetHttpVerbName ()
164 public override string GetHttpVersion ()
169 public override string GetLocalAddress ()
174 public override int GetLocalPort ()
179 public override string GetPathInfo ()
181 if (path_info == null) {
182 int idx = page.IndexOf ('/');
184 path_info = page.Substring (idx);
192 public override string GetQueryString ()
197 public override string GetRawUrl ()
199 if (raw_url == null){
200 string q = ((query == null || query == "") ? "" : "?" + query);
202 raw_url = Path.Combine (app_virtual_dir, page) + q;
207 public override string GetRemoteAddress ()
212 public override int GetRemotePort ()
217 public override string GetServerVariable (string name)
222 public override string GetUriPath ()
224 if (app_virtual_dir == "/")
225 return app_virtual_dir + page;
226 return app_virtual_dir + "/" + page;
229 public override IntPtr GetUserToken ()
234 public override string MapPath (string path)
239 if (!path.StartsWith (app_virtual_dir))
240 throw new ArgumentNullException ("path is not rooted in the virtual directory");
242 string rest = path.Substring (app_virtual_dir.Length);
243 if (rest.Length > 0 && rest [0] == '/')
244 rest = rest.Substring (1);
245 return Path.Combine (app_physical_dir, rest);
248 public override void SendKnownResponseHeader (int index, string value)
252 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
256 public override void SendResponseFromFile (string filename, long offset, long length)
260 public override void SendResponseFromMemory (byte [] data, int length)
262 output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
266 public override void SendStatus (int statusCode, string statusDescription)
270 public override void SendUnknownResponseHeader (string name, string value)