public IParsedRedirect ParseRedirect(
            IRedirect redirect)
        {
            var oldUrlParsed = _urlParser.Parse(
                redirect.OldUrl,
                _configuration.DefaultUrl,
                true);
            var newUrlParsed = _urlParser.Parse(
                redirect.NewUrl,
                _configuration.DefaultUrl,
                false);

            return(new ParsedRedirect
            {
                OldUrl = new Url
                {
                    Raw = redirect.OldUrl,
                    Parsed = oldUrlParsed,
                    Formatted = _urlFormatter.Format(
                        oldUrlParsed)
                },
                NewUrl = new Url
                {
                    Raw = redirect.NewUrl,
                    Parsed = newUrlParsed,
                    Formatted = _urlFormatter.Format(
                        newUrlParsed)
                },
                RedirectType = redirect.RedirectType
            });
        }
        public string Build(IEnumerable <IRedirect> redirects)
        {
            var routingRules = new List <string>();

            foreach (var redirect in redirects)
            {
                var oldUrlParsed = urlParser.Parse(
                    redirect.OldUrl,
                    configuration.DefaultUrl);
                var newUrlParsed = urlParser.Parse(
                    redirect.NewUrl,
                    configuration.DefaultUrl);

                var oldUrlHasRootPath = oldUrlParsed.Path.Equals("/");

                if (configuration.ExcludeOldUrlRootRedirects && oldUrlHasRootPath)
                {
                    continue;
                }

                routingRules.Add(
                    string.Format(
                        routingRuleTemplate,
                        BuildCondition(oldUrlParsed),
                        BuildRedirect(newUrlParsed)));
            }

            return(string.Format(
                       routingRulesTemplate,
                       string.Join(Environment.NewLine, routingRules)));
        }
        private IEnumerable <Rewrite> BuildRewrites(
            IEnumerable <IRedirect> redirects)
        {
            foreach (var redirect in redirects.ToList())
            {
                var oldUrlParsed = _urlParser.Parse(
                    redirect.OldUrl,
                    _configuration.DefaultUrl);
                var newUrlParsed = _urlParser.Parse(
                    redirect.NewUrl,
                    _configuration.DefaultUrl);

                var oldUrlHasRootPath = oldUrlParsed.Path.Equals("/");

                if (_configuration.ExcludeOldUrlRootRedirects && oldUrlHasRootPath)
                {
                    continue;
                }

                var segments = oldUrlParsed.Path.Split('/').Length - 1;

                var rewriteKey = BuildRewriteKey(
                    redirect.RedirectType,
                    segments,
                    redirect.OldUrlHasHost,
                    oldUrlParsed.Host,
                    oldUrlParsed.Path,
                    oldUrlParsed.Query,
                    oldUrlHasRootPath,
                    redirect.NewUrlHasHost,
                    newUrlParsed.Host);

                var rewriteName = BuildRewriteName(
                    redirect.RedirectType,
                    segments,
                    redirect.OldUrlHasHost,
                    oldUrlParsed.Host,
                    oldUrlParsed.Path,
                    oldUrlParsed.Query,
                    oldUrlHasRootPath,
                    redirect.NewUrlHasHost,
                    newUrlParsed.Host);

                yield return(new Rewrite
                {
                    Id = rewriteKey.ToMd5().ToLower(),
                    Key = rewriteKey,
                    Name = rewriteName,
                    RedirectType = redirect.RedirectType,
                    OldUrlHasRootPath = oldUrlHasRootPath,
                    OldUrlHasHost = redirect.OldUrlHasHost,
                    NewUrlHasHost = redirect.NewUrlHasHost,
                    OldUrl = oldUrlParsed,
                    NewUrl = newUrlParsed
                });
            }
        }
        public HttpResponse Get(string url)
        {
            var parsedUrl = _urlParser.Parse(
                url,
                null);

            if (!parsedUrl.IsValid)
            {
                throw new HttpException(
                          string.Format(
                              "Invalid url '{0}'",
                              url));
            }

            var host =
                _idn.GetAscii(parsedUrl.Host);

            var response = SendRequest(
                host,
                parsedUrl.Port,
                parsedUrl.Scheme.ToLower().StartsWith("https"),
                BuildRequest(
                    "GET",
                    host,
                    parsedUrl.PathAndQuery));

            return(ParseData(
                       response));
        }
        private MatchingRedirectResult GetMatchingRedirect(string url)
        {
            if (_oldUrlRedirectIndex.ContainsKey(url))
            {
                return(new MatchingRedirectResult
                {
                    HasMatch = true,
                    ResultRedirectType = RedirectType.Exact,
                    ParsedRedirect = _oldUrlRedirectIndex[url]
                });
            }

            var currentUrl = url;

            while (
                !string.IsNullOrWhiteSpace(currentUrl) &&
                !_oldUrlRedirectIndex.ContainsKey(currentUrl))
            {
                var parsedCurrentUrl = _urlParser.Parse(
                    currentUrl,
                    _configuration.DefaultUrl);

                var parentPath =
                    _urlHelper.GetParentPath(parsedCurrentUrl.Path);

                if (string.IsNullOrWhiteSpace(parentPath))
                {
                    return(new MatchingRedirectResult
                    {
                        HasMatch = false
                    });
                }

                parsedCurrentUrl.Path = parentPath;
                currentUrl            = _urlFormatter.Format(parsedCurrentUrl);
            }

            var parsedRedirect = !string.IsNullOrWhiteSpace(currentUrl) &&
                                 _oldUrlRedirectIndex.ContainsKey(currentUrl)
                                ? _oldUrlRedirectIndex[currentUrl]
                                : null;

            if (parsedRedirect != null &&
                parsedRedirect.RedirectType == RedirectType.Exact)
            {
                return(new MatchingRedirectResult
                {
                    HasMatch = false
                });
            }

            return(new MatchingRedirectResult
            {
                HasMatch = parsedRedirect != null,
                ResultRedirectType = RedirectType.Replace,
                ParsedRedirect = parsedRedirect
            });
        }
