Ejemplo n.º 1
0
        /// <summary>
        /// The default Constructor.
        /// </summary>
        /// <param name="path"></param>
        public WebScriptingServer(string path)
        {
            _path   = path;
            _server = new HttpCwsServer(path);
            _server.Register();

            Debug.WriteSuccess("HttpCwsServer", "Started for path: {0}", _path);
            Debug.WriteInfo("HttpCwsServer", "Base URL: {0}", BaseUrlDns);
            Debug.WriteInfo("HttpCwsServer", "Base URL: {0}", BaseUrlIp);

            _server.ReceivedRequestEvent += (sender, args) =>
            {
#if DEBUG
                CrestronConsole.PrintLine("Incoming http request from {0} {1}", args.Context.Request.UserHostAddress, args.Context.Request.UserHostName);
                CrestronConsole.PrintLine("Request AbsolutePath: {0}", args.Context.Request.Url.AbsolutePath);
                CrestronConsole.PrintLine("Request AbsoluteUri: {0}", args.Context.Request.Url.AbsoluteUri);
                CrestronConsole.PrintLine("Request PhysicalPath: {0}", args.Context.Request.PhysicalPath);
                CrestronConsole.PrintLine("Request PathAndQuery: {0}", args.Context.Request.Url.PathAndQuery);
                CrestronConsole.PrintLine("Request Query: {0}", args.Context.Request.Url.Query);
                CrestronConsole.PrintLine("Request Path: {0}", args.Context.Request.Path);
                CrestronConsole.PrintLine("Request Method: {0}", args.Context.Request.HttpMethod);
#endif
                try
                {
                    if (args.Context.Request.RouteData != null)
                    {
#if DEBUG
                        CrestronConsole.PrintLine("Request handler: {0}",
                                                  args.Context.Request.RouteData.RouteHandler.GetType());
                        CrestronConsole.PrintLine("Route URL Pattern: {0}", args.Context.Request.RouteData.Route.Url);
#endif
                    }
                    else if (RootHandler == null)
                    {
#if DEBUG
                        CrestronConsole.PrintLine("Request has no handler!");
#endif
                        HandleError(args.Context, 404, "Not Found", "The requested resource does not exist");
                    }
                    else if (args.Context.Request.PhysicalPath != string.Format("\\HTML\\{0}", _path) &&
                             args.Context.Request.PhysicalPath != string.Format("\\HTML\\{0}\\", _path))
                    {
#if DEBUG
                        CrestronConsole.PrintLine("Request handler: {0}", RootHandler.GetType());
#endif
                        RootHandler.ProcessRequest(args.Context);
                    }
                }
                catch (Exception e)
                {
                    CloudLog.Exception("Error in ApiServer main request handler", e);
                    HandleError(args.Context, 500, "Server Error", string.Format("{0}<BR>{1}", e.Message, e.StackTrace));
                }
            };
            CrestronEnvironment.ProgramStatusEventHandler += type =>
            {
                if (type == eProgramStatusEventType.Stopping)
                {
                    _server.Dispose();
                }
            };
        }