Esempio n. 1
0
        public bool Start(Simian simian)
        {
            int port = DEFAULT_HTTP_PORT;
            string hostname = null;
            string sslCertFile = null;
            IPHostEntry entry;
            IPAddress address;

            // Create a logger for the HTTP server
            HttpLogWriter httpLogger = new HttpLogWriter(m_log);

            // Create a default 404 handler
            m_notFoundHandler = new HttpRequestHandler(null, Default404Handler, true);

            #region Config Variables

            IConfig config = simian.Config.Configs["HTTP"];

            if (config != null)
            {
                port = config.GetInt("ListenPort", DEFAULT_HTTP_PORT);
                hostname = config.GetString("Hostname", null);
                sslCertFile = config.GetString("SSLCertFile", null);
            }

            if (String.IsNullOrEmpty(hostname))
            {
                hostname = Dns.GetHostName();
                entry = Dns.GetHostEntry(hostname);
                address = IPAddress.Any;
            }
            else
            {
                entry = Dns.GetHostEntry(hostname);
                if (entry != null && entry.AddressList.Length > 0)
                {
                    address = entry.AddressList[0];
                }
                else
                {
                    m_log.Warn("Could not resolve an IP address from hostname " + hostname + ", binding to all interfaces");
                    address = IPAddress.Any;
                }
            }

            #endregion Config Variables

            #region Initialization

            if (!String.IsNullOrEmpty(sslCertFile))
            {
                // HTTPS mode
                try { m_sslCertificate = new X509Certificate2(sslCertFile); }
                catch (Exception ex)
                {
                    m_log.Error("Failed to load SSL certificate file \"" + sslCertFile + "\": " + ex.Message);
                    return false;
                }

                m_uri = new Uri("https://" + hostname + (port != 80 ? (":" + port) : String.Empty));
                m_httpServer = HttpServer.HttpListener.Create(address, port, m_sslCertificate, RemoteCertificateValidationHandler, SslProtocols.Default, false);
            }
            else
            {
                // HTTP mode
                m_uri = new Uri("http://" + hostname + (port != 80 ? (":" + port) : String.Empty));
                m_httpServer = HttpServer.HttpListener.Create(address, port);
            }

            m_httpServer.LogWriter = httpLogger;
            m_httpServer.RequestReceived += RequestReceivedHandler;

            m_httpServer.Start(64);
            m_log.Info("HTTP server is listening at " + m_uri);

            #endregion Initialization

            return true;
        }
Esempio n. 2
0
        public bool Start(Simian simian)
        {
            int         port        = DEFAULT_HTTP_PORT;
            string      hostname    = null;
            string      sslCertFile = null;
            IPHostEntry entry;
            IPAddress   address;

            // Create a logger for the HTTP server
            HttpLogWriter httpLogger = new HttpLogWriter(m_log);

            // Create a default 404 handler
            m_notFoundHandler = new HttpRequestHandler(null, Default404Handler, true);

            #region Config Variables

            IConfig config = simian.Config.Configs["HTTP"];

            if (config != null)
            {
                port        = config.GetInt("ListenPort", DEFAULT_HTTP_PORT);
                hostname    = config.GetString("Hostname", null);
                sslCertFile = config.GetString("SSLCertFile", null);
            }

            if (String.IsNullOrEmpty(hostname))
            {
                hostname = Dns.GetHostName();
                entry    = Dns.GetHostEntry(hostname);
                address  = IPAddress.Any;
            }
            else
            {
                entry = Dns.GetHostEntry(hostname);
                if (entry != null && entry.AddressList.Length > 0)
                {
                    address = entry.AddressList[0];
                }
                else
                {
                    m_log.Warn("Could not resolve an IP address from hostname " + hostname + ", binding to all interfaces");
                    address = IPAddress.Any;
                }
            }

            #endregion Config Variables

            #region Initialization

            if (!String.IsNullOrEmpty(sslCertFile))
            {
                // HTTPS mode
                try { m_sslCertificate = new X509Certificate2(sslCertFile); }
                catch (Exception ex)
                {
                    m_log.Error("Failed to load SSL certificate file \"" + sslCertFile + "\": " + ex.Message);
                    return(false);
                }

                m_uri        = new Uri("https://" + hostname + (port != 80 ? (":" + port) : String.Empty));
                m_httpServer = HttpServer.HttpListener.Create(address, port, m_sslCertificate, RemoteCertificateValidationHandler, SslProtocols.Default, false);
            }
            else
            {
                // HTTP mode
                m_uri        = new Uri("http://" + hostname + (port != 80 ? (":" + port) : String.Empty));
                m_httpServer = HttpServer.HttpListener.Create(address, port);
            }

            m_httpServer.LogWriter        = httpLogger;
            m_httpServer.RequestReceived += RequestReceivedHandler;

            m_httpServer.Start(64);
            m_log.Info("HTTP server is listening at " + m_uri);

            #endregion Initialization

            return(true);
        }