UrlModule.cs 31 KB

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