ScriptsHttpRequests.cs 11 KB

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