ScriptsHttpRequests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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.Collections.Generic;
  29. using System.IO;
  30. using System.Net;
  31. using System.Text;
  32. using System.Threading;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. /*****************************************************
  41. *
  42. * ScriptsHttpRequests
  43. *
  44. * Implements the llHttpRequest and http_response
  45. * callback.
  46. *
  47. * Some stuff was already in LSLLongCmdHandler, and then
  48. * there was this file with a stub class in it. So,
  49. * I am moving some of the objects and functions out of
  50. * LSLLongCmdHandler, such as the HttpRequestClass, the
  51. * start and stop methods, and setting up pending and
  52. * completed queues. These are processed in the
  53. * LSLLongCmdHandler polling loop. Similiar to the
  54. * XMLRPCModule, since that seems to work.
  55. *
  56. * //TODO
  57. *
  58. * This probably needs some throttling mechanism but
  59. * it's wide open right now. This applies to both
  60. * number of requests and data volume.
  61. *
  62. * Linden puts all kinds of header fields in the requests.
  63. * Not doing any of that:
  64. * User-Agent
  65. * X-SecondLife-Shard
  66. * X-SecondLife-Object-Name
  67. * X-SecondLife-Object-Key
  68. * X-SecondLife-Region
  69. * X-SecondLife-Local-Position
  70. * X-SecondLife-Local-Velocity
  71. * X-SecondLife-Local-Rotation
  72. * X-SecondLife-Owner-Name
  73. * X-SecondLife-Owner-Key
  74. *
  75. * HTTPS support
  76. *
  77. * Configurable timeout?
  78. * Configurable max response size?
  79. * Configurable
  80. *
  81. * **************************************************/
  82. namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
  83. {
  84. public class HttpRequestModule : IRegionModule, IHttpRequestModule
  85. {
  86. private object HttpListLock = new object();
  87. private int httpTimeout = 30000;
  88. private string m_name = "HttpScriptRequests";
  89. private string m_proxyurl = "";
  90. private string m_proxyexcepts = "";
  91. // <request id, HttpRequestClass>
  92. private Dictionary<UUID, HttpRequestClass> m_pendingRequests;
  93. private Scene m_scene;
  94. // private Queue<HttpRequestClass> rpcQueue = new Queue<HttpRequestClass>();
  95. public HttpRequestModule()
  96. {
  97. }
  98. #region IHttpRequestModule Members
  99. public UUID MakeHttpRequest(string url, string parameters, string body)
  100. {
  101. return UUID.Zero;
  102. }
  103. public UUID StartHttpRequest(uint localID, UUID itemID, string url, List<string> parameters, Dictionary<string, string> headers, string body)
  104. {
  105. UUID reqID = UUID.Random();
  106. HttpRequestClass htc = new HttpRequestClass();
  107. // Partial implementation: support for parameter flags needed
  108. // see http://wiki.secondlife.com/wiki/LlHTTPRequest
  109. //
  110. // Parameters are expected in {key, value, ... , key, value}
  111. if (parameters != null)
  112. {
  113. string[] parms = parameters.ToArray();
  114. for (int i = 0; i < parms.Length; i += 2)
  115. {
  116. switch (Int32.Parse(parms[i]))
  117. {
  118. case (int)HttpRequestConstants.HTTP_METHOD:
  119. htc.HttpMethod = parms[i + 1];
  120. break;
  121. case (int)HttpRequestConstants.HTTP_MIMETYPE:
  122. htc.HttpMIMEType = parms[i + 1];
  123. break;
  124. case (int)HttpRequestConstants.HTTP_BODY_MAXLENGTH:
  125. // TODO implement me
  126. break;
  127. case (int)HttpRequestConstants.HTTP_VERIFY_CERT:
  128. // TODO implement me
  129. break;
  130. }
  131. }
  132. }
  133. htc.LocalID = localID;
  134. htc.ItemID = itemID;
  135. htc.Url = url;
  136. htc.ReqID = reqID;
  137. htc.HttpTimeout = httpTimeout;
  138. htc.OutboundBody = body;
  139. htc.ResponseHeaders = headers;
  140. htc.proxyurl = m_proxyurl;
  141. htc.proxyexcepts = m_proxyexcepts;
  142. lock (HttpListLock)
  143. {
  144. m_pendingRequests.Add(reqID, htc);
  145. }
  146. htc.Process();
  147. return reqID;
  148. }
  149. public void StopHttpRequest(uint m_localID, UUID m_itemID)
  150. {
  151. if (m_pendingRequests != null)
  152. {
  153. lock (HttpListLock)
  154. {
  155. HttpRequestClass tmpReq;
  156. if (m_pendingRequests.TryGetValue(m_itemID, out tmpReq))
  157. {
  158. tmpReq.Stop();
  159. m_pendingRequests.Remove(m_itemID);
  160. }
  161. }
  162. }
  163. }
  164. /*
  165. * TODO
  166. * Not sure how important ordering is is here - the next first
  167. * one completed in the list is returned, based soley on its list
  168. * position, not the order in which the request was started or
  169. * finsihed. I thought about setting up a queue for this, but
  170. * it will need some refactoring and this works 'enough' right now
  171. */
  172. public IServiceRequest GetNextCompletedRequest()
  173. {
  174. lock (HttpListLock)
  175. {
  176. foreach (UUID luid in m_pendingRequests.Keys)
  177. {
  178. HttpRequestClass tmpReq;
  179. if (m_pendingRequests.TryGetValue(luid, out tmpReq))
  180. {
  181. if (tmpReq.Finished)
  182. {
  183. return tmpReq;
  184. }
  185. }
  186. }
  187. }
  188. return null;
  189. }
  190. public void RemoveCompletedRequest(UUID id)
  191. {
  192. lock (HttpListLock)
  193. {
  194. HttpRequestClass tmpReq;
  195. if (m_pendingRequests.TryGetValue(id, out tmpReq))
  196. {
  197. tmpReq.Stop();
  198. tmpReq = null;
  199. m_pendingRequests.Remove(id);
  200. }
  201. }
  202. }
  203. #endregion
  204. #region IRegionModule Members
  205. public void Initialise(Scene scene, IConfigSource config)
  206. {
  207. m_scene = scene;
  208. m_scene.RegisterModuleInterface<IHttpRequestModule>(this);
  209. m_proxyurl = config.Configs["Startup"].GetString("HttpProxy");
  210. m_proxyexcepts = config.Configs["Startup"].GetString("HttpProxyExceptions");
  211. m_pendingRequests = new Dictionary<UUID, HttpRequestClass>();
  212. }
  213. public void PostInitialise()
  214. {
  215. }
  216. public void Close()
  217. {
  218. }
  219. public string Name
  220. {
  221. get { return m_name; }
  222. }
  223. public bool IsSharedModule
  224. {
  225. get { return true; }
  226. }
  227. #endregion
  228. }
  229. public class HttpRequestClass: IServiceRequest
  230. {
  231. // Constants for parameters
  232. // public const int HTTP_BODY_MAXLENGTH = 2;
  233. // public const int HTTP_METHOD = 0;
  234. // public const int HTTP_MIMETYPE = 1;
  235. // public const int HTTP_VERIFY_CERT = 3;
  236. private bool _finished;
  237. public bool Finished
  238. {
  239. get { return _finished; }
  240. }
  241. // public int HttpBodyMaxLen = 2048; // not implemented
  242. // Parameter members and default values
  243. public string HttpMethod = "GET";
  244. public string HttpMIMEType = "text/plain;charset=utf-8";
  245. public int HttpTimeout;
  246. // public bool HttpVerifyCert = true; // not implemented
  247. private Thread httpThread;
  248. // Request info
  249. private UUID _itemID;
  250. public UUID ItemID
  251. {
  252. get { return _itemID; }
  253. set { _itemID = value; }
  254. }
  255. private uint _localID;
  256. public uint LocalID
  257. {
  258. get { return _localID; }
  259. set { _localID = value; }
  260. }
  261. public DateTime Next;
  262. public string proxyurl;
  263. public string proxyexcepts;
  264. public string OutboundBody;
  265. private UUID _reqID;
  266. public UUID ReqID
  267. {
  268. get { return _reqID; }
  269. set { _reqID = value; }
  270. }
  271. public HttpWebRequest Request;
  272. public string ResponseBody;
  273. public List<string> ResponseMetadata;
  274. public Dictionary<string, string> ResponseHeaders;
  275. public int Status;
  276. public string Url;
  277. public void Process()
  278. {
  279. httpThread = new Thread(SendRequest);
  280. httpThread.Name = "HttpRequestThread";
  281. httpThread.Priority = ThreadPriority.BelowNormal;
  282. httpThread.IsBackground = true;
  283. _finished = false;
  284. httpThread.Start();
  285. ThreadTracker.Add(httpThread);
  286. }
  287. /*
  288. * TODO: More work on the response codes. Right now
  289. * returning 200 for success or 499 for exception
  290. */
  291. public void SendRequest()
  292. {
  293. HttpWebResponse response = null;
  294. StringBuilder sb = new StringBuilder();
  295. byte[] buf = new byte[8192];
  296. string tempString = null;
  297. int count = 0;
  298. try
  299. {
  300. Request = (HttpWebRequest) WebRequest.Create(Url);
  301. Request.Method = HttpMethod;
  302. Request.ContentType = HttpMIMEType;
  303. if (proxyurl != null && proxyurl.Length > 0)
  304. {
  305. if (proxyexcepts != null && proxyexcepts.Length > 0)
  306. {
  307. string[] elist = proxyexcepts.Split(';');
  308. Request.Proxy = new WebProxy(proxyurl, true, elist);
  309. }
  310. else
  311. {
  312. Request.Proxy = new WebProxy(proxyurl, true);
  313. }
  314. }
  315. foreach (KeyValuePair<string, string> entry in ResponseHeaders)
  316. if (entry.Key.ToLower().Equals("user-agent"))
  317. Request.UserAgent = entry.Value;
  318. else
  319. Request.Headers[entry.Key] = entry.Value;
  320. // Encode outbound data
  321. if (OutboundBody.Length > 0)
  322. {
  323. byte[] data = Encoding.UTF8.GetBytes(OutboundBody);
  324. Request.ContentLength = data.Length;
  325. Stream bstream = Request.GetRequestStream();
  326. bstream.Write(data, 0, data.Length);
  327. bstream.Close();
  328. }
  329. Request.Timeout = HttpTimeout;
  330. // execute the request
  331. response = (HttpWebResponse) Request.GetResponse();
  332. Stream resStream = response.GetResponseStream();
  333. do
  334. {
  335. // fill the buffer with data
  336. count = resStream.Read(buf, 0, buf.Length);
  337. // make sure we read some data
  338. if (count != 0)
  339. {
  340. // translate from bytes to ASCII text
  341. tempString = Encoding.UTF8.GetString(buf, 0, count);
  342. // continue building the string
  343. sb.Append(tempString);
  344. }
  345. } while (count > 0); // any more data to read?
  346. ResponseBody = sb.ToString();
  347. }
  348. catch (Exception e)
  349. {
  350. if (e is WebException && ((WebException)e).Status == WebExceptionStatus.ProtocolError)
  351. {
  352. HttpWebResponse webRsp = (HttpWebResponse)((WebException)e).Response;
  353. Status = (int)webRsp.StatusCode;
  354. ResponseBody = webRsp.StatusDescription;
  355. }
  356. else
  357. {
  358. Status = (int)OSHttpStatusCode.ClientErrorJoker;
  359. ResponseBody = e.Message;
  360. }
  361. _finished = true;
  362. return;
  363. }
  364. finally
  365. {
  366. if (response != null)
  367. response.Close();
  368. }
  369. Status = (int)OSHttpStatusCode.SuccessOk;
  370. _finished = true;
  371. }
  372. public void Stop()
  373. {
  374. try
  375. {
  376. httpThread.Abort();
  377. }
  378. catch (Exception)
  379. {
  380. }
  381. }
  382. }
  383. }