diff options
-rw-r--r-- | libpathod/pathod.py | 43 | ||||
-rw-r--r-- | test/test_pathod.py | 13 |
2 files changed, 44 insertions, 12 deletions
diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 50e30c45..1f4ff760 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -12,6 +12,9 @@ class PathodHandler(tcp.BaseHandler): def debug(self, s): logging.debug("%s:%s: %s"%(self.client_address[0], self.client_address[1], str(s))) + def info(self, s): + logging.info("%s:%s: %s"%(self.client_address[0], self.client_address[1], str(s))) + def handle_sni(self, connection): self.sni = connection.get_servername() @@ -23,7 +26,7 @@ class PathodHandler(tcp.BaseHandler): self.server.ssloptions["keyfile"], ) except tcp.NetLibError, v: - self.debug(v) + self.info(v) self.finish() while not self.finished: @@ -36,7 +39,19 @@ class PathodHandler(tcp.BaseHandler): if line == "": return None - method, path, httpversion = http.parse_init_http(line) + parts = http.parse_init_http(line) + if not parts: + s = "Invalid first line: %s"%line.rstrip() + self.info(s) + self.server.add_log( + dict( + type = "error", + msg = s + ) + ) + return None + method, path, httpversion = parts + headers = http.read_headers(self.rfile) content = http.read_http_body_request( self.rfile, self.wfile, headers, httpversion, None @@ -57,19 +72,25 @@ class PathodHandler(tcp.BaseHandler): "Error parsing response spec: %s\n"%v.msg + v.marked() ) + request_log = dict( + path = path, + method = method, + headers = headers.lst, + sni = self.sni, + remote_address = self.client_address, + httpversion = httpversion, + ) if crafted: response_log = crafted.serve(self.wfile) if response_log["disconnect"]: self.finish() - request_log = dict( - path = path, - method = method, - headers = headers.lst, - sni = self.sni, - remote_address = self.client_address, - httpversion = httpversion, + self.server.add_log( + dict( + type = "crafted", + request=request_log, + response=response_log + ) ) - self.server.add_log(dict(request=request_log, response=response_log)) else: cc = wsgi.ClientConn(self.client_address) req = wsgi.Request(cc, "http", method, path, headers, content) @@ -124,7 +145,7 @@ class Pathod(tcp.TCPServer): def handle_connection(self, request, client_address): h = PathodHandler(request, client_address, self) - h.handle() + h.handle() h.finish() def add_log(self, d): diff --git a/test/test_pathod.py b/test/test_pathod.py index bf15bc23..902074b7 100644 --- a/test/test_pathod.py +++ b/test/test_pathod.py @@ -1,5 +1,6 @@ import requests from libpathod import pathod, test, version +from netlib import tcp import tutils class _TestApplication: @@ -60,12 +61,22 @@ class TestDaemon: def get(self, spec): return requests.get("http://localhost:%s/p/%s"%(self.d.port, spec)) + def test_invalid_first_line(self): + c = tcp.TCPClient("localhost", self.d.port) + c.connect() + c.wfile.write("foo\n\n\n") + c.wfile.flush() + l = self.d.log()[0] + assert l["type"] == "error" + assert "foo" in l["msg"] + def test_info(self): assert tuple(self.d.info()["version"]) == version.IVERSION def test_logs(self): + l = len(self.d.log()) rsp = self.get("202") - assert len(self.d.log()) == 1 + assert len(self.d.log()) == l+1 assert self.d.clear_log() assert len(self.d.log()) == 0 |