1
0

ScriptsHttpRequests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.Framework;
  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.Scripting.HttpRequest
  81. {
  82. public class HttpRequestModule : IRegionModule, IHttpRequests
  83. {
  84. private object HttpListLock = new object();
  85. private int httpTimeout = 30000;
  86. private string m_name = "HttpScriptRequests";
  87. // <request id, HttpRequestClass>
  88. private Dictionary<LLUUID, HttpRequestClass> m_pendingRequests;
  89. private Scene m_scene;
  90. private Queue<HttpRequestClass> rpcQueue = new Queue<HttpRequestClass>();
  91. public HttpRequestModule()
  92. {
  93. }
  94. #region IHttpRequests Members
  95. public LLUUID MakeHttpRequest(string url, string parameters, string body)
  96. {
  97. return LLUUID.Zero;
  98. }
  99. public LLUUID StartHttpRequest(uint localID, LLUUID itemID, string url, List<string> parameters, string body)
  100. {
  101. LLUUID reqID = LLUUID.Random();
  102. HttpRequestClass htc = new HttpRequestClass();
  103. // Partial implementation: support for parameter flags needed
  104. // see http://wiki.secondlife.com/wiki/LlHTTPRequest
  105. //
  106. // Parameters are expected in {key, value, ... , key, value}
  107. if (parameters != null)
  108. {
  109. string[] parms = parameters.ToArray();
  110. for (int i = 0; i < parms.Length / 2; i += 2)
  111. {
  112. switch (Int32.Parse(parms[i]))
  113. {
  114. case HttpRequestClass.HTTP_METHOD:
  115. htc.httpMethod = parms[i + 1];
  116. break;
  117. case HttpRequestClass.HTTP_MIMETYPE:
  118. htc.httpMIMEType = parms[i + 1];
  119. break;
  120. case HttpRequestClass.HTTP_BODY_MAXLENGTH:
  121. // TODO implement me
  122. break;
  123. case HttpRequestClass.HTTP_VERIFY_CERT:
  124. // TODO implement me
  125. break;
  126. }
  127. }
  128. }
  129. htc.localID = localID;
  130. htc.itemID = itemID;
  131. htc.url = url;
  132. htc.reqID = reqID;
  133. htc.httpTimeout = httpTimeout;
  134. htc.outbound_body = body;
  135. lock (HttpListLock)
  136. {
  137. m_pendingRequests.Add(reqID, htc);
  138. }
  139. htc.process();
  140. return reqID;
  141. }
  142. public void StopHttpRequest(uint m_localID, LLUUID m_itemID)
  143. {
  144. if (m_pendingRequests != null)
  145. {
  146. lock (HttpListLock)
  147. {
  148. HttpRequestClass tmpReq;
  149. if (m_pendingRequests.TryGetValue(m_itemID, out tmpReq))
  150. {
  151. tmpReq.Stop();
  152. m_pendingRequests.Remove(m_itemID);
  153. }
  154. }
  155. }
  156. }
  157. /*
  158. * TODO
  159. * Not sure how important ordering is is here - the next first
  160. * one completed in the list is returned, based soley on its list
  161. * position, not the order in which the request was started or
  162. * finsihed. I thought about setting up a queue for this, but
  163. * it will need some refactoring and this works 'enough' right now
  164. */
  165. public HttpRequestClass GetNextCompletedRequest()
  166. {
  167. lock (HttpListLock)
  168. {
  169. foreach (LLUUID luid in m_pendingRequests.Keys)
  170. {
  171. HttpRequestClass tmpReq;
  172. if (m_pendingRequests.TryGetValue(luid, out tmpReq))
  173. {
  174. if (tmpReq.finished)
  175. {
  176. return tmpReq;
  177. }
  178. }
  179. }
  180. }
  181. return null;
  182. }
  183. public void RemoveCompletedRequest(LLUUID id)
  184. {
  185. lock (HttpListLock)
  186. {
  187. HttpRequestClass tmpReq;
  188. if (m_pendingRequests.TryGetValue(id, out tmpReq))
  189. {
  190. tmpReq.Stop();
  191. tmpReq = null;
  192. m_pendingRequests.Remove(id);
  193. }
  194. }
  195. }
  196. #endregion
  197. #region IRegionModule Members
  198. public void Initialise(Scene scene, IConfigSource config)
  199. {
  200. m_scene = scene;
  201. m_scene.RegisterModuleInterface<IHttpRequests>(this);
  202. m_pendingRequests = new Dictionary<LLUUID, HttpRequestClass>();
  203. }
  204. public void PostInitialise()
  205. {
  206. }
  207. public void Close()
  208. {
  209. }
  210. public string Name
  211. {
  212. get { return m_name; }
  213. }
  214. public bool IsSharedModule
  215. {
  216. get { return true; }
  217. }
  218. #endregion
  219. }
  220. public class HttpRequestClass
  221. {
  222. // Constants for parameters
  223. public const int HTTP_BODY_MAXLENGTH = 2;
  224. public const int HTTP_METHOD = 0;
  225. public const int HTTP_MIMETYPE = 1;
  226. public const int HTTP_VERIFY_CERT = 3;
  227. public bool finished;
  228. public int httpBodyMaxLen = 2048; // not implemented
  229. // Parameter members and default values
  230. public string httpMethod = "GET";
  231. public string httpMIMEType = "text/plain;charset=utf-8";
  232. private Thread httpThread;
  233. public int httpTimeout;
  234. public bool httpVerifyCert = true; // not implemented
  235. // Request info
  236. public LLUUID itemID;
  237. public uint localID;
  238. public DateTime next;
  239. public string outbound_body;
  240. public LLUUID reqID;
  241. public HttpWebRequest request;
  242. public string response_body;
  243. public List<string> response_metadata;
  244. public int status;
  245. public string url;
  246. public void process()
  247. {
  248. httpThread = new Thread(SendRequest);
  249. httpThread.Name = "HttpRequestThread";
  250. httpThread.Priority = ThreadPriority.BelowNormal;
  251. httpThread.IsBackground = true;
  252. finished = false;
  253. httpThread.Start();
  254. ThreadTracker.Add(httpThread);
  255. }
  256. /*
  257. * TODO: More work on the response codes. Right now
  258. * returning 200 for success or 499 for exception
  259. */
  260. public void SendRequest()
  261. {
  262. HttpWebResponse response = null;
  263. StringBuilder sb = new StringBuilder();
  264. byte[] buf = new byte[8192];
  265. string tempString = null;
  266. int count = 0;
  267. try
  268. {
  269. request = (HttpWebRequest)
  270. WebRequest.Create(url);
  271. request.Method = httpMethod;
  272. request.ContentType = httpMIMEType;
  273. request.Timeout = httpTimeout;
  274. // execute the request
  275. response = (HttpWebResponse)
  276. request.GetResponse();
  277. Stream resStream = response.GetResponseStream();
  278. do
  279. {
  280. // fill the buffer with data
  281. count = resStream.Read(buf, 0, buf.Length);
  282. // make sure we read some data
  283. if (count != 0)
  284. {
  285. // translate from bytes to ASCII text
  286. tempString = Encoding.UTF8.GetString(buf, 0, count);
  287. // continue building the string
  288. sb.Append(tempString);
  289. }
  290. } while (count > 0); // any more data to read?
  291. response_body = sb.ToString();
  292. }
  293. catch (Exception e)
  294. {
  295. status = 499;
  296. response_body = e.Message;
  297. finished = true;
  298. return;
  299. }
  300. status = 200;
  301. finished = true;
  302. }
  303. public void Stop()
  304. {
  305. try
  306. {
  307. httpThread.Abort();
  308. }
  309. catch (Exception)
  310. {
  311. }
  312. }
  313. }
  314. }