1
0

OSHttpXmlRpcHandler.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.Text.RegularExpressions;
  34. using System.Xml;
  35. using log4net;
  36. using Nwc.XmlRpc;
  37. namespace OpenSim.Framework.Servers.HttpServer
  38. {
  39. public delegate XmlRpcResponse OSHttpXmlRpcProcessor(XmlRpcRequest request);
  40. public class OSHttpXmlRpcHandler: OSHttpHandler
  41. {
  42. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// XmlRpcMethodMatch tries to reify (deserialize) an incoming
  45. /// XmlRpc request (and posts it to the "whiteboard") and
  46. /// checks whether the method name is one we are interested
  47. /// in.
  48. /// </summary>
  49. /// <returns>true if the handler is interested in the content;
  50. /// false otherwise</returns>
  51. protected bool XmlRpcMethodMatch(OSHttpRequest req)
  52. {
  53. XmlRpcRequest xmlRpcRequest = null;
  54. // check whether req is already reified
  55. // if not: reify (and post to whiteboard)
  56. try
  57. {
  58. if (req.Whiteboard.ContainsKey("xmlrequest"))
  59. {
  60. xmlRpcRequest = req.Whiteboard["xmlrequest"] as XmlRpcRequest;
  61. }
  62. else
  63. {
  64. StreamReader body = new StreamReader(req.InputStream);
  65. string requestBody = body.ReadToEnd();
  66. xmlRpcRequest = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody);
  67. req.Whiteboard["xmlrequest"] = xmlRpcRequest;
  68. }
  69. }
  70. catch (XmlException)
  71. {
  72. _log.ErrorFormat("[OSHttpXmlRpcHandler] failed to deserialize XmlRpcRequest from {0}", req.ToString());
  73. return false;
  74. }
  75. // check against methodName
  76. if ((null != xmlRpcRequest)
  77. && !String.IsNullOrEmpty(xmlRpcRequest.MethodName)
  78. && xmlRpcRequest.MethodName == _methodName)
  79. {
  80. _log.DebugFormat("[OSHttpXmlRpcHandler] located handler {0} for {1}", _methodName, req.ToString());
  81. return true;
  82. }
  83. return false;
  84. }
  85. // contains handler for processing XmlRpc Request
  86. private XmlRpcMethod _handler;
  87. // contains XmlRpc method name
  88. private string _methodName;
  89. /// <summary>
  90. /// Instantiate an XmlRpc handler.
  91. /// </summary>
  92. /// <param name="handler">XmlRpcMethod
  93. /// delegate</param>
  94. /// <param name="methodName">XmlRpc method name</param>
  95. /// <param name="path">XmlRpc path prefix (regular expression)</param>
  96. /// <param name="headers">Dictionary with header names and
  97. /// regular expressions to match content of headers</param>
  98. /// <param name="whitelist">IP whitelist of remote end points
  99. /// to accept (regular expression)</param>
  100. /// <remarks>
  101. /// Except for handler and methodName, all other parameters
  102. /// can be null, in which case they are not taken into account
  103. /// when the handler is being looked up.
  104. /// </remarks>
  105. public OSHttpXmlRpcHandler(XmlRpcMethod handler, string methodName, Regex path,
  106. Dictionary<string, Regex> headers, Regex whitelist)
  107. : base(new Regex(@"^POST$", RegexOptions.IgnoreCase | RegexOptions.Compiled), path, null, headers,
  108. new Regex(@"^(text|application)/xml", RegexOptions.IgnoreCase | RegexOptions.Compiled),
  109. whitelist)
  110. {
  111. _handler = handler;
  112. _methodName = methodName;
  113. }
  114. /// <summary>
  115. /// Instantiate an XmlRpc handler.
  116. /// </summary>
  117. /// <param name="handler">XmlRpcMethod
  118. /// delegate</param>
  119. /// <param name="methodName">XmlRpc method name</param>
  120. public OSHttpXmlRpcHandler(XmlRpcMethod handler, string methodName)
  121. : this(handler, methodName, null, null, null)
  122. {
  123. }
  124. /// <summary>
  125. /// Invoked by OSHttpRequestPump.
  126. /// </summary>
  127. public override OSHttpHandlerResult Process(OSHttpRequest request)
  128. {
  129. XmlRpcResponse xmlRpcResponse;
  130. string responseString;
  131. // check whether we are interested in this request
  132. if (!XmlRpcMethodMatch(request)) return OSHttpHandlerResult.Pass;
  133. OSHttpResponse resp = new OSHttpResponse(request);
  134. try
  135. {
  136. // reified XmlRpcRequest must still be on the whiteboard
  137. XmlRpcRequest xmlRpcRequest = request.Whiteboard["xmlrequest"] as XmlRpcRequest;
  138. xmlRpcResponse = _handler(xmlRpcRequest);
  139. responseString = XmlRpcResponseSerializer.Singleton.Serialize(xmlRpcResponse);
  140. resp.ContentType = "text/xml";
  141. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  142. resp.SendChunked = false;
  143. resp.ContentLength = buffer.Length;
  144. resp.ContentEncoding = Encoding.UTF8;
  145. resp.Body.Write(buffer, 0, buffer.Length);
  146. resp.Body.Flush();
  147. resp.Send();
  148. }
  149. catch (Exception ex)
  150. {
  151. _log.WarnFormat("[OSHttpXmlRpcHandler]: Error: {0}", ex.Message);
  152. return OSHttpHandlerResult.Pass;
  153. }
  154. return OSHttpHandlerResult.Done;
  155. }
  156. }
  157. }