From d3611777539471e53d4fdedf352ed755a4092415 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Sun, 3 Jul 2016 18:03:34 +0530 Subject: h2: move header parsing to netlib --- pathod/protocols/http2.py | 46 +++++++--------------------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) (limited to 'pathod') diff --git a/pathod/protocols/http2.py b/pathod/protocols/http2.py index c8728940..d94cd981 100644 --- a/pathod/protocols/http2.py +++ b/pathod/protocols/http2.py @@ -7,8 +7,7 @@ import hyperframe.frame from hpack.hpack import Encoder, Decoder from netlib import utils, strutils -from netlib.http import url -from netlib.http.http2 import framereader +from netlib.http import http2 import netlib.http.headers import netlib.http.response import netlib.http.request @@ -101,46 +100,15 @@ class HTTP2StateProtocol(object): timestamp_end = time.time() - authority = headers.get(':authority', b'') - method = headers.get(':method', 'GET') - scheme = headers.get(':scheme', 'https') - path = headers.get(':path', '/') - - headers.clear(":method") - headers.clear(":scheme") - headers.clear(":path") - - host = None - port = None - - if path == '*' or path.startswith("/"): - first_line_format = "relative" - elif method == 'CONNECT': - first_line_format = "authority" - if ":" in authority: - host, port = authority.split(":", 1) - else: - host = authority - else: - first_line_format = "absolute" - # FIXME: verify if path or :host contains what we need - scheme, host, port, _ = url.parse(path) - scheme = scheme.decode('ascii') - host = host.decode('ascii') - - if host is None: - host = 'localhost' - if port is None: - port = 80 if scheme == 'http' else 443 - port = int(port) + first_line_format, method, scheme, host, port, path = http2.parse_headers(headers) request = netlib.http.request.Request( first_line_format, - method.encode('ascii'), - scheme.encode('ascii'), - host.encode('ascii'), + method, + scheme, + host, port, - path.encode('ascii'), + path, b"HTTP/2.0", headers, body, @@ -286,7 +254,7 @@ class HTTP2StateProtocol(object): def read_frame(self, hide=False): while True: - frm = framereader.http2_read_frame(self.tcp_handler.rfile) + frm = http2.framereader.http2_read_frame(self.tcp_handler.rfile) if not hide and self.dump_frames: # pragma no cover print(frm.human_readable("<<")) -- cgit v1.2.3 From 23e295b37eb3aee9ebe7c88cc1ebb1dc2ffa9cf2 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sun, 3 Jul 2016 19:04:16 +0200 Subject: py3: fix bytes vs. str --- pathod/protocols/http2.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'pathod') diff --git a/pathod/protocols/http2.py b/pathod/protocols/http2.py index d94cd981..5ad120de 100644 --- a/pathod/protocols/http2.py +++ b/pathod/protocols/http2.py @@ -181,10 +181,10 @@ class HTTP2StateProtocol(object): headers = request.headers.copy() if ':authority' not in headers: - headers.insert(0, b':authority', authority.encode('ascii')) - headers.insert(0, b':scheme', request.scheme.encode('ascii')) - headers.insert(0, b':path', request.path.encode('ascii')) - headers.insert(0, b':method', request.method.encode('ascii')) + headers.insert(0, ':authority', authority) + headers.insert(0, ':scheme', request.scheme) + headers.insert(0, ':path', request.path) + headers.insert(0, ':method', request.method) if hasattr(request, 'stream_id'): stream_id = request.stream_id @@ -397,7 +397,7 @@ class HTTP2StateProtocol(object): self._handle_unexpected_frame(frm) headers = netlib.http.headers.Headers( - (k.encode('ascii'), v.encode('ascii')) for k, v in self.decoder.decode(header_blocks) + [[k, v] for k, v in self.decoder.decode(header_blocks, raw=True)] ) return stream_id, headers, body -- cgit v1.2.3 From d51cf543bb74755ed5dd17ed02859912ec557ef4 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 5 Jul 2016 15:11:32 -0700 Subject: remove clean_bin, clarify unicode handling --- pathod/log.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'pathod') diff --git a/pathod/log.py b/pathod/log.py index 1d3ec356..d39496e0 100644 --- a/pathod/log.py +++ b/pathod/log.py @@ -62,8 +62,9 @@ class LogCtx(object): for line in strutils.hexdump(data): self("\t%s %s %s" % line) else: - for i in strutils.clean_bin(data).split("\n"): - self("\t%s" % i) + data = data.decode("ascii", "replace").replace(u"\ufffd", u".") + for i in strutils.escape_control_characters(data).split(u"\n"): + self(u"\t%s" % i) def __call__(self, line): self.lines.append(line) -- cgit v1.2.3 From 8b564bc934b8b51734c41192ecb0135c264087cc Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 6 Jul 2016 16:47:32 -0700 Subject: fix pathod log encoding --- pathod/log.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'pathod') diff --git a/pathod/log.py b/pathod/log.py index d39496e0..47837101 100644 --- a/pathod/log.py +++ b/pathod/log.py @@ -62,9 +62,15 @@ class LogCtx(object): for line in strutils.hexdump(data): self("\t%s %s %s" % line) else: - data = data.decode("ascii", "replace").replace(u"\ufffd", u".") - for i in strutils.escape_control_characters(data).split(u"\n"): - self(u"\t%s" % i) + data = strutils.native( + strutils.escape_control_characters( + data + .decode("ascii", "replace") + .replace(u"\ufffd", u".") + ) + ) + for i in data.split("\n"): + self("\t%s" % i) def __call__(self, line): self.lines.append(line) -- cgit v1.2.3 From 64a867973d5bac136c2e1c3c11c457d6b04d6649 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 6 Jul 2016 21:03:17 -0700 Subject: sni is now str, not bytes --- pathod/pathod.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'pathod') diff --git a/pathod/pathod.py b/pathod/pathod.py index 3df86aae..7087cba6 100644 --- a/pathod/pathod.py +++ b/pathod/pathod.py @@ -89,7 +89,10 @@ class PathodHandler(tcp.BaseHandler): self.http2_framedump = http2_framedump def handle_sni(self, connection): - self.sni = connection.get_servername() + sni = connection.get_servername() + if sni: + sni = sni.decode("idna") + self.sni = sni def http_serve_crafted(self, crafted, logctx): error, crafted = self.server.check_policy( -- cgit v1.2.3