UrlModule.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 string Name
  69. {
  70. get { return "UrlModule"; }
  71. }
  72. public void Initialise(IConfigSource config)
  73. {
  74. }
  75. public void PostInitialise()
  76. {
  77. }
  78. public void AddRegion(Scene scene)
  79. {
  80. if (m_HttpServer == null)
  81. {
  82. // There can only be one
  83. //
  84. m_HttpServer = scene.CommsManager.HttpServer;
  85. }
  86. scene.RegisterModuleInterface<IUrlModule>(this);
  87. scene.EventManager.OnScriptReset += OnScriptReset;
  88. }
  89. public void RegionLoaded(Scene scene)
  90. {
  91. }
  92. public void RemoveRegion(Scene scene)
  93. {
  94. }
  95. public void Close()
  96. {
  97. }
  98. public UUID RequestURL(IScriptModule engine, SceneObjectPart host, UUID itemID)
  99. {
  100. UUID urlcode = UUID.Random();
  101. lock (m_UrlMap)
  102. {
  103. if (m_UrlMap.Count >= m_TotalUrls)
  104. {
  105. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" });
  106. return urlcode;
  107. }
  108. string url = "http://"+System.Environment.MachineName+":"+m_HttpServer.Port.ToString()+"/lslhttp/"+urlcode.ToString()+"/";
  109. UrlData urlData = new UrlData();
  110. urlData.hostID = host.UUID;
  111. urlData.itemID = itemID;
  112. urlData.engine = engine;
  113. urlData.url = url;
  114. urlData.urlcode = urlcode;
  115. urlData.requests = new Dictionary<UUID, RequestData>();
  116. m_UrlMap[url] = urlData;
  117. m_HttpServer.AddHTTPHandler("/lslhttp/"+urlcode.ToString()+"/", HttpRequestHandler);
  118. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_GRANTED", url });
  119. }
  120. return urlcode;
  121. }
  122. public UUID RequestSecureURL(IScriptModule engine, SceneObjectPart host, UUID itemID)
  123. {
  124. UUID urlcode = UUID.Random();
  125. engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" });
  126. return urlcode;
  127. }
  128. public void ReleaseURL(string url)
  129. {
  130. lock (m_UrlMap)
  131. {
  132. UrlData data;
  133. if (!m_UrlMap.TryGetValue(url, out data))
  134. return;
  135. foreach (UUID req in data.requests.Keys)
  136. m_RequestMap.Remove(req);
  137. RemoveUrl(data);
  138. m_UrlMap.Remove(url);
  139. }
  140. }
  141. public void HttpResponse(UUID request, int status, string body)
  142. {
  143. }
  144. public string GetHttpHeader(UUID request, string header)
  145. {
  146. return String.Empty;
  147. }
  148. public int GetFreeUrls()
  149. {
  150. return m_TotalUrls - m_UrlMap.Count;
  151. }
  152. public void ScriptRemoved(UUID itemID)
  153. {
  154. lock (m_UrlMap)
  155. {
  156. List<string> removeURLs = new List<string>();
  157. foreach (KeyValuePair<string, UrlData> url in m_UrlMap)
  158. {
  159. if (url.Value.itemID == itemID)
  160. {
  161. RemoveUrl(url.Value);
  162. removeURLs.Add(url.Key);
  163. foreach (UUID req in url.Value.requests.Keys)
  164. m_RequestMap.Remove(req);
  165. }
  166. }
  167. foreach (string urlname in removeURLs)
  168. m_UrlMap.Remove(urlname);
  169. }
  170. }
  171. public void ObjectRemoved(UUID objectID)
  172. {
  173. lock (m_UrlMap)
  174. {
  175. List<string> removeURLs = new List<string>();
  176. foreach (KeyValuePair<string, UrlData> url in m_UrlMap)
  177. {
  178. if (url.Value.hostID == objectID)
  179. {
  180. RemoveUrl(url.Value);
  181. removeURLs.Add(url.Key);
  182. foreach (UUID req in url.Value.requests.Keys)
  183. m_RequestMap.Remove(req);
  184. }
  185. }
  186. foreach (string urlname in removeURLs)
  187. m_UrlMap.Remove(urlname);
  188. }
  189. }
  190. private void RemoveUrl(UrlData data)
  191. {
  192. m_HttpServer.RemoveHTTPHandler("", "/lslhttp/"+data.urlcode.ToString()+"/");
  193. }
  194. private Hashtable HttpRequestHandler(Hashtable request)
  195. {
  196. foreach (KeyValuePair<string, Object> kvp in request)
  197. {
  198. m_log.DebugFormat("{0} = {1}", kvp.Key, kvp.Value.ToString());
  199. }
  200. Hashtable response = new Hashtable();
  201. response["int_response_code"] = 404;
  202. response["str_response_string"] = "Test";
  203. return response;
  204. }
  205. private void OnScriptReset(uint localID, UUID itemID)
  206. {
  207. ScriptRemoved(itemID);
  208. }
  209. }
  210. }