UrlModule.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Threading;
  29. using System.Collections.Generic;
  30. using System.Collections;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
  40. {
  41. public class UrlData
  42. {
  43. public UUID hostID;
  44. public UUID itemID;
  45. public IScriptModule engine;
  46. public string url;
  47. public UUID urlcode;
  48. public Dictionary<UUID, RequestData> requests;
  49. }
  50. public class RequestData
  51. {
  52. public UUID requestID;
  53. public Dictionary<string, string> headers;
  54. public string body;
  55. public ManualResetEvent ev;
  56. }
  57. public class UrlModule : ISharedRegionModule, IUrlModule
  58. {
  59. // private static readonly ILog m_log =
  60. // LogManager.GetLogger(
  61. // MethodBase.GetCurrentMethod().DeclaringType);
  62. private Dictionary<UUID, UrlData> m_RequestMap =
  63. new Dictionary<UUID, UrlData>();
  64. private Dictionary<string, UrlData> m_UrlMap =
  65. new Dictionary<string, UrlData>();
  66. private int m_TotalUrls = 100;
  67. private IHttpServer m_HttpServer = null;
  68. public Type ReplaceableInterface
  69. {
  70. get { return null; }
  71. }
  72. public string Name
  73. {
  74. get { return "UrlModule"; }
  75. }
  76. public void Initialise(IConfigSource config)
  77. {
  78. }
  79. public void PostInitialise()
  80. {
  81. }
  82. public void AddRegion(Scene scene)
  83. {
  84. if (m_HttpServer == null)
  85. {
  86. // There can only be one
  87. //
  88. m_HttpServer = MainServer.Instance;
  89. }
  90. scene.RegisterModuleInterface<IUrlModule>(this);
  91. scene.EventManager.OnScriptReset += OnScriptReset;
  92. }
  93. public void RegionLoaded(Scene scene)
  94. {
  95. }
  96. public void RemoveRegion(Scene scene)
  97. {
  98. }
  99. public void Close()
  100. {
  101. }
  102. public UUID RequestURL(IScriptModule engine, SceneObjectPart host, UUID itemID)
  103. {
  104. UUID urlcode = UUID.Random();
  105. lock (m_UrlMap)
  106. {
  107. if (m_UrlMap.Count >= m_TotalUrls)
  108. {
  109. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" });
  110. return urlcode;
  111. }
  112. string url = "http://"+System.Environment.MachineName+":"+m_HttpServer.Port.ToString()+"/lslhttp/"+urlcode.ToString()+"/";
  113. UrlData urlData = new UrlData();
  114. urlData.hostID = host.UUID;
  115. urlData.itemID = itemID;
  116. urlData.engine = engine;
  117. urlData.url = url;
  118. urlData.urlcode = urlcode;
  119. urlData.requests = new Dictionary<UUID, RequestData>();
  120. m_UrlMap[url] = urlData;
  121. m_HttpServer.AddHTTPHandler("/lslhttp/"+urlcode.ToString()+"/", HttpRequestHandler);
  122. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_GRANTED", url });
  123. }
  124. return urlcode;
  125. }
  126. public UUID RequestSecureURL(IScriptModule engine, SceneObjectPart host, UUID itemID)
  127. {
  128. UUID urlcode = UUID.Random();
  129. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" });
  130. return urlcode;
  131. }
  132. public void ReleaseURL(string url)
  133. {
  134. lock (m_UrlMap)
  135. {
  136. UrlData data;
  137. if (!m_UrlMap.TryGetValue(url, out data))
  138. return;
  139. foreach (UUID req in data.requests.Keys)
  140. m_RequestMap.Remove(req);
  141. RemoveUrl(data);
  142. m_UrlMap.Remove(url);
  143. }
  144. }
  145. public void HttpResponse(UUID request, int status, string body)
  146. {
  147. }
  148. public string GetHttpHeader(UUID request, string header)
  149. {
  150. return String.Empty;
  151. }
  152. public int GetFreeUrls()
  153. {
  154. return m_TotalUrls - m_UrlMap.Count;
  155. }
  156. public void ScriptRemoved(UUID itemID)
  157. {
  158. lock (m_UrlMap)
  159. {
  160. List<string> removeURLs = new List<string>();
  161. foreach (KeyValuePair<string, UrlData> url in m_UrlMap)
  162. {
  163. if (url.Value.itemID == itemID)
  164. {
  165. RemoveUrl(url.Value);
  166. removeURLs.Add(url.Key);
  167. foreach (UUID req in url.Value.requests.Keys)
  168. m_RequestMap.Remove(req);
  169. }
  170. }
  171. foreach (string urlname in removeURLs)
  172. m_UrlMap.Remove(urlname);
  173. }
  174. }
  175. public void ObjectRemoved(UUID objectID)
  176. {
  177. lock (m_UrlMap)
  178. {
  179. List<string> removeURLs = new List<string>();
  180. foreach (KeyValuePair<string, UrlData> url in m_UrlMap)
  181. {
  182. if (url.Value.hostID == objectID)
  183. {
  184. RemoveUrl(url.Value);
  185. removeURLs.Add(url.Key);
  186. foreach (UUID req in url.Value.requests.Keys)
  187. m_RequestMap.Remove(req);
  188. }
  189. }
  190. foreach (string urlname in removeURLs)
  191. m_UrlMap.Remove(urlname);
  192. }
  193. }
  194. private void RemoveUrl(UrlData data)
  195. {
  196. m_HttpServer.RemoveHTTPHandler("", "/lslhttp/"+data.urlcode.ToString()+"/");
  197. }
  198. private Hashtable HttpRequestHandler(Hashtable request)
  199. {
  200. string uri = request["uri"].ToString();
  201. //A solution to this ugly mess would be to use only the /lslhttp/<UUID>/ part of the URI as the key.
  202. UrlData url = m_UrlMap["http://"+System.Environment.MachineName+":"+m_HttpServer.Port.ToString()+uri];
  203. //UUID.Random() below is a hack! Eventually we will do HTTP requests and responses properly.
  204. url.engine.PostScriptEvent(url.itemID, "http_request", new Object[] { UUID.Random().ToString(), request["http-method"].ToString(), request["body"].ToString() });
  205. Hashtable response = new Hashtable();
  206. response["int_response_code"] = 200;
  207. response["str_response_string"] = "This is a generic response as OpenSim does not yet support proper responses. Your request has been passed to the object.";
  208. return response;
  209. }
  210. private void OnScriptReset(uint localID, UUID itemID)
  211. {
  212. ScriptRemoved(itemID);
  213. }
  214. }
  215. }