Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

New route resolution engine #988

Merged
merged 15 commits into from
Feb 16, 2013
Merged

New route resolution engine #988

merged 15 commits into from
Feb 16, 2013

Conversation

grumpydev
Copy link
Member

Switched from the old approach to a route Trie, which is faster, and much more readable.

Also removed the Indiana Jones and the Tuple of Doom that the old route resolver was returning and replaced it with a class.

Routing should (hopefully) be unaffected, but to be clear the new system works on route length and scoring as follows:

  • Longest routes take precedence - if a route has 5 matched segments it will win over one with 4, regardless of what kind of nodes they were. In the case of node that have 'default' values, if the default value is used then the node isn't counted towards the length.
  • Node scoring - if more than one result has the same length then each node in each route is totted up to a score, and the highest score wins. The scores for each node type are currently:
    • Literal node - 10000
    • Capture node - 1000
    • Regex node - 1000
    • Greedy capture node - 0

The full set of node types are:

  • Literal node - just text
  • Capture node - {foo}
  • Optional capture node - {foo?}
  • Optional capture node with default - {foo?defaultValue}
  • RegEx node - (?\d{2,4})
  • Greedy capture node - {foo*}

Note that now captures are not greedy by default, you need to specify the greedy capture if that's the behaviour you want (this fixes #785)

}


//if (capturedParameters.Any())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code

grumpydev added a commit that referenced this pull request Feb 16, 2013
New route resolution engine
@grumpydev grumpydev merged commit 072cf2e into NancyFx:master Feb 16, 2013
@grumpydev grumpydev deleted the RouteTrie branch April 7, 2014 09:17
@ti24horas
Copy link

Hi,
Tried lastest nuget version (1.2.0) with self host and an error occurs.
After trying earlier versions, I found this is the offending pull.

The error occurs as following:

  1. Created a Console App(x64) self host application using Nancy.Hosting.Self (ver 0.17.0) with Nancy ver 0.17.0
  2. Created a custom module inheriting NancyModule
  3. In constructor created the following routes:
public HomeModule()
        {
            Get["test.html"] = _ => "OK";
//            Get["/test.html"] = _ => "OK"; tried this too
            
            Get["/"] = _ => "Hello world";
        }
  1. When calling http://localhost:8080/test.html, following exception appears:
Nancy.RequestExecutionException: Oh noes! ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Nancy.Routing.Trie.Nodes.TrieNode.d__7.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Nancy.Routing.Trie.RouteResolverTrie.GetMatches(String method, String path, NancyContext context)
   at Nancy.Routing.DefaultRouteResolver.Resolve(NancyContext context)
   at Nancy.Routing.DefaultRequestDispatcher.InvokeRouteResolver(NancyContext context, String path, IEnumerable`1 acceptHeaders)
   at Nancy.Routing.DefaultRequestDispatcher.Resolve(NancyContext context)
   at Nancy.Routing.DefaultRequestDispatcher.Dispatch(NancyContext context)
   at Nancy.NancyEngine.InvokeRequestLifeCycle(NancyContext context, IPipelines pipelines)
   --- End of inner exception stack trace ---
   at Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)

Tested using chrome

versions prior this (0.16.1) works as usual, same code.

@ti24horas
Copy link

Hi guys,
Just downloaded from sources, referenced Nancy and SelfHost from csproj and it worked.
Even in debug mode. Don't know why this happened.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Route matching too greedy: parsed segments may contain segment seperators
3 participants