aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Common/XmlRpcCS/XmlRpcServer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Common/XmlRpcCS/XmlRpcServer.cs')
-rw-r--r--Common/XmlRpcCS/XmlRpcServer.cs266
1 files changed, 0 insertions, 266 deletions
diff --git a/Common/XmlRpcCS/XmlRpcServer.cs b/Common/XmlRpcCS/XmlRpcServer.cs
deleted file mode 100644
index 6d1f33a..0000000
--- a/Common/XmlRpcCS/XmlRpcServer.cs
+++ /dev/null
@@ -1,266 +0,0 @@
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*/
28namespace Nwc.XmlRpc
29{
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.Net;
34 using System.Net.Sockets;
35 using System.Text;
36 using System.Threading;
37 using System.Xml;
38
39 /// <summary>A restricted HTTP server for use with XML-RPC.</summary>
40 /// <remarks>It only handles POST requests, and only POSTs representing XML-RPC calls.
41 /// In addition to dispatching requests it also provides a registry for request handlers.
42 /// </remarks>
43 public class XmlRpcServer : IEnumerable
44 {
45#pragma warning disable 0414 // disable "private field assigned but not used"
46 const int RESPONDER_COUNT = 10;
47 private TcpListener _myListener;
48 private int _port;
49 private IPAddress _address;
50 private IDictionary _handlers;
51 private XmlRpcSystemObject _system;
52 private WaitCallback _wc;
53#pragma warning restore 0414
54
55 ///<summary>Constructor with port and address.</summary>
56 ///<remarks>This constructor sets up a TcpListener listening on the
57 ///given port and address. It also calls a Thread on the method StartListen().</remarks>
58 ///<param name="address"><c>IPAddress</c> value of the address to listen on.</param>
59 ///<param name="port"><c>Int</c> value of the port to listen on.</param>
60 public XmlRpcServer(IPAddress address, int port)
61 {
62 _port = port;
63 _address = address;
64 _handlers = new Hashtable();
65 _system = new XmlRpcSystemObject(this);
66 _wc = new WaitCallback(WaitCallback);
67 }
68
69 ///<summary>Basic constructor.</summary>
70 ///<remarks>This constructor sets up a TcpListener listening on the
71 ///given port. It also calls a Thread on the method StartListen(). IPAddress.Any
72 ///is assumed as the address here.</remarks>
73 ///<param name="port"><c>Int</c> value of the port to listen on.</param>
74 public XmlRpcServer(int port) : this(IPAddress.Any, port) { }
75
76 /// <summary>Start the server.</summary>
77 public void Start()
78 {
79 try
80 {
81 Stop();
82 //start listing on the given port
83 // IPAddress addr = IPAddress.Parse("127.0.0.1");
84 lock (this)
85 {
86 _myListener = new TcpListener(IPAddress.Any, _port);
87 _myListener.Start();
88 //start the thread which calls the method 'StartListen'
89 Thread th = new Thread(new ThreadStart(StartListen));
90 th.Start();
91 }
92 }
93 catch (Exception e)
94 {
95 Logger.WriteEntry("An Exception Occurred while Listening :" + e.ToString(), LogLevel.Error);
96 }
97 }
98
99 /// <summary>Stop the server.</summary>
100 public void Stop()
101 {
102 try
103 {
104 if (_myListener != null)
105 {
106 lock (this)
107 {
108 _myListener.Stop();
109 _myListener = null;
110 }
111 }
112 }
113 catch (Exception e)
114 {
115 Logger.WriteEntry("An Exception Occurred while stopping :" +
116 e.ToString(), LogLevel.Error);
117 }
118 }
119
120 /// <summary>Get an enumeration of my XML-RPC handlers.</summary>
121 /// <returns><c>IEnumerable</c> the handler enumeration.</returns>
122 public IEnumerator GetEnumerator()
123 {
124 return _handlers.GetEnumerator();
125 }
126
127 /// <summary>Retrieve a handler by name.</summary>
128 /// <param name="name"><c>String</c> naming a handler</param>
129 /// <returns><c>Object</c> that is the handler.</returns>
130 public Object this[String name]
131 {
132 get { return _handlers[name]; }
133 }
134
135 ///<summary>
136 ///This method Accepts new connections and dispatches them when appropriate.
137 ///</summary>
138 public void StartListen()
139 {
140 while (true && _myListener != null)
141 {
142 //Accept a new connection
143 XmlRpcResponder responder = new XmlRpcResponder(this, _myListener.AcceptTcpClient());
144 ThreadPool.QueueUserWorkItem(_wc, responder);
145 }
146 }
147
148
149 ///<summary>
150 ///Add an XML-RPC handler object by name.
151 ///</summary>
152 ///<param name="name"><c>String</c> XML-RPC dispatch name of this object.</param>
153 ///<param name="obj"><c>Object</c> The object that is the XML-RPC handler.</param>
154 public void Add(String name, Object obj)
155 {
156 _handlers.Add(name, obj);
157 }
158
159 ///<summary>Return a C# object.method name for and XML-RPC object.method name pair.</summary>
160 ///<param name="methodName">The XML-RPC object.method.</param>
161 ///<returns><c>String</c> of form object.method for the underlying C# method.</returns>
162 public String MethodName(String methodName)
163 {
164 int dotAt = methodName.LastIndexOf('.');
165
166 if (dotAt == -1)
167 {
168 throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
169 XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": Bad method name " + methodName);
170 }
171
172 String objectName = methodName.Substring(0, dotAt);
173 Object target = _handlers[objectName];
174
175 if (target == null)
176 {
177 throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
178 XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": Object " + objectName + " not found");
179 }
180
181 return target.GetType().FullName + "." + methodName.Substring(dotAt + 1);
182 }
183
184 ///<summary>Invoke a method described in a request.</summary>
185 ///<param name="req"><c>XmlRpcRequest</c> containing a method descriptions.</param>
186 /// <seealso cref="XmlRpcSystemObject.Invoke"/>
187 /// <seealso cref="XmlRpcServer.Invoke(String,String,IList)"/>
188 public Object Invoke(XmlRpcRequest req)
189 {
190 return Invoke(req.MethodNameObject, req.MethodNameMethod, req.Params);
191 }
192
193 ///<summary>Invoke a method on a named handler.</summary>
194 ///<param name="objectName"><c>String</c> The name of the handler.</param>
195 ///<param name="methodName"><c>String</c> The name of the method to invoke on the handler.</param>
196 ///<param name="parameters"><c>IList</c> The parameters to invoke the method with.</param>
197 /// <seealso cref="XmlRpcSystemObject.Invoke"/>
198 public Object Invoke(String objectName, String methodName, IList parameters)
199 {
200 Object target = _handlers[objectName];
201
202 if (target == null)
203 {
204 throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
205 XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": Object " + objectName + " not found");
206 }
207
208 return XmlRpcSystemObject.Invoke(target, methodName, parameters);
209 }
210
211 /// <summary>The method the thread pool invokes when a thread is available to handle an HTTP request.</summary>
212 /// <param name="responder">TcpClient from the socket accept.</param>
213 public void WaitCallback(object responder)
214 {
215 XmlRpcResponder resp = (XmlRpcResponder)responder;
216
217 if (resp.HttpReq.HttpMethod == "POST")
218 {
219 try
220 {
221 resp.Respond();
222 }
223 catch (Exception e)
224 {
225 Logger.WriteEntry("Failed on post: " + e, LogLevel.Error);
226 }
227 }
228 else
229 {
230 Logger.WriteEntry("Only POST methods are supported: " + resp.HttpReq.HttpMethod +
231 " ignored", LogLevel.Error);
232 }
233
234 resp.Close();
235 }
236
237 /// <summary>
238 /// This function send the Header Information to the client (Browser)
239 /// </summary>
240 /// <param name="sHttpVersion">HTTP Version</param>
241 /// <param name="sMIMEHeader">Mime Type</param>
242 /// <param name="iTotBytes">Total Bytes to be sent in the body</param>
243 /// <param name="sStatusCode"></param>
244 /// <param name="output">Socket reference</param>
245 static public void HttpHeader(string sHttpVersion, string sMIMEHeader, long iTotBytes, string sStatusCode, TextWriter output)
246 {
247 String sBuffer = "";
248
249 // if Mime type is not provided set default to text/html
250 if (sMIMEHeader.Length == 0)
251 {
252 sMIMEHeader = "text/html"; // Default Mime Type is text/html
253 }
254
255 sBuffer += sHttpVersion + sStatusCode + "\r\n";
256 sBuffer += "Connection: close\r\n";
257 if (iTotBytes > 0)
258 sBuffer += "Content-Length: " + iTotBytes + "\r\n";
259 sBuffer += "Server: XmlRpcServer \r\n";
260 sBuffer += "Content-Type: " + sMIMEHeader + "\r\n";
261 sBuffer += "\r\n";
262
263 output.Write(sBuffer);
264 }
265 }
266}