[mono/tests] Fix out of tree build.
[mono-project.git] / mcs / class / System.Web / System.Web.Hosting / SimpleWorkerRequest.cs
blob0b8b450ecc44e3507a67dac8e9e379fd40930c0f
1 //
2 // System.Web.Hosting.SimpleWorkerRequest.cs
3 //
4 // Author:
5 // Miguel de Icaza (miguel@novell.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
9 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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;
33 using System.IO;
34 using System.Runtime.InteropServices;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Web.Configuration;
38 using System.Web.UI;
39 using System.Web.Util;
41 namespace System.Web.Hosting {
43 // CAS
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 // attributes
47 [ComVisible (false)]
48 public class SimpleWorkerRequest : HttpWorkerRequest {
49 string page;
50 string query;
51 string app_virtual_dir;
52 string app_physical_dir;
53 string path_info;
54 TextWriter output;
56 bool hosted;
58 // computed
59 string raw_url;
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)
68 this.page = page;
69 this.query = query;
70 this.output = output;
72 app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
73 app_physical_dir = HttpRuntime.AppDomainAppPath;
74 hosted = true;
75 InitializePaths ();
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)
86 this.page = page;
87 this.query = query;
88 this.output = output;
89 app_virtual_dir = appVirtualDir;
90 app_physical_dir = appPhysicalDir;
91 InitializePaths ();
94 void InitializePaths ()
96 int idx = page.IndexOf ('/');
97 if (idx >= 0) {
98 path_info = page.Substring (idx);
99 page = page.Substring (0, idx);
100 } else {
101 path_info = "";
105 public override string MachineConfigPath {
106 get {
107 if (hosted) {
108 string path = ICalls.GetMachineConfigPath ();
109 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
110 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
112 return path;
114 return null;
118 public override string MachineInstallDirectory {
119 get {
120 if (hosted) {
121 string path = ICalls.GetMachineInstallDirectory ();
122 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
123 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
125 return path;
127 return null;
130 public override string RootWebConfigPath {
131 get { return WebConfigurationManager.OpenWebConfiguration ("~").FilePath; }
134 public override void EndOfRequest ()
138 public override void FlushResponse (bool finalFlush)
142 public override string GetAppPath ()
144 return app_virtual_dir;
147 public override string GetAppPathTranslated ()
149 if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
150 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
152 return app_physical_dir;
155 public override string GetFilePath ()
157 string result = UrlUtils.Combine (app_virtual_dir, page);
158 if (result == "")
159 return app_virtual_dir == "/" ? app_virtual_dir : app_virtual_dir + "/";
161 return result;
164 public override string GetFilePathTranslated ()
166 string local_page;
168 if (Path.DirectorySeparatorChar == '\\')
169 local_page = page.Replace ('/', '\\');
170 else
171 local_page = page;
173 string path = Path.Combine (app_physical_dir, local_page);
174 if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
175 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
177 return path;
180 public override string GetHttpVerbName ()
182 return "GET";
185 public override string GetHttpVersion ()
187 return "HTTP/1.0";
190 public override string GetLocalAddress ()
192 return "127.0.0.1";
195 public override int GetLocalPort ()
197 return 80;
200 public override string GetPathInfo ()
202 return path_info;
205 public override string GetQueryString ()
207 return query;
210 public override string GetRawUrl ()
212 if (raw_url == null){
213 string q = ((query == null || query == "") ? "" : "?" + query);
214 raw_url = UrlUtils.Combine (app_virtual_dir, page);
215 if (path_info != "") {
216 raw_url += "/" + path_info + q;
217 } else {
218 raw_url += q;
221 return raw_url;
224 public override string GetRemoteAddress ()
226 return "127.0.0.1";
229 public override int GetRemotePort ()
231 return 0;
234 public override string GetServerVariable (string name)
236 return "";
239 public override string GetUriPath ()
241 if (app_virtual_dir == "/")
242 return app_virtual_dir + page + path_info;
244 return app_virtual_dir + "/" + page + path_info;
247 public override IntPtr GetUserToken ()
249 return IntPtr.Zero;
252 public override string MapPath (string path)
254 if (!hosted)
255 return null;
256 if (path != null && path.Length == 0)
257 return app_physical_dir;
259 if (!path.StartsWith (app_virtual_dir))
260 throw new ArgumentNullException ("path is not rooted in the virtual directory");
262 string rest = path.Substring (app_virtual_dir.Length);
263 if (rest.Length > 0 && rest [0] == '/')
264 rest = rest.Substring (1);
265 if (Path.DirectorySeparatorChar != '/') // for windows suport
266 rest = rest.Replace ('/', Path.DirectorySeparatorChar);
267 return Path.Combine (app_physical_dir, rest);
270 public override void SendKnownResponseHeader (int index, string value)
274 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
278 public override void SendResponseFromFile (string filename, long offset, long length)
282 public override void SendResponseFromMemory (byte [] data, int length)
284 output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
288 public override void SendStatus (int statusCode, string statusDescription)
292 public override void SendUnknownResponseHeader (string name, string value)