コード例 #1
0
        public void ExecuteScript(string fullPath, Dictionary<string, string> urlParameters,
            ClientHttpResponse response, string extension, string mimeType, HTTPMethod method, string postData,
            string documentRoot, dynamic serverHandle, ScriptExecutionParameters executionParameters)
        {
            //Prepare JSScript
            var scriptContents = File.ReadAllText(fullPath);
            var scriptDir = Path.GetDirectoryName(fullPath);
            var jsEngine = new Engine(cfg => cfg.AllowClr());

            var undefined = Undefined.Instance;

            //Inject variables
            if (method == HTTPMethod.Get)
            {
                jsEngine.SetValue("_GET", urlParameters);
                jsEngine.SetValue("_SERVER", response.RequestHttpHeaders);
                jsEngine.SetValue("_POST", undefined);
            }
            if (method == HTTPMethod.Post)
            {
                jsEngine.SetValue("_GET", undefined);
                jsEngine.SetValue("_SERVER", response.RequestHttpHeaders);
                jsEngine.SetValue("_POST", urlParameters);
                jsEngine.SetValue("POST_DATA", postData);
            }

            //Globals
            jsEngine.SetValue("DocumentRoot", documentRoot);
            jsEngine.SetValue("__dirname__", scriptDir);

            switch (extension)
            {
                case ".jscx": //Fully-controlled script
                {
                    try
                    {
                        //Manipulate Scope
                        jsEngine.SetValue("response", response);
                        jsEngine.SetValue("FireHTTPServer", serverHandle);
                        jsEngine.SetValue("_mimeTypeMappings", CommonVariables.MimeTypeMappings);
                        jsEngine.SetValue("dirSep", _dirSep);
                        DefineScriptingApi(jsEngine);
                        jsEngine.Execute(scriptContents);
                        break;
                    }
                    catch (DeadRequestException)
                    {
                        throw; //Don't catch these.
                    }
                    catch (Exception ex)
                    {
                        var level = (int) jsEngine.GetValue("__error_reporting_level").AsNumber();
                        if (level > 0)
                        {
                            if (!response.HasFinishedSendingHeaders)
                            {
                                //If headers not sent, send default headers.
                                response.SendHeader("HTTP/1.1 200 OK");
                                response.SendHeader("Content-Type: text/plain");
                                response.SendEndHeaders();
                            }
                            response.OutputStream.WriteLine("Error in script execution. Stack trace:");
                            response.OutputStream.WriteLine(ex.ToString());
                            break;
                        }
                        throw;
                    }
                }
            }
        }
コード例 #2
0
 public override void SendFileWithMimeType(string path, string wwwroot, ClientHttpResponse response,
     Dictionary<string, string> requestParameters, HTTPMethod method, string postData)
 {
     path = path.Substring(1); //Remove slash at beginning
     var fullPath = Path.Combine(wwwroot, path);
     var fileInfo = new FileInfo(fullPath);
     var containingDir = fileInfo.Directory;
     var configFile = new FileInfo(containingDir?.FullName + _dirSep + CommonVariables.ConfigFileName);
     var faccess = _defaultFAccess;
     var fAccessFound = FindFAccess(ref configFile, wwwroot);
     if (fAccessFound)
     {
         var faccessJson = File.ReadAllText(configFile.FullName);
         faccess = JsonConvert.DeserializeObject<FAccessConfig>(faccessJson);
     }
     var requestHandled = HandleFAccessConfig(fileInfo, containingDir, faccess, response); //Process the FACCESS
     if (requestHandled)
         return;
     var extension = fileInfo.Extension;
     var mimeType = "application/octet-stream";
     if (CommonVariables.MimeTypeMappings.ContainsKey(extension))
     {
         mimeType = CommonVariables.MimeTypeMappings[extension];
     }
     if (!_executableFileTypes.Contains(extension))
     {
         var staticFileHookParameters = new FireHTTPHookParameters
         {
             RequestedDocumentFullPath = fullPath,
             RequestFileName = path,
             RequestMimeType = mimeType,
             RequestedFileExtension = extension
         };
         //Static file; if not executable, send normally
         using (var fs = File.OpenRead(fullPath))
         {
             staticFileHookParameters.RequestFileContentStream = fs;
             bool staticFileHookHandled = InvokeHook(FireHTTPServerHookType.StaticFileSendHook,
                 _currentHookExecutionScope, staticFileHookParameters);
             if (!staticFileHookHandled)
             {
                 SendStreamWithMimeType(fs, mimeType, response);
             }
         }
     }
     else
     {
         //Execute Script
         var executionParameters = new ScriptExecutionParameters();
         ExecuteScript(fullPath, requestParameters, response, extension, mimeType, method, postData, wwwroot,
             executionParameters);
     }
 }
