MainServer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 Dictionary<uint, BaseHttpServer> m_Servers = new Dictionary<uint, BaseHttpServer>();
  43. /// <summary>
  44. /// Control the printing of certain debug messages.
  45. /// </summary>
  46. /// <remarks>
  47. /// If DebugLevel >= 1 then short warnings are logged when receiving bad input data.
  48. /// If DebugLevel >= 2 then long warnings are logged when receiving bad input data.
  49. /// If DebugLevel >= 3 then short notices about all incoming non-poll HTTP requests are logged.
  50. /// If DebugLevel >= 4 then the time taken to fulfill the request is logged.
  51. /// If DebugLevel >= 5 then the start of the body of incoming non-poll HTTP requests will be logged.
  52. /// If DebugLevel >= 6 then the entire body of incoming non-poll HTTP requests will be logged.
  53. /// </remarks>
  54. public static int DebugLevel
  55. {
  56. get { return s_debugLevel; }
  57. set
  58. {
  59. s_debugLevel = value;
  60. lock (m_Servers)
  61. foreach (BaseHttpServer server in m_Servers.Values)
  62. server.DebugLevel = s_debugLevel;
  63. }
  64. }
  65. private static int s_debugLevel;
  66. /// <summary>
  67. /// Set the main HTTP server instance.
  68. /// </summary>
  69. /// <remarks>
  70. /// This will be used to register all handlers that listen to the default port.
  71. /// </remarks>
  72. /// <exception cref='Exception'>
  73. /// Thrown if the HTTP server has not already been registered via AddHttpServer()
  74. /// </exception>
  75. public static BaseHttpServer Instance
  76. {
  77. get { return instance; }
  78. set
  79. {
  80. lock (m_Servers)
  81. if (!m_Servers.ContainsValue(value))
  82. throw new Exception("HTTP server must already have been registered to be set as the main instance");
  83. instance = value;
  84. }
  85. }
  86. /// <summary>
  87. /// Get all the registered servers.
  88. /// </summary>
  89. /// <remarks>
  90. /// Returns a copy of the dictionary so this can be iterated through without locking.
  91. /// </remarks>
  92. /// <value></value>
  93. public static Dictionary<uint, BaseHttpServer> Servers
  94. {
  95. get { return new Dictionary<uint, BaseHttpServer>(m_Servers); }
  96. }
  97. public static void RegisterHttpConsoleCommands(ICommandConsole console)
  98. {
  99. console.Commands.AddCommand(
  100. "Comms", false, "show http-handlers",
  101. "show http-handlers",
  102. "Show all registered http handlers", HandleShowHttpHandlersCommand);
  103. console.Commands.AddCommand(
  104. "Debug", false, "debug http", "debug http <in|out|all> [<level>]",
  105. "Turn on http request logging.",
  106. "If in or all and\n"
  107. + " level <= 0 then no extra logging is done.\n"
  108. + " level >= 1 then short warnings are logged when receiving bad input data.\n"
  109. + " level >= 2 then long warnings are logged when receiving bad input data.\n"
  110. + " level >= 3 then short notices about all incoming non-poll HTTP requests are logged.\n"
  111. + " level >= 4 then the time taken to fulfill the request is logged.\n"
  112. + " level >= 5 then a sample from the beginning of the incoming data is logged.\n"
  113. + " level >= 6 then the entire incoming data is logged.\n"
  114. + " no level is specified then the current level is returned.\n\n"
  115. + "If out or all and\n"
  116. + " level >= 3 then short notices about all outgoing requests going through WebUtil are logged.\n"
  117. + " level >= 4 then the time taken to fulfill the request is logged.\n",
  118. HandleDebugHttpCommand);
  119. }
  120. /// <summary>
  121. /// Turn on some debugging values for OpenSim.
  122. /// </summary>
  123. /// <param name="args"></param>
  124. private static void HandleDebugHttpCommand(string module, string[] cmdparams)
  125. {
  126. if (cmdparams.Length < 3)
  127. {
  128. MainConsole.Instance.Output("Usage: debug http <in|out|all> 0..6");
  129. return;
  130. }
  131. bool inReqs = false;
  132. bool outReqs = false;
  133. bool allReqs = false;
  134. string subCommand = cmdparams[2];
  135. if (subCommand.ToLower() == "in")
  136. {
  137. inReqs = true;
  138. }
  139. else if (subCommand.ToLower() == "out")
  140. {
  141. outReqs = true;
  142. }
  143. else if (subCommand.ToLower() == "all")
  144. {
  145. allReqs = true;
  146. }
  147. else
  148. {
  149. MainConsole.Instance.Output("You must specify in, out or all");
  150. return;
  151. }
  152. if (cmdparams.Length >= 4)
  153. {
  154. string rawNewDebug = cmdparams[3];
  155. int newDebug;
  156. if (!int.TryParse(rawNewDebug, out newDebug))
  157. {
  158. MainConsole.Instance.OutputFormat("{0} is not a valid debug level", rawNewDebug);
  159. return;
  160. }
  161. if (newDebug < 0 || newDebug > 6)
  162. {
  163. MainConsole.Instance.OutputFormat("{0} is outside the valid debug level range of 0..6", newDebug);
  164. return;
  165. }
  166. if (allReqs || inReqs)
  167. {
  168. MainServer.DebugLevel = newDebug;
  169. MainConsole.Instance.OutputFormat("IN debug level set to {0}", newDebug);
  170. }
  171. if (allReqs || outReqs)
  172. {
  173. WebUtil.DebugLevel = newDebug;
  174. MainConsole.Instance.OutputFormat("OUT debug level set to {0}", newDebug);
  175. }
  176. }
  177. else
  178. {
  179. if (allReqs || inReqs)
  180. MainConsole.Instance.OutputFormat("Current IN debug level is {0}", MainServer.DebugLevel);
  181. if (allReqs || outReqs)
  182. MainConsole.Instance.OutputFormat("Current OUT debug level is {0}", WebUtil.DebugLevel);
  183. }
  184. }
  185. private static void HandleShowHttpHandlersCommand(string module, string[] args)
  186. {
  187. if (args.Length != 2)
  188. {
  189. MainConsole.Instance.Output("Usage: show http-handlers");
  190. return;
  191. }
  192. StringBuilder handlers = new StringBuilder();
  193. lock (m_Servers)
  194. {
  195. foreach (BaseHttpServer httpServer in m_Servers.Values)
  196. {
  197. handlers.AppendFormat(
  198. "Registered HTTP Handlers for server at {0}:{1}\n", httpServer.ListenIPAddress, httpServer.Port);
  199. handlers.AppendFormat("* XMLRPC:\n");
  200. foreach (String s in httpServer.GetXmlRpcHandlerKeys())
  201. handlers.AppendFormat("\t{0}\n", s);
  202. handlers.AppendFormat("* HTTP:\n");
  203. List<String> poll = httpServer.GetPollServiceHandlerKeys();
  204. foreach (String s in httpServer.GetHTTPHandlerKeys())
  205. handlers.AppendFormat("\t{0} {1}\n", s, (poll.Contains(s) ? "(poll service)" : string.Empty));
  206. // handlers.AppendFormat("* Agent:\n");
  207. // foreach (String s in httpServer.GetAgentHandlerKeys())
  208. // handlers.AppendFormat("\t{0}\n", s);
  209. handlers.AppendFormat("* LLSD:\n");
  210. foreach (String s in httpServer.GetLLSDHandlerKeys())
  211. handlers.AppendFormat("\t{0}\n", s);
  212. handlers.AppendFormat("* StreamHandlers ({0}):\n", httpServer.GetStreamHandlerKeys().Count);
  213. foreach (String s in httpServer.GetStreamHandlerKeys())
  214. handlers.AppendFormat("\t{0}\n", s);
  215. handlers.Append("\n");
  216. }
  217. }
  218. MainConsole.Instance.Output(handlers.ToString());
  219. }
  220. /// <summary>
  221. /// Register an already started HTTP server to the collection of known servers.
  222. /// </summary>
  223. /// <param name='server'></param>
  224. public static void AddHttpServer(BaseHttpServer server)
  225. {
  226. lock (m_Servers)
  227. {
  228. if (m_Servers.ContainsKey(server.Port))
  229. throw new Exception(string.Format("HTTP server for port {0} already exists.", server.Port));
  230. m_Servers.Add(server.Port, server);
  231. }
  232. }
  233. /// <summary>
  234. /// Removes the http server listening on the given port.
  235. /// </summary>
  236. /// <remarks>
  237. /// It is the responsibility of the caller to do clean up.
  238. /// </remarks>
  239. /// <param name='port'></param>
  240. /// <returns></returns>
  241. public static bool RemoveHttpServer(uint port)
  242. {
  243. lock (m_Servers)
  244. return m_Servers.Remove(port);
  245. }
  246. /// <summary>
  247. /// Does this collection of servers contain one with the given port?
  248. /// </summary>
  249. /// <remarks>
  250. /// Unlike GetHttpServer, this will not instantiate a server if one does not exist on that port.
  251. /// </remarks>
  252. /// <param name='port'></param>
  253. /// <returns>true if a server with the given port is registered, false otherwise.</returns>
  254. public static bool ContainsHttpServer(uint port)
  255. {
  256. lock (m_Servers)
  257. return m_Servers.ContainsKey(port);
  258. }
  259. /// <summary>
  260. /// Get the default http server or an http server for a specific port.
  261. /// </summary>
  262. /// <remarks>
  263. /// If the requested HTTP server doesn't already exist then a new one is instantiated and started.
  264. /// </remarks>
  265. /// <returns></returns>
  266. /// <param name='port'>If 0 then the default HTTP server is returned.</param>
  267. public static IHttpServer GetHttpServer(uint port)
  268. {
  269. return GetHttpServer(port, null);
  270. }
  271. /// <summary>
  272. /// Get the default http server, an http server for a specific port
  273. /// and/or an http server bound to a specific address
  274. /// </summary>
  275. /// <remarks>
  276. /// If the requested HTTP server doesn't already exist then a new one is instantiated and started.
  277. /// </remarks>
  278. /// <returns></returns>
  279. /// <param name='port'>If 0 then the default HTTP server is returned.</param>
  280. /// <param name='ipaddr'>A specific IP address to bind to. If null then the default IP address is used.</param>
  281. public static IHttpServer GetHttpServer(uint port, IPAddress ipaddr)
  282. {
  283. if (port == 0)
  284. return Instance;
  285. if (instance != null && port == Instance.Port)
  286. return Instance;
  287. lock (m_Servers)
  288. {
  289. if (m_Servers.ContainsKey(port))
  290. return m_Servers[port];
  291. m_Servers[port] = new BaseHttpServer(port);
  292. if (ipaddr != null)
  293. m_Servers[port].ListenIPAddress = ipaddr;
  294. m_Servers[port].Start();
  295. return m_Servers[port];
  296. }
  297. }
  298. }
  299. }