µWeb documentation - Request_Router

When a request is sent to µWeb, the first thing that needs to be made is the decision where and how to handle it. The request router is the place where this happens. The router employed in µWeb is based on regular expressions, and delegates the request to the handler associated with the first matching expression.

Before we explain everything there is know about routes and handlers, an example of how a router would look. The example given is a very stripped down version of the handler from the uweb_info example project:

 1import uweb
 2from uweb.uweb_info import pages
 3
 4PAGE_CLASS = pages.PageMaker
 5ROUTES = (
 6    ('/static/(.*)', 'Static'),
 7    ('/(broken.*)', 'FourOhFour'),
 8    ('/haltandcatchfire', 'MakeFail'),
 9    ('/json', 'Json'),
10    ('/text', 'Text'),
11    ('/redirect/(.*)', 'Redirect'),
12    ('/OpenIDLogin', '_OpenIdInitiate'),
13    ('/OpenIDValidate', '_OpenIdValidate'),
14    ('/ULF-Challenge', '_ULF_Challenge'),
15    ('/ULF-Login', '_ULF_Verify'),
16    ('/(.*)', 'Index'))
17
18uweb.ServerSetup()
In the example above, the following things happen:

When a request arrives, it is checked against all the defined routes. Each of the routes is a 2-tuple, with a regular expression and a method name (as string). The request URL is matched against the regular expression, and if it succeeds, the method name will be resolved on the PAGE_CLASS. This method will then be executed, and the router will not continue searching for a next match.

To illustrate the previous with an example request "/haltandcatchfire":

  1. The PAGE_CLASS is instantiated into a live PageMaker
  2. The first route tuple is inspected
    • Its regex is '/static/(.*)'
    • The request does not match the regex
  3. The second route tuple is inspected
    • Its regex is '/(broken.*)'
    • The request does not match the regex
  4. The third route tuple is inspected
    • Its regex is '/haltandcatchfire'
    • The request matches the regex
    • The associated handler method name is 'MakeFail'
    • The method MakeFail is resolved on our PageMaker instance, resulting in pages.PageMaker.MakeFail
    • This method is executed and its results will be sent to the client
  5. Router has ended after the third inspected route

Arguments from the request string

Oftentimes, there are parts of the request string that are needed further on in the process. While it's possible to extract these in the PageMaker methods, this is inconvenient, and the router has the means to do this. As can be seen in the router example above, some of the regular expressions have capture groups defined. These capture groups are provided to the PageMaker method as positional arguments. For example:

Requesting "/user/elmer/edit/27" on the following router:

1PAGE_CLASS = blog.BlogPages
2ROUTES = (
3    ('/user/(\w*)/edit/(\d*)', 'EditArticle'),
4    ('/', 'Index'))

Will end up calling the method blog.BlogPages.EditArticle with the arguments ('Elmer', '27'). Note that all arguments are provided as strings, type-conversion is left to the developer.

Typical regexes for request matching

How to use various other url formats:

blog comments powered by Disqus