ServletWorkerRequest.jvm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. Console.WriteLine("Constructor 3 of ServletWorkerRequest!! -->");
  122. if (AppDomain.CurrentDomain.GetData (".appPath") == null)
  123. throw new HttpException ("Invalid app domain");
  124. //_Page = Page;
  125. ParsePathInfo ();
  126. //_Query = Query;
  127. _AppVirtualPath = AppVirtualPath;
  128. _AppPhysicalPath = CheckAndAddSlash (AppPhysicalPath);
  129. _ServletOutputStream = output;
  130. _HasInstallInfo = false;
  131. #if DEBUG
  132. LogCurrentPageLocation();
  133. #endif
  134. }
  135. public ServletWorkerRequest (string Page, string Query)
  136. {
  137. _Page = Page;
  138. _Query = Query;
  139. AppDomain current = AppDomain.CurrentDomain;
  140. object o = current.GetData (".appPath");
  141. if (o == null)
  142. throw new HttpException ("Cannot get .appPath");
  143. _AppPhysicalPath = o.ToString ();
  144. o = current.GetData (".hostingVirtualPath");
  145. if (o == null)
  146. throw new HttpException ("Cannot get .hostingVirtualPath");
  147. _AppVirtualPath = o.ToString ();
  148. o = current.GetData (".hostingInstallDir");
  149. if (o == null)
  150. throw new HttpException ("Cannot get .hostingInstallDir");
  151. _AppInstallPath = o.ToString ();
  152. if (_AppPhysicalPath == null)
  153. throw new HttpException ("Invalid app domain");
  154. _HasInstallInfo = true;
  155. ExtractPagePathInfo();
  156. }
  157. public ServletOutputStream ServletOutputStream
  158. {
  159. get
  160. {
  161. return _ServletOutputStream;
  162. }
  163. }
  164. public HttpServlet Servlet
  165. {
  166. get {
  167. return _HttpServlet;
  168. }
  169. }
  170. public HttpServletRequest ServletRequest
  171. {
  172. get{
  173. return _HttpServletRequest;
  174. }
  175. }
  176. public HttpServletResponse ServletResponse
  177. {
  178. get{
  179. return _HttpServletResponse;
  180. }
  181. }
  182. [MonoTODO("Implement security")]
  183. public override string MachineInstallDirectory
  184. {
  185. get {
  186. if (_HasInstallInfo)
  187. return _AppInstallPath;
  188. return ICalls.GetMachineInstallDirectory ();
  189. }
  190. }
  191. public override string MachineConfigPath
  192. {
  193. get { return ICalls.GetMachineConfigPath (); }
  194. }
  195. public override void EndOfRequest ()
  196. {
  197. _ServletOutputStream = null;
  198. _HttpServlet = null;
  199. _HttpServletRequest = null;
  200. _HttpServletResponse = null;
  201. if (_endOfSendCallback != null)
  202. _endOfSendCallback(this, _endOfSendArgs);
  203. }
  204. public override void FlushResponse (bool finalFlush)
  205. {
  206. _ServletOutputStream.flush();
  207. if (finalFlush)
  208. _ServletOutputStream.close();
  209. }
  210. public override string GetAppPath ()
  211. {
  212. return _AppVirtualPath;
  213. }
  214. public override string GetAppPathTranslated ()
  215. {
  216. return _AppPhysicalPath;
  217. }
  218. public override string GetFilePath ()
  219. {
  220. return CreatePath (false);
  221. }
  222. public override string GetFilePathTranslated ()
  223. {
  224. string page = _Page;
  225. if (Path.DirectorySeparatorChar != '/')
  226. page = _Page.Replace ('/', Path.DirectorySeparatorChar);
  227. if (page [0] == Path.DirectorySeparatorChar)
  228. page = page.Substring (1);
  229. return (Path.Combine (_AppPhysicalPath, page));
  230. }
  231. public override string GetHttpVerbName ()
  232. {
  233. return _HttpServletRequest.getMethod();
  234. }
  235. public override string GetHttpVersion ()
  236. {
  237. return _HttpServletRequest.getProtocol();
  238. }
  239. public override string GetLocalAddress ()
  240. {
  241. return _HttpServletRequest.getServerName();
  242. }
  243. public override int GetLocalPort ()
  244. {
  245. return _HttpServletRequest.getServerPort();
  246. }
  247. public override string GetPathInfo ()
  248. {
  249. return (null != _PathInfo) ? _PathInfo : String.Empty;
  250. }
  251. public override string GetQueryString ()
  252. {
  253. return _Query;
  254. }
  255. public override string GetRawUrl ()
  256. {
  257. string path = CreatePath (true);
  258. if (null != _Query && _Query.Length > 0)
  259. return path + "?" + _Query;
  260. return path;
  261. }
  262. public override string GetRemoteAddress()
  263. {
  264. return _HttpServletRequest.getRemoteAddr();
  265. }
  266. public override int GetRemotePort()
  267. {
  268. try
  269. {
  270. return _HttpServletRequest.getRemotePort();
  271. }
  272. catch(Exception e) //should catch also java.lang.Throwable
  273. {
  274. //if servlet API is 2.3 and below - there is no
  275. //method getRemotePort in ServletRequest interface...
  276. //should be described as limitation.
  277. return 0;
  278. }
  279. }
  280. public override string GetServerVariable(string name)
  281. {
  282. return String.Empty;
  283. }
  284. public override string GetUriPath()
  285. {
  286. return CreatePath (true);
  287. }
  288. public override IntPtr GetUserToken()
  289. {
  290. return IntPtr.Zero;
  291. }
  292. public override string MapPath (string path)
  293. {
  294. if (path.StartsWith(_AppVirtualPath))
  295. {
  296. path = path.Remove(0,_AppVirtualPath.Length);
  297. if (path.StartsWith("/"))
  298. path = path.Remove(0,1);
  299. }
  300. //string realPath = Servlet.getServletContext().getRealPath(path);
  301. // if (Path.IsPathRooted(path))
  302. // return path;
  303. // if (!path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL)&&
  304. // !path.StartsWith("/") && !path.StartsWith("\\")&& !Path.IsPathRooted(path))
  305. // return IAppDomainConfig.WAR_ROOT_SYMBOL + "/" + path;
  306. // else if (!path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL)&& !Path.IsPathRooted(path))
  307. // return IAppDomainConfig.WAR_ROOT_SYMBOL + path;
  308. // else
  309. // return path;
  310. if (path.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL))
  311. {
  312. return path;
  313. }
  314. string retVal = IAppDomainConfig.WAR_ROOT_SYMBOL;
  315. if (!path.StartsWith("/") && !path.StartsWith("\\"))
  316. retVal += "/";
  317. retVal += path;
  318. return retVal;
  319. }
  320. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  321. {
  322. }
  323. public override void SendResponseFromFile (string filename, long offset, long length)
  324. {
  325. }
  326. public override void SendResponseFromMemory (byte [] data, int length)
  327. {
  328. sbyte [] sdata = vmw.common.TypeUtils.ToSByteArray(data);
  329. _ServletOutputStream.write(sdata, 0 , length);
  330. }
  331. public override void SendStatus(int statusCode, string statusDescription)
  332. {
  333. _HttpServletResponse.setStatus(statusCode, statusDescription);
  334. }
  335. public override void SendUnknownResponseHeader(string name, string value)
  336. {
  337. _HttpServletResponse.addHeader(name, value);
  338. }
  339. public override bool HeadersSent ()
  340. {
  341. return false;
  342. }
  343. public override void SendCalculatedContentLength (int contentLength)
  344. {
  345. //FIXME: Should we ignore this for apache2?
  346. SendUnknownResponseHeader ("Content-Length", contentLength.ToString ());
  347. }
  348. public override void SendKnownResponseHeader (int index, string value)
  349. {
  350. if (HeadersSent ())
  351. return;
  352. string headerName = HttpWorkerRequest.GetKnownResponseHeaderName (index);
  353. SendUnknownResponseHeader (headerName, value);
  354. }
  355. // Create's a path string
  356. private string CheckAndAddSlash(string sPath)
  357. {
  358. if (null == sPath)
  359. return null;
  360. if (String.Empty == sPath)
  361. return SLASH;
  362. if (!sPath.EndsWith ("" + Path.DirectorySeparatorChar))
  363. return sPath + Path.DirectorySeparatorChar;
  364. return sPath;
  365. }
  366. // Creates a path string
  367. private string CheckAndAddVSlash(string sPath)
  368. {
  369. if (null == sPath)
  370. return null;
  371. if (!sPath.EndsWith ("/"))
  372. return sPath + "/";
  373. return sPath;
  374. }
  375. // Create's a path string
  376. private string CreatePath (bool bIncludePathInfo)
  377. {
  378. string sPath = Path.Combine (_AppVirtualPath, _Page);
  379. if (bIncludePathInfo && null != _PathInfo)
  380. {
  381. sPath += _PathInfo;
  382. }
  383. return sPath;
  384. }
  385. // "The extra path information, as given by the client. In
  386. // other words, scripts can be accessed by their virtual
  387. // pathname, followed by extra information at the end of this
  388. // path. The extra information is sent as PATH_INFO."
  389. private void ExtractPagePathInfo ()
  390. {
  391. if (_Page == null || _Page == String.Empty)
  392. return;
  393. string FullPath = GetFilePathTranslated ();
  394. int PathInfoLength = 0;
  395. string LastFile = String.Empty;
  396. while (PathInfoLength < _Page.Length) {
  397. if (LastFile.Length > 0) {
  398. // increase it by the length of the file plus
  399. // a "/"
  400. //
  401. PathInfoLength += LastFile.Length + 1;
  402. }
  403. if (File.Exists (FullPath) == true)
  404. break;
  405. if (Directory.Exists (FullPath) == true) {
  406. PathInfoLength -= (LastFile.Length + 1);
  407. break;
  408. }
  409. LastFile = Path.GetFileName (FullPath);
  410. FullPath = Path.GetDirectoryName (FullPath);
  411. }
  412. if (PathInfoLength <= 0 || PathInfoLength > _Page.Length)
  413. return;
  414. _PathInfo = _Page.Substring (_Page.Length - PathInfoLength);
  415. _Page = _Page.Substring (0, _Page.Length - PathInfoLength);
  416. }
  417. // Parses out the string after / known as the "path info"
  418. private void ParsePathInfo ()
  419. {
  420. int iPos = _Page.LastIndexOf("/");
  421. if (iPos >= 0) {
  422. _PathInfo = _Page.Substring (iPos);
  423. _Page = _Page.Substring (0, iPos);
  424. }
  425. }
  426. public override string GetKnownRequestHeader (int index)
  427. {
  428. if (_HttpServletRequest == null)
  429. return null;
  430. string headerName = HttpWorkerRequest.GetKnownRequestHeaderName (index);
  431. return _HttpServletRequest.getHeader(headerName);
  432. }
  433. public override string GetUnknownRequestHeader (string name)
  434. {
  435. if (_HttpServletRequest == null)
  436. return null;
  437. return _HttpServletRequest.getHeader(name);
  438. }
  439. public override string [][] GetUnknownRequestHeaders ()
  440. {
  441. if (unknownHeaders == null) {
  442. ArrayList pairs = new ArrayList ();
  443. for (java.util.Enumeration he = _HttpServletRequest.getHeaderNames(); he.hasMoreElements() ;) {
  444. string key = (string) he.nextElement();
  445. int index = HttpWorkerRequest.GetKnownRequestHeaderIndex (key);
  446. if (index != -1)
  447. continue;
  448. pairs.Add (new string [] {key, _HttpServletRequest.getHeader(key)});
  449. }
  450. if (pairs.Count != 0) {
  451. unknownHeaders = new string [pairs.Count][];
  452. for (int i = 0; i < pairs.Count; i++)
  453. unknownHeaders [i] = (string []) pairs [i];
  454. }
  455. }
  456. if (unknownHeaders == null) unknownHeaders = new string [0][];
  457. return unknownHeaders;
  458. }
  459. public override int ReadEntityBody (byte [] buffer, int size)
  460. {
  461. if (buffer == null || size == 0)
  462. return 0;
  463. sbyte [] sbuffer = new sbyte [size];
  464. int r = _HttpServletRequest.getInputStream().read(sbuffer, 0, size);
  465. for (int i=0; i < r; i++) buffer[i] = (byte) sbuffer[i];
  466. return (r==-1)?0:r;
  467. }
  468. public override void SetEndOfSendNotification(System.Web.HttpWorkerRequest.EndOfSendNotification callback, object extraData)
  469. {
  470. _endOfSendCallback = callback;
  471. _endOfSendArgs = extraData;
  472. }
  473. // Prints some stats about the current _Page.
  474. private void LogCurrentPageLocation()
  475. {
  476. #if DEBUG
  477. Console.WriteLine(" relpath=" + _AppVirtualPath);
  478. Console.WriteLine(" physical path=" + _AppPhysicalPath);
  479. Console.WriteLine(" page=" + _Page);
  480. #endif
  481. }
  482. }
  483. }