示例#1
0
        public static void Execute(HttpExchange ex)
        {
            string       rawCode = File.ReadAllText(ex.requestedResource);
            CodeExecuter ce      = new CodeExecuter(ex, rawCode);

            currentProcessor = ce;
            Exception e;

            if (!ce.Process(out e))
            {
                ex.ProcessResponse(ErrorFormatter.FormatServerError(500, "Internal Server Error", "The LWASP Server had a problem while processing " + ex.requestedResource + ".<br/>Message: " + e.Message));
            }
            else
            {
                object   o;
                string[] output = ce.Execute(out o);
                if (output == null)                   // TODO complete this code
                {
                    if (o is CompilerErrorCollection) // Compilation error
                    {
                        string errorCollection = "";
                        foreach (CompilerError er in (CompilerErrorCollection)o)
                        {
                            errorCollection += ErrorFormatter.FormatCompilerError(er, ce.FinalCode) + "<br/>";
                        }
                        ex.ProcessResponse(errorCollection);
                    }
                    else if (o is Exception) // Runtime error
                    {
                        if (((Exception)o).Message == "LWASP Error")
                        {
                            ex.oWrite("<h1>500 Internal Server Error</h1><br/>Server error message was: " + e.Message);
                            ex.oClose();
                        }
                        else
                        {
                            ex.ProcessResponse(ErrorFormatter.FormatException((Exception)o, ce.FinalCode));
                        }
                    }
                }
                else
                {
                    string fCode = ce.BaseCode;
                    for (int i = 0; i < output.Length; i++)
                    {
                        fCode = fCode.ReplaceFirst("{LWASP_CODE_SEGMENT_" + i + "_}", output[i]);
                    }
                    currentProcessor.connection.ProcessResponse(fCode.Trim());
                }
            }
        }
示例#2
0
        /// <summary>
        /// Includes other files in the code, using the include tag
        /// </summary>
        /// <param name="code">The code to be included</param>
        /// <param name="resourceName">The list of resources, starting from the requested resource and ending with the current</param>
        /// <returns>The final code (without include tags)</returns>
        public static string GenerateInclusions(string code, List <string> resourceName)
        {
            Regex r = new Regex(@"<%include (.+?)%>", RegexOptions.IgnoreCase);
            Match m = r.Match(code);

            while (m.Success)
            {
                string included = m.Value.ReplaceFirst("<%include", "").ReplaceFirst("%>", "").Trim();
                string filePath = "web_docs" + (included.StartsWith("/") ? "" : "/") + included;
                try
                {
                    resourceName.Add(included);
                    code = code.Replace(m.Value, GenerateInclusions(TraceWebpage(File.ReadAllText(filePath), resourceName[resourceName.Count - 1]), resourceName));
                }
                catch
                {
                    code = code.Replace(m.Value, ErrorFormatter.FormatIncludeError(resourceName));
                }
                resourceName.Remove(included);
                m = m.NextMatch();
            }
            return(code);
        }