Esempio n. 1
0
        public ViewPageRef AddTemplate(string templatePath, string templateContents)
        {
            var templateFile = VirtualPathProvider.GetFile(templatePath);
            var templateName = templateFile.Name.WithoutExtension();

            TemplateService templateService;

            if (!templateServices.TryGetValue(templateFile.Extension, out templateService))
            {
                throw new ConfigurationErrorsException(
                          "No BaseType registered with extension " + templateFile.Extension + " for template " + templateFile.Name);
            }

            var template = new ViewPageRef(this, templatePath, templateName, templateContents, RazorPageType.Template)
            {
                LastModified = templateFile.LastModified,
                Service      = templateService,
            };

            MasterPageTemplates.Add(templatePath, template);

            try
            {
                //template.Compile();
                TemplateProvider.QueuePageToCompile(template);
                return(template);
            }
            catch (Exception ex)
            {
                Log.Error("AddViewPage() template.Prepare(): " + ex.Message, ex);
                return(null);
            }
        }
Esempio n. 2
0
        private void HandleCompilationException(ViewPageRef page, Exception ex)
        {
            var tcex = ex as TemplateCompilationException;

            if (page == null)
            {
                Log.Error("Error compiling Razor page", ex);
                if (tcex != null)
                {
                    Log.Error(tcex.Errors.Dump());
                }
                return;
            }

            if (tcex != null)
            {
                Log.Error("Error compiling page {0}".Fmt(page.Name));
                Log.Error(tcex.Errors.Dump());
            }

            var errorViewPage = new ErrorViewPage(this, ex)
            {
                Name     = page.Name,
                PageType = page.PageType,
                FilePath = page.FilePath,
            };

            errorViewPage.Compile();
            AddViewPage(errorViewPage);
            Log.Error("Razor AddViewPage() page.Prepare(): " + ex.Message, ex);
        }
Esempio n. 3
0
        public void AddPage(ViewPageRef page)
        {
            try
            {
                TemplateProvider.QueuePageToCompile(page);
                AddViewPage(page);
            }
            catch (Exception ex)
            {
                HandleCompilationException(page, ex);
            }

            try
            {
                var templatePath = page.Template;
                if (page.Template == null)
                {
                    return;
                }

                if (MasterPageTemplates.ContainsKey(templatePath))
                {
                    return;
                }

                var templateFile     = VirtualPathProvider.GetFile(templatePath);
                var templateContents = GetPageContents(templateFile);
                AddTemplate(templatePath, templateContents);
            }
            catch (Exception ex)
            {
                Log.Error("Error compiling template " + page.Template + ": " + ex.Message, ex);
            }
        }
Esempio n. 4
0
        public void ReloadIfNeeeded(ViewPageRef razorPage)
        {
            if (razorPage == null || razorPage.FilePath == null || !WatchForModifiedPages)
            {
                return;
            }

            var latestPage = GetLatestPage(razorPage);

            if (latestPage.LastModified > razorPage.LastModified)
            {
                razorPage.Reload(GetPageContents(latestPage), latestPage.LastModified);
            }

            ViewPageRef template;

            if (razorPage.Template != null &&
                this.MasterPageTemplates.TryGetValue(razorPage.Template, out template))
            {
                latestPage = GetLatestPage(template);
                if (latestPage.LastModified > template.LastModified)
                {
                    template.Reload(GetPageContents(latestPage), latestPage.LastModified);
                }
            }
        }
Esempio n. 5
0
        public bool ProcessRazorPage(IHttpRequest httpReq, ViewPageRef razorPage, object dto, IHttpResponse httpRes)
        {
            //Add extensible way to control caching
            //httpRes.AddHeaderLastModified(razorPage.GetLastModified());

            var razorTemplate = ExecuteTemplate(dto, razorPage.PageName, razorPage.Template, httpReq, httpRes);
            var html          = razorTemplate.Result;

            var htmlBytes = html.ToUtf8Bytes();

            //var hasBom = html.Contains((char)65279);
            //TODO: Replace sad hack replacing the BOM with whitespace (ASP.NET+Mono):
            for (var i = 0; i < htmlBytes.Length - 3; i++)
            {
                if (htmlBytes[i] == 0xEF && htmlBytes[i + 1] == 0xBB && htmlBytes[i + 2] == 0xBF)
                {
                    htmlBytes[i]     = (byte)' ';
                    htmlBytes[i + 1] = (byte)' ';
                    htmlBytes[i + 2] = (byte)' ';
                }
            }

            httpRes.OutputStream.Write(htmlBytes, 0, htmlBytes.Length);

            var disposable = razorTemplate as IDisposable;

            if (disposable != null)
            {
                disposable.Dispose();
            }
            httpRes.EndServiceStackRequest(skipHeaders: true);

            return(true);
        }
