XmlRpcResponder.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. */
  28. namespace Nwc.XmlRpc
  29. {
  30. using System;
  31. using System.Xml;
  32. using System.Net.Sockets;
  33. /// <summary>The class is a container of the context of an XML-RPC dialog on the server side.</summary>
  34. /// <remarks>Instances of this class maintain the context for an individual XML-RPC server
  35. /// side dialog. Namely they manage an inbound deserializer and an outbound serializer. </remarks>
  36. public class XmlRpcResponder
  37. {
  38. private XmlRpcRequestDeserializer _deserializer = new XmlRpcRequestDeserializer();
  39. private XmlRpcResponseSerializer _serializer = new XmlRpcResponseSerializer();
  40. private XmlRpcServer _server;
  41. private TcpClient _client;
  42. private SimpleHttpRequest _httpReq;
  43. /// <summary>The SimpleHttpRequest based on the TcpClient.</summary>
  44. public SimpleHttpRequest HttpReq
  45. {
  46. get { return _httpReq; }
  47. }
  48. /// <summary>Basic constructor.</summary>
  49. /// <param name="server">XmlRpcServer that this XmlRpcResponder services.</param>
  50. /// <param name="client">TcpClient with the connection.</param>
  51. public XmlRpcResponder(XmlRpcServer server, TcpClient client)
  52. {
  53. _server = server;
  54. _client = client;
  55. _httpReq = new SimpleHttpRequest(_client);
  56. }
  57. /// <summary>Call close to insure proper shutdown.</summary>
  58. ~XmlRpcResponder()
  59. {
  60. Close();
  61. }
  62. ///<summary>Respond using this responders HttpReq.</summary>
  63. public void Respond()
  64. {
  65. Respond(HttpReq);
  66. }
  67. /// <summary>Handle an HTTP request containing an XML-RPC request.</summary>
  68. /// <remarks>This method deserializes the XML-RPC request, invokes the
  69. /// described method, serializes the response (or fault) and sends the XML-RPC response
  70. /// back as a valid HTTP page.
  71. /// </remarks>
  72. /// <param name="httpReq"><c>SimpleHttpRequest</c> containing the request.</param>
  73. public void Respond(SimpleHttpRequest httpReq)
  74. {
  75. XmlRpcRequest xmlRpcReq = (XmlRpcRequest)_deserializer.Deserialize(httpReq.Input);
  76. XmlRpcResponse xmlRpcResp = new XmlRpcResponse();
  77. try
  78. {
  79. xmlRpcResp.Value = _server.Invoke(xmlRpcReq);
  80. }
  81. catch (XmlRpcException e)
  82. {
  83. xmlRpcResp.SetFault(e.FaultCode, e.FaultString);
  84. }
  85. catch (Exception e2)
  86. {
  87. xmlRpcResp.SetFault(XmlRpcErrorCodes.APPLICATION_ERROR,
  88. XmlRpcErrorCodes.APPLICATION_ERROR_MSG + ": " + e2.Message);
  89. }
  90. if (Logger.Delegate != null)
  91. Logger.WriteEntry(xmlRpcResp.ToString(), LogLevel.Information);
  92. XmlRpcServer.HttpHeader(httpReq.Protocol, "text/xml", 0, " 200 OK", httpReq.Output);
  93. httpReq.Output.Flush();
  94. XmlTextWriter xml = new XmlTextWriter(httpReq.Output);
  95. _serializer.Serialize(xml, xmlRpcResp);
  96. xml.Flush();
  97. httpReq.Output.Flush();
  98. }
  99. ///<summary>Close all contained resources, both the HttpReq and client.</summary>
  100. public void Close()
  101. {
  102. if (_httpReq != null)
  103. {
  104. _httpReq.Close();
  105. _httpReq = null;
  106. }
  107. if (_client != null)
  108. {
  109. _client.Close();
  110. _client = null;
  111. }
  112. }
  113. }
  114. }