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