XmlRpcResponder.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. namespace Nwc.XmlRpc
  2. {
  3. using System;
  4. using System.Xml;
  5. using System.Net.Sockets;
  6. /// <summary>The class is a container of the context of an XML-RPC dialog on the server side.</summary>
  7. /// <remarks>Instances of this class maintain the context for an individual XML-RPC server
  8. /// side dialog. Namely they manage an inbound deserializer and an outbound serializer. </remarks>
  9. public class XmlRpcResponder
  10. {
  11. private XmlRpcRequestDeserializer _deserializer = new XmlRpcRequestDeserializer();
  12. private XmlRpcResponseSerializer _serializer = new XmlRpcResponseSerializer();
  13. private XmlRpcServer _server;
  14. private TcpClient _client;
  15. private SimpleHttpRequest _httpReq;
  16. /// <summary>The SimpleHttpRequest based on the TcpClient.</summary>
  17. public SimpleHttpRequest HttpReq
  18. {
  19. get { return _httpReq; }
  20. }
  21. /// <summary>Basic constructor.</summary>
  22. /// <param name="server">XmlRpcServer that this XmlRpcResponder services.</param>
  23. /// <param name="client">TcpClient with the connection.</param>
  24. public XmlRpcResponder(XmlRpcServer server, TcpClient client)
  25. {
  26. _server = server;
  27. _client = client;
  28. _httpReq = new SimpleHttpRequest(_client);
  29. }
  30. /// <summary>Call close to insure proper shutdown.</summary>
  31. ~XmlRpcResponder()
  32. {
  33. Close();
  34. }
  35. ///<summary>Respond using this responders HttpReq.</summary>
  36. public void Respond()
  37. {
  38. Respond(HttpReq);
  39. }
  40. /// <summary>Handle an HTTP request containing an XML-RPC request.</summary>
  41. /// <remarks>This method deserializes the XML-RPC request, invokes the
  42. /// described method, serializes the response (or fault) and sends the XML-RPC response
  43. /// back as a valid HTTP page.
  44. /// </remarks>
  45. /// <param name="httpReq"><c>SimpleHttpRequest</c> containing the request.</param>
  46. public void Respond(SimpleHttpRequest httpReq)
  47. {
  48. XmlRpcRequest xmlRpcReq = (XmlRpcRequest)_deserializer.Deserialize(httpReq.Input);
  49. XmlRpcResponse xmlRpcResp = new XmlRpcResponse();
  50. try
  51. {
  52. xmlRpcResp.Value = _server.Invoke(xmlRpcReq);
  53. }
  54. catch (XmlRpcException e)
  55. {
  56. xmlRpcResp.SetFault(e.FaultCode, e.FaultString);
  57. }
  58. catch (Exception e2)
  59. {
  60. xmlRpcResp.SetFault(XmlRpcErrorCodes.APPLICATION_ERROR,
  61. XmlRpcErrorCodes.APPLICATION_ERROR_MSG + ": " + e2.Message);
  62. }
  63. if (Logger.Delegate != null)
  64. Logger.WriteEntry(xmlRpcResp.ToString(), LogLevel.Information);
  65. XmlRpcServer.HttpHeader(httpReq.Protocol, "text/xml", 0, " 200 OK", httpReq.Output);
  66. httpReq.Output.Flush();
  67. XmlTextWriter xml = new XmlTextWriter(httpReq.Output);
  68. _serializer.Serialize(xml, xmlRpcResp);
  69. xml.Flush();
  70. httpReq.Output.Flush();
  71. }
  72. ///<summary>Close all contained resources, both the HttpReq and client.</summary>
  73. public void Close()
  74. {
  75. if (_httpReq != null)
  76. {
  77. _httpReq.Close();
  78. _httpReq = null;
  79. }
  80. if (_client != null)
  81. {
  82. _client.Close();
  83. _client = null;
  84. }
  85. }
  86. }
  87. }