With long middleware chains and functions being passed as parameters down the chain it can get a bit confusing to keep track of program flow.
Program flow is actually very straightfoward. The first piece of middleware is run first, any changes to the environ
dictionary are passed on to the next piece of middleware and so on down the chain. Once the start_response function is called the status
, headers
and application output are sent back up the chain to the server where they are sent to the web browser.
Here is a test application demonstrating middleware and program flow (the headers are not valid HTTP headers obviously):
#!/usr/bin/env python import sys; sys.path.append('../../../') import web.wsgi.base, time class Application(web.wsgi.base.BaseApplication): def start(self): self.output('Environ Order:\n') self.environ['Application'] = time.time() time.sleep(1) self.headers.append(('Appliction',str(time.time()))) self.output('Middleware1 ',self.environ['Middleware1']) self.output('\n') self.output('Middleware2 ',self.environ['Middleware2']) self.output('\n') self.output('Application ', self.environ['Application']) self.output('\n') class Middleware1(web.wsgi.base.BaseMiddleware): def environ(self, environ): time.sleep(1) environ['Middleware1'] = time.time() return environ def headers(self, headers): time.sleep(1) headers.append(('Middleware1',str(time.time()))) return headers def transform(self, output): return output + ['Middleware1\n'] class Middleware2(web.wsgi.base.BaseMiddleware): def environ(self, environ): time.sleep(1) environ['Middleware2'] = time.time() return environ def headers(self, headers): time.sleep(1) headers.append(('Middleware2',str(time.time()))) return headers def transform(self, output): return output + ['Middleware2\n'] print "Running test..." application = web.wsgi.runCGI(Middleware1(Middleware2(Application())))
The program will not run from a WSGI server because of the incorrect HTTP headers but you can run it from the command line. The output should look something like this:
Status: 200 OK Content-type: text/html Appliction: 1105847968.69 Middleware2: 1105847969.69 Middleware1: 1105847970.69 Environ Order: Middleware1 1105847966.68 Middleware2 1105847967.69 Application 1105847967.69 Transform Order: Middleware2 Middleware1
You can see that environ
is modified by Middleware1
then Middleware2
then Application
. Headers and return transforms are made in exactly the opposite order.
At each stage of the application and middleware chain the component can either return an list of strings in one go or return an iterable.