Merge pull request #3563 from lewurm/interpreter
[mono.git] / mcs / class / System.Web / System.Web.Hosting / SimpleWorkerRequest.cs
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 //
8
9 //
10 // Copyright (C) 2005,2006 Novell, Inc (http://www.novell.com)
11 //
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.
30 //
31
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;
40
41 namespace System.Web.Hosting {
42
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;
55
56                 bool hosted;
57                         
58                 // computed
59                 string raw_url;
60
61                 //
62                 // Constructor used when the target application domain
63                 // was created with ApplicationHost.CreateApplicationHost
64                 //
65                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
66                 public SimpleWorkerRequest (string page, string query, TextWriter output)
67                 {
68                         this.page = page;
69                         this.query = query;
70                         this.output = output;
71
72                         app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
73                         app_physical_dir = HttpRuntime.AppDomainAppPath;
74                         hosted = true;
75                         InitializePaths ();
76                 }
77
78                 //
79                 // Creates a SimpleWorkerRequest that can be used from any AppDomain
80                 //
81                 // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
82                 //
83                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
84                 public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
85                 {
86                         this.page = page;
87                         this.query = query;
88                         this.output = output;
89                         app_virtual_dir = appVirtualDir;
90                         app_physical_dir = appPhysicalDir;
91                         InitializePaths ();
92                 }
93
94                 void InitializePaths ()
95                 {
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 = "";
102                         }
103                 }
104
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 (); 
111                                         }
112                                         return path;
113                                 }
114                                 return null;
115                         }
116                 }
117
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 (); 
124                                         }
125                                         return path;
126                                 }
127                                 return null;
128                         }
129                 }
130                 public override string RootWebConfigPath {
131                         get { return WebConfigurationManager.OpenWebConfiguration ("~").FilePath; }
132                 }
133
134                 public override void EndOfRequest ()
135                 {
136                 }
137                 
138                 public override void FlushResponse (bool finalFlush)
139                 {
140                 }
141                 
142                 public override string GetAppPath ()
143                 {
144                         return app_virtual_dir;
145                 }
146
147                 public override string GetAppPathTranslated ()
148                 {
149                         if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
150                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand (); 
151                         }
152                         return app_physical_dir;
153                 }
154
155                 public override string GetFilePath ()
156                 {
157                         string result = UrlUtils.Combine (app_virtual_dir, page);
158                         if (result == "") 
159                                 return app_virtual_dir == "/" ? app_virtual_dir : app_virtual_dir + "/"; 
160
161                         return result;
162                 }
163
164                 public override string GetFilePathTranslated ()
165                 {
166                         string local_page;
167                         
168                         if (Path.DirectorySeparatorChar == '\\')
169                                 local_page = page.Replace ('/', '\\');
170                         else
171                                 local_page = page;
172                         
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 (); 
176                         }
177                         return path;
178                 }
179
180                 public override string GetHttpVerbName ()
181                 {
182                         return "GET";
183                 }
184                 
185                 public override string GetHttpVersion ()
186                 {
187                         return "HTTP/1.0";
188                 }
189                 
190                 public override string GetLocalAddress ()
191                 {
192                         return "127.0.0.1";
193                 }
194
195                 public override int GetLocalPort ()
196                 {
197                         return 80;
198                 }
199
200                 public override string GetPathInfo ()
201                 {
202                         return path_info;
203                 }
204
205                 public override string GetQueryString ()
206                 {
207                         return query;
208                 }
209                 
210                 public override string GetRawUrl ()
211                 {
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;
219                                 }
220                         }
221                         return raw_url;
222                 }
223                 
224                 public override string GetRemoteAddress ()
225                 {
226                         return "127.0.0.1";
227                 }
228                 
229                 public override int GetRemotePort ()
230                 {
231                         return 0;
232                 }
233                 
234                 public override string GetServerVariable (string name)
235                 {
236                         return "";
237                 }
238
239                 public override string GetUriPath ()
240                 {
241                         if (app_virtual_dir == "/")
242                                 return app_virtual_dir +  page + path_info;
243
244                         return app_virtual_dir + "/" + page + path_info;
245                 }
246
247                 public override IntPtr GetUserToken ()
248                 {
249                         return IntPtr.Zero;
250                 }
251
252                 public override string MapPath (string path)
253                 {
254                         if (!hosted)
255                                 return null;
256                         if (path != null && path.Length == 0)
257                                 return app_physical_dir;
258                         
259                         if (!path.StartsWith (app_virtual_dir))
260                                 throw new ArgumentNullException ("path is not rooted in the virtual directory");
261
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);
268                 }
269
270                 public override void SendKnownResponseHeader (int index, string value)
271                 {
272                 }
273
274                 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
275                 {
276                 }
277
278                 public override void SendResponseFromFile (string filename, long offset, long length)
279                 {
280                 }
281
282                 public override void SendResponseFromMemory (byte [] data, int length)
283                 {
284                         output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
285                 }
286
287
288                 public override void SendStatus (int statusCode, string statusDescription)
289                 {
290                 }
291
292                 public override void SendUnknownResponseHeader (string name, string value)
293                 {
294                 }
295         }
296 }