Esempio n. 1
0
        public static string RemoveProxyHttpUrls(string html)
        {
            var baseHandlerUrl = MailStoragePathCombiner.GetProxyHttpHandlerUrl() + "?url=";

            var doc = new HtmlDocument();

            doc.LoadHtml(html);

            var nodesWithProxy = doc.DocumentNode.DescendantsAndSelf()
                                 .Where(n => n.HasAttributes && n.Attributes.Any(a => a.Value.Contains(baseHandlerUrl)))
                                 .ToList();

            var needRewrite = false;

            foreach (var n in nodesWithProxy)
            {
                var attributes = n.Attributes.Where(a => a.Value.Contains(baseHandlerUrl));

                foreach (var attribute in attributes)
                {
                    var splited = attribute.Value.Split(new[] { baseHandlerUrl },
                                                        StringSplitOptions.RemoveEmptyEntries);

                    if (!splited.Any())
                    {
                        continue;
                    }

                    var raw = splited.Length > 1 ? splited[1] : splited[0];

                    var end = raw.IndexOfAny(new[] { '\"', ')' });

                    var encodedUrl = end == -1 ? raw : raw.Substring(0, end);

                    var url = Encoding.UTF8.GetString(Convert.FromBase64String(encodedUrl));

                    attribute.Value = attribute.Value.Replace(baseHandlerUrl + encodedUrl, url);

                    needRewrite = true;
                }
            }

            return(needRewrite ? doc.DocumentNode.OuterHtml : html);
        }