RestClient.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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
  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 = 90000;
  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. using (Stream dst = _request.GetRequestStream())
  370. {
  371. m_log.Info("[REST]: GetRequestStream is ok");
  372. byte[] buf = new byte[1024];
  373. int length = src.Read(buf, 0, 1024);
  374. m_log.Info("[REST]: First Read is ok");
  375. while (length > 0)
  376. {
  377. dst.Write(buf, 0, length);
  378. length = src.Read(buf, 0, 1024);
  379. }
  380. }
  381. try
  382. {
  383. _response = (HttpWebResponse)_request.GetResponse();
  384. }
  385. catch (WebException e)
  386. {
  387. m_log.WarnFormat("[REST]: Request {0} {1} failed with status {2} and message {3}",
  388. RequestMethod, _request.RequestUri, e.Status, e.Message);
  389. return null;
  390. }
  391. catch (Exception e)
  392. {
  393. m_log.WarnFormat(
  394. "[REST]: Request {0} {1} failed with exception {2} {3}",
  395. RequestMethod, _request.RequestUri, e.Message, e.StackTrace);
  396. return null;
  397. }
  398. if (WebUtil.DebugLevel >= 5)
  399. {
  400. using (Stream responseStream = _response.GetResponseStream())
  401. {
  402. using (StreamReader reader = new StreamReader(responseStream))
  403. {
  404. string responseStr = reader.ReadToEnd();
  405. WebUtil.LogResponseDetail(reqnum, responseStr);
  406. }
  407. }
  408. }
  409. if (_response != null)
  410. _response.Close();
  411. // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
  412. // TODO! Implement timeout, without killing the server
  413. // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
  414. //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
  415. return null;
  416. }
  417. #region Async Invocation
  418. public IAsyncResult BeginRequest(AsyncCallback callback, object state)
  419. {
  420. /// <summary>
  421. /// In case, we are invoked asynchroneously this object will keep track of the state
  422. /// </summary>
  423. AsyncResult<Stream> ar = new AsyncResult<Stream>(callback, state);
  424. Util.FireAndForget(RequestHelper, ar, "RestClient.BeginRequest");
  425. return ar;
  426. }
  427. public Stream EndRequest(IAsyncResult asyncResult)
  428. {
  429. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  430. // Wait for operation to complete, then return result or
  431. // throw exception
  432. return ar.EndInvoke();
  433. }
  434. private void RequestHelper(Object asyncResult)
  435. {
  436. // We know that it's really an AsyncResult<DateTime> object
  437. AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult;
  438. try
  439. {
  440. // Perform the operation; if sucessful set the result
  441. Stream s = Request(null);
  442. ar.SetAsCompleted(s, false);
  443. }
  444. catch (Exception e)
  445. {
  446. // If operation fails, set the exception
  447. ar.HandleException(e, false);
  448. }
  449. }
  450. #endregion Async Invocation
  451. }
  452. internal class SimpleAsyncResult : IAsyncResult
  453. {
  454. private readonly AsyncCallback m_callback;
  455. /// <summary>
  456. /// Is process completed?
  457. /// </summary>
  458. /// <remarks>Should really be boolean, but VolatileRead has no boolean method</remarks>
  459. private byte m_completed;
  460. /// <summary>
  461. /// Did process complete synchronously?
  462. /// </summary>
  463. /// <remarks>I have a hard time imagining a scenario where this is the case, again, same issue about
  464. /// booleans and VolatileRead as m_completed
  465. /// </remarks>
  466. private byte m_completedSynchronously;
  467. private readonly object m_asyncState;
  468. private ManualResetEvent m_waitHandle;
  469. private Exception m_exception;
  470. internal SimpleAsyncResult(AsyncCallback cb, object state)
  471. {
  472. m_callback = cb;
  473. m_asyncState = state;
  474. m_completed = 0;
  475. m_completedSynchronously = 1;
  476. }
  477. #region IAsyncResult Members
  478. public object AsyncState
  479. {
  480. get { return m_asyncState; }
  481. }
  482. public WaitHandle AsyncWaitHandle
  483. {
  484. get
  485. {
  486. if (m_waitHandle == null)
  487. {
  488. bool done = IsCompleted;
  489. ManualResetEvent mre = new ManualResetEvent(done);
  490. if (Interlocked.CompareExchange(ref m_waitHandle, mre, null) != null)
  491. {
  492. mre.Close();
  493. }
  494. else
  495. {
  496. if (!done && IsCompleted)
  497. {
  498. m_waitHandle.Set();
  499. }
  500. }
  501. }
  502. return m_waitHandle;
  503. }
  504. }
  505. public bool CompletedSynchronously
  506. {
  507. get { return Thread.VolatileRead(ref m_completedSynchronously) == 1; }
  508. }
  509. public bool IsCompleted
  510. {
  511. get { return Thread.VolatileRead(ref m_completed) == 1; }
  512. }
  513. #endregion
  514. #region class Methods
  515. internal void SetAsCompleted(bool completedSynchronously)
  516. {
  517. m_completed = 1;
  518. if (completedSynchronously)
  519. m_completedSynchronously = 1;
  520. else
  521. m_completedSynchronously = 0;
  522. SignalCompletion();
  523. }
  524. internal void HandleException(Exception e, bool completedSynchronously)
  525. {
  526. m_completed = 1;
  527. if (completedSynchronously)
  528. m_completedSynchronously = 1;
  529. else
  530. m_completedSynchronously = 0;
  531. m_exception = e;
  532. SignalCompletion();
  533. }
  534. private void SignalCompletion()
  535. {
  536. if (m_waitHandle != null) m_waitHandle.Set();
  537. if (m_callback != null) m_callback(this);
  538. }
  539. public void EndInvoke()
  540. {
  541. // This method assumes that only 1 thread calls EndInvoke
  542. if (!IsCompleted)
  543. {
  544. // If the operation isn't done, wait for it
  545. AsyncWaitHandle.WaitOne();
  546. AsyncWaitHandle.Close();
  547. m_waitHandle.Close();
  548. m_waitHandle = null; // Allow early GC
  549. }
  550. // Operation is done: if an exception occured, throw it
  551. if (m_exception != null) throw m_exception;
  552. }
  553. #endregion
  554. }
  555. internal class AsyncResult<T> : SimpleAsyncResult
  556. {
  557. private T m_result = default(T);
  558. public AsyncResult(AsyncCallback asyncCallback, Object state) :
  559. base(asyncCallback, state)
  560. {
  561. }
  562. public void SetAsCompleted(T result, bool completedSynchronously)
  563. {
  564. // Save the asynchronous operation's result
  565. m_result = result;
  566. // Tell the base class that the operation completed
  567. // sucessfully (no exception)
  568. base.SetAsCompleted(completedSynchronously);
  569. }
  570. public new T EndInvoke()
  571. {
  572. base.EndInvoke();
  573. return m_result;
  574. }
  575. }
  576. }