2 // System.Web.Hosting.SimpleWorkerRequest.cs
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // (class signature from Bob Smith <bob@thestuff.net> (C) )
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
12 using System.Runtime.InteropServices;
13 using System.Web.Util;
15 namespace System.Web.Hosting
17 [MonoTODO("Implement security demands on the path usage functions (and review)")]
19 public class SimpleWorkerRequest : HttpWorkerRequest
22 private string _Query;
23 private string _PathInfo = String.Empty;
24 private string _AppVirtualPath;
25 private string _AppPhysicalPath;
26 private string _AppInstallPath;
27 private TextWriter _Output;
28 private bool _HasInstallInfo;
30 private SimpleWorkerRequest ()
34 public SimpleWorkerRequest (string Page, string Query, TextWriter Output)
39 AppDomain current = AppDomain.CurrentDomain;
40 object o = current.GetData (".appPath");
42 throw new HttpException ("Cannot get .appPath");
43 _AppPhysicalPath = o.ToString ();
45 o = current.GetData (".hostingVirtualPath");
47 throw new HttpException ("Cannot get .hostingVirtualPath");
48 _AppVirtualPath = o.ToString ();
50 o = current.GetData (".hostingInstallDir");
52 throw new HttpException ("Cannot get .hostingInstallDir");
53 _AppInstallPath = o.ToString ();
56 if (_AppPhysicalPath == null)
57 throw new HttpException ("Invalid app domain");
59 _HasInstallInfo = true;
61 ExtractPagePathInfo();
64 public SimpleWorkerRequest (string AppVirtualPath,
65 string AppPhysicalPath,
70 if (AppDomain.CurrentDomain.GetData (".appPath") == null)
71 throw new HttpException ("Invalid app domain");
75 _AppVirtualPath = AppVirtualPath;
76 _AppPhysicalPath = CheckAndAddSlash (AppPhysicalPath);
78 _HasInstallInfo = false;
80 ExtractPagePathInfo();
83 [MonoTODO("Implement security")]
84 public override string MachineInstallDirectory
88 return _AppInstallPath;
90 return ICalls.GetMachineInstallDirectory ();
94 public override string MachineConfigPath
96 get { return ICalls.GetMachineConfigPath (); }
99 public override void EndOfRequest ()
103 public override void FlushResponse (bool finalFlush)
107 public override string GetAppPath ()
109 return _AppVirtualPath;
112 public override string GetAppPathTranslated ()
114 return _AppPhysicalPath;
117 public override string GetFilePath ()
119 return CreatePath (false);
122 public override string GetFilePathTranslated ()
126 if (Path.DirectorySeparatorChar != '/')
127 page = _Page.Replace ('/', Path.DirectorySeparatorChar);
129 if (page [0] == Path.DirectorySeparatorChar)
130 page = page.Substring (1);
132 return (Path.Combine (_AppPhysicalPath, page));
135 public override string GetHttpVerbName ()
140 public override string GetHttpVersion ()
145 public override string GetLocalAddress ()
150 public override int GetLocalPort ()
155 public override string GetPathInfo ()
160 public override string GetQueryString ()
165 public override string GetRawUrl ()
167 string path = CreatePath (true);
168 if (null != _Query && _Query.Length > 0)
169 return path + "?" + _Query;
174 public override string GetRemoteAddress()
179 public override int GetRemotePort()
184 public override string GetServerVariable(string name)
189 public override string GetUriPath()
191 return CreatePath (true);
194 public override IntPtr GetUserToken()
199 public override string MapPath (string path)
201 string sPath = _AppPhysicalPath.Substring (0, _AppPhysicalPath.Length - 1);
202 if (path != null && path.Length > 0 && path [0] != '/')
205 char sep = Path.DirectorySeparatorChar;
206 if (path.StartsWith(_AppVirtualPath)) {
208 return sPath + path.Substring (_AppVirtualPath.Length);
210 return sPath + path.Substring (_AppVirtualPath.Length).Replace ('/', sep);
216 public override void SendKnownResponseHeader (int index, string value)
220 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
224 public override void SendResponseFromFile (string filename, long offset, long length)
228 public override void SendResponseFromMemory (byte [] data, int length)
230 _Output.Write (Encoding.Default.GetChars (data, 0, length));
233 public override void SendStatus(int statusCode, string statusDescription)
237 public override void SendUnknownResponseHeader(string name, string value)
241 // Create's a path string
242 private string CheckAndAddSlash(string sPath)
247 if (!sPath.EndsWith ("" + Path.DirectorySeparatorChar))
248 return sPath + Path.DirectorySeparatorChar;
253 // Create's a path string
254 private string CreatePath (bool bIncludePathInfo)
256 string sPath = Path.Combine (_AppVirtualPath, _Page);
258 if (bIncludePathInfo)
266 // "The extra path information, as given by the client. In
267 // other words, scripts can be accessed by their virtual
268 // pathname, followed by extra information at the end of this
269 // path. The extra information is sent as PATH_INFO."
270 private void ExtractPagePathInfo ()
272 if (_Page == null || _Page == String.Empty)
275 string FullPath = GetFilePathTranslated ();
276 int PathInfoLength = 0;
277 string LastFile = String.Empty;
279 while (PathInfoLength < _Page.Length) {
280 if (LastFile.Length > 0) {
281 // increase it by the length of the file plus
284 PathInfoLength += LastFile.Length + 1;
287 if (File.Exists (FullPath) == true)
290 if (Directory.Exists (FullPath) == true) {
291 PathInfoLength -= (LastFile.Length + 1);
295 LastFile = Path.GetFileName (FullPath);
296 FullPath = Path.GetDirectoryName (FullPath);
299 if (PathInfoLength <= 0 || PathInfoLength > _Page.Length)
302 _PathInfo = _Page.Substring (_Page.Length - PathInfoLength);
303 _Page = _Page.Substring (0, _Page.Length - PathInfoLength);