URLParser

URLParser

URL parsing is done through objects which are subclasses of the URLParser class. Application delegates most of the URL parsing to these objects.

Application has a single “root” URL parser, which is used to parse all URLs. This parser then can pass the request on to other parsers, usually taking off parts of the URL with each step.

This root parser is generally ContextParser, which is instantiated and set up by Application (accessible through Application.rootURLParser).

class URLParser.ContextParser(app)

Bases: URLParser

Find the context of a request.

ContextParser uses the Application.config context settings to find the context of the request. It then passes the request to a FileParser rooted in the context path.

The context is the first element of the URL, or if no context matches that then it is the default context (and the entire URL is passed to the default context’s FileParser).

There is generally only one ContextParser, which can be found as application.rootURLParser().

__init__(app)

Create ContextParser.

ContextParser is usually created by Application, which passes all requests to it.

In __init__ we take the Contexts setting from Application.config and parse it slightly.

absContextPath(path)

Get absolute context path.

Resolves relative paths, which are assumed to be relative to the Application’s serverSidePath (the working directory).

addContext(name, path)

Add a context to the system.

The context will be imported as a package, going by name, from the given directory path. The directory doesn’t have to match the context name.

findServletForTransaction(trans)

Returns a servlet for the transaction.

This is the top-level entry point, below it parse is used.

parse(trans, requestPath)

Parse request.

Get the context name, and dispatch to a FileParser rooted in the context’s path.

The context name and file path are stored in the request (accessible through Request.serverSidePath and Request.contextName).

resolveDefaultContext(dest)

Find default context.

Figure out if the default context refers to an existing context, the same directory as an existing context, or a unique directory.

Returns the name of the context that the default context refers to, or ‘default’ if the default context is unique.

class URLParser.ServletFactoryManagerClass

Bases: object

Manage servlet factories.

This singleton (called ServletFactoryManager) collects and manages all the servlet factories that are installed.

See addServletFactory for adding new factories, and servletForFile for getting the factories back.

__init__()
addServletFactory(factory)

Add a new servlet factory.

Servlet factories can add themselves with:

ServletFactoryManager.addServletFactory(factory)

The factory must have an extensions method, which should return a list of extensions that the factory handles (like ['.ht']). The special extension .* will match any file if no other factory is found. See ServletFactory for more information.

factoryForFile(path)

Get a factory for a filename.

reset()
servletForFile(trans, path)

Get a servlet for a filename and transaction.

Uses factoryForFile to find the factory, which creates the servlet.

class URLParser.URLParameterParser(fileParser=None)

Bases: URLParser

Strips named parameters out of the URL.

E.g. in /path/SID=123/etc the SID=123 will be removed from the URL, and a field will be set in the request (so long as no field by that name already exists – if a field does exist the variable is thrown away). These are put in the place of GET or POST variables.

It should be put in an __init__, like:

from URLParser import URLParameterParser
urlParserHook = URLParameterParser()

Or (slightly less efficient):

from URLParser import URLParameterParser as SubParser

__init__(fileParser=None)
findServletForTransaction(trans)

Returns a servlet for the transaction.

This is the top-level entry point, below it parse is used.

parse(trans, requestPath)

Delegates to parseHook.

static parseHook(trans, requestPath, hook)

Munges the path.

The hook is the FileParser object that originally called this – we just want to strip stuff out of the URL and then give it back to the FileParser instance, which can actually find the servlet.

class URLParser.URLParser

Bases: object

URLParser is the base class for all URL parsers.

Though its functionality is sparse, it may be expanded in the future. Subclasses should implement a parse method, and may also want to implement an __init__ method with arguments that control how the parser works (for instance, passing a starting path for the parser)

The parse method is where most of the work is done. It takes two arguments – the transaction and the portion of the URL that is still to be parsed. The transaction may (and usually is) modified along the way. The URL is passed through so that you can take pieces off the front, and then pass the reduced URL to another parser. The method should return a servlet (never None).

If you cannot find a servlet, or some other (somewhat) expected error occurs, you should raise an exception. HTTPNotFound probably being the most interesting.

findServletForTransaction(trans)

Returns a servlet for the transaction.

This is the top-level entry point, below it parse is used.

URLParser.application()

Returns the global Application.

URLParser.initApp(app)

Initialize the application.

Installs the proper servlet factories, and gets some settings from Application.config. Also saves the application in _globalApplication for future calls to the application() function.

This needs to be called before any of the URLParser-derived classes are instantiated.

URLParser.initParser(app)

Initialize the FileParser Class.