From effd29b51a3b54c2d346919bd09933fc6f440397 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 31 May 2015 18:03:41 +1200 Subject: [PATCH] Refactor logging, fix conversion to websocket handler --- libpathod/log.py | 24 ++++++++++++++++++------ libpathod/pathod.py | 35 ++++++++++++++++------------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/libpathod/log.py b/libpathod/log.py index 568d4d66e..c60a151cd 100644 --- a/libpathod/log.py +++ b/libpathod/log.py @@ -4,9 +4,20 @@ import netlib.utils import netlib.tcp import netlib.http +TIMEFMT = '%d-%m-%y %H:%M:%S' + + +def write(fp, lines): + if fp: + fp.write( + "%s: " % datetime.datetime.now().strftime(TIMEFMT) + ) + for i in lines: + fp.write(i) + fp.flush() + class Log: - TIMEFMT = '%d-%m-%y %H:%M:%S' def __init__(self, fp, hex, rfile, wfile): self.lines = [] @@ -42,12 +53,13 @@ class Log: self("Disconnected") elif exc_type == netlib.http.HttpError: self("HTTP Error: %s" % exc_value.message) - self.fp.write( - "%s: "%datetime.datetime.now().strftime(self.TIMEFMT) + write( + self.fp, + [ + "\n".join(self.lines), + "\n" + ] ) - self.fp.write("\n".join(self.lines)) - self.fp.write("\n") - self.fp.flush() def suppress(self): self.suppressed = True diff --git a/libpathod/pathod.py b/libpathod/pathod.py index a3f32ce74..56348fbb0 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -66,11 +66,6 @@ class PathodHandler(tcp.BaseHandler): tcp.BaseHandler.__init__(self, connection, address, server) self.settings = copy.copy(settings) - def info(self, s): - logger.info( - "%s:%s: %s" % (self.address.host, self.address.port, str(s)) - ) - def handle_sni(self, connection): self.sni = connection.get_servername() @@ -81,18 +76,17 @@ class PathodHandler(tcp.BaseHandler): if error: err = language.http.make_error_response(error) language.serve(err, self.wfile, self.settings) - log = dict( + return None, dict( type="error", msg = error ) - return None, log if self.server.explain and not isinstance( crafted, language.http.PathodErrorResponse ): crafted = crafted.freeze(self.settings) - self.info(">> Spec: %s" % crafted.spec()) + log.write(self.logfp, ">> Spec: %s" % crafted.spec()) response_log = language.serve( crafted, self.wfile, @@ -219,9 +213,9 @@ class PathodHandler(tcp.BaseHandler): path, self.server.craftanchor): spec = urllib.unquote(path)[len(self.server.craftanchor) + 1:] - key = websockets.check_client_handshake(headers) - self.settings.websocket_key = key - if key and not spec: + websocket_key = websockets.check_client_handshake(headers) + self.settings.websocket_key = websocket_key + if websocket_key and not spec: spec = "ws" lg("crafting spec: %s" % spec) try: @@ -232,8 +226,11 @@ class PathodHandler(tcp.BaseHandler): "Parse Error", "Error parsing response spec: %s\n" % v.msg + v.marked() ) - _, retlog["response"] = self.serve_crafted(crafted) - return self.handle_websocket, retlog + nexthandler, retlog["response"] = self.serve_crafted(crafted) + if nexthandler and websocket_key: + return self.handle_websocket, retlog + else: + return nexthandler, retlog elif self.server.noweb: crafted = language.http.make_error_response("Access Denied") language.serve(crafted, self.wfile, self.settings) @@ -287,14 +284,14 @@ class PathodHandler(tcp.BaseHandler): msg=s ) ) - self.info(s) + log.write(self.logfp, s) return self.settimeout(self.server.timeout) handler = self.handle_http_request while not self.finished: - handler, log = handler() - if log: - self.addlog(log) + handler, l = handler() + if l: + self.addlog(l) if not handler: return @@ -388,7 +385,7 @@ class Pathod(tcp.TCPServer): h.handle() h.finish() except tcp.NetLibDisconnect: # pragma: no cover - h.info("Disconnect") + log.write(self.logfp, "Disconnect") self.add_log( dict( type="error", @@ -397,7 +394,7 @@ class Pathod(tcp.TCPServer): ) return except tcp.NetLibTimeout: - h.info("Timeout") + log.write(self.logfp, "Timeout") self.add_log( dict( type="timeout",