RestClient.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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.Reflection;
  32. using System.Text;
  33. using System.Threading;
  34. using System.Web;
  35. using log4net;
  36. using OpenSim.Framework.ServiceAuth;
  37. namespace OpenSim.Framework.Communications
  38. {
  39. /// <summary>
  40. /// Implementation of a generic REST client
  41. /// </summary>
  42. /// <remarks>
  43. /// This class is a generic implementation of a REST (Representational State Transfer) web service. This
  44. /// class is designed to execute both synchronously and asynchronously.
  45. ///
  46. /// Internally the implementation works as a two stage asynchronous web-client.
  47. /// When the request is initiated, RestClient will query asynchronously for for a web-response,
  48. /// sleeping until the initial response is returned by the server. Once the initial response is retrieved
  49. /// the second stage of asynchronous requests will be triggered, in an attempt to read of the response
  50. /// object into a memorystream as a sequence of asynchronous reads.
  51. ///
  52. /// The asynchronisity of RestClient is designed to move as much processing into the back-ground, allowing
  53. /// other threads to execute, while it waits for a response from the web-service. RestClient itself can be
  54. /// invoked by the caller in either synchronous mode or asynchronous modes.
  55. /// </remarks>
  56. public class RestClient : IDisposable
  57. {
  58. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  59. // private string realuri;
  60. #region member variables
  61. /// <summary>
  62. /// The base Uri of the web-service e.g. http://www.google.com
  63. /// </summary>
  64. private string _url;
  65. /// <summary>
  66. /// Path elements of the query
  67. /// </summary>
  68. private List<string> _pathElements = new List<string>();
  69. /// <summary>
  70. /// Parameter elements of the query, e.g. min=34
  71. /// </summary>
  72. private Dictionary<string, string> _parameterElements = new Dictionary<string, string>();
  73. /// <summary>
  74. /// Request method. E.g. GET, POST, PUT or DELETE
  75. /// </summary>
  76. private string _method;
  77. /// <summary>
  78. /// Temporary buffer used to store bytes temporarily as they come in from the server
  79. /// </summary>
  80. private byte[] _readbuf;
  81. /// <summary>
  82. /// MemoryStream representing the resulting resource
  83. /// </summary>
  84. private Stream _resource;
  85. /// <summary>
  86. /// WebRequest object, held as a member variable
  87. /// </summary>
  88. private HttpWebRequest _request;
  89. /// <summary>
  90. /// WebResponse object, held as a member variable, so we can close it
  91. /// </summary>
  92. private HttpWebResponse _response;
  93. /// <summary>
  94. /// This flag will help block the main synchroneous method, in case we run in synchroneous mode
  95. /// </summary>
  96. //public static ManualResetEvent _allDone = new ManualResetEvent(false);
  97. /// <summary>
  98. /// Default time out period
  99. /// </summary>
  100. //private const int DefaultTimeout = 10*1000; // 10 seconds timeout
  101. /// <summary>
  102. /// Default Buffer size of a block requested from the web-server
  103. /// </summary>
  104. private const int BufferSize = 4096; // Read blocks of 4 KB.
  105. /// <summary>
  106. /// if an exception occours during async processing, we need to save it, so it can be
  107. /// rethrown on the primary thread;
  108. /// </summary>
  109. private Exception _asyncException;
  110. #endregion member variables
  111. #region constructors
  112. /// <summary>
  113. /// Instantiate a new RestClient
  114. /// </summary>
  115. /// <param name="url">Web-service to query, e.g. http://osgrid.org:8003</param>
  116. public RestClient(string url)
  117. {
  118. _url = url;
  119. _readbuf = new byte[BufferSize];
  120. _resource = new MemoryStream();
  121. _request = null;
  122. _response = null;
  123. _lock = new object();
  124. }
  125. private object _lock;
  126. #endregion constructors
  127. #region Dispose
  128. private bool disposed = false;
  129. public void Dispose()
  130. {
  131. Dispose(true);
  132. GC.SuppressFinalize(this);
  133. }
  134. protected virtual void Dispose(bool disposing)
  135. {
  136. if (disposed)
  137. return;
  138. if (disposing)
  139. {
  140. _resource.Dispose();
  141. }
  142. disposed = true;
  143. }
  144. #endregion Dispose
  145. /// <summary>
  146. /// Add a path element to the query, e.g. assets
  147. /// </summary>
  148. /// <param name="element">path entry</param>
  149. public void AddResourcePath(string element)
  150. {
  151. if (isSlashed(element))
  152. _pathElements.Add(element.Substring(0, element.Length - 1));
  153. else
  154. _pathElements.Add(element);
  155. }
  156. /// <summary>
  157. /// Add a query parameter to the Url
  158. /// </summary>
  159. /// <param name="name">Name of the parameter, e.g. min</param>
  160. /// <param name="value">Value of the parameter, e.g. 42</param>
  161. public void AddQueryParameter(string name, string value)
  162. {
  163. try
  164. {
  165. _parameterElements.Add(HttpUtility.UrlEncode(name), HttpUtility.UrlEncode(value));
  166. }
  167. catch (ArgumentException)
  168. {
  169. m_log.Error("[REST]: Query parameter " + name + " is already added.");
  170. }
  171. catch (Exception e)
  172. {
  173. m_log.Error("[REST]: An exception was raised adding query parameter to dictionary. Exception: {0}",e);
  174. }
  175. }
  176. /// <summary>
  177. /// Add a query parameter to the Url
  178. /// </summary>
  179. /// <param name="name">Name of the parameter, e.g. min</param>
  180. public void AddQueryParameter(string name)
  181. {
  182. try
  183. {
  184. _parameterElements.Add(HttpUtility.UrlEncode(name), null);
  185. }
  186. catch (ArgumentException)
  187. {
  188. m_log.Error("[REST]: Query parameter " + name + " is already added.");
  189. }
  190. catch (Exception e)
  191. {
  192. m_log.Error("[REST]: An exception was raised adding query parameter to dictionary. Exception: {0}",e);
  193. }
  194. }
  195. /// <summary>
  196. /// Web-Request method, e.g. GET, PUT, POST, DELETE
  197. /// </summary>
  198. public string RequestMethod
  199. {
  200. get { return _method; }
  201. set { _method = value; }
  202. }
  203. /// <summary>
  204. /// True if string contains a trailing slash '/'
  205. /// </summary>
  206. /// <param name="s">string to be examined</param>
  207. /// <returns>true if slash is present</returns>
  208. private static bool isSlashed(string s)
  209. {
  210. return s.Substring(s.Length - 1, 1) == "/";
  211. }
  212. /// <summary>
  213. /// Build a Uri based on the initial Url, path elements and parameters
  214. /// </summary>
  215. /// <returns>fully constructed Uri</returns>
  216. private Uri buildUri()
  217. {
  218. StringBuilder sb = new StringBuilder();
  219. sb.Append(_url);
  220. foreach (string e in _pathElements)
  221. {
  222. sb.Append("/");
  223. sb.Append(e);
  224. }
  225. bool firstElement = true;
  226. foreach (KeyValuePair<string, string> kv in _parameterElements)
  227. {
  228. if (firstElement)
  229. {
  230. sb.Append("?");
  231. firstElement = false;
  232. }
  233. else
  234. sb.Append("&");
  235. sb.Append(kv.Key);
  236. if (!string.IsNullOrEmpty(kv.Value))
  237. {
  238. sb.Append("=");
  239. sb.Append(kv.Value);
  240. }
  241. }
  242. // realuri = sb.ToString();
  243. //m_log.InfoFormat("[REST CLIENT]: RestURL: {0}", realuri);
  244. return new Uri(sb.ToString());
  245. }
  246. #region Async communications with server
  247. /// <summary>
  248. /// Async method, invoked when a block of data has been received from the service
  249. /// </summary>
  250. /// <param name="ar"></param>
  251. private void StreamIsReadyDelegate(IAsyncResult ar)
  252. {
  253. try
  254. {
  255. Stream s = (Stream) ar.AsyncState;
  256. int read = s.EndRead(ar);
  257. if (read > 0)
  258. {
  259. _resource.Write(_readbuf, 0, read);
  260. // IAsyncResult asynchronousResult =
  261. // s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s);
  262. s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s);
  263. // TODO! Implement timeout, without killing the server
  264. //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  265. }
  266. else
  267. {
  268. s.Close();
  269. //_allDone.Set();
  270. }
  271. }
  272. catch (Exception e)
  273. {
  274. //_allDone.Set();
  275. _asyncException = e;
  276. }
  277. }
  278. #endregion Async communications with server
  279. /// <summary>
  280. /// Perform a synchronous request
  281. /// </summary>
  282. public Stream Request()
  283. {
  284. return Request(null);
  285. }
  286. /// <summary>
  287. /// Perform a synchronous request
  288. /// </summary>
  289. public Stream Request(IServiceAuth auth)
  290. {
  291. lock (_lock)
  292. {
  293. _request = (HttpWebRequest) WebRequest.Create(buildUri());
  294. _request.KeepAlive = false;
  295. _request.ContentType = "application/xml";
  296. _request.Timeout = 200000;
  297. _request.Method = RequestMethod;
  298. _asyncException = null;
  299. if (auth != null)
  300. auth.AddAuthorization(_request.Headers);
  301. int reqnum = WebUtil.RequestNumber++;
  302. if (WebUtil.DebugLevel >= 3)
  303. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri);
  304. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  305. try
  306. {
  307. using (_response = (HttpWebResponse) _request.GetResponse())
  308. {
  309. using (Stream src = _response.GetResponseStream())
  310. {
  311. int length = src.Read(_readbuf, 0, BufferSize);
  312. while (length > 0)
  313. {
  314. _resource.Write(_readbuf, 0, length);
  315. length = src.Read(_readbuf, 0, BufferSize);
  316. }
  317. // TODO! Implement timeout, without killing the server
  318. // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
  319. //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  320. // _allDone.WaitOne();
  321. }
  322. }
  323. }
  324. catch (WebException e)
  325. {
  326. using (HttpWebResponse errorResponse = e.Response as HttpWebResponse)
  327. {
  328. if (null != errorResponse && HttpStatusCode.NotFound == errorResponse.StatusCode)
  329. {
  330. // This is often benign. E.g., requesting a missing asset will return 404.
  331. m_log.DebugFormat("[REST CLIENT] Resource not found (404): {0}", _request.Address.ToString());
  332. }
  333. else
  334. {
  335. m_log.Error(string.Format("[REST CLIENT] Error fetching resource from server: {0} ", _request.Address.ToString()), e);
  336. }
  337. }
  338. return null;
  339. }
  340. if (_asyncException != null)
  341. throw _asyncException;
  342. if (_resource != null)
  343. {
  344. _resource.Flush();
  345. _resource.Seek(0, SeekOrigin.Begin);
  346. }
  347. if (WebUtil.DebugLevel >= 5)
  348. WebUtil.LogResponseDetail(reqnum, _resource);
  349. return _resource;
  350. }
  351. }
  352. public Stream Request(Stream src, IServiceAuth auth)
  353. {
  354. _request = (HttpWebRequest) WebRequest.Create(buildUri());
  355. _request.KeepAlive = false;
  356. _request.ContentType = "application/xml";
  357. _request.Timeout = 900000;
  358. _request.Method = RequestMethod;
  359. _asyncException = null;
  360. _request.ContentLength = src.Length;
  361. if (auth != null)
  362. auth.AddAuthorization(_request.Headers);
  363. src.Seek(0, SeekOrigin.Begin);
  364. int reqnum = WebUtil.RequestNumber++;
  365. if (WebUtil.DebugLevel >= 3)
  366. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri);
  367. if (WebUtil.DebugLevel >= 5)
  368. WebUtil.LogOutgoingDetail(string.Format("SEND {0}: ", reqnum), src);
  369. Stream dst = _request.GetRequestStream();
  370. byte[] buf = new byte[1024];
  371. int length = src.Read(buf, 0, 1024);
  372. while (length > 0)
  373. {
  374. dst.Write(buf, 0, length);
  375. length = src.Read(buf, 0, 1024);
  376. }
  377. try
  378. {
  379. _response = (HttpWebResponse)_request.GetResponse();
  380. }
  381. catch (WebException e)
  382. {
  383. m_log.WarnFormat("[REST]: Request {0} {1} failed with status {2} and message {3}",
  384. RequestMethod, _request.RequestUri, e.Status, e.Message);
  385. return null;
  386. }
  387. catch (Exception e)
  388. {
  389. m_log.WarnFormat(
  390. "[REST]: Request {0} {1} failed with exception {2} {3}",
  391. RequestMethod, _request.RequestUri, e.Message, e.StackTrace);
  392. return null;
  393. }
  394. if (WebUtil.DebugLevel >= 5)
  395. {
  396. using (Stream responseStream = _response.GetResponseStream())
  397. {
  398. using (StreamReader reader = new StreamReader(responseStream))
  399. {
  400. string responseStr = reader.ReadToEnd();
  401. WebUtil.LogResponseDetail(reqnum, responseStr);
  402. }
  403. }
  404. }
  405. _response.Close();
  406. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  407. // TODO! Implement timeout, without killing the server
  408. // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
  409. //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  410. return null;
  411. }
  412. #region Async Invocation
  413. public IAsyncResult BeginRequest(AsyncCallback callback, object state)
  414. {
  415. /// <summary>
  416. /// In case, we are invoked asynchroneously this object will keep track of the state
  417. /// </summary>
  418. AsyncResult<Stream> ar = new AsyncResult<Stream>(callback, state);
  419. Util.FireAndForget(RequestHelper, ar, "RestClient.BeginRequest");
  420. return ar;
  421. }
  422. public Stream EndRequest(IAsyncResult asyncResult)
  423. {
  424. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  425. // Wait for operation to complete, then return result or
  426. // throw exception
  427. return ar.EndInvoke();
  428. }
  429. private void RequestHelper(Object asyncResult)
  430. {
  431. // We know that it's really an AsyncResult<DateTime> object
  432. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  433. try
  434. {
  435. // Perform the operation; if sucessful set the result
  436. Stream s = Request(null);
  437. ar.SetAsCompleted(s, false);
  438. }
  439. catch (Exception e)
  440. {
  441. // If operation fails, set the exception
  442. ar.HandleException(e, false);
  443. }
  444. }
  445. #endregion Async Invocation
  446. }
  447. }