Пример #1
0
        public async Task <CheckedPage> CheckPage(int id, bool checkEntireDocument = false, int timeout = 30)
        {
            var node = Umbraco.TypedContent(id);

            if (node == null)
            {
                throw new ArgumentOutOfRangeException("No node could be found with an id of " + id);
            }

            if (timeout < 1)
            {
                timeout = 1;
            }

            CheckedPage page = new CheckedPage(node);

            HttpCheckerService checker = new HttpCheckerService();

            checker.Timeout = TimeSpan.FromSeconds(timeout);

            string html = await checker.GetHtmlFromUrl(new Uri(node.UrlAbsolute()));

            HtmlParsingService parser = new HtmlParsingService(new Uri(Request.RequestUri.GetLeftPart(UriPartial.Authority)));

            parser.CheckEntireDocument = checkEntireDocument;

            var links = parser.GetLinksFromHtmlDocument(html);

            if (links != null && links.Any())
            {
                page.CheckedLinks = await checker.CheckLinks(links);
            }

            return(page);
        }
        public async Task <CheckedPage> CheckPage(int id, bool checkEntireDocument = false, int timeout = 30, bool omitPortDuringChecks = false, bool checkInternalLinksOnly = false, string showErrors = null)
        {
            var node = Umbraco.TypedContent(id);

            if (node == null)
            {
                throw new ArgumentOutOfRangeException("No node could be found with an id of " + id);
            }

            timeout = timeout < 1 ? 1 : timeout;

            var page = new CheckedPage(node);

            var checker = new HttpCheckerService
            {
                Timeout = TimeSpan.FromSeconds(timeout)
            };

            string html = await checker.GetHtmlFromUrl(new Uri(node.UrlAbsolute()));

            var parser = new HtmlParsingService(new Uri(Request.RequestUri.GetLeftPart(UriPartial.Authority)), !omitPortDuringChecks)
            {
                CheckEntireDocument = checkEntireDocument
            };

            var links = parser.GetLinksFromHtmlDocument(html);

            if (links != null && links.Any())
            {
                page.CheckedLinks = checkInternalLinksOnly ? await checker.CheckLinks(links.Where(n => n.IsInternal)) : await checker.CheckLinks(links);
            }

            if (!String.IsNullOrEmpty(showErrors))
            {
                var errorStatusCodes = showErrors.Split(',').Select(e => e.Trim());
                page.CheckedLinks = page.CheckedLinks.Where(l => l.IsSuccessCode || errorStatusCodes.Contains(l.StatusCode)); // filter results by status code
            }

            return(page);
        }
        public async Task <CheckedPage> CheckPage(int id, bool checkEntireDocument = false, int timeout = 30, bool omitPortDuringChecks = false, bool checkInternalLinksOnly = false, bool onlyShowErrors = false, string showErrors = "300,400,500")
        {
            var node = Umbraco.TypedContent(id);

            if (node == null)
            {
                throw new ArgumentOutOfRangeException("No node could be found with an id of " + id);
            }

            if (timeout < 1)
            {
                timeout = 1;
            }

            CheckedPage page = new CheckedPage(node);

            HttpCheckerService checker = new HttpCheckerService();

            checker.Timeout = TimeSpan.FromSeconds(timeout);

            string html = await checker.GetHtmlFromUrl(new Uri(node.UrlAbsolute()));

            HtmlParsingService parser = new HtmlParsingService(new Uri(Request.RequestUri.GetLeftPart(UriPartial.Authority)), !omitPortDuringChecks);

            parser.CheckEntireDocument = checkEntireDocument;

            var links = parser.GetLinksFromHtmlDocument(html);

            if (links != null && links.Any())
            {
                if (checkInternalLinksOnly)
                {
                    var internalLinks = links.Where(n => n.IsInternal);
                    page.CheckedLinks = await checker.CheckLinks(internalLinks);
                }
                else
                {
                    page.CheckedLinks = await checker.CheckLinks(links);
                }
            }

            //Set Display based on error type & options
            var displayAllErrors  = showErrors == "300,400,500";
            var displayErrorCodes = showErrors.Split(',').ToList();
            var displayPrefixes   = new List <string>();

            if (!onlyShowErrors)
            {
                displayErrorCodes.Add("200");
            }

            foreach (var code in displayErrorCodes)
            {
                if (code.Length > 0)
                {
                    var prefix = code.Substring(0, 1);
                    displayPrefixes.Add(prefix);
                }
            }

            if (page.CheckedLinks != null && page.CheckedLinks.Any())
            {
                var updatedLinks = new List <Link>();

                //var linksWithCodes = page.CheckedLinks.Where(n => !string.IsNullOrEmpty(n.StatusCode)).ToList();
                //var linksWithOutCodes = page.CheckedLinks.Where(n => string.IsNullOrEmpty(n.StatusCode)).ToList();

                foreach (var checkedLink in page.CheckedLinks.ToList())
                {
                    //Link with status code
                    if (!string.IsNullOrEmpty(checkedLink.StatusCode))
                    {
                        var statusCodePrefix = checkedLink.StatusCode.Substring(0, 1);
                        if (displayPrefixes.Contains(statusCodePrefix))
                        {
                            checkedLink.IsDisplayCode = true;
                        }
                        else
                        {
                            checkedLink.IsDisplayCode = false;
                        }
                    }
                    else //Link without status code
                    {
                        if (displayAllErrors)
                        {
                            //Also show errors without a code
                            checkedLink.IsDisplayCode = true;
                        }
                        else
                        {
                            checkedLink.IsDisplayCode = false;
                        }
                    }

                    updatedLinks.Add(checkedLink);
                }


                //foreach (var code in displayErrorCodes)
                //{
                //    if (code.Length > 0)
                //    {
                //        var prefix = code.Substring(0, 1);
                //        var matches = linksWithCodes.Where(n => n.StatusCode.StartsWith(prefix)).ToList();
                //        if (matches.Any())
                //        {
                //            foreach (var match in matches)
                //            {
                //                match.IsDisplayCode = true;
                //                updatedLinks.Add(match);
                //            }
                //        }
                //    }
                //}

                //if (displayAllErrors)
                //{
                //    //Also show errors without a code
                //    foreach (var match in linksWithOutCodes)
                //    {
                //        match.IsDisplayCode = true;
                //        updatedLinks.Add(match);
                //    }
                //}

                page.CheckedLinks = updatedLinks;
            }

            return(page);
        }