diff --git a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs index 31af44cd8e..1d5b4268c2 100644 --- a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs @@ -53,6 +53,8 @@ namespace OpenSim.Framework.Servers.HttpServer private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private HttpServerLogWriter httpserverlog = new HttpServerLogWriter(); + public int DebugLevel { get; set; } + private volatile int NotSocketErrors = 0; public volatile bool HTTPDRunning = false; @@ -79,11 +81,6 @@ namespace OpenSim.Framework.Servers.HttpServer private PollServiceRequestManager m_PollServiceManager; - /// - /// Control the printing of certain debug messages. - /// - public int DebugLevel { get; set; } - public uint SSLPort { get { return m_sslport; } @@ -450,7 +447,7 @@ namespace OpenSim.Framework.Servers.HttpServer if (TryGetStreamHandler(handlerKey, out requestHandler)) { - if (DebugLevel >= 1) + if (DebugLevel >= 3) m_log.DebugFormat( "[BASE HTTP SERVER]: Found stream handler for {0} {1} {2} {3}", request.HttpMethod, request.Url.PathAndQuery, requestHandler.Name, requestHandler.Description); @@ -531,7 +528,7 @@ namespace OpenSim.Framework.Servers.HttpServer case null: case "text/html": - if (DebugLevel >= 1) + if (DebugLevel >= 3) m_log.DebugFormat( "[BASE HTTP SERVER]: Found a {0} content type handler for {1} {2}", request.ContentType, request.HttpMethod, request.Url.PathAndQuery); @@ -543,7 +540,7 @@ namespace OpenSim.Framework.Servers.HttpServer case "application/xml+llsd": case "application/llsd+json": - if (DebugLevel >= 1) + if (DebugLevel >= 3) m_log.DebugFormat( "[BASE HTTP SERVER]: Found a {0} content type handler for {1} {2}", request.ContentType, request.HttpMethod, request.Url.PathAndQuery); @@ -564,7 +561,7 @@ namespace OpenSim.Framework.Servers.HttpServer //m_log.Info("[Debug BASE HTTP SERVER]: Checking for LLSD Handler"); if (DoWeHaveALLSDHandler(request.RawUrl)) { - if (DebugLevel >= 1) + if (DebugLevel >= 3) m_log.DebugFormat( "[BASE HTTP SERVER]: Found a {0} content type handler for {1} {2}", request.ContentType, request.HttpMethod, request.Url.PathAndQuery); @@ -574,7 +571,7 @@ namespace OpenSim.Framework.Servers.HttpServer // m_log.DebugFormat("[BASE HTTP SERVER]: Checking for HTTP Handler for request {0}", request.RawUrl); else if (DoWeHaveAHTTPHandler(request.RawUrl)) { - if (DebugLevel >= 1) + if (DebugLevel >= 3) m_log.DebugFormat( "[BASE HTTP SERVER]: Found a {0} content type handler for {1} {2}", request.ContentType, request.HttpMethod, request.Url.PathAndQuery); @@ -583,8 +580,7 @@ namespace OpenSim.Framework.Servers.HttpServer } else { - - if (DebugLevel >= 1) + if (DebugLevel >= 3) m_log.DebugFormat( "[BASE HTTP SERVER]: Assuming a generic XMLRPC request for {0} {1}", request.HttpMethod, request.Url.PathAndQuery); @@ -793,8 +789,23 @@ namespace OpenSim.Framework.Servers.HttpServer { xmlRprcRequest = (XmlRpcRequest) (new XmlRpcRequestDeserializer()).Deserialize(requestBody); } - catch (XmlException) + catch (XmlException e) { + if (DebugLevel >= 1) + { + if (DebugLevel >= 2) + m_log.Warn( + string.Format( + "[BASE HTTP SERVER]: Got XMLRPC request with invalid XML from {0}. XML was '{1}'. Sending blank response. Exception ", + request.RemoteIPEndPoint, requestBody), + e); + else + { + m_log.WarnFormat( + "[BASE HTTP SERVER]: Got XMLRPC request with invalid XML from {0}, length {1}. Sending blank response.", + request.RemoteIPEndPoint, requestBody.Length); + } + } } if (xmlRprcRequest != null) @@ -1570,6 +1581,9 @@ namespace OpenSim.Framework.Servers.HttpServer private void StartHTTP() { + m_log.InfoFormat( + "[BASE HTTP SERVER]: Starting {0} server on port {1}", UseSSL ? "HTTPS" : "HTTP", Port); + try { //m_httpListener = new HttpListener(); diff --git a/OpenSim/Framework/Servers/MainServer.cs b/OpenSim/Framework/Servers/MainServer.cs index b8ab8d9b26..07ff60c326 100644 --- a/OpenSim/Framework/Servers/MainServer.cs +++ b/OpenSim/Framework/Servers/MainServer.cs @@ -25,57 +25,209 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +using System; using System.Collections.Generic; using System.Reflection; using System.Net; using log4net; +using OpenSim.Framework; +using OpenSim.Framework.Console; using OpenSim.Framework.Servers.HttpServer; namespace OpenSim.Framework.Servers { public class MainServer { - private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); +// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static BaseHttpServer instance = null; - private static Dictionary m_Servers = - new Dictionary(); + private static Dictionary m_Servers = new Dictionary(); + /// + /// Control the printing of certain debug messages. + /// + /// + /// If DebugLevel >= 1, then short warnings are logged when receiving bad input data. + /// If DebugLevel >= 2, then long warnings are logged when receiving bad input data. + /// If DebugLevel >= 3, then short notices about all incoming non-poll HTTP requests are logged. + /// + public static int DebugLevel + { + get { return s_debugLevel; } + set + { + s_debugLevel = value; + + lock (m_Servers) + foreach (BaseHttpServer server in m_Servers.Values) + server.DebugLevel = s_debugLevel; + } + } + + private static int s_debugLevel; + + /// + /// Set the main HTTP server instance. + /// + /// + /// This will be used to register all handlers that listen to the default port. + /// + /// + /// Thrown if the HTTP server has not already been registered via AddHttpServer() + /// public static BaseHttpServer Instance { get { return instance; } - set { instance = value; } + + set + { + lock (m_Servers) + if (!m_Servers.ContainsValue(value)) + throw new Exception("HTTP server must already have been registered to be set as the main instance"); + + instance = value; + } } - public static IHttpServer GetHttpServer(uint port) + /// + /// Get all the registered servers. + /// + /// + /// Returns a copy of the dictionary so this can be iterated through without locking. + /// + /// + public static Dictionary Servers { - return GetHttpServer(port,null); + get { return new Dictionary(m_Servers); } } + + public static void RegisterHttpConsoleCommands(ICommandConsole console) + { + console.Commands.AddCommand( + "Comms", false, "debug http", "debug http []", + "Turn on inbound non-poll http request debugging.", + "If level <= 0, then no extra logging is done.\n" + + "If level >= 1, then short warnings are logged when receiving bad input data.\n" + + "If level >= 2, then long warnings are logged when receiving bad input data.\n" + + "If level >= 3, then short notices about all incoming non-poll HTTP requests are logged.\n" + + "If no level is specified then the current level is returned.", + HandleDebugHttpCommand); + } + + /// + /// Turn on some debugging values for OpenSim. + /// + /// + private static void HandleDebugHttpCommand(string module, string[] args) + { + if (args.Length == 3) + { + int newDebug; + if (int.TryParse(args[2], out newDebug)) + { + MainServer.DebugLevel = newDebug; + MainConsole.Instance.OutputFormat("Debug http level set to {0}", newDebug); + } + } + else if (args.Length == 2) + { + MainConsole.Instance.OutputFormat("Current debug http level is {0}", MainServer.DebugLevel); + } + else + { + MainConsole.Instance.Output("Usage: debug http 0..3"); + } + } + + /// + /// Register an already started HTTP server to the collection of known servers. + /// + /// public static void AddHttpServer(BaseHttpServer server) { - m_Servers.Add(server.Port, server); + lock (m_Servers) + { + if (m_Servers.ContainsKey(server.Port)) + throw new Exception(string.Format("HTTP server for port {0} already exists.", server.Port)); + + m_Servers.Add(server.Port, server); + } } + /// + /// Removes the http server listening on the given port. + /// + /// + /// It is the responsibility of the caller to do clean up. + /// + /// + /// + public static bool RemoveHttpServer(uint port) + { + lock (m_Servers) + return m_Servers.Remove(port); + } + + /// + /// Does this collection of servers contain one with the given port? + /// + /// + /// Unlike GetHttpServer, this will not instantiate a server if one does not exist on that port. + /// + /// + /// true if a server with the given port is registered, false otherwise. + public static bool ContainsHttpServer(uint port) + { + lock (m_Servers) + return m_Servers.ContainsKey(port); + } + + /// + /// Get the default http server or an http server for a specific port. + /// + /// + /// If the requested HTTP server doesn't already exist then a new one is instantiated and started. + /// + /// + /// If 0 then the default HTTP server is returned. + public static IHttpServer GetHttpServer(uint port) + { + return GetHttpServer(port, null); + } + + /// + /// Get the default http server, an http server for a specific port + /// and/or an http server bound to a specific address + /// + /// + /// If the requested HTTP server doesn't already exist then a new one is instantiated and started. + /// + /// + /// If 0 then the default HTTP server is returned. + /// A specific IP address to bind to. If null then the default IP address is used. public static IHttpServer GetHttpServer(uint port, IPAddress ipaddr) { if (port == 0) return Instance; + if (instance != null && port == Instance.Port) return Instance; - if (m_Servers.ContainsKey(port)) + lock (m_Servers) + { + if (m_Servers.ContainsKey(port)) + return m_Servers[port]; + + m_Servers[port] = new BaseHttpServer(port); + + if (ipaddr != null) + m_Servers[port].ListenIPAddress = ipaddr; + + m_Servers[port].Start(); + return m_Servers[port]; - - m_Servers[port] = new BaseHttpServer(port); - - if (ipaddr != null) - m_Servers[port].ListenIPAddress = ipaddr; - - m_log.InfoFormat("[MAIN HTTP SERVER]: Starting main http server on port {0}", port); - m_Servers[port].Start(); - - return m_Servers[port]; + } } } -} +} \ No newline at end of file diff --git a/OpenSim/Region/Application/OpenSim.cs b/OpenSim/Region/Application/OpenSim.cs index f4812e63eb..f418d7b596 100644 --- a/OpenSim/Region/Application/OpenSim.cs +++ b/OpenSim/Region/Application/OpenSim.cs @@ -231,6 +231,8 @@ namespace OpenSim /// private void RegisterConsoleCommands() { + MainServer.RegisterHttpConsoleCommands(m_console); + m_console.Commands.AddCommand("Objects", false, "force update", "force update", "Force the update of all objects on clients", @@ -248,14 +250,6 @@ namespace OpenSim + "If an avatar name is given then only packets from that avatar are logged", Debug); - m_console.Commands.AddCommand("Comms", false, "debug http", - "debug http ", - "Turn on inbound http request debugging for everything except the event queue (see debug eq).", - "If level >= 2 then the handler used to service the request is logged.\n" - + "If level >= 1 then incoming HTTP requests are logged.\n" - + "If level <= 0 then no extra http logging is done.\n", - Debug); - m_console.Commands.AddCommand("Comms", false, "debug teleport", "debug teleport", "Toggle teleport route debugging", Debug); m_console.Commands.AddCommand("Regions", false, "debug scene", @@ -914,21 +908,6 @@ namespace OpenSim break; - case "http": - if (args.Length == 3) - { - int newDebug; - if (int.TryParse(args[2], out newDebug)) - { - MainServer.Instance.DebugLevel = newDebug; - MainConsole.Instance.OutputFormat("Debug http level set to {0}", newDebug); - break; - } - } - - MainConsole.Instance.Output("Usage: debug http 0..2"); - break; - case "scene": if (args.Length == 4) { diff --git a/OpenSim/Region/ClientStack/Linden/Caps/EventQueue/Tests/EventQueueTests.cs b/OpenSim/Region/ClientStack/Linden/Caps/EventQueue/Tests/EventQueueTests.cs index c25b58cf8f..cd70410680 100644 --- a/OpenSim/Region/ClientStack/Linden/Caps/EventQueue/Tests/EventQueueTests.cs +++ b/OpenSim/Region/ClientStack/Linden/Caps/EventQueue/Tests/EventQueueTests.cs @@ -51,7 +51,16 @@ namespace OpenSim.Region.ClientStack.Linden.Tests [SetUp] public void SetUp() { - MainServer.Instance = new BaseHttpServer(9999, false, 9998, ""); + uint port = 9999; + uint sslPort = 9998; + + // This is an unfortunate bit of clean up we have to do because MainServer manages things through static + // variables and the VM is not restarted between tests. + MainServer.RemoveHttpServer(port); + + BaseHttpServer server = new BaseHttpServer(port, false, sslPort, ""); + MainServer.AddHttpServer(server); + MainServer.Instance = server; IConfigSource config = new IniConfigSource(); config.AddConfig("Startup"); diff --git a/OpenSim/Region/ClientStack/RegionApplicationBase.cs b/OpenSim/Region/ClientStack/RegionApplicationBase.cs index 6e78d6d774..c4324e8592 100644 --- a/OpenSim/Region/ClientStack/RegionApplicationBase.cs +++ b/OpenSim/Region/ClientStack/RegionApplicationBase.cs @@ -94,22 +94,19 @@ namespace OpenSim.Region.ClientStack m_log.InfoFormat("[REGION SERVER]: Starting HTTP server on port {0}", m_httpServerPort); m_httpServer.Start(); + MainServer.AddHttpServer(m_httpServer); MainServer.Instance = m_httpServer; // "OOB" Server if (m_networkServersInfo.ssl_listener) { - BaseHttpServer server = null; - server = new BaseHttpServer( + BaseHttpServer server = new BaseHttpServer( m_networkServersInfo.https_port, m_networkServersInfo.ssl_listener, m_networkServersInfo.cert_path, m_networkServersInfo.cert_pass); - // Add the server to m_Servers - if(server != null) - { - m_log.InfoFormat("[REGION SERVER]: Starting HTTPS server on port {0}", server.Port); - MainServer.AddHttpServer(server); - server.Start(); - } + + m_log.InfoFormat("[REGION SERVER]: Starting HTTPS server on port {0}", server.Port); + MainServer.AddHttpServer(server); + server.Start(); } base.StartupSpecific(); diff --git a/OpenSim/Region/CoreModules/Scripting/XMLRPC/XMLRPCModule.cs b/OpenSim/Region/CoreModules/Scripting/XMLRPC/XMLRPCModule.cs index 40ffcb4a70..0003af2cc9 100644 --- a/OpenSim/Region/CoreModules/Scripting/XMLRPC/XMLRPCModule.cs +++ b/OpenSim/Region/CoreModules/Scripting/XMLRPC/XMLRPCModule.cs @@ -131,11 +131,12 @@ namespace OpenSim.Region.CoreModules.Scripting.XMLRPC { // Start http server // Attach xmlrpc handlers - m_log.Info("[XML RPC MODULE]: " + - "Starting up XMLRPC Server on port " + m_remoteDataPort + " for llRemoteData commands."); - BaseHttpServer httpServer = new BaseHttpServer((uint) m_remoteDataPort); +// m_log.InfoFormat( +// "[XML RPC MODULE]: Starting up XMLRPC Server on port {0} for llRemoteData commands.", +// m_remoteDataPort); + + IHttpServer httpServer = MainServer.GetHttpServer((uint)m_remoteDataPort); httpServer.AddXmlRPCHandler("llRemoteData", XmlRpcRemoteData); - httpServer.Start(); } } diff --git a/OpenSim/Server/Base/HttpServerBase.cs b/OpenSim/Server/Base/HttpServerBase.cs index d47155984f..7ba0ca8053 100644 --- a/OpenSim/Server/Base/HttpServerBase.cs +++ b/OpenSim/Server/Base/HttpServerBase.cs @@ -42,42 +42,9 @@ namespace OpenSim.Server.Base { // Logger // - private static readonly ILog m_Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog m_Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - // The http server instance - // - protected BaseHttpServer m_HttpServer = null; - protected uint m_Port = 0; - protected Dictionary m_Servers = - new Dictionary(); - protected uint m_consolePort = 0; - - public IHttpServer HttpServer - { - get { return m_HttpServer; } - } - - public uint DefaultPort - { - get { return m_Port; } - } - - public IHttpServer GetHttpServer(uint port) - { - m_Log.InfoFormat("[SERVER]: Requested port {0}", port); - if (port == m_Port) - return HttpServer; - - if (m_Servers.ContainsKey(port)) - return m_Servers[port]; - - m_Servers[port] = new BaseHttpServer(port); - - m_Log.InfoFormat("[SERVER]: Starting new HTTP server on port {0}", port); - m_Servers[port].Start(); - - return m_Servers[port]; - } + private uint m_consolePort; // Handle all the automagical stuff // @@ -94,19 +61,21 @@ namespace OpenSim.Server.Base System.Console.WriteLine("Section 'Network' not found, server can't start"); Thread.CurrentThread.Abort(); } + uint port = (uint)networkConfig.GetInt("port", 0); if (port == 0) { - Thread.CurrentThread.Abort(); } - // + bool ssl_main = networkConfig.GetBoolean("https_main",false); bool ssl_listener = networkConfig.GetBoolean("https_listener",false); m_consolePort = (uint)networkConfig.GetInt("ConsolePort", 0); - m_Port = port; + + BaseHttpServer httpServer = null; + // // This is where to make the servers: // @@ -118,8 +87,7 @@ namespace OpenSim.Server.Base // if ( !ssl_main ) { - m_HttpServer = new BaseHttpServer(port); - + httpServer = new BaseHttpServer(port); } else { @@ -135,10 +103,12 @@ namespace OpenSim.Server.Base System.Console.WriteLine("Password for X509 certificate is missing, server can't start."); Thread.CurrentThread.Abort(); } - m_HttpServer = new BaseHttpServer(port, ssl_main, cert_path, cert_pass); + + httpServer = new BaseHttpServer(port, ssl_main, cert_path, cert_pass); } - MainServer.Instance = m_HttpServer; + MainServer.AddHttpServer(httpServer); + MainServer.Instance = httpServer; // If https_listener = true, then add an ssl listener on the https_port... if ( ssl_listener == true ) { @@ -157,43 +127,24 @@ namespace OpenSim.Server.Base System.Console.WriteLine("Password for X509 certificate is missing, server can't start."); Thread.CurrentThread.Abort(); } - // Add our https_server - BaseHttpServer server = null; - server = new BaseHttpServer(https_port, ssl_listener, cert_path, cert_pass); - if (server != null) - { - m_Log.InfoFormat("[SERVER]: Starting HTTPS server on port {0}", https_port); - m_Servers.Add(https_port,server); - } - else - System.Console.WriteLine(String.Format("Failed to start HTTPS server on port {0}",https_port)); + + MainServer.AddHttpServer(new BaseHttpServer(https_port, ssl_listener, cert_path, cert_pass)); } } protected override void Initialise() { - m_Log.InfoFormat("[SERVER]: Starting HTTP server on port {0}", m_HttpServer.Port); - m_HttpServer.Start(); + foreach (BaseHttpServer s in MainServer.Servers.Values) + s.Start(); - if (m_Servers.Count > 0) - { - foreach (BaseHttpServer s in m_Servers.Values) - { - if (!s.UseSSL) - m_Log.InfoFormat("[SERVER]: Starting HTTP server on port {0}", s.Port); - else - m_Log.InfoFormat("[SERVER]: Starting HTTPS server on port {0}", s.Port); - - s.Start(); - } - } + MainServer.RegisterHttpConsoleCommands(MainConsole.Instance); if (MainConsole.Instance is RemoteConsole) { if (m_consolePort == 0) - ((RemoteConsole)MainConsole.Instance).SetServer(m_HttpServer); + ((RemoteConsole)MainConsole.Instance).SetServer(MainServer.Instance); else - ((RemoteConsole)MainConsole.Instance).SetServer(GetHttpServer(m_consolePort)); + ((RemoteConsole)MainConsole.Instance).SetServer(MainServer.GetHttpServer(m_consolePort)); } } } diff --git a/OpenSim/Server/ServerMain.cs b/OpenSim/Server/ServerMain.cs index 9503c4cff6..21fb6785c3 100644 --- a/OpenSim/Server/ServerMain.cs +++ b/OpenSim/Server/ServerMain.cs @@ -30,6 +30,7 @@ using log4net; using System.Reflection; using System; using System.Collections.Generic; +using OpenSim.Framework.Servers; using OpenSim.Framework.Servers.HttpServer; using OpenSim.Server.Base; using OpenSim.Server.Handlers.Base; @@ -92,27 +93,24 @@ namespace OpenSim.Server if (parts.Length > 1) friendlyName = parts[1]; - IHttpServer server = m_Server.HttpServer; - if (port != 0) - server = m_Server.GetHttpServer(port); + IHttpServer server; - if (port != m_Server.DefaultPort && port != 0) - m_log.InfoFormat("[SERVER]: Loading {0} on port {1}", friendlyName, port); + if (port != 0) + server = MainServer.GetHttpServer(port); else - m_log.InfoFormat("[SERVER]: Loading {0}", friendlyName); + server = MainServer.Instance; + + m_log.InfoFormat("[SERVER]: Loading {0} on port {1}", friendlyName, server.Port); IServiceConnector connector = null; - Object[] modargs = new Object[] { m_Server.Config, server, - configName }; - connector = ServerUtils.LoadPlugin(conn, - modargs); + Object[] modargs = new Object[] { m_Server.Config, server, configName }; + connector = ServerUtils.LoadPlugin(conn, modargs); + if (connector == null) { modargs = new Object[] { m_Server.Config, server }; - connector = - ServerUtils.LoadPlugin(conn, - modargs); + connector = ServerUtils.LoadPlugin(conn, modargs); } if (connector != null) @@ -132,4 +130,4 @@ namespace OpenSim.Server return 0; } } -} +} \ No newline at end of file diff --git a/prebuild.xml b/prebuild.xml index 3a64af905e..21bf71f725 100644 --- a/prebuild.xml +++ b/prebuild.xml @@ -1480,6 +1480,7 @@ +