Esempio n. 6
0
        private void AddViewPage(ViewPageRef page)
        {
            switch (page.PageType)
            {
            case RazorPageType.ViewPage:
                ViewPages.Add(page.Name, page);
                break;

            case RazorPageType.SharedViewPage:
                ViewSharedPages.Add(page.Name, page);
                break;

            case RazorPageType.ContentPage:
                ContentPages.Add(page.FilePath.WithoutExtension().TrimStart(DirSeps), page);
                break;
            }
        }
Esempio n. 7
0
        private IVirtualFile GetLatestPage(ViewPageRef razorPage)
        {
            var file = VirtualPathProvider.GetFile(razorPage.FilePath);

            return(file);
        }
Esempio n. 8
0
        public void Configure(IAppHost appHost)
        {
            this.AppHost = appHost;
            appHost.ViewEngines.Add(this);

            //Default to watching modfied pages in DebugMode
            if (!WatchForModifiedPages)
            {
                WatchForModifiedPages = appHost.Config.DebugMode;
            }

            //Default to parallel execution in DebugMode
            if (!this.TemplateProvider.CompileInParallelWithNoOfThreads.HasValue)
            {
                this.TemplateProvider.CompileInParallelWithNoOfThreads = appHost.Config.DebugMode
                    ? Environment.ProcessorCount * 2
                    : 0;
            }

            foreach (var ns in EndpointHostConfig.RazorNamespaces)
            {
                TemplateNamespaces.Add(ns);
            }

            this.ReplaceTokens = appHost.Config.HtmlReplaceTokens ?? new Dictionary <string, string>();
            var webHostUrl = appHost.Config.WebHostUrl;

            if (VirtualPathProvider == null)
            {
                VirtualPathProvider = AppHost.VirtualPathProvider;
            }

            Init();

            RegisterRazorPages(appHost.Config.WebHostPhysicalPath);

            appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => {
                ViewPageRef razorPage = null;

                if (catchAllPathsNotFound.Contains(pathInfo))
                {
                    return(null);
                }

                try
                {
                    razorPage = FindByPathInfo(pathInfo);
                }
                catch (TemplateCompilationException tcex)
                {
                    if (appHost.Config.DebugMode && tcex.HttpCompileException != null)
                    {
                        throw tcex.HttpCompileException;
                    }
                    throw;
                }

                if (WatchForModifiedPages)
                {
                    ReloadIfNeeeded(razorPage);
                }

                if (razorPage == null)
                {
                    foreach (var entry in RazorExtensionBaseTypes)
                    {
                        if (pathInfo.EndsWith("." + entry.Key))
                        {
                            pathInfo = pathInfo.EndsWith(DefaultPage + "." + entry.Key, StringComparison.InvariantCultureIgnoreCase)
                                ? pathInfo.Substring(0, pathInfo.Length - (DefaultPage + "." + entry.Key).Length)
                                : pathInfo.WithoutExtension();

                            return(new RedirectHttpHandler {
                                AbsoluteUrl = webHostUrl.IsNullOrEmpty()
                                    ? null
                                    : webHostUrl.CombineWith(pathInfo),
                                RelativeUrl = webHostUrl.IsNullOrEmpty()
                                    ? pathInfo
                                    : null
                            });
                        }
                    }

                    if (catchAllPathsNotFound.Count > 1000) //prevent DDOS
                    {
                        catchAllPathsNotFound = new HashSet <string>();
                    }

                    var tmp = new HashSet <string>(catchAllPathsNotFound)
                    {
                        pathInfo
                    };
                    catchAllPathsNotFound = tmp;
                    return(null);
                }

                return(new RazorHandler(pathInfo)
                {
                    RazorFormat = this,
                    RazorPage = razorPage,
                    RequestName = "RazorPage",
                });
            });
        }