2005-12-06 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Collections;
32 using System.IO;
33 using System.Runtime.InteropServices;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Web.UI;
37 using System.Web.Util;
38
39 namespace System.Web.Hosting {
40
41         // CAS
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         // attributes
45         [ComVisible (false)]
46         public class SimpleWorkerRequest : HttpWorkerRequest {
47                 string page;
48                 string query;
49                 string app_virtual_dir;
50                 string app_physical_dir;
51                 string path_info;
52                 TextWriter output;
53
54                 bool hosted;
55                         
56                 // computed
57                 string raw_url;
58                 
59                 //
60                 // Constructor used when the target application domain
61                 // was created with ApplicationHost.CreateApplicationHost
62                 //
63                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
64                 public SimpleWorkerRequest (string page, string query, TextWriter output)
65                 {
66                         this.page = page;
67                         this.query = query;
68                         this.output = output;
69
70                         app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
71                         app_physical_dir = HttpRuntime.AppDomainAppPath;
72                         hosted = true;
73                 }
74
75                 //
76                 // Creates a SimpleWorkerRequest that can be used from any AppDomain
77                 //
78                 // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
79                 //
80                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
81                 public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
82                 {
83                         this.page = page;
84                         this.query = query;
85                         this.output = output;
86                         app_virtual_dir = appVirtualDir;
87                         app_physical_dir = appPhysicalDir;
88                 }
89                 
90                 
91                 public override string MachineConfigPath {
92                         get {
93                                 if (hosted) {
94                                         string path = ICalls.GetMachineConfigPath ();
95                                         if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
96                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand (); 
97                                         }
98                                         return path;
99                                 }
100                                 return null;
101                         }
102                 }
103
104                 public override string MachineInstallDirectory {
105                         get {
106                                 if (hosted) {
107                                         string path = ICalls.GetMachineInstallDirectory ();
108                                         if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
109                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand (); 
110                                         }
111                                         return path;
112                                 }
113                                 return null;
114                         }
115                 }
116
117                 public override void EndOfRequest ()
118                 {
119                 }
120                 
121                 public override void FlushResponse (bool finalFlush)
122                 {
123                 }
124                 
125                 public override string GetAppPath ()
126                 {
127                         return app_virtual_dir;
128                 }
129
130                 public override string GetAppPathTranslated ()
131                 {
132                         if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
133                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand (); 
134                         }
135                         return app_physical_dir;
136                 }
137
138                 public override string GetFilePath ()
139                 {
140                         return Path.Combine (app_virtual_dir, page);
141                 }
142
143                 public override string GetFilePathTranslated ()
144                 {
145                         string local_page;
146                         
147                         if (Path.DirectorySeparatorChar == '\\')
148                                 local_page = page.Replace ('/', '\\');
149                         else
150                                 local_page = page;
151                         
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 (); 
155                         }
156                         return path;
157                 }
158
159                 public override string GetHttpVerbName ()
160                 {
161                         return "GET";
162                 }
163                 
164                 public override string GetHttpVersion ()
165                 {
166                         return "HTTP/1.0";
167                 }
168                 
169                 public override string GetLocalAddress ()
170                 {
171                         return "127.0.0.1";
172                 }
173
174                 public override int GetLocalPort ()
175                 {
176                         return 80;
177                 }
178
179                 public override string GetPathInfo ()
180                 {
181                         if (path_info == null) {
182                                 int idx = page.IndexOf ('/');
183                                 if (idx >= 0) {
184                                         path_info = page.Substring (idx);
185                                 } else {
186                                         path_info = "";
187                                 }
188                         }
189                         return path_info;
190                 }
191
192                 public override string GetQueryString ()
193                 {
194                         return query;
195                 }
196                 
197                 public override string GetRawUrl ()
198                 {
199                         if (raw_url == null){
200                                 string q = ((query == null || query == "") ? "" : "?" + query);
201                                 
202                                 raw_url = Path.Combine (app_virtual_dir, page) + q;
203                         }
204                         return raw_url;
205                 }
206                 
207                 public override string GetRemoteAddress ()
208                 {
209                         return "127.0.0.1";
210                 }
211                 
212                 public override int GetRemotePort ()
213                 {
214                         return 0;
215                 }
216                 
217                 public override string GetServerVariable (string name)
218                 {
219                         return "";
220                 }
221
222                 public override string GetUriPath ()
223                 {
224                         if (app_virtual_dir == "/")
225                                 return app_virtual_dir +  page;
226                         return app_virtual_dir + "/" + page;
227                 }
228
229                 public override IntPtr GetUserToken ()
230                 {
231                         return IntPtr.Zero;
232                 }
233
234                 public override string MapPath (string path)
235                 {
236                         if (!hosted)
237                                 return null;
238
239                         if (!path.StartsWith (app_virtual_dir))
240                                 throw new ArgumentNullException ("path is not rooted in the virtual directory");
241
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);
246                 }
247
248                 public override void SendKnownResponseHeader (int index, string value)
249                 {
250                 }
251
252                 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
253                 {
254                 }
255
256                 public override void SendResponseFromFile (string filename, long offset, long length)
257                 {
258                 }
259
260                 public override void SendResponseFromMemory (byte [] data, int length)
261                 {
262                         output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
263                 }
264
265
266                 public override void SendStatus (int statusCode, string statusDescription)
267                 {
268                 }
269
270                 public override void SendUnknownResponseHeader (string name, string value)
271                 {
272                 }
273         }
274 }