Class | Mongrel::URIClassifier |
In: |
lib/mongrel.rb
ext/http11/http11.c |
Parent: | Object |
handler_map | [R] |
Initializes a new URIClassifier object that you can use to associate URI sequences with objects. You can actually use it with any string sequence and any objects, but it’s mostly used with URIs.
It uses TST from www.octavian.org/cs/software.html to build an ternary search trie to hold all of the URIs. It uses this to do an initial search for the a URI prefix, and then to break the URI into SCRIPT_NAME and PATH_INFO portions. It actually will do two searches most of the time in order to find the right handler for the registered prefix portion.
Registers the SampleHandler (one for all requests) with the "/someuri". When URIClassifier::resolve is called with "/someuri" it’ll return SampleHandler immediately. When called with "/someuri/iwant" it’ll also return SomeHandler immediatly, with no additional searches, but it will return path info with "/iwant".
You actually can reuse this class to register nearly anything and quickly resolve it. This could be used for caching, fast mapping, etc. The downside is it uses much more memory than a Hash, but it can be a lot faster. It’s main advantage is that it works on prefixes, which is damn hard to get right with a Hash.
Attempts to resolve either the whole URI or at the longest prefix, returning the prefix (as script_info), path (as path_info), and registered handler (usually an HttpHandler). If it doesn’t find a handler registered at the longest match then it returns nil,nil,nil.
Because the resolver uses a trie you are able to register a handler at any character in the URI and it will be handled as long as it’s the longest prefix. So, if you registered handler 1 at "/something/lik", and 2 at "/something/like/that", then a a search for "/something/like" would give you 1. A search for "/something/like/that/too" would give you 2.
This is very powerful since it means you can also attach handlers to parts of the ; (semi-colon) separated path params, any part of the path, use off chars, anything really. It also means that it’s very efficient to do this only taking as long as the URI has characters.
A slight modification to the CGI 1.2 standard is given for handlers registered to "/". CGI expects all CGI scripts to be at some script path, so it doesn’t really say anything about a script that handles the root. To make this work, the resolver will detect that the requested handler is at "/", and return that for script_name, and then simply return the full URI back as path_info.
It expects strings with no embedded ’\0’ characters. Don‘t try other string-like stuff yet.
Returns the URIs that have been registered with this classifier so far. The URIs returned should not be modified as this will cause a memory leak. You can use this to inspect the contents of the URIClassifier.