MainServer.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 OpenSimulator 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.Reflection;
  30. using System.Net;
  31. using System.Text;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. namespace OpenSim.Framework.Servers
  37. {
  38. public class MainServer
  39. {
  40. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private static BaseHttpServer instance = null;
  42. private static BaseHttpServer unsecureinstance = null;
  43. private static Dictionary<uint, BaseHttpServer> m_Servers = new Dictionary<uint, BaseHttpServer>();
  44. /// <summary>
  45. /// Control the printing of certain debug messages.
  46. /// </summary>
  47. /// <remarks>
  48. /// If DebugLevel >= 1 then short warnings are logged when receiving bad input data.
  49. /// If DebugLevel >= 2 then long warnings are logged when receiving bad input data.
  50. /// If DebugLevel >= 3 then short notices about all incoming non-poll HTTP requests are logged.
  51. /// If DebugLevel >= 4 then the time taken to fulfill the request is logged.
  52. /// If DebugLevel >= 5 then the start of the body of incoming non-poll HTTP requests will be logged.
  53. /// If DebugLevel >= 6 then the entire body of incoming non-poll HTTP requests will be logged.
  54. /// </remarks>
  55. public static int DebugLevel
  56. {
  57. get { return s_debugLevel; }
  58. set
  59. {
  60. s_debugLevel = value;
  61. lock (m_Servers)
  62. foreach (BaseHttpServer server in m_Servers.Values)
  63. server.DebugLevel = s_debugLevel;
  64. }
  65. }
  66. private static int s_debugLevel;
  67. /// <summary>
  68. /// Set the main HTTP server instance.
  69. /// </summary>
  70. /// <remarks>
  71. /// This will be used to register all handlers that listen to the default port.
  72. /// </remarks>
  73. /// <exception cref='Exception'>
  74. /// Thrown if the HTTP server has not already been registered via AddHttpServer()
  75. /// </exception>
  76. public static BaseHttpServer Instance
  77. {
  78. get { return instance; }
  79. set
  80. {
  81. lock (m_Servers)
  82. {
  83. if (!m_Servers.ContainsValue(value))
  84. throw new Exception("HTTP server must already have been registered to be set as the main instance");
  85. instance = value;
  86. }
  87. }
  88. }
  89. public static BaseHttpServer UnSecureInstance
  90. {
  91. get { return unsecureinstance; }
  92. set
  93. {
  94. lock (m_Servers)
  95. if (!m_Servers.ContainsValue(value))
  96. throw new Exception("HTTP server must already have been registered to be set as the main instance");
  97. unsecureinstance = value;
  98. }
  99. }
  100. /// <summary>
  101. /// Get all the registered servers.
  102. /// </summary>
  103. /// <remarks>
  104. /// Returns a copy of the dictionary so this can be iterated through without locking.
  105. /// </remarks>
  106. /// <value></value>
  107. public static Dictionary<uint, BaseHttpServer> Servers
  108. {
  109. get { return new Dictionary<uint, BaseHttpServer>(m_Servers); }
  110. }
  111. public static void RegisterHttpConsoleCommands(ICommandConsole console)
  112. {
  113. console.Commands.AddCommand(
  114. "Comms", false, "show http-handlers",
  115. "show http-handlers",
  116. "Show all registered http handlers", HandleShowHttpHandlersCommand);
  117. console.Commands.AddCommand(
  118. "Debug", false, "debug http", "debug http <in|out|all> [<level>]",
  119. "Turn on http request logging.",
  120. "If in or all and\n"
  121. + " level <= 0 then no extra logging is done.\n"
  122. + " level >= 1 then short warnings are logged when receiving bad input data.\n"
  123. + " level >= 2 then long warnings are logged when receiving bad input data.\n"
  124. + " level >= 3 then short notices about all incoming non-poll HTTP requests are logged.\n"
  125. + " level >= 4 then the time taken to fulfill the request is logged.\n"
  126. + " level >= 5 then a sample from the beginning of the data is logged.\n"
  127. + " level >= 6 then the entire data is logged.\n"
  128. + " no level is specified then the current level is returned.\n\n"
  129. + "If out or all and\n"
  130. + " level >= 3 then short notices about all outgoing requests going through WebUtil are logged.\n"
  131. + " level >= 4 then the time taken to fulfill the request is logged.\n"
  132. + " level >= 5 then a sample from the beginning of the data is logged.\n"
  133. + " level >= 6 then the entire data is logged.\n",
  134. HandleDebugHttpCommand);
  135. }
  136. /// <summary>
  137. /// Turn on some debugging values for OpenSim.
  138. /// </summary>
  139. /// <param name="args"></param>
  140. private static void HandleDebugHttpCommand(string module, string[] cmdparams)
  141. {
  142. if (cmdparams.Length < 3)
  143. {
  144. MainConsole.Instance.Output("Usage: debug http <in|out|all> 0..6");
  145. return;
  146. }
  147. bool inReqs = false;
  148. bool outReqs = false;
  149. bool allReqs = false;
  150. string subCommand = cmdparams[2];
  151. if (subCommand.ToLower() == "in")
  152. {
  153. inReqs = true;
  154. }
  155. else if (subCommand.ToLower() == "out")
  156. {
  157. outReqs = true;
  158. }
  159. else if (subCommand.ToLower() == "all")
  160. {
  161. allReqs = true;
  162. }
  163. else
  164. {
  165. MainConsole.Instance.Output("You must specify in, out or all");
  166. return;
  167. }
  168. if (cmdparams.Length >= 4)
  169. {
  170. string rawNewDebug = cmdparams[3];
  171. int newDebug;
  172. if (!int.TryParse(rawNewDebug, out newDebug))
  173. {
  174. MainConsole.Instance.Output("{0} is not a valid debug level", rawNewDebug);
  175. return;
  176. }
  177. if (newDebug < 0 || newDebug > 6)
  178. {
  179. MainConsole.Instance.Output("{0} is outside the valid debug level range of 0..6", newDebug);
  180. return;
  181. }
  182. if (allReqs || inReqs)
  183. {
  184. MainServer.DebugLevel = newDebug;
  185. MainConsole.Instance.Output("IN debug level set to {0}", newDebug);
  186. }
  187. if (allReqs || outReqs)
  188. {
  189. WebUtil.DebugLevel = newDebug;
  190. MainConsole.Instance.Output("OUT debug level set to {0}", newDebug);
  191. }
  192. }
  193. else
  194. {
  195. if (allReqs || inReqs)
  196. MainConsole.Instance.Output("Current IN debug level is {0}", DebugLevel);
  197. if (allReqs || outReqs)
  198. MainConsole.Instance.Output("Current OUT debug level is {0}", WebUtil.DebugLevel);
  199. }
  200. }
  201. private static void HandleShowHttpHandlersCommand(string module, string[] args)
  202. {
  203. if (args.Length != 2)
  204. {
  205. MainConsole.Instance.Output("Usage: show http-handlers");
  206. return;
  207. }
  208. StringBuilder handlers = new StringBuilder();
  209. lock (m_Servers)
  210. {
  211. foreach (BaseHttpServer httpServer in m_Servers.Values)
  212. {
  213. handlers.AppendFormat(
  214. "Registered HTTP Handlers for server at {0}:{1}\n", httpServer.ListenIPAddress, httpServer.Port);
  215. List<string> lst = httpServer.GetGLobalMethodsKeys();
  216. if (lst.Count > 0)
  217. {
  218. handlers.AppendFormat("* Global query methods ({0}):\n", lst.Count);
  219. foreach (string s in lst)
  220. handlers.AppendFormat("\t***:{0}\n", s);
  221. }
  222. lst = httpServer.GetXmlRpcHandlerKeys();
  223. if (lst.Count > 0)
  224. {
  225. handlers.AppendFormat("* XMLRPC methods ({0}):\n",lst.Count);
  226. foreach (string s in lst)
  227. handlers.AppendFormat("\t{0}\n", s);
  228. }
  229. lst = httpServer.GetJsonRpcHandlerKeys();
  230. if (lst.Count > 0)
  231. {
  232. handlers.AppendFormat("* JSONRPC methods ({0}):\n", lst.Count);
  233. foreach (string s in lst)
  234. handlers.AppendFormat("\t{0}\n", s);
  235. }
  236. lst = httpServer.GetIndexPHPHandlerKeys();
  237. if (lst.Count > 0)
  238. {
  239. handlers.AppendFormat("* index.php methods ({0}):\n", lst.Count);
  240. foreach (string s in lst)
  241. handlers.AppendFormat("\t{0}\n", s);
  242. }
  243. lst = httpServer.GetHTTPHandlerKeys();
  244. if (lst.Count > 0)
  245. {
  246. handlers.AppendFormat("* HTTP ({0}):\n", lst.Count);
  247. foreach (string s in lst)
  248. handlers.AppendFormat("\t{0}\n", s);
  249. }
  250. lst = httpServer.GetPollServiceHandlerKeys();
  251. if (lst.Count > 0)
  252. {
  253. handlers.AppendFormat("* HTTP poll ({0}):\n", lst.Count);
  254. foreach (string s in lst)
  255. handlers.AppendFormat("\t{0}\n", s);
  256. }
  257. lst = httpServer.GetLLSDHandlerKeys();
  258. if (lst.Count > 0)
  259. {
  260. handlers.AppendFormat("* LLSD ({0}):\n", lst.Count);
  261. foreach (string s in lst)
  262. handlers.AppendFormat("\t{0}\n", s);
  263. }
  264. lst = httpServer.GetStreamHandlerKeys();
  265. if (lst.Count > 0)
  266. {
  267. handlers.AppendFormat("* StreamHandlers ({0}):\n", lst.Count);
  268. foreach (string s in lst)
  269. handlers.AppendFormat("\t{0}\n", s);
  270. }
  271. lst = httpServer.GetSimpleStreamHandlerKeys();
  272. if (lst.Count > 0)
  273. {
  274. handlers.AppendFormat("* SimpleStreamHandlers ({0}):\n", lst.Count);
  275. foreach (string s in lst)
  276. handlers.AppendFormat("\t***:{0}\n", s);
  277. }
  278. handlers.Append("\n");
  279. }
  280. }
  281. MainConsole.Instance.Output(handlers.ToString());
  282. }
  283. /// <summary>
  284. /// Register an already started HTTP server to the collection of known servers.
  285. /// </summary>
  286. /// <param name='server'></param>
  287. public static void AddHttpServer(BaseHttpServer server)
  288. {
  289. lock (m_Servers)
  290. {
  291. if (m_Servers.ContainsKey(server.Port))
  292. throw new Exception(string.Format("HTTP server for port {0} already exists.", server.Port));
  293. m_Servers.Add(server.Port, server);
  294. }
  295. }
  296. /// <summary>
  297. /// Removes the http server listening on the given port.
  298. /// </summary>
  299. /// <remarks>
  300. /// It is the responsibility of the caller to do clean up.
  301. /// </remarks>
  302. /// <param name='port'></param>
  303. /// <returns></returns>
  304. public static bool RemoveHttpServer(uint port)
  305. {
  306. lock (m_Servers)
  307. {
  308. if (instance != null && instance.Port == port)
  309. instance = null;
  310. return m_Servers.Remove(port);
  311. }
  312. }
  313. /// <summary>
  314. /// Does this collection of servers contain one with the given port?
  315. /// </summary>
  316. /// <remarks>
  317. /// Unlike GetHttpServer, this will not instantiate a server if one does not exist on that port.
  318. /// </remarks>
  319. /// <param name='port'></param>
  320. /// <returns>true if a server with the given port is registered, false otherwise.</returns>
  321. public static bool ContainsHttpServer(uint port)
  322. {
  323. lock (m_Servers)
  324. return m_Servers.ContainsKey(port);
  325. }
  326. /// <summary>
  327. /// Get the default http server or an http server for a specific port.
  328. /// </summary>
  329. /// <remarks>
  330. /// If the requested HTTP server doesn't already exist then a new one is instantiated and started.
  331. /// </remarks>
  332. /// <returns></returns>
  333. /// <param name='port'>If 0 then the default HTTP server is returned.</param>
  334. public static IHttpServer GetHttpServer(uint port)
  335. {
  336. return GetHttpServer(port, null);
  337. }
  338. /// <summary>
  339. /// Get the default http server, an http server for a specific port
  340. /// and/or an http server bound to a specific address
  341. /// </summary>
  342. /// <remarks>
  343. /// If the requested HTTP server doesn't already exist then a new one is instantiated and started.
  344. /// </remarks>
  345. /// <returns></returns>
  346. /// <param name='port'>If 0 then the default HTTP server is returned.</param>
  347. /// <param name='ipaddr'>A specific IP address to bind to. If null then the default IP address is used.</param>
  348. public static IHttpServer GetHttpServer(uint port, IPAddress ipaddr)
  349. {
  350. if (port == 0)
  351. return Instance;
  352. if (instance != null && port == Instance.Port)
  353. return Instance;
  354. lock (m_Servers)
  355. {
  356. if (m_Servers.ContainsKey(port))
  357. return m_Servers[port];
  358. m_Servers[port] = new BaseHttpServer(port);
  359. if (ipaddr != null)
  360. m_Servers[port].ListenIPAddress = ipaddr;
  361. m_Servers[port].Start();
  362. return m_Servers[port];
  363. }
  364. }
  365. public static void Stop()
  366. {
  367. lock (m_Servers)
  368. {
  369. foreach (BaseHttpServer httpServer in m_Servers.Values)
  370. {
  371. httpServer.Stop(true);
  372. }
  373. }
  374. }
  375. }
  376. }