ScriptsHttpRequests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 OpenSim 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.IO;
  30. using System.Net;
  31. using System.Text;
  32. using System.Threading;
  33. using libsecondlife;
  34. using Nini.Config;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.Environment.Scenes;
  37. /*****************************************************
  38. *
  39. * ScriptsHttpRequests
  40. *
  41. * Implements the llHttpRequest and http_response
  42. * callback.
  43. *
  44. * Some stuff was already in LSLLongCmdHandler, and then
  45. * there was this file with a stub class in it. So,
  46. * I am moving some of the objects and functions out of
  47. * LSLLongCmdHandler, such as the HttpRequestClass, the
  48. * start and stop methods, and setting up pending and
  49. * completed queues. These are processed in the
  50. * LSLLongCmdHandler polling loop. Similiar to the
  51. * XMLRPCModule, since that seems to work.
  52. *
  53. * //TODO
  54. *
  55. * This probably needs some throttling mechanism but
  56. * its wide open right now. This applies to both
  57. * number of requests and data volume.
  58. *
  59. * Linden puts all kinds of header fields in the requests.
  60. * Not doing any of that:
  61. * User-Agent
  62. * X-SecondLife-Shard
  63. * X-SecondLife-Object-Name
  64. * X-SecondLife-Object-Key
  65. * X-SecondLife-Region
  66. * X-SecondLife-Local-Position
  67. * X-SecondLife-Local-Velocity
  68. * X-SecondLife-Local-Rotation
  69. * X-SecondLife-Owner-Name
  70. * X-SecondLife-Owner-Key
  71. *
  72. * HTTPS support
  73. *
  74. * Configurable timeout?
  75. * Configurable max repsonse size?
  76. * Configurable
  77. *
  78. * **************************************************/
  79. namespace OpenSim.Region.Environment.Modules
  80. {
  81. public class ScriptHTTPRequests : IRegionModule, IHttpRequests
  82. {
  83. private Scene m_scene;
  84. private Queue<HttpRequestClass> rpcQueue = new Queue<HttpRequestClass>();
  85. private object HttpListLock = new object();
  86. private string m_name = "HttpScriptRequests";
  87. private int httpTimeout = 30000;
  88. // <request id, HttpRequestClass>
  89. private Dictionary<LLUUID, HttpRequestClass> m_pendingRequests;
  90. public ScriptHTTPRequests()
  91. {
  92. }
  93. public void Initialise(Scene scene, IConfigSource config)
  94. {
  95. m_scene = scene;
  96. m_scene.RegisterModuleInterface<IHttpRequests>(this);
  97. m_pendingRequests = new Dictionary<LLUUID, HttpRequestClass>();
  98. }
  99. public void PostInitialise()
  100. {
  101. }
  102. public void Close()
  103. {
  104. }
  105. public string Name
  106. {
  107. get { return m_name; }
  108. }
  109. public bool IsSharedModule
  110. {
  111. get { return true; }
  112. }
  113. public LLUUID MakeHttpRequest(string url, string parameters, string body)
  114. {
  115. return LLUUID.Zero;
  116. }
  117. public LLUUID StartHttpRequest(uint localID, LLUUID itemID, string url, List<string> parameters, string body)
  118. {
  119. LLUUID reqID = LLUUID.Random();
  120. HttpRequestClass htc = new HttpRequestClass();
  121. // Partial implementation: support for parameter flags needed
  122. // see http://wiki.secondlife.com/wiki/LlHTTPRequest
  123. //
  124. // Parameters are expected in {key, value, ... , key, value}
  125. if (parameters != null)
  126. {
  127. string[] parms = parameters.ToArray();
  128. for (int i = 0; i < parms.Length/2; i += 2)
  129. {
  130. switch (Int32.Parse(parms[i]))
  131. {
  132. case HttpRequestClass.HTTP_METHOD:
  133. htc.httpMethod = parms[i + 1];
  134. break;
  135. case HttpRequestClass.HTTP_MIMETYPE:
  136. htc.httpMIMEType = parms[i + 1];
  137. break;
  138. case HttpRequestClass.HTTP_BODY_MAXLENGTH:
  139. // TODO implement me
  140. break;
  141. case HttpRequestClass.HTTP_VERIFY_CERT:
  142. // TODO implement me
  143. break;
  144. }
  145. }
  146. }
  147. htc.localID = localID;
  148. htc.itemID = itemID;
  149. htc.url = url;
  150. htc.reqID = reqID;
  151. htc.httpTimeout = httpTimeout;
  152. htc.outbound_body = body;
  153. lock (HttpListLock)
  154. {
  155. m_pendingRequests.Add(reqID, htc);
  156. }
  157. htc.process();
  158. return reqID;
  159. }
  160. public void StopHttpRequest(uint m_localID, LLUUID m_itemID)
  161. {
  162. if(m_pendingRequests != null) {
  163. lock (HttpListLock)
  164. {
  165. HttpRequestClass tmpReq;
  166. if (m_pendingRequests.TryGetValue(m_itemID, out tmpReq))
  167. {
  168. tmpReq.Stop();
  169. m_pendingRequests.Remove(m_itemID);
  170. }
  171. }
  172. }
  173. }
  174. /*
  175. * TODO
  176. * Not sure how important ordering is is here - the next first
  177. * one completed in the list is returned, based soley on its list
  178. * position, not the order in which the request was started or
  179. * finsihed. I thought about setting up a queue for this, but
  180. * it will need some refactoring and this works 'enough' right now
  181. */
  182. public HttpRequestClass GetNextCompletedRequest()
  183. {
  184. lock (HttpListLock)
  185. {
  186. foreach (LLUUID luid in m_pendingRequests.Keys)
  187. {
  188. HttpRequestClass tmpReq;
  189. if (m_pendingRequests.TryGetValue(luid, out tmpReq))
  190. {
  191. if (tmpReq.finished)
  192. {
  193. return tmpReq;
  194. }
  195. }
  196. }
  197. }
  198. return null;
  199. }
  200. public void RemoveCompletedRequest(LLUUID id)
  201. {
  202. lock (HttpListLock)
  203. {
  204. HttpRequestClass tmpReq;
  205. if (m_pendingRequests.TryGetValue(id, out tmpReq))
  206. {
  207. tmpReq.Stop();
  208. tmpReq = null;
  209. m_pendingRequests.Remove(id);
  210. }
  211. }
  212. }
  213. }
  214. //
  215. // HTTP REAQUEST
  216. // This class was originally in LSLLongCmdHandler
  217. //
  218. // TODO: setter/getter methods, maybe pass some in
  219. // constructor
  220. //
  221. public class HttpRequestClass
  222. {
  223. // Constants for parameters
  224. public const int HTTP_METHOD = 0;
  225. public const int HTTP_MIMETYPE = 1;
  226. public const int HTTP_BODY_MAXLENGTH = 2;
  227. public const int HTTP_VERIFY_CERT = 3;
  228. // Parameter members and default values
  229. public string httpMethod = "GET";
  230. public string httpMIMEType = "text/plain;charset=utf-8";
  231. public int httpBodyMaxLen = 2048; // not implemented
  232. public bool httpVerifyCert = true; // not implemented
  233. // Request info
  234. public uint localID;
  235. public LLUUID itemID;
  236. public LLUUID reqID;
  237. public int httpTimeout;
  238. public string url;
  239. public string outbound_body;
  240. public DateTime next;
  241. public int status;
  242. public bool finished;
  243. public List<string> response_metadata;
  244. public string response_body;
  245. public HttpWebRequest request;
  246. private Thread httpThread;
  247. public void process()
  248. {
  249. httpThread = new Thread(SendRequest);
  250. httpThread.Name = "HttpRequestThread";
  251. httpThread.Priority = ThreadPriority.BelowNormal;
  252. httpThread.IsBackground = true;
  253. finished = false;
  254. httpThread.Start();
  255. OpenSim.Framework.ThreadTracker.Add(httpThread);
  256. }
  257. /*
  258. * TODO: More work on the response codes. Right now
  259. * returning 200 for success or 499 for exception
  260. */
  261. public void SendRequest()
  262. {
  263. HttpWebResponse response = null;
  264. StringBuilder sb = new StringBuilder();
  265. byte[] buf = new byte[8192];
  266. string tempString = null;
  267. int count = 0;
  268. try
  269. {
  270. request = (HttpWebRequest)
  271. WebRequest.Create(url);
  272. request.Method = httpMethod;
  273. request.ContentType = httpMIMEType;
  274. request.Timeout = httpTimeout;
  275. // execute the request
  276. response = (HttpWebResponse)
  277. request.GetResponse();
  278. Stream resStream = response.GetResponseStream();
  279. do
  280. {
  281. // fill the buffer with data
  282. count = resStream.Read(buf, 0, buf.Length);
  283. // make sure we read some data
  284. if (count != 0)
  285. {
  286. // translate from bytes to ASCII text
  287. tempString = Encoding.UTF8.GetString(buf, 0, count);
  288. // continue building the string
  289. sb.Append(tempString);
  290. }
  291. } while (count > 0); // any more data to read?
  292. response_body = sb.ToString();
  293. }
  294. catch (Exception e)
  295. {
  296. status = 499;
  297. response_body = e.Message;
  298. finished = true;
  299. return;
  300. }
  301. status = 200;
  302. finished = true;
  303. }
  304. public void Stop()
  305. {
  306. try
  307. {
  308. httpThread.Abort();
  309. }
  310. catch (Exception)
  311. {
  312. }
  313. }
  314. }
  315. }