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