Ejemplo n.º 1
0
        public void Handle(HttpContext context)
        {
            JSApplication application = null;

            if (context.Application != null)
            {
                application = (JSApplication)context.Application["JSApplication"];
            }
            if (application == null)
            {
                application = new JSApplication(AppDomain.CurrentDomain.BaseDirectory);
            }

            var mainTemplate = application.Settings.Root + application.Settings.TemplateFolder + application.Settings.Entry;

            if (mainTemplate.EndsWith(".js"))
            {
                ProcessRequest(context);
            }
            else if (mainTemplate.EndsWith(".xdoc"))
            {
                application.XDocService.ProcessRequest(context);
            }

            if (context.Application != null)
            {
                context.Application["JSApplication"] = application;
            }
        }
Ejemplo n.º 2
0
        public void ProcessRequest(HttpContext context)
        {
            JSApplication application = null;
            JSSession     session     = null;

            if (context.Application != null)
            {
                application = (JSApplication)context.Application["JSApplication"];
            }
            if (application == null)
            {
                application = new JSApplication(AppDomain.CurrentDomain.BaseDirectory);
            }

            if (context.Session != null)
            {
                session = (JSSession)context.Session["JSSession"];
            }
            if (session == null)
            {
                session = new JSSession();
            }

            ProcessRequest(context, application, session);

            if (context.Application != null)
            {
                context.Application["JSApplication"] = application;
            }
            if (context.Session != null)
            {
                context.Session["JSSession"] = session;
            }
        }
Ejemplo n.º 3
0
        public string RunTemplate(string template, string data, ref XHTMLMerge.SVCache svCache)
        {
            var application = new JSApplication();
            var session     = new JSSession();

            return(RunTemplate(template, data, ref application, ref session, ref svCache));
        }
Ejemplo n.º 4
0
        public void ProcessRequest(HttpContext context, JSApplication application, JSSession session)
        {
            XHTMLMerge.SVCache svCache = null;

            var path = GetPath(context.Request);

            var arguments = Core.Tool.Construct("Object", application.Engine.Scope);

            arguments.Set("request", CreateRequest(context, path, application.Engine.Scope));

            var response = Core.Tool.Construct("Object", application.Engine.Scope);

            response.Set("contentType", new Core.Javascript.String("text/html"));
            response.Set("statusCode", new Core.Javascript.Number(200));
            arguments.Set("response", response);

            var responseString = RunTemplate(application.Settings.Entry, arguments, ref application, ref session, ref svCache);

            try {
                context.Response.ContentType = response.Get <Core.Javascript.String>("contentType").Value;
                context.Response.StatusCode  = (int)response.Get <Core.Javascript.Number>("statusCode").Value;
            }catch (Exception) {
                // TODO: log error
            }

            context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            context.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With");

            var buffer = Encoding.UTF8.GetBytes(responseString);
            var output = context.Response.OutputStream;

            output.Write(buffer, 0, buffer.Length);
            output.Close();
        }
Ejemplo n.º 5
0
 public void Set()
 {
     Request     = _request;
     Application = _application;
     Session     = _session;
     Buffer      = _buffer;
 }
Ejemplo n.º 6
0
        // Every template is executed via this function
        public string RunTemplate(string template, Core.Javascript.Object arguments, ref JSApplication application, ref JSSession session, ref XHTMLMerge.SVCache svCache)
        {
            try {
                if (arguments == null)
                {
                    arguments = Core.Tool.Construct("Object", application.Engine.Scope);
                }

                var scope = new Scope(application.Engine.Scope, null, ScopeType.Session);
                scope.SetVariable("__application__", new Foreign(application));
                scope.SetVariable("__session__", new Foreign(session));
                scope.SetVariable("__svCache__", new Foreign(svCache));

                // TODO: better way to forward session
                var result = External.Functions.include(
                    Static.Undefined,
                    new Constant[] { new Core.Javascript.String(template), arguments },
                    scope
                    );

                if (result is Core.Javascript.String s)
                {
                    return(s.Value);
                }
            } catch (Error e) {
                return(e.Message + "\n" + string.Join("\n", e.StackTrace.Select(loc => Debug.GetFileName(loc.FileId) + " (" + loc.LineNr + ")")));
            } catch (Exception e) {
                return("System error - " + e.ToString());
            }

            return("");
        }
Ejemplo n.º 7
0
        public Block GetFile(string name, JSApplication application)
        {
            var path = name;

            if (!System.IO.Path.IsPathRooted(path))
            {
                path = application.Settings.Root + application.Settings.TemplateFolder + path;
            }
            var key = Core.Tool.NormalizePath(path);

            if (Files.ContainsKey(key))
            {
                var modified = System.IO.File.GetLastWriteTime(path);
                if (modified > Files[key].LastModified)
                {
                    Files[key] = LoadFile(path);
                }

                return(Files[key].Node);
            }
            else
            {
                var sourceFile = LoadFile(path);
                Files[key] = sourceFile;
                return(sourceFile.Node);
            }
        }
Ejemplo n.º 8
0
 public State(Request request, JSApplication application, JSSession session, StringBuilder buffer)
 {
     _request     = request;
     _application = application;
     _session     = session;
     _buffer      = buffer;
 }
Ejemplo n.º 9
0
        public string RunTemplate(string template, string data, ref JSApplication application, ref JSSession session, ref XHTMLMerge.SVCache svCache)
        {
            try {
                dynamic arguments = new NetJSObject();
                return(RunTemplate(template, arguments, ref application, ref session, ref svCache));
            } catch { }

            return("invalid arguments (must be valid json)");
        }
