ServletWorkerRequest.jvm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. //
  2. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  3. //
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. using System;
  25. using System.IO;
  26. using System.Text;
  27. using System.Runtime.InteropServices;
  28. using System.Web.Util;
  29. using vmw.common;
  30. using System.Web.J2EE;
  31. using System.Collections;
  32. using System.Web;
  33. using javax.servlet;
  34. using javax.servlet.http;
  35. namespace System.Web.Hosting
  36. {
  37. [MonoTODO("Implement security demands on the path usage functions (and review)")]
  38. [ComVisible (false)]
  39. internal class ServletWorkerRequest : HttpWorkerRequest
  40. {
  41. private string _Page;
  42. private string _Query;
  43. private string _PathInfo = String.Empty;
  44. private string _AppVirtualPath;
  45. private string _AppPhysicalPath;
  46. private string _AppInstallPath;
  47. private bool _HasInstallInfo;
  48. private static string SLASH = "/";
  49. private ServletOutputStream _ServletOutputStream;
  50. private HttpServlet _HttpServlet;
  51. private HttpServletRequest _HttpServletRequest;
  52. private HttpServletResponse _HttpServletResponse;
  53. private string [][] unknownHeaders;
  54. private HttpWorkerRequest.EndOfSendNotification _endOfSendCallback;
  55. private object _endOfSendArgs;
  56. private ServletWorkerRequest ()
  57. {
  58. }
  59. public ServletWorkerRequest (HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp, ServletOutputStream output)
  60. :this(String.Empty, String.Empty ,String.Empty, String.Empty ,null)
  61. {
  62. #if DEBUG
  63. Console.WriteLine("Constructor 1 of ServletWorkerRequest!! -->");
  64. #endif
  65. _HttpServlet = servlet;
  66. _HttpServletRequest = req;
  67. _HttpServletResponse = resp;
  68. _ServletOutputStream = output;
  69. string contextPath = req.getContextPath();
  70. string requestURI = req.getRequestURI();
  71. if (requestURI.Equals(contextPath) ||
  72. ((requestURI.Length - contextPath.Length) == 1) && requestURI[requestURI.Length-1] == '/' && requestURI.StartsWith(contextPath))
  73. requestURI = contextPath + req.getServletPath();
  74. _Page = requestURI.Substring(contextPath.Length);
  75. if (_Page.StartsWith("/"))
  76. _Page = _Page.Substring(1);
  77. _Query = req.getQueryString();
  78. //_PathInfo = req.getPathInfo();
  79. _AppVirtualPath = req.getContextPath();
  80. _AppPhysicalPath = J2EEUtils.GetApplicationRealPath(servlet.getServletConfig());
  81. #if DEBUG
  82. LogCurrentPageLocation();
  83. #endif
  84. }
  85. public ServletWorkerRequest (string Page, string Query, ServletOutputStream output)
  86. :this(Page, Query)
  87. {
  88. #if DEBUG
  89. Console.WriteLine("Constructor 2 of ServletWorkerRequest!! -->");
  90. #endif
  91. //_Page = Page;
  92. ParsePathInfo ();
  93. //_Query = Query;
  94. AppDomain current = AppDomain.CurrentDomain;
  95. object o = current.GetData (".appPath");
  96. if (o == null)
  97. throw new HttpException ("Cannot get .appPath");
  98. _AppPhysicalPath = (string)current.GetData(IAppDomainConfig.WEB_APP_DIR);
  99. o = current.GetData (".hostingVirtualPath");
  100. if (o == null)
  101. throw new HttpException ("Cannot get .hostingVirtualPath");
  102. _AppVirtualPath = CheckAndAddVSlash (o.ToString ());
  103. o = current.GetData (".hostingInstallDir");
  104. if (o == null)
  105. throw new HttpException ("Cannot get .hostingInstallDir");
  106. _AppInstallPath = o.ToString ();
  107. _ServletOutputStream = output;
  108. if (_AppPhysicalPath == null)
  109. throw new HttpException ("Invalid app domain");
  110. _HasInstallInfo = true;
  111. #if DEBUG
  112. LogCurrentPageLocation();
  113. #endif
  114. }
  115. public ServletWorkerRequest (string AppVirtualPath,
  116. string AppPhysicalPath,
  117. string Page,
  118. string Query,
  119. ServletOutputStream output) : this (Page, Query)
  120. {
  121. #if DEBUG
  122. Console.WriteLine("Constructor 3 of ServletWorkerRequest!! -->");
  123. #endif
  124. if (AppDomain.CurrentDomain.GetData (".appPath") == null)
  125. throw new HttpException ("Invalid app domain");
  126. //_Page = Page;
  127. ParsePathInfo ();
  128. //_Query = Query;
  129. _AppVirtualPath = AppVirtualPath;
  130. _AppPhysicalPath = CheckAndAddSlash (AppPhysicalPath);
  131. _ServletOutputStream = output;
  132. _HasInstallInfo = false;
  133. #if DEBUG
  134. LogCurrentPageLocation();
  135. #endif
  136. }
  137. public ServletWorkerRequest (string Page, string Query)
  138. {
  139. _Page = Page;
  140. _Query = Query;
  141. AppDomain current = AppDomain.CurrentDomain;
  142. object o = current.GetData (".appPath");
  143. if (o == null)
  144. throw new HttpException ("Cannot get .appPath");
  145. _AppPhysicalPath = o.ToString ();
  146. o = current.GetData (".hostingVirtualPath");
  147. if (o == null)
  148. throw new HttpException ("Cannot get .hostingVirtualPath");
  149. _AppVirtualPath = o.ToString ();
  150. o = current.GetData (".hostingInstallDir");
  151. if (o == null)
  152. throw new HttpException ("Cannot get .hostingInstallDir");
  153. _AppInstallPath = o.ToString ();
  154. if (_AppPhysicalPath == null)
  155. throw new HttpException ("Invalid app domain");
  156. _HasInstallInfo = true;
  157. ExtractPagePathInfo();
  158. }
  159. public ServletOutputStream ServletOutputStream
  160. {
  161. get
  162. {
  163. return _ServletOutputStream;
  164. }
  165. }
  166. public HttpServlet Servlet
  167. {
  168. get {
  169. return _HttpServlet;
  170. }
  171. }
  172. public HttpServletRequest ServletRequest
  173. {
  174. get{
  175. return _HttpServletRequest;
  176. }
  177. }
  178. public HttpServletResponse ServletResponse
  179. {
  180. get{
  181. return _HttpServletResponse;
  182. }
  183. }
  184. [MonoTODO("Implement security")]
  185. public override string MachineInstallDirectory
  186. {
  187. get {
  188. if (_HasInstallInfo)
  189. return _AppInstallPath;
  190. return ICalls.GetMachineInstallDirectory ();
  191. }
  192. }
  193. public override string MachineConfigPath
  194. {
  195. get { return ICalls.GetMachineConfigPath (); }
  196. }
  197. public override void EndOfRequest ()
  198. {
  199. _ServletOutputStream = null;
  200. _HttpServlet = null;
  201. _HttpServletRequest = null;
  202. _HttpServletResponse = null;
  203. if (_endOfSendCallback != null)
  204. _endOfSendCallback(this, _endOfSendArgs);
  205. }
  206. public override void FlushResponse (bool finalFlush)
  207. {
  208. _ServletOutputStream.flush();
  209. if (finalFlush)
  210. _ServletOutputStream.close();
  211. }
  212. public override string GetAppPath ()
  213. {
  214. return _AppVirtualPath;
  215. }
  216. public override string GetAppPathTranslated ()
  217. {
  218. return _AppPhysicalPath;
  219. }
  220. public override string GetFilePath ()
  221. {
  222. return CreatePath (false);
  223. }
  224. public override string GetFilePathTranslated ()
  225. {
  226. string page = _Page;
  227. if (Path.DirectorySeparatorChar != '/')
  228. page = _Page.Replace ('/', Path.DirectorySeparatorChar);
  229. if (page [0] == Path.DirectorySeparatorChar)
  230. page = page.Substring (1);
  231. return (Path.Combine (_AppPhysicalPath, page));
  232. }
  233. public override string GetHttpVerbName ()
  234. {
  235. return _HttpServletRequest.getMethod();
  236. }
  237. public override string GetHttpVersion ()
  238. {
  239. return _HttpServletRequest.getProtocol();
  240. }
  241. public override string GetLocalAddress ()
  242. {
  243. return _HttpServletRequest.getServerName();
  244. }
  245. public override int GetLocalPort ()
  246. {
  247. return _HttpServletRequest.getServerPort();
  248. }
  249. public override string GetPathInfo ()
  250. {
  251. return (null != _PathInfo) ? _PathInfo : String.Empty;
  252. }
  253. public override string GetQueryString ()
  254. {
  255. return _Query;
  256. }
  257. public override string GetRawUrl ()
  258. {
  259. string path = CreatePath (true);
  260. if (null != _Query && _Query.Length > 0)
  261. return path + "?" + _Query;
  262. return path;
  263. }
  264. public override string GetRemoteAddress()
  265. {
  266. return _HttpServletRequest.getRemoteAddr();
  267. }
  268. public override int GetRemotePort()
  269. {
  270. try
  271. {
  272. return _HttpServletRequest.getRemotePort();
  273. }
  274. catch(Exception e) //should catch also java.lang.Throwable
  275. {
  276. //if servlet API is 2.3 and below - there is no
  277. //method getRemotePort in ServletRequest interface...
  278. //should be described as limitation.
  279. return 0;
  280. }
  281. }
  282. public override string GetServerVariable(string name)
  283. {
  284. return String.Empty;
  285. }
  286. public override string GetUriPath()
  287. {
  288. return CreatePath (true);
  289. }
  290. public override IntPtr GetUserToken()
  291. {
  292. return IntPtr.Zero;
  293. }
  294. public override string MapPath (string path)
  295. {
  296. if (path.StartsWith(_AppVirtualPath))
  297. {
  298. path = path.Remove(0,_AppVirtualPath.Length);
  299. if (path.StartsWith("/"))
  300. path = path.Remove(0,1);
  301. }
  302. //string realPath = Servlet.getServletContext().getRealPath(path);
  303. // if (Path.IsPathRooted(path))
  304. // return path;
  305. // if (!path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL)&&
  306. // !path.StartsWith("/") && !path.StartsWith("\\")&& !Path.IsPathRooted(path))
  307. // return IAppDomainConfig.WAR_ROOT_SYMBOL + "/" + path;
  308. // else if (!path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL)&& !Path.IsPathRooted(path))
  309. // return IAppDomainConfig.WAR_ROOT_SYMBOL + path;
  310. // else
  311. // return path;
  312. if (path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL))
  313. {
  314. return path;
  315. }
  316. string retVal = IAppDomainConfig.WAR_ROOT_SYMBOL;
  317. if (!path.StartsWith("/") && !path.StartsWith("\\"))
  318. retVal += "/";
  319. retVal += path;
  320. return retVal;
  321. }
  322. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  323. {
  324. }
  325. public override void SendResponseFromFile (string filename, long offset, long length)
  326. {
  327. }
  328. public override void SendResponseFromMemory (byte [] data, int length)
  329. {
  330. sbyte [] sdata = vmw.common.TypeUtils.ToSByteArray(data);
  331. _ServletOutputStream.write(sdata, 0 , length);
  332. }
  333. public override void SendStatus(int statusCode, string statusDescription)
  334. {
  335. _HttpServletResponse.setStatus(statusCode, statusDescription);
  336. }
  337. public override void SendUnknownResponseHeader(string name, string value)
  338. {
  339. _HttpServletResponse.addHeader(name, value);
  340. }
  341. public override bool HeadersSent ()
  342. {
  343. return false;
  344. }
  345. public override void SendCalculatedContentLength (int contentLength)
  346. {
  347. //FIXME: Should we ignore this for apache2?
  348. SendUnknownResponseHeader ("Content-Length", contentLength.ToString ());
  349. }
  350. public override void SendKnownResponseHeader (int index, string value)
  351. {
  352. if (HeadersSent ())
  353. return;
  354. string headerName = HttpWorkerRequest.GetKnownResponseHeaderName (index);
  355. SendUnknownResponseHeader (headerName, value);
  356. }
  357. // Create's a path string
  358. private string CheckAndAddSlash(string sPath)
  359. {
  360. if (null == sPath)
  361. return null;
  362. if (String.Empty == sPath)
  363. return SLASH;
  364. if (!sPath.EndsWith ("" + Path.DirectorySeparatorChar))
  365. return sPath + Path.DirectorySeparatorChar;
  366. return sPath;
  367. }
  368. // Creates a path string
  369. private string CheckAndAddVSlash(string sPath)
  370. {
  371. if (null == sPath)
  372. return null;
  373. if (!sPath.EndsWith ("/"))
  374. return sPath + "/";
  375. return sPath;
  376. }
  377. // Create's a path string
  378. private string CreatePath (bool bIncludePathInfo)
  379. {
  380. string sPath = Path.Combine (_AppVirtualPath, _Page);
  381. if (bIncludePathInfo && null != _PathInfo)
  382. {
  383. sPath += _PathInfo;
  384. }
  385. return sPath;
  386. }
  387. // "The extra path information, as given by the client. In
  388. // other words, scripts can be accessed by their virtual
  389. // pathname, followed by extra information at the end of this
  390. // path. The extra information is sent as PATH_INFO."
  391. private void ExtractPagePathInfo ()
  392. {
  393. if (_Page == null || _Page == String.Empty)
  394. return;
  395. string FullPath = GetFilePathTranslated ();
  396. int PathInfoLength = 0;
  397. string LastFile = String.Empty;
  398. while (PathInfoLength < _Page.Length) {
  399. if (LastFile.Length > 0) {
  400. // increase it by the length of the file plus
  401. // a "/"
  402. //
  403. PathInfoLength += LastFile.Length + 1;
  404. }
  405. if (File.Exists (FullPath) == true)
  406. break;
  407. if (Directory.Exists (FullPath) == true) {
  408. PathInfoLength -= (LastFile.Length + 1);
  409. break;
  410. }
  411. LastFile = Path.GetFileName (FullPath);
  412. FullPath = Path.GetDirectoryName (FullPath);
  413. }
  414. if (PathInfoLength <= 0 || PathInfoLength > _Page.Length)
  415. return;
  416. _PathInfo = _Page.Substring (_Page.Length - PathInfoLength);
  417. _Page = _Page.Substring (0, _Page.Length - PathInfoLength);
  418. }
  419. // Parses out the string after / known as the "path info"
  420. private void ParsePathInfo ()
  421. {
  422. int iPos = _Page.LastIndexOf("/");
  423. if (iPos >= 0) {
  424. _PathInfo = _Page.Substring (iPos);
  425. _Page = _Page.Substring (0, iPos);
  426. }
  427. }
  428. public override string GetKnownRequestHeader (int index)
  429. {
  430. if (_HttpServletRequest == null)
  431. return null;
  432. string headerName = HttpWorkerRequest.GetKnownRequestHeaderName (index);
  433. return _HttpServletRequest.getHeader(headerName);
  434. }
  435. public override string GetUnknownRequestHeader (string name)
  436. {
  437. if (_HttpServletRequest == null)
  438. return null;
  439. return _HttpServletRequest.getHeader(name);
  440. }
  441. public override string [][] GetUnknownRequestHeaders ()
  442. {
  443. if (unknownHeaders == null) {
  444. ArrayList pairs = new ArrayList ();
  445. for (java.util.Enumeration he = _HttpServletRequest.getHeaderNames(); he.hasMoreElements() ;) {
  446. string key = (string) he.nextElement();
  447. int index = HttpWorkerRequest.GetKnownRequestHeaderIndex (key);
  448. if (index != -1)
  449. continue;
  450. pairs.Add (new string [] {key, _HttpServletRequest.getHeader(key)});
  451. }
  452. if (pairs.Count != 0) {
  453. unknownHeaders = new string [pairs.Count][];
  454. for (int i = 0; i < pairs.Count; i++)
  455. unknownHeaders [i] = (string []) pairs [i];
  456. }
  457. }
  458. if (unknownHeaders == null) unknownHeaders = new string [0][];
  459. return unknownHeaders;
  460. }
  461. public override int ReadEntityBody (byte [] buffer, int size)
  462. {
  463. if (buffer == null || size == 0)
  464. return 0;
  465. sbyte [] sbuffer = new sbyte [size];
  466. int r = _HttpServletRequest.getInputStream().read(sbuffer, 0, size);
  467. for (int i=0; i < r; i++) buffer[i] = (byte) sbuffer[i];
  468. return (r==-1)?0:r;
  469. }
  470. public override void SetEndOfSendNotification(System.Web.HttpWorkerRequest.EndOfSendNotification callback, object extraData)
  471. {
  472. _endOfSendCallback = callback;
  473. _endOfSendArgs = extraData;
  474. }
  475. // Prints some stats about the current _Page.
  476. private void LogCurrentPageLocation()
  477. {
  478. #if DEBUG
  479. Console.WriteLine(" relpath=" + _AppVirtualPath);
  480. Console.WriteLine(" physical path=" + _AppPhysicalPath);
  481. Console.WriteLine(" page=" + _Page);
  482. #endif
  483. }
  484. }
  485. }