UrlModule.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Text;
  34. using System.Net;
  35. using System.Net.Sockets;
  36. using log4net;
  37. using Mono.Addins;
  38. using Nini.Config;
  39. using OpenMetaverse;
  40. using OpenSim.Framework;
  41. using OpenSim.Framework.Servers;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenSim.Region.Framework.Interfaces;
  44. using OpenSim.Region.Framework.Scenes;
  45. namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
  46. {
  47. public class UrlData
  48. {
  49. public UUID hostID;
  50. public UUID groupID;
  51. public UUID itemID;
  52. public IScriptModule engine;
  53. public string url;
  54. public UUID urlcode;
  55. public Dictionary<UUID, RequestData> requests;
  56. public bool isSsl;
  57. public Scene scene;
  58. public bool allowXss;
  59. }
  60. public class RequestData
  61. {
  62. public UUID requestID;
  63. public Dictionary<string, string> headers;
  64. public string body;
  65. public int responseCode;
  66. public string responseBody;
  67. public string responseType = "text/plain";
  68. //public ManualResetEvent ev;
  69. public bool requestDone;
  70. public int startTime;
  71. public bool responseSent;
  72. public string uri;
  73. public UUID hostID;
  74. public Scene scene;
  75. }
  76. /// <summary>
  77. /// This module provides external URLs for in-world scripts.
  78. /// </summary>
  79. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UrlModule")]
  80. public class UrlModule : ISharedRegionModule, IUrlModule
  81. {
  82. private static readonly ILog m_log =
  83. LogManager.GetLogger(
  84. MethodBase.GetCurrentMethod().DeclaringType);
  85. protected readonly Dictionary<UUID, UrlData> m_RequestMap = new Dictionary<UUID, UrlData>();
  86. protected readonly Dictionary<string, UrlData> m_UrlMap = new Dictionary<string, UrlData>();
  87. protected readonly Dictionary<UUID, int> m_countsPerSOG = new Dictionary<UUID, int>();
  88. protected bool m_enabled = false;
  89. protected string m_ErrorStr;
  90. protected uint m_HttpsPort = 0;
  91. protected IHttpServer m_HttpServer = null;
  92. protected IHttpServer m_HttpsServer = null;
  93. public string ExternalHostNameForLSL { get; protected set; }
  94. /// <summary>
  95. /// The default maximum number of urls
  96. /// </summary>
  97. public const int DefaultTotalUrls = 15000;
  98. /// <summary>
  99. /// Maximum number of external urls that can be set up by this module.
  100. /// </summary>
  101. public int TotalUrls { get; set; }
  102. public Type ReplaceableInterface
  103. {
  104. get { return typeof(IUrlModule); }
  105. }
  106. public string Name
  107. {
  108. get { return "UrlModule"; }
  109. }
  110. public void Initialise(IConfigSource config)
  111. {
  112. IConfig networkConfig = config.Configs["Network"];
  113. m_enabled = false;
  114. if (networkConfig != null)
  115. {
  116. ExternalHostNameForLSL = config.Configs["Network"].GetString("ExternalHostNameForLSL", null);
  117. bool ssl_enabled = config.Configs["Network"].GetBoolean("https_listener", false);
  118. if (ssl_enabled)
  119. m_HttpsPort = (uint)config.Configs["Network"].GetInt("https_port", (int)m_HttpsPort);
  120. }
  121. else
  122. {
  123. m_ErrorStr = "[Network] configuration missing, HTTP listener for LSL disabled";
  124. m_log.Warn("[URL MODULE]: " + m_ErrorStr);
  125. return;
  126. }
  127. if (String.IsNullOrWhiteSpace(ExternalHostNameForLSL))
  128. {
  129. m_ErrorStr = "ExternalHostNameForLSL not defined in configuration, HTTP listener for LSL disabled";
  130. m_log.Warn("[URL MODULE]: " + m_ErrorStr);
  131. return;
  132. }
  133. IPAddress ia = null;
  134. ia = Util.GetHostFromDNS(ExternalHostNameForLSL);
  135. if (ia == null)
  136. {
  137. m_ErrorStr = "Could not resolve ExternalHostNameForLSL, HTTP listener for LSL disabled";
  138. m_log.Warn("[URL MODULE]: " + m_ErrorStr);
  139. return;
  140. }
  141. m_enabled = true;
  142. m_ErrorStr = String.Empty;
  143. IConfig llFunctionsConfig = config.Configs["LL-Functions"];
  144. if (llFunctionsConfig != null)
  145. TotalUrls = llFunctionsConfig.GetInt("max_external_urls_per_simulator", DefaultTotalUrls);
  146. else
  147. TotalUrls = DefaultTotalUrls;
  148. }
  149. public void PostInitialise()
  150. {
  151. }
  152. public void AddRegion(Scene scene)
  153. {
  154. if (m_enabled && m_HttpServer == null)
  155. {
  156. // There can only be one
  157. //
  158. m_HttpServer = MainServer.Instance;
  159. //
  160. // We can use the https if it is enabled
  161. if (m_HttpsPort > 0)
  162. {
  163. m_HttpsServer = MainServer.GetHttpServer(m_HttpsPort);
  164. }
  165. }
  166. scene.RegisterModuleInterface<IUrlModule>(this);
  167. scene.EventManager.OnScriptReset += OnScriptReset;
  168. }
  169. public void RegionLoaded(Scene scene)
  170. {
  171. IScriptModule[] scriptModules = scene.RequestModuleInterfaces<IScriptModule>();
  172. foreach (IScriptModule scriptModule in scriptModules)
  173. {
  174. scriptModule.OnScriptRemoved += ScriptRemoved;
  175. scriptModule.OnObjectRemoved += ObjectRemoved;
  176. }
  177. }
  178. public void RemoveRegion(Scene scene)
  179. {
  180. // Drop references to that scene
  181. foreach (KeyValuePair<string, UrlData> kvp in m_UrlMap)
  182. {
  183. if (kvp.Value.scene == scene)
  184. kvp.Value.scene = null;
  185. }
  186. foreach (KeyValuePair<UUID, UrlData> kvp in m_RequestMap)
  187. {
  188. if (kvp.Value.scene == scene)
  189. kvp.Value.scene = null;
  190. }
  191. }
  192. public void Close()
  193. {
  194. }
  195. public UUID RequestURL(IScriptModule engine, SceneObjectPart host, UUID itemID, Hashtable options)
  196. {
  197. UUID urlcode = UUID.Random();
  198. if(!m_enabled)
  199. {
  200. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", m_ErrorStr });
  201. return urlcode;
  202. }
  203. lock (m_UrlMap)
  204. {
  205. if (m_UrlMap.Count >= TotalUrls)
  206. {
  207. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED",
  208. "Too many URLs already open" });
  209. return urlcode;
  210. }
  211. string url = "http://" + ExternalHostNameForLSL + ":" + m_HttpServer.Port.ToString() + "/lslhttp/" + urlcode.ToString();
  212. UUID groupID = host.ParentGroup.UUID;
  213. UrlData urlData = new UrlData();
  214. urlData.hostID = host.UUID;
  215. urlData.groupID = groupID;
  216. urlData.itemID = itemID;
  217. urlData.engine = engine;
  218. urlData.url = url;
  219. urlData.urlcode = urlcode;
  220. urlData.isSsl = false;
  221. urlData.requests = new Dictionary<UUID, RequestData>();
  222. urlData.scene = host.ParentGroup.Scene;
  223. urlData.allowXss = false;
  224. if (options != null && options["allowXss"] != null)
  225. urlData.allowXss = true;
  226. m_UrlMap[url] = urlData;
  227. if (m_countsPerSOG.TryGetValue(groupID, out int urlcount))
  228. m_countsPerSOG[groupID] = ++urlcount;
  229. else
  230. m_countsPerSOG[groupID] = 1;
  231. string uri = "/lslhttp/" + urlcode.ToString();
  232. PollServiceEventArgs args
  233. = new PollServiceEventArgs(HttpRequestHandler, uri, HasEvents, GetEvents, NoEvents, Drop, urlcode, 25000);
  234. m_HttpServer.AddPollServiceHTTPHandlerVarPath(args);
  235. //m_log.DebugFormat(
  236. // "[URL MODULE]: Set up incoming request url {0} for {1} in {2} {3}",
  237. // uri, itemID, host.Name, host.LocalId);
  238. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_GRANTED", url + "/"});
  239. }
  240. return urlcode;
  241. }
  242. public UUID RequestSecureURL(IScriptModule engine, SceneObjectPart host, UUID itemID, Hashtable options)
  243. {
  244. UUID urlcode = UUID.Random();
  245. if(!m_enabled)
  246. {
  247. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", m_ErrorStr });
  248. return urlcode;
  249. }
  250. if (m_HttpsServer == null)
  251. {
  252. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" });
  253. return urlcode;
  254. }
  255. lock (m_UrlMap)
  256. {
  257. if (m_UrlMap.Count >= TotalUrls)
  258. {
  259. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED",
  260. "Too many URLs already open" });
  261. return urlcode;
  262. }
  263. string url = "https://" + ExternalHostNameForLSL + ":" + m_HttpsServer.Port.ToString() + "/lslhttps/" + urlcode.ToString();
  264. UUID groupID = host.ParentGroup.UUID;
  265. UrlData urlData = new UrlData();
  266. urlData.hostID = host.UUID;
  267. urlData.groupID = groupID;
  268. urlData.itemID = itemID;
  269. urlData.engine = engine;
  270. urlData.url = url;
  271. urlData.urlcode = urlcode;
  272. urlData.isSsl = true;
  273. urlData.requests = new Dictionary<UUID, RequestData>();
  274. urlData.allowXss = false;
  275. if (options != null && options["allowXss"] != null)
  276. urlData.allowXss = true;
  277. m_UrlMap[url] = urlData;
  278. if (m_countsPerSOG.TryGetValue(groupID, out int urlcount))
  279. m_countsPerSOG[groupID] = ++urlcount;
  280. else
  281. m_countsPerSOG[groupID] = 1;
  282. string uri = "/lslhttps/" + urlcode.ToString();
  283. PollServiceEventArgs args
  284. = new PollServiceEventArgs(HttpRequestHandler, uri, HasEvents, GetEvents, NoEvents, Drop, urlcode, 25000);
  285. m_HttpsServer.AddPollServiceHTTPHandlerVarPath(args);
  286. //m_log.DebugFormat(
  287. // "[URL MODULE]: Set up incoming secure request url {0} for {1} in {2} {3}",
  288. // uri, itemID, host.Name, host.LocalId);
  289. // keep ending / because legacy
  290. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_GRANTED", url + "/"});
  291. }
  292. return urlcode;
  293. }
  294. public void ReleaseURL(string url)
  295. {
  296. lock (m_UrlMap)
  297. {
  298. UrlData data;
  299. url = url.TrimEnd(new char[]{'/'});
  300. if (!m_UrlMap.TryGetValue(url, out data))
  301. {
  302. return;
  303. }
  304. lock (m_RequestMap)
  305. {
  306. foreach (UUID req in data.requests.Keys)
  307. m_RequestMap.Remove(req);
  308. }
  309. // m_log.DebugFormat(
  310. // "[URL MODULE]: Releasing url {0} for {1} in {2}",
  311. // url, data.itemID, data.hostID);
  312. RemoveUrl(data);
  313. m_UrlMap.Remove(url);
  314. }
  315. }
  316. public void HttpContentType(UUID request, string type)
  317. {
  318. lock (m_UrlMap)
  319. {
  320. if (m_RequestMap.ContainsKey(request))
  321. {
  322. UrlData urlData = m_RequestMap[request];
  323. urlData.requests[request].responseType = type;
  324. }
  325. else
  326. {
  327. m_log.Info("[HttpRequestHandler] There is no http-in request with id " + request.ToString());
  328. }
  329. }
  330. }
  331. public void HttpResponse(UUID request, int status, string body)
  332. {
  333. lock (m_RequestMap)
  334. {
  335. if (m_RequestMap.ContainsKey(request))
  336. {
  337. UrlData urlData = m_RequestMap[request];
  338. if (!urlData.requests[request].responseSent)
  339. {
  340. string responseBody = body;
  341. if (urlData.requests[request].responseType.Equals("text/plain"))
  342. {
  343. string value;
  344. if (urlData.requests[request].headers.TryGetValue("user-agent", out value))
  345. {
  346. if (value != null && value.IndexOf("MSIE") >= 0)
  347. {
  348. // wrap the html escaped response if the target client is IE
  349. // It ignores "text/plain" if the body is html
  350. responseBody = "<html>" + System.Web.HttpUtility.HtmlEncode(body) + "</html>";
  351. }
  352. }
  353. }
  354. urlData.requests[request].responseCode = status;
  355. urlData.requests[request].responseBody = responseBody;
  356. //urlData.requests[request].ev.Set();
  357. urlData.requests[request].requestDone = true;
  358. urlData.requests[request].responseSent = true;
  359. }
  360. }
  361. else
  362. {
  363. m_log.Info("[HttpRequestHandler] There is no http-in request with id " + request.ToString());
  364. }
  365. }
  366. }
  367. public string GetHttpHeader(UUID requestId, string header)
  368. {
  369. lock (m_RequestMap)
  370. {
  371. if (m_RequestMap.ContainsKey(requestId))
  372. {
  373. UrlData urlData = m_RequestMap[requestId];
  374. string value;
  375. if (urlData.requests[requestId].headers.TryGetValue(header, out value))
  376. return value;
  377. }
  378. else
  379. {
  380. m_log.Warn("[HttpRequestHandler] There was no http-in request with id " + requestId);
  381. }
  382. }
  383. return String.Empty;
  384. }
  385. public int GetFreeUrls()
  386. {
  387. lock (m_UrlMap)
  388. return TotalUrls - m_UrlMap.Count;
  389. }
  390. public void ScriptRemoved(UUID itemID)
  391. {
  392. // m_log.DebugFormat("[URL MODULE]: Removing script {0}", itemID);
  393. lock (m_UrlMap)
  394. {
  395. List<string> removeURLs = new List<string>();
  396. foreach (KeyValuePair<string, UrlData> url in m_UrlMap)
  397. {
  398. if (url.Value.itemID == itemID)
  399. {
  400. RemoveUrl(url.Value);
  401. removeURLs.Add(url.Key);
  402. lock (m_RequestMap)
  403. {
  404. foreach (UUID req in url.Value.requests.Keys)
  405. m_RequestMap.Remove(req);
  406. }
  407. }
  408. }
  409. foreach (string urlname in removeURLs)
  410. m_UrlMap.Remove(urlname);
  411. }
  412. }
  413. public void ObjectRemoved(UUID objectID)
  414. {
  415. lock (m_UrlMap)
  416. {
  417. List<string> removeURLs = new List<string>();
  418. foreach (KeyValuePair<string, UrlData> url in m_UrlMap)
  419. {
  420. if (url.Value.hostID == objectID)
  421. {
  422. RemoveUrl(url.Value);
  423. removeURLs.Add(url.Key);
  424. lock (m_RequestMap)
  425. {
  426. foreach (UUID req in url.Value.requests.Keys)
  427. m_RequestMap.Remove(req);
  428. }
  429. }
  430. }
  431. foreach (string urlname in removeURLs)
  432. m_UrlMap.Remove(urlname);
  433. }
  434. }
  435. protected void RemoveUrl(UrlData data)
  436. {
  437. if (data.isSsl)
  438. m_HttpsServer.RemovePollServiceHTTPHandler("", "/lslhttps/"+data.urlcode.ToString());
  439. else
  440. m_HttpServer.RemovePollServiceHTTPHandler("", "/lslhttp/"+data.urlcode.ToString());
  441. if(m_countsPerSOG.TryGetValue(data.groupID, out int count))
  442. {
  443. --count;
  444. if(count <= 0)
  445. m_countsPerSOG.Remove(data.groupID);
  446. else
  447. m_countsPerSOG[data.groupID] = count;
  448. }
  449. }
  450. protected Hashtable NoEvents(UUID requestID, UUID sessionID)
  451. {
  452. Hashtable response = new Hashtable();
  453. UrlData url;
  454. int startTime = 0;
  455. lock (m_RequestMap)
  456. {
  457. if (!m_RequestMap.ContainsKey(requestID))
  458. return response;
  459. url = m_RequestMap[requestID];
  460. startTime = url.requests[requestID].startTime;
  461. }
  462. if (System.Environment.TickCount - startTime > 25000)
  463. {
  464. response["int_response_code"] = 500;
  465. response["str_response_string"] = "Script timeout";
  466. response["content_type"] = "text/plain";
  467. response["keepalive"] = false;
  468. //remove from map
  469. lock (url.requests)
  470. {
  471. url.requests.Remove(requestID);
  472. }
  473. lock (m_RequestMap)
  474. {
  475. m_RequestMap.Remove(requestID);
  476. }
  477. return response;
  478. }
  479. return response;
  480. }
  481. protected bool HasEvents(UUID requestID, UUID sessionID)
  482. {
  483. UrlData url=null;
  484. lock (m_RequestMap)
  485. {
  486. if (!m_RequestMap.ContainsKey(requestID))
  487. {
  488. return false;
  489. }
  490. url = m_RequestMap[requestID];
  491. }
  492. lock (url.requests)
  493. {
  494. if (!url.requests.ContainsKey(requestID))
  495. {
  496. return false;
  497. }
  498. else
  499. {
  500. if (System.Environment.TickCount - url.requests[requestID].startTime > 25000)
  501. {
  502. return true;
  503. }
  504. if (url.requests[requestID].requestDone)
  505. return true;
  506. else
  507. return false;
  508. }
  509. }
  510. }
  511. protected void Drop(UUID requestID, UUID sessionID)
  512. {
  513. UrlData url = null;
  514. lock (m_RequestMap)
  515. {
  516. if (m_RequestMap.ContainsKey(requestID))
  517. {
  518. url = m_RequestMap[requestID];
  519. m_RequestMap.Remove(requestID);
  520. if(url != null)
  521. {
  522. lock (url.requests)
  523. {
  524. if(url.requests.ContainsKey(requestID))
  525. url.requests.Remove(requestID);
  526. }
  527. }
  528. }
  529. }
  530. }
  531. protected Hashtable GetEvents(UUID requestID, UUID sessionID)
  532. {
  533. UrlData url = null;
  534. RequestData requestData = null;
  535. lock (m_RequestMap)
  536. {
  537. if (!m_RequestMap.ContainsKey(requestID))
  538. return NoEvents(requestID,sessionID);
  539. url = m_RequestMap[requestID];
  540. }
  541. lock (url.requests)
  542. {
  543. requestData = url.requests[requestID];
  544. }
  545. if (!requestData.requestDone)
  546. return NoEvents(requestID,sessionID);
  547. Hashtable response = new Hashtable();
  548. if (System.Environment.TickCount - requestData.startTime > 25000)
  549. {
  550. response["int_response_code"] = 500;
  551. response["str_response_string"] = "Script timeout";
  552. response["content_type"] = "text/plain";
  553. response["keepalive"] = false;
  554. return response;
  555. }
  556. //put response
  557. response["int_response_code"] = requestData.responseCode;
  558. response["str_response_string"] = requestData.responseBody;
  559. response["content_type"] = requestData.responseType;
  560. response["keepalive"] = false;
  561. if (url.allowXss)
  562. response["access_control_allow_origin"] = "*";
  563. //remove from map
  564. lock (url.requests)
  565. {
  566. url.requests.Remove(requestID);
  567. }
  568. lock (m_RequestMap)
  569. {
  570. m_RequestMap.Remove(requestID);
  571. }
  572. return response;
  573. }
  574. public void HttpRequestHandler(UUID requestID, OSHttpRequest request)
  575. {
  576. lock (request)
  577. {
  578. string uri = request.RawUrl;
  579. if(uri.Length < 45)
  580. {
  581. request.InputStream.Dispose();
  582. return;
  583. }
  584. try
  585. {
  586. //string uri_full = "http://" + ExternalHostNameForLSL + ":" + m_HttpServer.Port.ToString() + uri;// "/lslhttp/" + urlcode.ToString() + "/";
  587. string uri_tmp;
  588. string pathInfo;
  589. int pos = uri.IndexOf("/", 45); // /lslhttp/uuid/ <-
  590. if (pos >= 45)
  591. {
  592. uri_tmp = uri.Substring(0, pos);
  593. pathInfo = uri.Substring(pos);
  594. }
  595. else
  596. {
  597. uri_tmp = uri;
  598. pathInfo = string.Empty;
  599. }
  600. string urlkey;
  601. if (uri.Contains("lslhttps"))
  602. urlkey = "https://" + ExternalHostNameForLSL + ":" + m_HttpsServer.Port.ToString() + uri_tmp;
  603. //m_UrlMap[];
  604. else
  605. urlkey = "http://" + ExternalHostNameForLSL + ":" + m_HttpServer.Port.ToString() + uri_tmp;
  606. if (!m_UrlMap.TryGetValue(urlkey, out UrlData url))
  607. {
  608. //m_log.Warn("[HttpRequestHandler]: http-in request failed; no such url: "+urlkey.ToString());
  609. request.InputStream.Dispose();
  610. return;
  611. }
  612. //for llGetHttpHeader support we need to store original URI here
  613. //to make x-path-info / x-query-string / x-script-url / x-remote-ip headers
  614. //as per http://wiki.secondlife.com/wiki/LlGetHTTPHeader
  615. RequestData requestData = new RequestData();
  616. requestData.requestID = requestID;
  617. requestData.requestDone = false;
  618. requestData.startTime = System.Environment.TickCount;
  619. requestData.uri = uri;
  620. requestData.hostID = url.hostID;
  621. requestData.scene = url.scene;
  622. if (requestData.headers == null)
  623. requestData.headers = new Dictionary<string, string>();
  624. NameValueCollection headers = request.Headers;
  625. if (headers.Count > 0)
  626. {
  627. for(int i = 0; i < headers.Count; ++i)
  628. {
  629. string name = headers.GetKey(i);
  630. if (!string.IsNullOrEmpty(name))
  631. requestData.headers[name] = headers[i];
  632. }
  633. }
  634. NameValueCollection query = request.QueryString;
  635. if (query.Count > 0)
  636. {
  637. StringBuilder sb = new StringBuilder();
  638. for (int i = 0; i < query.Count; ++i)
  639. {
  640. string key = query.GetKey(i);
  641. if (string.IsNullOrEmpty(key))
  642. sb.AppendFormat("{0}&", query[i]);
  643. else
  644. sb.AppendFormat("{0}={1}&", key, query[i]);
  645. }
  646. if (sb.Length > 1)
  647. sb.Remove(sb.Length - 1, 1);
  648. requestData.headers["x-query-string"] = sb.ToString();
  649. }
  650. else
  651. requestData.headers["x-query-string"] = String.Empty;
  652. //if this machine is behind DNAT/port forwarding, currently this is being
  653. //set to address of port forwarding router
  654. requestData.headers["x-remote-ip"] = request.RemoteIPEndPoint.Address.ToString();
  655. requestData.headers["x-path-info"] = pathInfo;
  656. requestData.headers["x-script-url"] = url.url;
  657. //requestData.ev = new ManualResetEvent(false);
  658. lock (url.requests)
  659. {
  660. url.requests.Add(requestID, requestData);
  661. }
  662. lock (m_RequestMap)
  663. {
  664. //add to request map
  665. m_RequestMap.Add(requestID, url);
  666. }
  667. string requestBody;
  668. if (request.InputStream.Length > 0)
  669. {
  670. using (StreamReader reader = new StreamReader(request.InputStream, Encoding.UTF8))
  671. requestBody = reader.ReadToEnd();
  672. }
  673. else
  674. requestBody = string.Empty;
  675. request.InputStream.Dispose();
  676. url.engine.PostScriptEvent(url.itemID, "http_request", new Object[] { requestID.ToString(), request.HttpMethod, requestBody });
  677. return;
  678. }
  679. catch (Exception we)
  680. {
  681. //Hashtable response = new Hashtable();
  682. m_log.Warn("[HttpRequestHandler]: http-in request failed");
  683. m_log.Warn(we.Message);
  684. m_log.Warn(we.StackTrace);
  685. }
  686. }
  687. }
  688. protected void OnScriptReset(uint localID, UUID itemID)
  689. {
  690. ScriptRemoved(itemID);
  691. }
  692. public int GetUrlCount(UUID groupID)
  693. {
  694. if (!m_enabled)
  695. return 0;
  696. lock (m_UrlMap)
  697. {
  698. m_countsPerSOG.TryGetValue(groupID, out int count);
  699. return count;
  700. }
  701. }
  702. }
  703. }