UrlModule.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Threading;
  29. using System.Collections.Generic;
  30. using System.Collections;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
  41. {
  42. /// <summary>
  43. /// Data describing an external URL set up by a script.
  44. /// </summary>
  45. public class UrlData
  46. {
  47. /// <summary>
  48. /// Scene object part hosting the script
  49. /// </summary>
  50. public UUID hostID;
  51. /// <summary>
  52. /// The item ID of the script that requested the URL.
  53. /// </summary>
  54. public UUID itemID;
  55. /// <summary>
  56. /// The script engine that runs the script.
  57. /// </summary>
  58. public IScriptModule engine;
  59. /// <summary>
  60. /// The generated URL.
  61. /// </summary>
  62. public string url;
  63. /// <summary>
  64. /// The random UUID component of the generated URL.
  65. /// </summary>
  66. public UUID urlcode;
  67. /// <summary>
  68. /// The external requests currently being processed or awaiting retrieval for this URL.
  69. /// </summary>
  70. public Dictionary<UUID, RequestData> requests;
  71. }
  72. public class RequestData
  73. {
  74. public UUID requestID;
  75. public Dictionary<string, string> headers;
  76. public string body;
  77. public int responseCode;
  78. public string responseBody;
  79. public string responseType = "text/plain";
  80. //public ManualResetEvent ev;
  81. public bool requestDone;
  82. public int startTime;
  83. public string uri;
  84. }
  85. /// <summary>
  86. /// This module provides external URLs for in-world scripts.
  87. /// </summary>
  88. public class UrlModule : ISharedRegionModule, IUrlModule
  89. {
  90. private static readonly ILog m_log =
  91. LogManager.GetLogger(
  92. MethodBase.GetCurrentMethod().DeclaringType);
  93. /// <summary>
  94. /// Indexs the URL request metadata (which script requested it, outstanding requests, etc.) by the request ID
  95. /// randomly generated when a request is received for this URL.
  96. /// </summary>
  97. /// <remarks>
  98. /// Manipulation or retrieval from this dictionary must be locked on m_UrlMap to preserve consistency with
  99. /// m_UrlMap
  100. /// </remarks>
  101. private Dictionary<UUID, UrlData> m_RequestMap = new Dictionary<UUID, UrlData>();
  102. /// <summary>
  103. /// Indexs the URL request metadata (which script requested it, outstanding requests, etc.) by the full URL
  104. /// </summary>
  105. private Dictionary<string, UrlData> m_UrlMap = new Dictionary<string, UrlData>();
  106. /// <summary>
  107. /// Maximum number of external urls that can be set up by this module.
  108. /// </summary>
  109. private int m_TotalUrls = 100;
  110. private uint https_port = 0;
  111. private IHttpServer m_HttpServer = null;
  112. private IHttpServer m_HttpsServer = null;
  113. private string m_ExternalHostNameForLSL = "";
  114. public string ExternalHostNameForLSL
  115. {
  116. get { return m_ExternalHostNameForLSL; }
  117. }
  118. public Type ReplaceableInterface
  119. {
  120. get { return null; }
  121. }
  122. public string Name
  123. {
  124. get { return "UrlModule"; }
  125. }
  126. public void Initialise(IConfigSource config)
  127. {
  128. m_ExternalHostNameForLSL = config.Configs["Network"].GetString("ExternalHostNameForLSL", System.Environment.MachineName);
  129. bool ssl_enabled = config.Configs["Network"].GetBoolean("https_listener",false);
  130. if (ssl_enabled)
  131. https_port = (uint) config.Configs["Network"].GetInt("https_port",0);
  132. IConfig llFunctionsConfig = config.Configs["LL-Functions"];
  133. if (llFunctionsConfig != null)
  134. m_TotalUrls = llFunctionsConfig.GetInt("max_external_urls_per_simulator", m_TotalUrls);
  135. }
  136. public void PostInitialise()
  137. {
  138. }
  139. public void AddRegion(Scene scene)
  140. {
  141. if (m_HttpServer == null)
  142. {
  143. // There can only be one
  144. //
  145. m_HttpServer = MainServer.Instance;
  146. //
  147. // We can use the https if it is enabled
  148. if (https_port > 0)
  149. {
  150. m_HttpsServer = MainServer.GetHttpServer(https_port);
  151. }
  152. }
  153. scene.RegisterModuleInterface<IUrlModule>(this);
  154. scene.EventManager.OnScriptReset += OnScriptReset;
  155. }
  156. public void RegionLoaded(Scene scene)
  157. {
  158. IScriptModule[] scriptModules = scene.RequestModuleInterfaces<IScriptModule>();
  159. foreach (IScriptModule scriptModule in scriptModules)
  160. {
  161. scriptModule.OnScriptRemoved += ScriptRemoved;
  162. scriptModule.OnObjectRemoved += ObjectRemoved;
  163. }
  164. }
  165. public void RemoveRegion(Scene scene)
  166. {
  167. }
  168. public void Close()
  169. {
  170. }
  171. public UUID RequestURL(IScriptModule engine, SceneObjectPart host, UUID itemID)
  172. {
  173. UUID urlcode = UUID.Random();
  174. lock (m_UrlMap)
  175. {
  176. if (m_UrlMap.Count >= m_TotalUrls)
  177. {
  178. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" });
  179. return urlcode;
  180. }
  181. string url = "http://" + m_ExternalHostNameForLSL + ":" + m_HttpServer.Port.ToString() + "/lslhttp/" + urlcode.ToString() + "/";
  182. UrlData urlData = new UrlData();
  183. urlData.hostID = host.UUID;
  184. urlData.itemID = itemID;
  185. urlData.engine = engine;
  186. urlData.url = url;
  187. urlData.urlcode = urlcode;
  188. urlData.requests = new Dictionary<UUID, RequestData>();
  189. m_UrlMap[url] = urlData;
  190. string uri = "/lslhttp/" + urlcode.ToString() + "/";
  191. m_HttpServer.AddPollServiceHTTPHandler(
  192. uri,
  193. new PollServiceEventArgs(HttpRequestHandler, HasEvents, GetEvents, NoEvents, urlcode));
  194. m_log.DebugFormat(
  195. "[URL MODULE]: Set up incoming request url {0} for {1} in {2} {3}",
  196. uri, itemID, host.Name, host.LocalId);
  197. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_GRANTED", url });
  198. }
  199. return urlcode;
  200. }
  201. public UUID RequestSecureURL(IScriptModule engine, SceneObjectPart host, UUID itemID)
  202. {
  203. UUID urlcode = UUID.Random();
  204. if (m_HttpsServer == null)
  205. {
  206. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" });
  207. return urlcode;
  208. }
  209. lock (m_UrlMap)
  210. {
  211. if (m_UrlMap.Count >= m_TotalUrls)
  212. {
  213. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" });
  214. return urlcode;
  215. }
  216. string url = "https://" + m_ExternalHostNameForLSL + ":" + m_HttpsServer.Port.ToString() + "/lslhttps/" + urlcode.ToString() + "/";
  217. UrlData urlData = new UrlData();
  218. urlData.hostID = host.UUID;
  219. urlData.itemID = itemID;
  220. urlData.engine = engine;
  221. urlData.url = url;
  222. urlData.urlcode = urlcode;
  223. urlData.requests = new Dictionary<UUID, RequestData>();
  224. m_UrlMap[url] = urlData;
  225. string uri = "/lslhttps/" + urlcode.ToString() + "/";
  226. m_HttpsServer.AddPollServiceHTTPHandler(
  227. uri,
  228. new PollServiceEventArgs(HttpRequestHandler, HasEvents, GetEvents, NoEvents, urlcode));
  229. m_log.DebugFormat(
  230. "[URL MODULE]: Set up incoming secure request url {0} for {1} in {2} {3}",
  231. uri, itemID, host.Name, host.LocalId);
  232. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_GRANTED", url });
  233. }
  234. return urlcode;
  235. }
  236. public void ReleaseURL(string url)
  237. {
  238. lock (m_UrlMap)
  239. {
  240. UrlData data;
  241. if (!m_UrlMap.TryGetValue(url, out data))
  242. {
  243. return;
  244. }
  245. foreach (UUID req in data.requests.Keys)
  246. m_RequestMap.Remove(req);
  247. m_log.DebugFormat(
  248. "[URL MODULE]: Releasing url {0} for {1} in {2}",
  249. url, data.itemID, data.hostID);
  250. RemoveUrl(data);
  251. m_UrlMap.Remove(url);
  252. }
  253. }
  254. public void HttpContentType(UUID request, string type)
  255. {
  256. lock (m_UrlMap)
  257. {
  258. if (m_RequestMap.ContainsKey(request))
  259. {
  260. UrlData urlData = m_RequestMap[request];
  261. urlData.requests[request].responseType = type;
  262. }
  263. else
  264. {
  265. m_log.Info("[HttpRequestHandler] There is no http-in request with id " + request.ToString());
  266. }
  267. }
  268. }
  269. public void HttpResponse(UUID request, int status, string body)
  270. {
  271. lock (m_UrlMap)
  272. {
  273. if (m_RequestMap.ContainsKey(request))
  274. {
  275. UrlData urlData = m_RequestMap[request];
  276. urlData.requests[request].responseCode = status;
  277. urlData.requests[request].responseBody = body;
  278. //urlData.requests[request].ev.Set();
  279. urlData.requests[request].requestDone =true;
  280. }
  281. else
  282. {
  283. m_log.Info("[HttpRequestHandler] There is no http-in request with id " + request.ToString());
  284. }
  285. }
  286. }
  287. public string GetHttpHeader(UUID requestId, string header)
  288. {
  289. lock (m_UrlMap)
  290. {
  291. if (m_RequestMap.ContainsKey(requestId))
  292. {
  293. UrlData urlData = m_RequestMap[requestId];
  294. string value;
  295. if (urlData.requests[requestId].headers.TryGetValue(header, out value))
  296. return value;
  297. }
  298. else
  299. {
  300. m_log.Warn("[HttpRequestHandler] There was no http-in request with id " + requestId);
  301. }
  302. }
  303. return String.Empty;
  304. }
  305. public int GetFreeUrls()
  306. {
  307. lock (m_UrlMap)
  308. return m_TotalUrls - m_UrlMap.Count;
  309. }
  310. public void ScriptRemoved(UUID itemID)
  311. {
  312. // m_log.DebugFormat("[URL MODULE]: Removing script {0}", itemID);
  313. lock (m_UrlMap)
  314. {
  315. List<string> removeURLs = new List<string>();
  316. foreach (KeyValuePair<string, UrlData> url in m_UrlMap)
  317. {
  318. if (url.Value.itemID == itemID)
  319. {
  320. RemoveUrl(url.Value);
  321. removeURLs.Add(url.Key);
  322. foreach (UUID req in url.Value.requests.Keys)
  323. m_RequestMap.Remove(req);
  324. }
  325. }
  326. foreach (string urlname in removeURLs)
  327. m_UrlMap.Remove(urlname);
  328. }
  329. }
  330. public void ObjectRemoved(UUID objectID)
  331. {
  332. lock (m_UrlMap)
  333. {
  334. List<string> removeURLs = new List<string>();
  335. foreach (KeyValuePair<string, UrlData> url in m_UrlMap)
  336. {
  337. if (url.Value.hostID == objectID)
  338. {
  339. RemoveUrl(url.Value);
  340. removeURLs.Add(url.Key);
  341. foreach (UUID req in url.Value.requests.Keys)
  342. m_RequestMap.Remove(req);
  343. }
  344. }
  345. foreach (string urlname in removeURLs)
  346. m_UrlMap.Remove(urlname);
  347. }
  348. }
  349. private void RemoveUrl(UrlData data)
  350. {
  351. m_HttpServer.RemoveHTTPHandler("", "/lslhttp/" + data.urlcode.ToString() + "/");
  352. }
  353. private Hashtable NoEvents(UUID requestID, UUID sessionID)
  354. {
  355. Hashtable response = new Hashtable();
  356. UrlData urlData;
  357. lock (m_UrlMap)
  358. {
  359. // We need to return a 404 here in case the request URL was removed at exactly the same time that a
  360. // request was made. In this case, the request thread can outrace llRemoveURL() and still be polling
  361. // for the request ID.
  362. if (!m_RequestMap.ContainsKey(requestID))
  363. {
  364. response["int_response_code"] = 404;
  365. response["str_response_string"] = "";
  366. response["keepalive"] = false;
  367. response["reusecontext"] = false;
  368. return response;
  369. }
  370. urlData = m_RequestMap[requestID];
  371. if (System.Environment.TickCount - urlData.requests[requestID].startTime > 25000)
  372. {
  373. response["int_response_code"] = 500;
  374. response["str_response_string"] = "Script timeout";
  375. response["content_type"] = "text/plain";
  376. response["keepalive"] = false;
  377. response["reusecontext"] = false;
  378. //remove from map
  379. urlData.requests.Remove(requestID);
  380. m_RequestMap.Remove(requestID);
  381. return response;
  382. }
  383. }
  384. return response;
  385. }
  386. private bool HasEvents(UUID requestID, UUID sessionID)
  387. {
  388. lock (m_UrlMap)
  389. {
  390. // We return true here because an external URL request that happened at the same time as an llRemoveURL()
  391. // can still make it through to HttpRequestHandler(). That will return without setting up a request
  392. // when it detects that the URL has been removed. The poller, however, will continue to ask for
  393. // events for that request, so here we will signal that there are events and in GetEvents we will
  394. // return a 404.
  395. if (!m_RequestMap.ContainsKey(requestID))
  396. {
  397. return true;
  398. }
  399. UrlData urlData = m_RequestMap[requestID];
  400. if (!urlData.requests.ContainsKey(requestID))
  401. {
  402. return true;
  403. }
  404. // Trigger return of timeout response.
  405. if (System.Environment.TickCount - urlData.requests[requestID].startTime > 25000)
  406. {
  407. return true;
  408. }
  409. return urlData.requests[requestID].requestDone;
  410. }
  411. }
  412. private Hashtable GetEvents(UUID requestID, UUID sessionID, string request)
  413. {
  414. Hashtable response;
  415. lock (m_UrlMap)
  416. {
  417. UrlData url = null;
  418. RequestData requestData = null;
  419. if (!m_RequestMap.ContainsKey(requestID))
  420. return NoEvents(requestID, sessionID);
  421. url = m_RequestMap[requestID];
  422. requestData = url.requests[requestID];
  423. if (!requestData.requestDone)
  424. return NoEvents(requestID, sessionID);
  425. response = new Hashtable();
  426. if (System.Environment.TickCount - requestData.startTime > 25000)
  427. {
  428. response["int_response_code"] = 500;
  429. response["str_response_string"] = "Script timeout";
  430. response["content_type"] = "text/plain";
  431. response["keepalive"] = false;
  432. response["reusecontext"] = false;
  433. return response;
  434. }
  435. //put response
  436. response["int_response_code"] = requestData.responseCode;
  437. response["str_response_string"] = requestData.responseBody;
  438. response["content_type"] = requestData.responseType;
  439. // response["content_type"] = "text/plain";
  440. response["keepalive"] = false;
  441. response["reusecontext"] = false;
  442. //remove from map
  443. url.requests.Remove(requestID);
  444. m_RequestMap.Remove(requestID);
  445. }
  446. return response;
  447. }
  448. public void HttpRequestHandler(UUID requestID, Hashtable request)
  449. {
  450. string uri = request["uri"].ToString();
  451. bool is_ssl = uri.Contains("lslhttps");
  452. try
  453. {
  454. Hashtable headers = (Hashtable)request["headers"];
  455. // string uri_full = "http://" + m_ExternalHostNameForLSL + ":" + m_HttpServer.Port.ToString() + uri;// "/lslhttp/" + urlcode.ToString() + "/";
  456. int pos1 = uri.IndexOf("/");// /lslhttp
  457. int pos2 = uri.IndexOf("/", pos1 + 1);// /lslhttp/
  458. int pos3 = uri.IndexOf("/", pos2 + 1);// /lslhttp/<UUID>/
  459. string uri_tmp = uri.Substring(0, pos3 + 1);
  460. //HTTP server code doesn't provide us with QueryStrings
  461. string pathInfo;
  462. string queryString;
  463. queryString = "";
  464. pathInfo = uri.Substring(pos3);
  465. UrlData urlData = null;
  466. lock (m_UrlMap)
  467. {
  468. string url;
  469. if (is_ssl)
  470. url = "https://" + m_ExternalHostNameForLSL + ":" + m_HttpsServer.Port.ToString() + uri_tmp;
  471. else
  472. url = "http://" + m_ExternalHostNameForLSL + ":" + m_HttpServer.Port.ToString() + uri_tmp;
  473. // Avoid a race - the request URL may have been released via llRequestUrl() whilst this
  474. // request was being processed.
  475. if (!m_UrlMap.TryGetValue(url, out urlData))
  476. return;
  477. //for llGetHttpHeader support we need to store original URI here
  478. //to make x-path-info / x-query-string / x-script-url / x-remote-ip headers
  479. //as per http://wiki.secondlife.com/wiki/LlGetHTTPHeader
  480. RequestData requestData = new RequestData();
  481. requestData.requestID = requestID;
  482. requestData.requestDone = false;
  483. requestData.startTime = System.Environment.TickCount;
  484. requestData.uri = uri;
  485. if (requestData.headers == null)
  486. requestData.headers = new Dictionary<string, string>();
  487. foreach (DictionaryEntry header in headers)
  488. {
  489. string key = (string)header.Key;
  490. string value = (string)header.Value;
  491. requestData.headers.Add(key, value);
  492. }
  493. foreach (DictionaryEntry de in request)
  494. {
  495. if (de.Key.ToString() == "querystringkeys")
  496. {
  497. System.String[] keys = (System.String[])de.Value;
  498. foreach (String key in keys)
  499. {
  500. if (request.ContainsKey(key))
  501. {
  502. string val = (String)request[key];
  503. queryString = queryString + key + "=" + val + "&";
  504. }
  505. }
  506. if (queryString.Length > 1)
  507. queryString = queryString.Substring(0, queryString.Length - 1);
  508. }
  509. }
  510. //if this machine is behind DNAT/port forwarding, currently this is being
  511. //set to address of port forwarding router
  512. requestData.headers["x-remote-ip"] = requestData.headers["remote_addr"];
  513. requestData.headers["x-path-info"] = pathInfo;
  514. requestData.headers["x-query-string"] = queryString;
  515. requestData.headers["x-script-url"] = urlData.url;
  516. urlData.requests.Add(requestID, requestData);
  517. m_RequestMap.Add(requestID, urlData);
  518. }
  519. urlData.engine.PostScriptEvent(
  520. urlData.itemID,
  521. "http_request",
  522. new Object[] { requestID.ToString(), request["http-method"].ToString(), request["body"].ToString() });
  523. }
  524. catch (Exception we)
  525. {
  526. //Hashtable response = new Hashtable();
  527. m_log.Warn("[HttpRequestHandler]: http-in request failed");
  528. m_log.Warn(we.Message);
  529. m_log.Warn(we.StackTrace);
  530. }
  531. }
  532. private void OnScriptReset(uint localID, UUID itemID)
  533. {
  534. ScriptRemoved(itemID);
  535. }
  536. }
  537. }