Ejemplo n.º 10
0
 public FunctionRequest(dynamic function, JSApplication application, Action <object> resultCallback, JSSession session = null, object argument0 = null, object argument1 = null, object argument2 = null, object argument3 = null)
 {
     _function      = function;
     _argument0     = argument0;
     _argument1     = argument1;
     _argument2     = argument2;
     _argument3     = argument3;
     State          = new State(this, application, session ?? new JSSession(), new StringBuilder());
     ResultCallback = resultCallback;
 }
Ejemplo n.º 11
0
        public string RunTemplate(string template, string data, ref JSApplication application, ref JSSession session, ref XHTMLMerge.SVCache svCache)
        {
            var arguments = JSON.parse(null, new[] { new Core.Javascript.String(data) }, application.Engine.Scope);

            if (arguments is Core.Javascript.Object a)
            {
                return(RunTemplate(template, a, ref application, ref session, ref svCache));
            }
            return("invalid arguments (must be valid json)");
        }
Ejemplo n.º 12
0
        public void RunCode(string code, JSApplication application, JSSession session, Action <object> resultCallback)
        {
            var request = new ScriptRequest(
                application.Compile(code),
                application,
                resultCallback,
                session
                );

            application.AddRequest(request);
        }
Ejemplo n.º 13
0
        public void RunScriptSync(string template, JSApplication application, JSSession session, Action <object> resultCallback)
        {
            var request = new ScriptRequest(
                application.Cache.GetScript(template, application),
                application,
                resultCallback,
                session
                );

            request.Call();
        }
Ejemplo n.º 14
0
        public string RunTemplate(string template, Core.Javascript.Object arguments, ref JSApplication application)
        {
            if (application == null)
            {
                application = new JSApplication();
            }

            var session = new JSSession();
            var svCache = new XHTMLMerge.SVCache();

            return(RunTemplate(template, arguments, ref application, ref session, ref svCache));
        }
Ejemplo n.º 15
0
        public V8Script GetScript(string name, JSApplication application)
        {
            var path = GetPath(name, application, true);

            try {
                var source = ReadFile(path);
                return(application.Compile(path, source));
            } catch (System.IO.IOException) {
                application.Error(new IOError($"Could not load file '{path}'"), ErrorStage.Compilation);
            } catch (Exception e) {
                application.Error(e, ErrorStage.Compilation);
            }

            // Return an empty script to not block rest of execution
            return(application.Compile(path, ""));
        }
Ejemplo n.º 16
0
        public dynamic GetResource(string name, bool returnVar, JSApplication application)
        {
            var path = GetPath(name, application, true);

            try {
                var source   = ReadFile(path);
                var code     = Transpiler.TranspileTemplate(source, returnVar);
                var script   = application.Compile(path, code);
                var function = application.Evaluate(script);
                return(function);
            } catch (System.IO.IOException) {
                application.Error(new IOError($"Could not load file '{path}'"), ErrorStage.Compilation);
            } catch (Exception e) {
                application.Error(e, ErrorStage.Compilation);
            }

            // Return an empty function to not block rest of execution
            var c = Transpiler.TranspileTemplate("", returnVar);
            var s = application.Compile(name, c);
            var f = application.Evaluate(s);

            return(f);
        }
Ejemplo n.º 17
0
        public string GetPath(string name, JSApplication application, bool inSource)
        {
            var path = name;

            if (path.StartsWith("/"))
            {
                path = application.Settings.Root + (inSource ? application.Settings.TemplateFolder : "") + path;
            }
            else if (!System.IO.Path.IsPathRooted(path))
            {
                var currentLocation = GetCurrentLocation();
                if (currentLocation.Length == 0)
                {
                    path = application.Settings.Root + (inSource ? application.Settings.TemplateFolder : "") + path;
                }
                else
                {
                    var nameParts = currentLocation.Split(new[] { '/', '\\' });
                    path = string.Join("/", nameParts.Take(nameParts.Length - 1)) + "/" + name;
                }
            }
            return(path);
        }
Ejemplo n.º 18
0
        public string RunTemplate(string template, string data, ref JSApplication application, ref JSSession session)
        {
            var svCache = new XHTMLMerge.SVCache();

            return(RunTemplate(template, data, ref application, ref session, ref svCache));
        }
Ejemplo n.º 19
0
        // Every template is executed via this function
        public string RunTemplate(string template, Dictionary <string, object> arguments, ref JSApplication application, ref JSSession session, ref XHTMLMerge.SVCache svCache)
        {
            try {
                if (arguments == null)
                {
                    arguments = new Dictionary <string, object>();
                }

                NetJS.API.XDoc.SetXDocInfo(new API.XDoc.XDocInfo()
                {
                    AppCache = null, SVCache = svCache
                });

                // TODO: better way to forward session
                dynamic args = new NetJSObject();
                foreach (var pair in arguments)
                {
                    args[pair.Key] = pair.Value;
                }

                var result = API.Functions.include(template, args);

                return(result.ToString());
            } catch (ScriptEngineException se) {
                return(se.ErrorDetails);
            } catch (Error e) {
                return(e.ToString());
            } catch (Exception e) {
                return("System error - " + e.ToString());
            }
        }
Ejemplo n.º 20
0
 public ScriptRequest(Microsoft.ClearScript.V8.V8Script script, JSApplication application, Action <object> resultCallback, JSSession session = null)
 {
     Script         = script;
     State          = new State(this, application, session ?? new JSSession(), new StringBuilder());
     ResultCallback = resultCallback;
 }