Example #1
0
        public static WebRequestRouteMatch FromRegexMatch(Regex pattern, Match match)
        {
            Contract.Requires(pattern != null);
            Contract.Requires(match != null);

            WebRequestRouteMatch routeMatch = new WebRequestRouteMatch();

            foreach (string groupName in pattern.GetGroupNames())
            {
                if (groupName == null)
                {
                    continue;
                }

                Group group = match.Groups[groupName];

                if (@group.Captures.Count > 1)
                {
                    throw new NotImplementedException("group.Captures.Count > 1");
                }

                if ([email protected])
                {
                    routeMatch.AddParameter(groupName, null);
                }
                else
                {
                    routeMatch.AddParameter(groupName, @group.Captures[0].Value);
                }
            }

            return(routeMatch);
        }
Example #2
0
        public bool ProcessIfMatch(IWebContext context, out IWebCommandResult result)
        {
            result = null;

            if (context.HttpMethod != httpMethod)
            {
                return(false);
            }

            UriBuilder urlBuilder = new UriBuilder(context.Url);

            // ensure the URL path is decoded
            string urlPath = HttpUtility.UrlDecode(urlBuilder.Path);

            if (urlPath == null)
            {
                throw new InvalidOperationException("Cannot process the URL '{0}'".Fmt(context.Url));
            }

            string applicationPath = context.ApplicationPath;

            if (applicationPath != null)
            {
                if (!urlPath.StartsWith(applicationPath, StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }

                bool endsWithSlash = applicationPath.EndsWith("/", StringComparison.Ordinal);
                int  startIndex    = applicationPath.Length - (endsWithSlash ? 1 : 0);
                if (startIndex > urlPath.Length)
                {
                    return(false);
                }

                urlPath = urlPath.Substring(startIndex);
            }

            if (urlPath.Length > 1)
            {
                int endCharsCutOff = 0;
                if (urlPath.EndsWith("/", StringComparison.OrdinalIgnoreCase))
                {
                    endCharsCutOff++;
                }

                urlPath = urlPath.Substring(1, urlPath.Length - (1 + endCharsCutOff));
            }
            else
            {
                if (urlPath.Length >= 1 && urlPath[0] != '/')
                {
                    throw new InvalidOperationException("URL path is invalid: '{0}'".Fmt(urlPath));
                }
                urlPath = string.Empty;
            }

            Match match = routeRegex.Match(urlPath);

            if (!match.Success)
            {
                return(false);
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Executing web command {0}", command);
            }

            WebRequestRouteMatch routeMatch = WebRequestRouteMatch.FromRegexMatch(routeRegex, match);

            result = command.Execute(context, routeMatch);

            return(true);
        }