2 // System.Web.Hosting.SimpleWorkerRequest.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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
;
34 using System
.Runtime
.InteropServices
;
35 using System
.Security
;
36 using System
.Security
.Permissions
;
37 using System
.Web
.Configuration
;
39 using System
.Web
.Util
;
41 namespace System
.Web
.Hosting
{
44 [AspNetHostingPermission (SecurityAction
.LinkDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
45 [AspNetHostingPermission (SecurityAction
.InheritanceDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
48 public class SimpleWorkerRequest
: HttpWorkerRequest
{
51 string app_virtual_dir
;
52 string app_physical_dir
;
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
)
72 app_virtual_dir
= HttpRuntime
.AppDomainAppVirtualPath
;
73 app_physical_dir
= HttpRuntime
.AppDomainAppPath
;
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
)
89 app_virtual_dir
= appVirtualDir
;
90 app_physical_dir
= appPhysicalDir
;
94 void InitializePaths ()
96 int idx
= page
.IndexOf ('/');
98 path_info
= page
.Substring (idx
);
99 page
= page
.Substring (0, idx
);
105 public override string MachineConfigPath
{
108 string path
= ICalls
.GetMachineConfigPath ();
109 if (SecurityManager
.SecurityEnabled
&& (path
!= null) && (path
.Length
> 0)) {
110 new FileIOPermission (FileIOPermissionAccess
.PathDiscovery
, path
).Demand ();
118 public override string MachineInstallDirectory
{
121 string path
= ICalls
.GetMachineInstallDirectory ();
122 if (SecurityManager
.SecurityEnabled
&& (path
!= null) && (path
.Length
> 0)) {
123 new FileIOPermission (FileIOPermissionAccess
.PathDiscovery
, path
).Demand ();
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
);
159 return app_virtual_dir
== "/" ? app_virtual_dir
: app_virtual_dir
+ "/";
164 public override string GetFilePathTranslated ()
168 if (Path
.DirectorySeparatorChar
== '\\')
169 local_page
= page
.Replace ('/', '\\');
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 ();
180 public override string GetHttpVerbName ()
185 public override string GetHttpVersion ()
190 public override string GetLocalAddress ()
195 public override int GetLocalPort ()
200 public override string GetPathInfo ()
205 public override string GetQueryString ()
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
;
224 public override string GetRemoteAddress ()
229 public override int GetRemotePort ()
234 public override string GetServerVariable (string name
)
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 ()
252 public override string MapPath (string path
)
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)