UrlModule.cs 28 KB

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