コード例 #1
0
        public void ProcessRequest(HttpContext context)
        {
            string examplesRoot = context.Server.MapPath(UIHelpers.ApplicationRoot + "/Examples/");
            string codeRoot     = context.Server.MapPath(UIHelpers.ApplicationRoot + "/Code/");
            string path;
            string url;

            string transmit = context.Request["t"];

            if (!string.IsNullOrEmpty(transmit))
            {
                url = context.Request["e"];
                if (string.IsNullOrEmpty(url))
                {
                    return;
                }
                url  = "/Examples" + url;
                path = context.Server.MapPath(url);

                if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture) &&
                    !path.StartsWith(codeRoot, true, CultureInfo.CurrentCulture))
                {
                    return;
                }

                context.Response.ContentType = "application/octet-stream";
                context.Response.AppendHeader("Connection", "keep-alive");
                context.Response.AppendHeader("Content-Disposition",
                                              string.Concat(" attachment; filename = ", new DirectoryInfo(path).Name,
                                                            ".zip"));

                ZipFiles(path);
                context.Response.End();
                return;
            }

            context.Response.ContentType = "text/html";

            url  = context.Request["f"];
            path = context.Server.MapPath(url);
            FileInfo fi = new FileInfo(path);

            if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture) &&
                !path.StartsWith(codeRoot, true, CultureInfo.CurrentCulture))
            {
                return;
            }

            HighlighterBase hb = null;

            switch (fi.Extension.ToLowerInvariant())
            {
            case ".aspx":
            case ".ascx":
            case ".master":
                hb = new ASPXHighlighter();
                break;

            case ".cs":
                hb = new CSharpHighlighter();
                break;

            case ".xml":
            case ".xsl":
                hb = new XMLHighlighter();
                break;

            case ".js":
                hb = new JavaScriptHighlighter();
                break;

            case ".css":
                hb = new CSSHighlighter();
                break;

            default:
                context.Response.ContentType = "text/plain";
                context.Response.Write(File.ReadAllText(fi.FullName));
                return;
            }
            context.Response.Write(HighLighterUtils.SourceToHtml(File.ReadAllText(fi.FullName), hb));
        }
コード例 #2
0
        public void ProcessRequest(HttpContext context)
        {
            string examplesRoot = context.Server.MapPath(UIHelpers.ApplicationRoot + "/Examples/");
            string codeRoot     = context.Server.MapPath(UIHelpers.ApplicationRoot + "/Code/");
            string path;
            string url;

            string transmit = context.Request["t"];

            if (!string.IsNullOrEmpty(transmit))
            {
                url = context.Request["e"];
                if (string.IsNullOrEmpty(url))
                {
                    return;
                }
                url  = "/Examples" + url;
                path = context.Server.MapPath(url);

                if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture) &&
                    !path.StartsWith(codeRoot, true, CultureInfo.CurrentCulture))
                {
                    return;
                }

                context.Response.ContentType = "application/octet-stream";
                context.Response.AppendHeader("Connection", "keep-alive");
                context.Response.AppendHeader("Content-Disposition",
                                              string.Concat(" attachment; filename = ", new DirectoryInfo(path).Name,
                                                            ".zip"));

                ZipFiles(path);
                context.Response.End();
                return;
            }

            context.Response.ContentType = "text/html";

            url  = context.Request["f"];
            path = context.Server.MapPath(url);
            FileInfo fi = new FileInfo(path);

            if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture) &&
                !path.StartsWith(codeRoot, true, CultureInfo.CurrentCulture))
            {
                return;
            }

            HighlighterBase hb     = null;
            bool            markup = false;

            switch (fi.Extension.ToLowerInvariant())
            {
            case ".aspx":
            case ".ascx":
            case ".master":
                hb     = new ASPXHighlighter();
                markup = true;
                break;

            case ".cs":
                hb = new CSharpHighlighter();
                break;

            case ".xml":
            case ".xsl":
                hb = new XMLHighlighter();
                break;

            case ".js":
                hb = new JavaScriptHighlighter();
                break;

            case ".css":
                hb = new CSSHighlighter();
                break;

            default:
                context.Response.ContentType = "text/plain";
                context.Response.Write(File.ReadAllText(fi.FullName));
                return;
            }

            string fileText = File.ReadAllText(fi.FullName);

            if (markup)
            {
                string origCSScriptStart = @"<script runat=""server"">";
                string origCSScriptEnd   = @"</script>";
                string origJSScriptStart = @"<script>";
                string origJSScriptEnd   = @"</script>";
                string origCSScriptText  = CutFromTo(fileText, origCSScriptStart, origCSScriptEnd);
                string origJSScriptText  = CutFromTo(fileText, origJSScriptStart, origJSScriptEnd);

                fileText = HighLighterUtils.SourceToHtml(fileText, hb);

                if (!string.IsNullOrEmpty(origCSScriptText))
                {
                    string strCSScriptStart = CutFromTo(HighLighterUtils.SourceToHtml(origCSScriptStart, hb), ">", "</pre>");
                    string strCSScriptEnd   = CutFromTo(HighLighterUtils.SourceToHtml(origCSScriptEnd, hb), ">", "</pre>");
                    string csText           = CutFromTo(fileText, strCSScriptStart, strCSScriptEnd);

                    fileText = fileText.Replace(csText, CutFromTo(HighLighterUtils.CSharpToHtml(origCSScriptText), ">", "</pre>"));
                }

                if (!string.IsNullOrEmpty(origJSScriptText))
                {
                    string strJSScriptStart = CutFromTo(HighLighterUtils.SourceToHtml(origJSScriptStart, hb), ">", "</pre>");
                    string strJSScriptEnd   = CutFromTo(HighLighterUtils.SourceToHtml(origJSScriptEnd, hb), ">", "</pre>");
                    string jsText           = CutFromTo(fileText, strJSScriptStart, strJSScriptEnd);

                    fileText = fileText.Replace(jsText, CutFromTo(HighLighterUtils.JsToHtml(origJSScriptText), ">", "</pre>"));
                }

                context.Response.Write(fileText);
            }
            else
            {
                context.Response.Write(HighLighterUtils.SourceToHtml(fileText, hb));
            }
        }