Add pre/post request hooks

This commit is contained in:
jbergstroem 2010-07-23 10:00:54 +02:00 committed by Paul J. Davis
parent aca70fbec8
commit 0d67447d19
3 changed files with 41 additions and 3 deletions

View File

@ -569,7 +569,8 @@ class Postfork(Setting):
The callable needs to accept two instance variables for the Arbiter and The callable needs to accept two instance variables for the Arbiter and
new Worker. new Worker.
""" """
class WhenReady(Setting): class WhenReady(Setting):
name = "when_ready" name = "when_ready"
section = "Server Hooks" section = "Server Hooks"
@ -584,7 +585,8 @@ class WhenReady(Setting):
The callable needs to accept a single instance variable for the Arbiter. The callable needs to accept a single instance variable for the Arbiter.
""" """
class PreExec(Setting): class PreExec(Setting):
name = "pre_exec" name = "pre_exec"
section = "Server Hooks" section = "Server Hooks"
@ -599,5 +601,37 @@ class PreExec(Setting):
The callable needs to accept a single instance variable for the Arbiter. The callable needs to accept a single instance variable for the Arbiter.
""" """
class PreRequest(Setting):
name = "pre_request"
section = "Server Hooks"
validator = validate_callable(2)
type = "callable"
def def_pre_request(worker, req):
worker.log.debug("%s %s" % (req.method, req.path))
def_pre_request = staticmethod(def_pre_request)
default = def_pre_request
desc = """\
Called just before a worker processes the request.
The callable needs to accept two instance variables for the Worker and
the Request.
"""
class PostRequest(Setting):
name = "post_request"
section = "Server Hooks"
validator = validate_callable(2)
type = "callable"
def def_post_request(worker, req):
pass
def_post_request = staticmethod(def_post_request)
default = def_post_request
desc = """\
Called after a worker processes the request.
The callable needs to accept two instance variables for the Worker and
the Request.
"""

View File

@ -61,6 +61,7 @@ class AsyncWorker(base.Worker):
def handle_request(self, req, sock, addr): def handle_request(self, req, sock, addr):
try: try:
debug = self.cfg.debug or False debug = self.cfg.debug or False
self.cfg.pre_request(self, req)
resp, environ = wsgi.create(req, sock, addr, self.address, resp, environ = wsgi.create(req, sock, addr, self.address,
self.cfg) self.cfg)
respiter = self.wsgi(environ, resp.start_response) respiter = self.wsgi(environ, resp.start_response)
@ -73,6 +74,7 @@ class AsyncWorker(base.Worker):
respiter.close() respiter.close()
if req.should_close(): if req.should_close():
raise StopIteration() raise StopIteration()
self.cfg.post_request(self, req)
except Exception, e: except Exception, e:
#Only send back traceback in HTTP in debug mode. #Only send back traceback in HTTP in debug mode.
if not self.debug: if not self.debug:

View File

@ -94,6 +94,7 @@ class SyncWorker(base.Worker):
def handle_request(self, req, client, addr): def handle_request(self, req, client, addr):
try: try:
debug = self.cfg.debug or False debug = self.cfg.debug or False
self.cfg.pre_request(self, req)
resp, environ = wsgi.create(req, client, addr, resp, environ = wsgi.create(req, client, addr,
self.address, self.cfg) self.address, self.cfg)
respiter = self.wsgi(environ, resp.start_response) respiter = self.wsgi(environ, resp.start_response)
@ -102,6 +103,7 @@ class SyncWorker(base.Worker):
resp.close() resp.close()
if hasattr(respiter, "close"): if hasattr(respiter, "close"):
respiter.close() respiter.close()
self.cfg.post_request(self, req)
except socket.error: except socket.error:
raise raise
except Exception, e: except Exception, e: