2009-06-12 Bill Holmes <billholmes54@gmail.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 //      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 #if NET_2_0
131                 public override string RootWebConfigPath {
132                         get { return WebConfigurationManager.OpenWebConfiguration ("~").FilePath; }
133                 }
134 #endif
135
136                 public override void EndOfRequest ()
137                 {
138                 }
139                 
140                 public override void FlushResponse (bool finalFlush)
141                 {
142                 }
143                 
144                 public override string GetAppPath ()
145                 {
146                         return app_virtual_dir;
147                 }
148
149                 public override string GetAppPathTranslated ()
150                 {
151                         if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
152                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand (); 
153                         }
154                         return app_physical_dir;
155                 }
156
157                 public override string GetFilePath ()
158                 {
159                         string result = UrlUtils.Combine (app_virtual_dir, page);
160                         if (result == "") 
161                                 return app_virtual_dir == "/" ? app_virtual_dir : app_virtual_dir + "/"; 
162
163                         return result;
164                 }
165
166                 public override string GetFilePathTranslated ()
167                 {
168                         string local_page;
169                         
170                         if (Path.DirectorySeparatorChar == '\\')
171                                 local_page = page.Replace ('/', '\\');
172                         else
173                                 local_page = page;
174                         
175                         string path = Path.Combine (app_physical_dir, local_page);
176                         if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
177                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand (); 
178                         }
179                         return path;
180                 }
181
182                 public override string GetHttpVerbName ()
183                 {
184                         return "GET";
185                 }
186                 
187                 public override string GetHttpVersion ()
188                 {
189                         return "HTTP/1.0";
190                 }
191                 
192                 public override string GetLocalAddress ()
193                 {
194                         return "127.0.0.1";
195                 }
196
197                 public override int GetLocalPort ()
198                 {
199                         return 80;
200                 }
201
202                 public override string GetPathInfo ()
203                 {
204                         return path_info;
205                 }
206
207                 public override string GetQueryString ()
208                 {
209                         return query;
210                 }
211                 
212                 public override string GetRawUrl ()
213                 {
214                         if (raw_url == null){
215                                 string q = ((query == null || query == "") ? "" : "?" + query);
216                                 raw_url = UrlUtils.Combine (app_virtual_dir, page);
217                                 if (path_info != "") {
218                                         raw_url += "/" + path_info + q;
219                                 } else {
220                                         raw_url += q;
221                                 }
222                         }
223                         return raw_url;
224                 }
225                 
226                 public override string GetRemoteAddress ()
227                 {
228                         return "127.0.0.1";
229                 }
230                 
231                 public override int GetRemotePort ()
232                 {
233                         return 0;
234                 }
235                 
236                 public override string GetServerVariable (string name)
237                 {
238                         return "";
239                 }
240
241                 public override string GetUriPath ()
242                 {
243                         if (app_virtual_dir == "/")
244                                 return app_virtual_dir +  page + path_info;
245
246                         return app_virtual_dir + "/" + page + path_info;
247                 }
248
249                 public override IntPtr GetUserToken ()
250                 {
251                         return IntPtr.Zero;
252                 }
253
254                 public override string MapPath (string path)
255                 {
256                         if (!hosted)
257                                 return null;
258                         if (path != null && path.Length == 0)
259                                 return app_physical_dir;
260                         
261                         if (!path.StartsWith (app_virtual_dir))
262                                 throw new ArgumentNullException ("path is not rooted in the virtual directory");
263
264                         string rest = path.Substring (app_virtual_dir.Length);
265                         if (rest.Length > 0 && rest [0] == '/')
266                                 rest = rest.Substring (1);
267                         if (Path.DirectorySeparatorChar != '/') // for windows suport
268                                 rest = rest.Replace ('/', Path.DirectorySeparatorChar);
269                         return Path.Combine (app_physical_dir, rest);
270                 }
271
272                 public override void SendKnownResponseHeader (int index, string value)
273                 {
274                 }
275
276                 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
277                 {
278                 }
279
280                 public override void SendResponseFromFile (string filename, long offset, long length)
281                 {
282                 }
283
284                 public override void SendResponseFromMemory (byte [] data, int length)
285                 {
286                         output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
287                 }
288
289
290                 public override void SendStatus (int statusCode, string statusDescription)
291                 {
292                 }
293
294                 public override void SendUnknownResponseHeader (string name, string value)
295                 {
296                 }
297         }
298 }