示例#1
0
        public static void Delete(LanguageInterpreter sender, FunctionArgs args)
        {
            if (EUtils.CheckArgs(1, args))
            {
                object param = args.EvaluateParameters()[0];

                if (EUtils.CheckArgType(param, typeof(string)))
                {
                    string filepath = (string)param;

                    if (File.Exists(filepath))
                    {
                        File.Delete(filepath);
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Error deleting file.", "The specified file does not exist.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                }
            }
            else
            {
                sender.Interpreter.HandleException(Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
            }
        }
示例#2
0
        public static void Create(LanguageInterpreter sender, FunctionArgs args)
        {
            if (EUtils.CheckArgs(1, args))
            {
                object param = args.EvaluateParameters()[0];

                if (EUtils.CheckArgType(param, typeof(string)))
                {
                    string filepath = (string)param;

                    if (!File.Exists(filepath))
                    {
                        // could just use .Dispose(); ...
                        using (File.Create(filepath)) { }
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Error creating file.", "A file already exists at the specified path.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                }
            }
            else
            {
                sender.Interpreter.HandleException(Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
            }
        }
示例#3
0
        void json_parse(LanguageInterpreter sender, FunctionArgs args)
        {
            if (EUtils.CheckArgs(1, args))
            {
                object param = args.EvaluateParameters()[0];

                if (EUtils.CheckArgType(param, typeof(string)))
                {
                    string json_str = (string)param;

                    dynamic parsed_json = DynamicJson.Parse(json_str);

                    if (parsed_json.IsArray)
                    {
                        AnonymousVariableArray finalArray = VariableArrayFromDynamic(parsed_json);

                        args.HasResult = true;
                        args.Result    = finalArray;
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Unimplemented", "Only arrays are implemented in the JSON parser.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                }
            }
            else
            {
                sender.Interpreter.HandleException(Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
            }
        }
示例#4
0
        void requestroute_get(LanguageInterpreter sender, FunctionArgs args)
        {
            if (EUtils.CheckArgs(2, args))
            {
                object param  = args.EvaluateParameters()[0];
                object param2 = args.EvaluateParameters()[1];

                /**/

                if (EUtils.CheckArgType(param, typeof(string)))
                {
                    if (EUtils.CheckArgType(param2, typeof(UserFunction)) || EUtils.CheckArgType(param2, typeof(String)))
                    {
                        string relative_path = (string)param;

                        //sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "info: new get request handler [" + relative_path + "]", "detail", "dunno"));

                        if (EUtils.CheckArgType(param2, typeof(UserFunction)))
                        {
                            UserFunction handler = (UserFunction)param2;

                            routedHTTPInterpreterServer.RequestHandlers.Add(relative_path, new RequestHandler(handler, RequestMethod.GET, relative_path));
                        }
                        else
                        {
                            string file = (string)param2;

                            if (!System.IO.File.Exists(file) && !System.IO.Directory.Exists(file))
                            {
                                sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "File or folder non-existent", "The specified file or folder '" + file + "' does not exist.", "Check the file path provided for errors."));
                            }
                            else
                            {
                                routedHTTPInterpreterServer.RequestHandlers.Add(relative_path, new RequestHandler(file, RequestMethod.GET, relative_path));
                            }
                        }
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'UserFunction' or 'String', got '" + param2.GetType().Name + "'.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                }
            }
            else
            {
                sender.Interpreter.HandleException(Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
            }
        }
示例#5
0
        public static void Exists(LanguageInterpreter sender, FunctionArgs args)
        {
            if (EUtils.CheckArgs(1, args))
            {
                object param = args.EvaluateParameters()[0];

                if (EUtils.CheckArgType(param, typeof(string)))
                {
                    args.HasResult = true;
                    args.Result    = File.Exists((string)param);
                }
                else
                {
                    sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                }
            }
            else
            {
                sender.Interpreter.HandleException(Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
            }
        }
示例#6
0
        public static void Append(LanguageInterpreter sender, FunctionArgs args)
        {
            if (EUtils.CheckArgs(2, args))
            {
                object param  = args.EvaluateParameters()[0];
                object param2 = args.EvaluateParameters()[1];

                if (EUtils.CheckArgType(param, typeof(string)))
                {
                    if (EUtils.CheckArgType(param2, typeof(string)))
                    {
                        string filepath = (string)param;
                        string text     = (string)param2;

                        if (File.Exists(filepath))
                        {
                            args.HasResult = true;
                            File.AppendAllText(filepath, text);
                        }
                        else
                        {
                            sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Error appending to file.", "The specified file does not exist.", ""));
                        }
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param2.GetType().Name + "'.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                }
            }
            else
            {
                sender.Interpreter.HandleException(Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
            }
        }
示例#7
0
        void json_stringify(LanguageInterpreter sender, FunctionArgs args)
        {
            if (EUtils.CheckArgs(1, args))
            {
                object param = args.EvaluateParameters()[0];

                if (EUtils.CheckArgType(param, typeof(AnonymousVariableArray)))
                {
                    AnonymousVariableArray array = (AnonymousVariableArray)param;

                    args.HasResult = true;
                    args.Result    = array.SerializeToJSON();
                }
                else
                {
                    sender.Interpreter.HandleException(new Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'AnonymousVariableArray', got '" + param.GetType().Name + "'.", ""));
                }
            }
            else
            {
                sender.Interpreter.HandleException(Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
            }
        }
示例#8
0
        void server_ProcessRequest(HttpListenerContext context)
        {
            /*context.Response.AddHeader("Content-Type", "application/x-force-download");
             * context.Response.ContentLength64 = data.Length;*/

            context.Response.StatusCode = 200;



            Interpreter.Interpreter.HandleWrite = (string msg) =>
            {
                writeStrToContext(context, msg);
            };

            Interpreter.Interpreter.HandleNonEvaluatedLine = (string line) =>
            {
                Interpreter.Interpreter.HandleWrite(line);
            };

            Interpreter.Interpreter.HandleException = (AzLang.Exception exception) =>
            {
                //context.Response.OutputStream..writeFailure();

                string msg = "[EXCEPTION] " + exception.File + ":" + exception.Line + "\n  Message: " + exception.Message + "\n  Detail: " + exception.Detail + "\n  Suggestions: " + exception.Suggestions + "\n";

                Console.WriteLine(msg);

                writeStrToContext(context, msg);

                context.Response.OutputStream.Close();

                //Reload();
            };

            Interpreter.Interpreter.NonStockMethods["flush"] = (LanguageInterpreter sender, NCalc.FunctionArgs args) =>
            {
                if (context.Response.OutputStream.CanWrite)
                {
                    context.Response.OutputStream.Flush();
                }
            };

            Interpreter.Interpreter.NonStockMethods["status_code"] = (LanguageInterpreter sender, NCalc.FunctionArgs args) =>
            {
                args.HasResult = true;

                if (EUtils.CheckArgs(1, args))
                {
                    object param = args.EvaluateParameters()[0];

                    if (EUtils.CheckArgType(param, typeof(int)))
                    {
                        int code = (int)param;

                        context.Response.StatusCode = code;
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'Int32', got '" + param.GetType().Name + "'.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(AzLang.Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
                }
            };

            Interpreter.Interpreter.NonStockMethods["web_get_client_ip"] = (LanguageInterpreter sender, NCalc.FunctionArgs args) =>
            {
                args.HasResult = true;

                try
                {
                    args.Result = ((IPEndPoint)context.Request.RemoteEndPoint).Address.ToString();
                }
                catch
                {
                }
            };

            Interpreter.Interpreter.NonStockMethods["web_get_client_port"] = (LanguageInterpreter sender, NCalc.FunctionArgs args) =>
            {
                args.HasResult = true;
                try
                {
                    args.Result = ((IPEndPoint)context.Request.RemoteEndPoint).Port.ToString();
                }
                catch
                {
                }
            };

            Interpreter.Interpreter.NonStockMethods["get"] = (LanguageInterpreter sender, NCalc.FunctionArgs args) =>
            {
                args.HasResult = true;

                if (EUtils.CheckArgs(1, args))
                {
                    object param = args.EvaluateParameters()[0];

                    if (EUtils.CheckArgType(param, typeof(string)))
                    {
                        string get_var = (string)param;

                        if (context.Request.QueryString.AllKeys.Contains(get_var))
                        {
                            args.Result = context.Request.QueryString[get_var];
                        }
                        else
                        {
                            sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Undefined array key.", "The specified GET variable is non-existent.", ""));
                        }
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(AzLang.Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
                }
            };

            Interpreter.Interpreter.NonStockMethods["cookies_contains"] = (LanguageInterpreter sender, NCalc.FunctionArgs args) =>
            {
                args.HasResult = true;

                if (EUtils.CheckArgs(1, args))
                {
                    object param = args.EvaluateParameters()[0];

                    if (EUtils.CheckArgType(param, typeof(string)))
                    {
                        string cookie_name = (string)param;

                        args.Result = context.Request.Cookies[cookie_name] != null;
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(AzLang.Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
                }
            };

            Interpreter.Interpreter.NonStockMethods["cookies"] = (LanguageInterpreter sender, NCalc.FunctionArgs args) =>
            {
                args.HasResult = true;

                if (EUtils.CheckArgs(1, args))
                {
                    object param = args.EvaluateParameters()[0];

                    if (EUtils.CheckArgType(param, typeof(string)))
                    {
                        string cookie_name = (string)param;

                        if (context.Request.Cookies[cookie_name] != null)
                        {
                            args.Result = context.Request.Cookies[cookie_name].Value;
                        }
                        else
                        {
                            sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Undefined array key.", "The specified Cookie name is non-existent.", ""));
                        }
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(AzLang.Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
                }
            };

            Interpreter.Interpreter.NonStockMethods["header"] = (LanguageInterpreter sender, NCalc.FunctionArgs args) =>
            {
                args.HasResult = true;

                if (EUtils.CheckArgs(2, args))
                {
                    object param  = args.EvaluateParameters()[0];
                    object param2 = args.EvaluateParameters()[1];

                    if (EUtils.CheckArgType(param, typeof(string)))
                    {
                        if (EUtils.CheckArgType(param2, typeof(string)))
                        {
                            string header = (string)param;
                            string value  = (string)param2;

                            context.Response.Headers.Add(header, value);
                        }
                        else
                        {
                            sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                        }
                    }
                    else
                    {
                        sender.Interpreter.HandleException(new AzLang.Exception(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, "Invalid parameter type.", "Function expects parameter of type 'String', got '" + param.GetType().Name + "'.", ""));
                    }
                }
                else
                {
                    sender.Interpreter.HandleException(AzLang.Consts.Exceptions.ParameterCountMismatch(sender.Interpreter.CurrentFile, sender.Interpreter.CurrentLine, 1));
                }
            };

            if (context.Request.Url.AbsolutePath.StartsWith("/_azlang_res/"))
            {
                context.Response.Headers["Cache-Control"] = "max-age=120";

                string req_icon = context.Request.Url.AbsolutePath.Remove(0, 13);

                req_icon = req_icon.Split('.')[0];

                if (File.Exists("Resources/" + req_icon + ".png"))
                {
                    byte[] image = imageToByteArray((Image)ResourceIcon(req_icon));

                    context.Response.OutputStream.Write(image, 0, image.Length);
                }
                else
                {
                    Handle404(context, context.Request.Url.AbsolutePath);
                }
            }
            else
            {
                Dictionary <string, RequestHandler> matches = RequestHandlers.Where(val => val.Key == context.Request.Url.AbsolutePath && val.Value.Method.ToString().ToLower() == context.Request.HttpMethod.ToLower()).ToDictionary(val => val.Key, val => val.Value);

                if (matches.Count > 0)
                {
                    RequestHandler match = matches.First().Value;



                    if (match.Type == HandlerType.UserFunction)
                    {
                        NCalc.FunctionArgs handler_args = new NCalc.FunctionArgs();

                        handler_args.Parameters = new NCalc.Expression[1];

                        handler_args.Parameters[0] = new NCalc.Expression(new RequestContext(context));

                        Interpreter.ExecuteUserFunction(match.HandlerFunction, handler_args);
                    }
                    else if (match.Type == HandlerType.File)
                    {
                        if (File.Exists(match.FilePath))
                        {
                            byte[] bytes = File.ReadAllBytes(match.FilePath);

                            context.Response.OutputStream.Write(bytes, 0, bytes.Length);

                            //writeStrToContext(context, File.ReadAllText(match.FilePath));
                        }
                        else
                        {
                            Handle404(context, match.FilePath);

                            //Interpreter.Interpreter.HandleException(new AzLang.Exception(Interpreter.Interpreter.CurrentFile, Interpreter.Interpreter.CurrentLine, "File '" + match.FilePath + "' is non-existent", "", ""));
                        }
                    }

                    Console.WriteLine(((IPEndPoint)context.Request.RemoteEndPoint).Address.ToString() + ":" + ((IPEndPoint)context.Request.RemoteEndPoint).Port.ToString() + "; request handled: " + match.UriAbsPath + ", method: " + match.Method);
                }
                else
                {
                    Dictionary <string, RequestHandler> dir_matches = RequestHandlers.Where(val => context.Request.Url.AbsolutePath.StartsWith(val.Key) && val.Value.Method.ToString().ToLower() == context.Request.HttpMethod.ToLower() && val.Value.Type == HandlerType.File).ToDictionary(val => val.Key, val => val.Value);

                    if (dir_matches.Count > 0)
                    {
                        RequestHandler dir_match = dir_matches.First().Value;

                        if (Directory.Exists(dir_match.FilePath))
                        {
                            string relative_path = context.Request.Url.AbsolutePath.Remove(context.Request.Url.AbsolutePath.IndexOf(dir_match.UriAbsPath), dir_match.UriAbsPath.Length);

                            if (File.Exists(dir_match.FilePath + relative_path))
                            {
                                byte[] bytes = File.ReadAllBytes(dir_match.FilePath + relative_path);

                                context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                            }
                            else
                            {
                                if (Directory.Exists(dir_match.FilePath + relative_path))
                                {
                                    HandleDirectoryListing(context, dir_match.FilePath + relative_path);
                                }
                                else
                                {
                                    Handle404(context, dir_match.FilePath + relative_path.Remove(0, 1));
                                }
                            }
                        }
                        else
                        {
                            Handle404(context, dir_match.FilePath);
                        }
                    }
                    else
                    {
                        context.Response.StatusCode = 503;

                        writeStrToContext(context, "<h1>Request handler not defined.</h1>The request handler for \"<em>" + context.Request.Url.AbsolutePath + "</em>\" via method <b>" + context.Request.HttpMethod + "</b> is non-existent.");
                    }
                }
            }

            //context.Response.OutputStream.Write(data, 0, data.Length);

            try
            {
                if (context.Response.OutputStream.CanWrite)
                {
                    context.Response.Close();
                }
            }
            catch
            {
            }
        }