Exemple #1
0
        public ActionResult CheckIfUrlIsOk(int articleId)
        {
            var ok      = true;
            var metaKey = "x-frame-options-allowed";

            var article        = FeedService.GetArticle(articleId);
            var alreadyChecked = article.GetMetadataValue <bool?>(metaKey);

            if (alreadyChecked.HasValue)
            {
                return(Json(alreadyChecked));
            }

            try
            {
                var url     = article.Url;
                var request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "HEAD";
                request.Headers["Accept-Encoding"] = "gzip,deflate";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36";
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                request.KeepAlive = true;
                request.Timeout   = 10000;
                request.MaximumAutomaticRedirections = 3;
                request.MaximumResponseHeadersLength = 1024 * 64 * 64;
                if (request != null)
                {
                    var response      = request.GetResponse() as HttpWebResponse;
                    var headers       = response.Headers;
                    var xFrameOptions = string.Empty;

                    if (response != null)
                    {
                        response.Close();
                    }

                    if (response.Headers != null)
                    {
                        xFrameOptions = response.Headers["X-Frame-Options"];
                    }
                    if (!xFrameOptions.IsNullOrEmpty())
                    {
                        if (xFrameOptions == "deny" ||
                            xFrameOptions == "SAMEORIGIN" ||
                            !xFrameOptions.Contains("rssheap"))
                        {
                            ok = false;
                            FeedService.FlagArticle(articleId);
                        }
                    }
                }
            }
            catch { }

            article.AddMetadata(metaKey, ok);
            article.SaveMetadata();
            return(Json(ok));
        }