UrlModule.cs 29 KB

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