Commit 5252f94c authored by Denis Bilenko's avatar Denis Bilenko

wsgi: pass server instance to handler's constructor to match pywsgi's API; log...

wsgi: pass server instance to handler's constructor to match pywsgi's API; log message to server's log rather than to stdout
parent 10644071
...@@ -16,8 +16,9 @@ __all__ = ['WSGIServer', ...@@ -16,8 +16,9 @@ __all__ = ['WSGIServer',
class WSGIHandler(object): class WSGIHandler(object):
def __init__(self, request): def __init__(self, request, server):
self.request = request self.request = request
self.server = server
self.code = None self.code = None
self.reason = None self.reason = None
self.headers = None self.headers = None
...@@ -76,10 +77,12 @@ class WSGIHandler(object): ...@@ -76,10 +77,12 @@ class WSGIHandler(object):
return '%s - - [%s] "%s %s HTTP/%s.%s" %s %s "%s" "%s"' % args return '%s - - [%s] "%s %s HTTP/%s.%s" %s %s "%s" "%s"' % args
def log_request(self, *args): def log_request(self, *args):
print self.format_request(*args) log = self.server.log
if log is not None:
log.write(self.format_request(*args) + '\n')
def prepare_env(self, req, server): def prepare_env(self, req):
env = server.get_environ() env = self.server.get_environ()
if '?' in req.uri: if '?' in req.uri:
path, query = req.uri.split('?', 1) path, query = req.uri.split('?', 1)
else: else:
...@@ -99,12 +102,12 @@ class WSGIHandler(object): ...@@ -99,12 +102,12 @@ class WSGIHandler(object):
env[header] = value env[header] = value
return env return env
def handle(self, server): def handle(self):
req = self.request req = self.request
env = self.prepare_env(req, server) env = self.prepare_env(req)
try: try:
try: try:
result = server.application(env, self.start_response) result = self.server.application(env, self.start_response)
try: try:
self.data.extend(result) self.data.extend(result)
finally: finally:
...@@ -115,15 +118,16 @@ class WSGIHandler(object): ...@@ -115,15 +118,16 @@ class WSGIHandler(object):
except: except:
traceback.print_exc() traceback.print_exc()
try: try:
sys.stderr.write('Failed to handle request:\n request = %s\n application = %s\n\n' % (req, server.application)) sys.stderr.write('%s: Failed to handle request:\n request = %s\n application = %s\n\n' %
except: (self.server, req, self.server.application))
traceback.print_exc() except Exception:
sys.exc_clear() pass
# do not call self.end, this will cause core.http to reply with 500 # do not call self.end so that core.http replies with 500
self = None self = None
return return
finally: finally:
if self is not None: sys.exc_clear()
if self is not None and self.code is not None:
self.end(env) self.end(env)
...@@ -165,8 +169,8 @@ class WSGIServer(HTTPServer): ...@@ -165,8 +169,8 @@ class WSGIServer(HTTPServer):
self.__dict__.pop('application', None) self.__dict__.pop('application', None)
def handle(self, req): def handle(self, req):
handler = self.handler_class(req) handler = self.handler_class(req, self)
handler.handle(self) handler.handle()
def extract_application(filename): def extract_application(filename):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment