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;
36 using System.Web.Configuration;
38 using System.Web.Util;
40 namespace System.Web.Hosting {
43 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 public class SimpleWorkerRequest : HttpWorkerRequest {
50 string app_virtual_dir;
51 string app_physical_dir;
61 // Constructor used when the target application domain
62 // was created with ApplicationHost.CreateApplicationHost
64 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
65 public SimpleWorkerRequest (string page, string query, TextWriter output)
71 app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
72 app_physical_dir = HttpRuntime.AppDomainAppPath;
77 // Creates a SimpleWorkerRequest that can be used from any AppDomain
79 // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
81 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
82 public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
87 app_virtual_dir = appVirtualDir;
88 app_physical_dir = appPhysicalDir;
92 public override string MachineConfigPath {
95 string path = ICalls.GetMachineConfigPath ();
96 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
97 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
105 public override string MachineInstallDirectory {
108 string path = ICalls.GetMachineInstallDirectory ();
109 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
110 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
118 public override string RootWebConfigPath {
119 get { return WebConfigurationManager.OpenWebConfiguration ("~").FilePath; }
123 public override void EndOfRequest ()
127 public override void FlushResponse (bool finalFlush)
131 public override string GetAppPath ()
133 return app_virtual_dir;
136 public override string GetAppPathTranslated ()
138 if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
139 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
141 return app_physical_dir;
144 public override string GetFilePath ()
146 return Path.Combine (app_virtual_dir, page);
149 public override string GetFilePathTranslated ()
153 if (Path.DirectorySeparatorChar == '\\')
154 local_page = page.Replace ('/', '\\');
158 string path = Path.Combine (app_physical_dir, local_page);
159 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
160 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
165 public override string GetHttpVerbName ()
170 public override string GetHttpVersion ()
175 public override string GetLocalAddress ()
180 public override int GetLocalPort ()
185 public override string GetPathInfo ()
187 if (path_info == null) {
188 int idx = page.IndexOf ('/');
190 path_info = page.Substring (idx);
198 public override string GetQueryString ()
203 public override string GetRawUrl ()
205 if (raw_url == null){
206 string q = ((query == null || query == "") ? "" : "?" + query);
208 raw_url = Path.Combine (app_virtual_dir, page) + q;
213 public override string GetRemoteAddress ()
218 public override int GetRemotePort ()
223 public override string GetServerVariable (string name)
228 public override string GetUriPath ()
230 if (app_virtual_dir == "/")
231 return app_virtual_dir + page;
232 return app_virtual_dir + "/" + page;
235 public override IntPtr GetUserToken ()
240 public override string MapPath (string path)
245 if (!path.StartsWith (app_virtual_dir))
246 throw new ArgumentNullException ("path is not rooted in the virtual directory");
248 string rest = path.Substring (app_virtual_dir.Length);
249 if (rest.Length > 0 && rest [0] == '/')
250 rest = rest.Substring (1);
251 return Path.Combine (app_physical_dir, rest);
254 public override void SendKnownResponseHeader (int index, string value)
258 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
262 public override void SendResponseFromFile (string filename, long offset, long length)
266 public override void SendResponseFromMemory (byte [] data, int length)
268 output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
272 public override void SendStatus (int statusCode, string statusDescription)
276 public override void SendUnknownResponseHeader (string name, string value)