OSHttpXmlRpcHandler.cs 7.4 KB

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