1
0

JsonRpcRequestManager.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Net;
  29. using System.Net.Sockets;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.IO;
  33. using OpenMetaverse.StructuredData;
  34. using OpenMetaverse;
  35. using log4net;
  36. namespace OpenSim.Framework.Servers.HttpServer
  37. {
  38. /// <summary>
  39. /// Json rpc request manager.
  40. /// </summary>
  41. public class JsonRpcRequestManager
  42. {
  43. static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. public JsonRpcRequestManager()
  45. {
  46. }
  47. /// <summary>
  48. /// Sends json-rpc request with a serializable type.
  49. /// </summary>
  50. /// <returns>
  51. /// OSD Map.
  52. /// </returns>
  53. /// <param name='parameters'>
  54. /// Serializable type .
  55. /// </param>
  56. /// <param name='method'>
  57. /// Json-rpc method to call.
  58. /// </param>
  59. /// <param name='uri'>
  60. /// URI of json-rpc service.
  61. /// </param>
  62. /// <param name='jsonId'>
  63. /// Id for our call.
  64. /// </param>
  65. public bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId)
  66. {
  67. if (jsonId is null)
  68. throw new ArgumentNullException("jsonId");
  69. if (uri is null)
  70. throw new ArgumentNullException("uri");
  71. if (method is null)
  72. throw new ArgumentNullException("method");
  73. if (parameters is null)
  74. throw new ArgumentNullException("parameters");
  75. if(string.IsNullOrWhiteSpace(uri))
  76. return false;
  77. OSDMap request = new()
  78. {
  79. { "jsonrpc", OSD.FromString("2.0") },
  80. { "id", OSD.FromString(jsonId) },
  81. { "method", OSD.FromString(method) },
  82. { "params", OSD.SerializeMembers(parameters) }
  83. };
  84. OSDMap outerResponse;
  85. try
  86. {
  87. outerResponse = WebUtil.PostToService(uri, request, 10000, true);
  88. }
  89. catch (Exception e)
  90. {
  91. m_log.Debug(string.Format("JsonRpc request '{0}' to {1} failed", method, uri), e);
  92. return false;
  93. }
  94. OSD osdtmp;
  95. if (!outerResponse.TryGetValue("_Result", out osdtmp) || (osdtmp is not OSDMap response))
  96. {
  97. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  98. method, uri, OSDParser.SerializeJsonString(outerResponse));
  99. return false;
  100. }
  101. if (response.TryGetValue("error", out osdtmp))
  102. {
  103. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
  104. method, uri, OSDParser.SerializeJsonString(osdtmp));
  105. return false;
  106. }
  107. if (!response.TryGetValue("result", out osdtmp) || (osdtmp is not OSDMap resultmap))
  108. {
  109. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  110. method, uri, OSDParser.SerializeJsonString(response));
  111. return false;
  112. }
  113. OSD.DeserializeMembers(ref parameters, resultmap);
  114. return true;
  115. }
  116. /// <summary>
  117. /// Sends json-rpc request with OSD parameter.
  118. /// </summary>
  119. /// <returns>
  120. /// The rpc request.
  121. /// </returns>
  122. /// <param name='data'>
  123. /// data - incoming as parameters, outgoing as result/error
  124. /// </param>
  125. /// <param name='method'>
  126. /// Json-rpc method to call.
  127. /// </param>
  128. /// <param name='uri'>
  129. /// URI of json-rpc service.
  130. /// </param>
  131. /// <param name='jsonId'>
  132. /// If set to <c>true</c> json identifier.
  133. /// </param>
  134. public bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
  135. {
  136. if (string.IsNullOrEmpty(jsonId))
  137. jsonId = UUID.Random().ToString();
  138. OSDMap request = new()
  139. {
  140. { "jsonrpc", OSD.FromString("2.0") },
  141. { "id", OSD.FromString(jsonId) },
  142. { "method", OSD.FromString(method) },
  143. { "params", data }
  144. };
  145. OSDMap outerresponse;
  146. try
  147. {
  148. outerresponse = WebUtil.PostToService(uri, request, 10000, true);
  149. }
  150. catch (Exception e)
  151. {
  152. m_log.Debug(string.Format("JsonRpc request '{0}' to {1} failed", method, uri), e);
  153. return false;
  154. }
  155. OSD osdtmp;
  156. if (!outerresponse.TryGetValue("_Result", out osdtmp) || (osdtmp is not OSDMap response))
  157. {
  158. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  159. method, uri, OSDParser.SerializeJsonString(outerresponse));
  160. return false;
  161. }
  162. if (response.TryGetValue("error", out osdtmp))
  163. {
  164. data = osdtmp;
  165. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
  166. method, uri, OSDParser.SerializeJsonString(osdtmp));
  167. return false;
  168. }
  169. data = response;
  170. return true;
  171. }
  172. }
  173. }