Exemplo n.º 6
0
        public void Process(IUrlParser parser, IEvaluator evaluator)
        {
            if (parser == null) throw new ArgumentNullException("parser");
            if (evaluator == null) throw new ArgumentNullException("evaluator");

            var words = parser.Parse(_url);
            var buzzwords = words.Where(evaluator.IsBuzzword);
            foreach (var buzzword in buzzwords)
            {
                ApplyEvent(new BuzzwordFoundEvent(buzzword));
            }
        }
Exemplo n.º 7
0
        public string Combine(
            string url1,
            string url2)
        {
            var url1Parsed = _urlParser.Parse(
                url1,
                _configuration.DefaultUrl);

            if (!url1Parsed.IsValid)
            {
                throw new UriFormatException(
                          string.Format(
                              "Url '{0}' format is not valid",
                              url1));
            }

            var url2Parsed = _urlParser.Parse(
                url2,
                _configuration.DefaultUrl);

            if (!url2Parsed.IsValid)
            {
                throw new UriFormatException(
                          string.Format(
                              "Url '{0}' format is not valid",
                              url2));
            }

            return(_urlFormatter.Format(
                       new ParsedUrl
            {
                Scheme = url1Parsed.Scheme,
                Host = url1Parsed.Host,
                Port = url1Parsed.Port,
                Path = url2Parsed.Path,
                Query = url2Parsed.Query
            }));
        }
        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            if (reader.Value == null)
            {
                return(null);
            }

            return(_urlParser.Parse(
                       reader.Value.ToString(),
                       null));
        }
Exemplo n.º 9
0
        /// <summary>
        /// writes parsed URLs to output
        /// </summary>
        public void Convert()
        {
            var urls   = provider.GetUrls();
            var parsed = urls.Select(u =>
            {
                var result = parser.Parse(u);
                if (result == null)
                {
                    logger.Log(u);
                }

                return(result);
            });

            writer.WriteUrls(parsed);
        }
Exemplo n.º 10
0
        public XDocument Parse()
        {
            var sourceUrls = _dataProvider.GetUrlsData();

            var parsedUrls = sourceUrls.Select(uri => _parser.Parse(uri));

            var document = new XDocument();

            var urlAdresses = new XElement("urlAddresses");

            foreach (var url in parsedUrls)
            {
                urlAdresses.Add(CreateXmlUrlAdressNode(url));
            }

            document.Add(urlAdresses);

            return(document);
        }
Exemplo n.º 11
0
        public string Replace(string url, IParsedRedirect parsedRedirect)
        {
            var parsedUrl = _urlParser.Parse(
                url,
                _configuration.DefaultUrl);

            var oldUrlSegments = parsedUrl.Path.Split('/');
            var newUrlSegments = parsedRedirect.NewUrl.Parsed.Path.Split('/');

            var replacedUrlSegments = newUrlSegments.Concat(
                oldUrlSegments
                .Skip(newUrlSegments.Length)
                .Take(oldUrlSegments.Length - newUrlSegments.Length));

            var replacedPath = string.Join(
                "/",
                replacedUrlSegments);

            parsedUrl.Path = replacedPath;

            return(_urlFormatter.Format(parsedUrl));
        }
Exemplo n.º 12
0
 /// <summary>Finds an item by traversing names from the starting point root.</summary>
 /// <param name="url">The url that should be traversed.</param>
 /// <returns>The content item matching the supplied url.</returns>
 public ContentItem Parse(string url)
 {
     return(inner.Parse(url));
 }
        public void ParseUrlWithAbsoluteUri()
        {
            var rawUrl = "http://www.example.local";
            var url    = _urlParser.Parse(
                rawUrl,
                null);

            Assert.IsNotNull(url);
            Assert.AreEqual(
                "http",
                url.Scheme);
            Assert.AreEqual(
                "www.example.local",
                url.Host);
            Assert.AreEqual(
                80,
                url.Port);
            Assert.AreEqual(
                "/",
                url.Path);
        }