예제 #1
0
        void parser_TagParsed(ILocation location, TagType tagtype, string id, TagAttributes attributes)
        {
            string tmp = "";

            if (String.Compare(id, "script", true) == 0)
            {
                if (tagtype == TagType.Tag)
                {
                    parser.VerbatimID = "script";
                }
            }

            switch (tagtype)
            {
            case TagType.Tag:
            case TagType.Text:
            case TagType.ServerComment:
            case TagType.SelfClosing:
            case TagType.Include:
            case TagType.DataBinding:
            case TagType.Close:
            case TagType.CodeRenderExpression:
                // AppendText(location.PlainText, true);
                AppendText(location.PlainText);
                break;

            case TagType.Directive:
                if (id.CompareTo("Import") == 0)
                {
                    eb.AppendNameSpace(attributes["Namespace"].ToString());
                }
                else if (id.CompareTo("Assembly") == 0)
                {
                    eb.AppendAssembly(attributes["Name"].ToString());
                }
                else if (id.CompareTo("SessionRequest") == 0)
                {
                    if (attributes["Check"] != null)
                    {
                        eb.AppendCodeIfAndReturn(m, "!CheckSession(false)");
                    }
                    else if (attributes["CheckAndCreate"] != null)
                    {
                        eb.AppendCodeIfAndReturn(m, "!CheckSession(true)");
                    }
                    else if (attributes["CheckManual"] != null)
                    {
                        break;
                    }
                }
                break;

            case TagType.CodeRender:
                AppendText(id, false);
                break;
            }
        }
예제 #2
0
        public override void Interpret()
        {
            if (File.Exists(context.Request.PhysicalPathFile))
            {
                using (FileStream fs = new FileStream(context.Request.PhysicalPathFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // calculamos el hashcode para
                    string hashcode      = Conversion.CalculateHashCode(fs);
                    string mainClassName = "CodeRender_" + Path.GetFileNameWithoutExtension(context.Request.PhysicalPathFile);

                    Assembly asam = cache.Get(hashcode) as Assembly;

                    if (asam == null)
                    {
                        StreamReader reader = new StreamReader(fs, Encoding.Default, true, 4096);
                        parser             = new AspParser(context.Request.PhysicalPathFile, reader);
                        parser.TagParsed  += new TagParsedHandler(parser_TagParsed);
                        parser.TextParsed += new TextParsedHandler(parser_TextParsed);
                        parser.Error      += new ParseErrorHandler(parser_Error);

                        eb = new EvaluatorBuilder();

                        eb.BeginCompile("ASP");
                        eb.AppendNameSpace("System");
                        eb.AppendNameSpace("System.Web");

                        CodeTypeDeclaration c = eb.AppendClass(mainClassName, typeof(DinamicPage));

                        // eb.CreatePropertyGetForObject(c, context.App.GetType(), "App", "Context.App");
                        // eb.CreatePropertyGetForObject(c, context.SessionRequest == null ? typeof(Object) : context.SessionRequest.GetType(), "SessionRequest", "Context.SessionRequest");

                        m = eb.AppendMethod(c, "Render", MemberAttributes.Override);

                        parser.Parse();

                        asam = eb.EndCompile();

                        // añadimos al cache
                        cache.Add(hashcode, asam);
                    }

                    DinamicPage page = asam.CreateInstance("ASP." + mainClassName)
                                       as DinamicPage;

                    page.Me              = this;
                    page.Context         = context;
                    page.OutStream       = new StringBuilder();
                    context.Reply.Render = page;

                    try
                    {
                        page.Render();
                    }
                    catch (Exception err)
                    {
                        page.OutStream.AppendFormat("Error: {0}\n{1}", err.Message, err.StackTrace);
                    }

                    // forzar descarga de archivo
                    if (!context.Reply.ForcedFileDownload && !context.Reply.LoadedStream)
                    {
                        context.Reply.LoadText(page.OutStream.ToString());
                    }
                }
            }
            else
            {
                context.Reply.Code = Codes.NOT_FOUND;
            }
        }