コード例 #3
0
 private void ExecuteScript(string fullPath, Dictionary<string, string> urlParameters,
     ClientHttpResponse response, string extension, string mimeType, HTTPMethod method, string postData,
     string documentRoot, ScriptExecutionParameters executionParameters)
 {
     //Select launcher
     var launcherProgram = _scriptInterpreterCache[extension];
     launcherProgram.ExecuteScript(fullPath, urlParameters, response, extension, mimeType, method, postData,
         documentRoot, this, executionParameters);
 }
コード例 #4
0
        public void ExecuteScript(string fullPath, Dictionary<string, string> urlParameters,
            ClientHttpResponse response, string extension, string mimeType, HTTPMethod method, string postData,
            string documentRoot, dynamic serverHandle, ScriptExecutionParameters executionParameters)
        {
            //Prepare ExaScript
            var scriptContents = File.ReadAllText(fullPath);
            var scriptDir = Path.GetDirectoryName(fullPath);
            var escLauncher = new ExaScriptLauncher();
            dynamic escExecutionScope = escLauncher.UnderlyingInstance.Scope;
            var escEngine = escLauncher.UnderlyingInstance.Engine;

            //Inject variables
            //Inject code
            if (method == HTTPMethod.Get)
            {
                escExecutionScope._GET = urlParameters;
                escExecutionScope._SERVER = response.RequestHttpHeaders;
                escExecutionScope._POST = null;
            }
            if (method == HTTPMethod.Post)
            {
                escExecutionScope._GET = null;
                escExecutionScope._SERVER = response.RequestHttpHeaders;
                escExecutionScope._POST = urlParameters;
                escExecutionScope.POST_DATA = postData;
            }

            //Globals
            escExecutionScope.DocumentRoot = documentRoot;
            escExecutionScope.__dirname__ = scriptDir;

            switch (extension)
            {
                case ".esc": //Simple executable script
                {
                    //Manipulate Scope

                    //Send Headers
                    response.SendHeader("HTTP/1.1 200 OK");
                    response.SendHeader("Content-Type: " + mimeType);
                    response.SendEndHeaders();

                    escLauncher.LoadCode(scriptContents);
                    var result = escLauncher.RunCode();
                    response.OutputStream.WriteLine(result);
                    break;
                }
                case ".escx": //Fully-controlled script
                {
                    try
                    {
                        //Manipulate Scope
                        escExecutionScope.response = response;
                        escExecutionScope.FireHTTPServer = serverHandle;
                        escExecutionScope._mimeTypeMappings = CommonVariables.MimeTypeMappings;
                        escExecutionScope.dirSep = _dirSep;
                        DefineScriptingApi(escExecutionScope); //Add all the API functions
                        escLauncher.LoadCode(scriptContents);
                        escLauncher.RunCode();
                        break;
                    }
                    catch (DeadRequestException)
                    {
                        throw; //Don't catch these.
                    }
                    catch (Exception ex)
                    {
                        int level = escExecutionScope.__error_reporting_level;
                        if (level <= 0) throw;
                        if (!response.HasFinishedSendingHeaders)
                        {
                            //If headers not sent, send default headers.
                            response.SendHeader("HTTP/1.1 200 OK");
                            response.SendHeader("Content-Type: text/plain");
                            response.SendEndHeaders();
                        }
                        response.OutputStream.WriteLine("Error in script execution. Stack trace:");
                        response.OutputStream.WriteLine(ex.ToString());
                        break;
                    }
                }
            }
        }