aboutsummaryrefslogtreecommitdiffstats
path: root/pathod
diff options
context:
space:
mode:
Diffstat (limited to 'pathod')
-rw-r--r--pathod/language/http.py2
-rw-r--r--pathod/language/http2.py2
-rw-r--r--pathod/language/websockets.py5
-rw-r--r--pathod/log.py2
-rw-r--r--pathod/pathoc.py29
-rw-r--r--pathod/pathod.py18
-rw-r--r--pathod/protocols/websockets.py2
-rw-r--r--pathod/test.py14
8 files changed, 33 insertions, 41 deletions
diff --git a/pathod/language/http.py b/pathod/language/http.py
index 4cc7db5f..5bd6e385 100644
--- a/pathod/language/http.py
+++ b/pathod/language/http.py
@@ -209,7 +209,7 @@ class Response(_HTTPMessage):
base.TokValueLiteral(i[1].decode()))
)
if not self.raw:
- if not get_header("Content-Length", self.headers):
+ if not get_header(b"Content-Length", self.headers):
if not self.body:
length = 0
else:
diff --git a/pathod/language/http2.py b/pathod/language/http2.py
index ea4fcd27..2693446e 100644
--- a/pathod/language/http2.py
+++ b/pathod/language/http2.py
@@ -188,7 +188,7 @@ class Response(_HTTP2Message):
body = body.string()
resp = http.Response(
- (2, 0),
+ b'HTTP/2.0',
self.status_code.string(),
b'',
headers,
diff --git a/pathod/language/websockets.py b/pathod/language/websockets.py
index 9b752b7e..417944af 100644
--- a/pathod/language/websockets.py
+++ b/pathod/language/websockets.py
@@ -1,10 +1,11 @@
import random
import string
import netlib.websockets
+from netlib import strutils
import pyparsing as pp
from . import base, generators, actions, message
-NESTED_LEADER = "pathod!"
+NESTED_LEADER = b"pathod!"
class WF(base.CaselessLiteral):
@@ -193,7 +194,7 @@ class WebsocketFrame(message.Message):
bodygen = self.rawbody.value.get_generator(settings)
length = len(self.rawbody.value.get_generator(settings))
elif self.nested_frame:
- bodygen = NESTED_LEADER + self.nested_frame.parsed.spec()
+ bodygen = NESTED_LEADER + strutils.always_bytes(self.nested_frame.parsed.spec())
length = len(bodygen)
else:
bodygen = None
diff --git a/pathod/log.py b/pathod/log.py
index 1d3ec356..23e9a2ce 100644
--- a/pathod/log.py
+++ b/pathod/log.py
@@ -62,7 +62,7 @@ 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"):
+ for i in strutils.clean_bin(data).split(b"\n"):
self("\t%s" % i)
def __call__(self, line):
diff --git a/pathod/pathoc.py b/pathod/pathoc.py
index 21fc9845..478ce2a2 100644
--- a/pathod/pathoc.py
+++ b/pathod/pathoc.py
@@ -42,7 +42,7 @@ class SSLInfo(object):
def __str__(self):
parts = [
- "Application Layer Protocol: %s" % self.alp,
+ "Application Layer Protocol: %s" % strutils.native(self.alp, "utf8"),
"Cipher: %s, %s bit, %s" % self.cipher,
"SSL certificate chain:"
]
@@ -50,18 +50,25 @@ class SSLInfo(object):
parts.append(" Certificate [%s]" % n)
parts.append("\tSubject: ")
for cn in i.get_subject().get_components():
- parts.append("\t\t%s=%s" % cn)
+ parts.append("\t\t%s=%s" % (
+ strutils.native(cn[0], "utf8"),
+ strutils.native(cn[1], "utf8"))
+ )
parts.append("\tIssuer: ")
for cn in i.get_issuer().get_components():
- parts.append("\t\t%s=%s" % cn)
+ parts.append("\t\t%s=%s" % (
+ strutils.native(cn[0], "utf8"),
+ strutils.native(cn[1], "utf8"))
+ )
parts.extend(
[
"\tVersion: %s" % i.get_version(),
"\tValidity: %s - %s" % (
- i.get_notBefore(), i.get_notAfter()
+ strutils.native(i.get_notBefore(), "utf8"),
+ strutils.native(i.get_notAfter(), "utf8")
),
"\tSerial: %s" % i.get_serial_number(),
- "\tAlgorithm: %s" % i.get_signature_algorithm()
+ "\tAlgorithm: %s" % strutils.native(i.get_signature_algorithm(), "utf8")
]
)
pk = i.get_pubkey()
@@ -73,7 +80,7 @@ class SSLInfo(object):
parts.append("\tPubkey: %s bit %s" % (pk.bits(), t))
s = certutils.SSLCert(i)
if s.altnames:
- parts.append("\tSANs: %s" % " ".join(s.altnames))
+ parts.append("\tSANs: %s" % " ".join(strutils.native(n, "utf8") for n in s.altnames))
return "\n".join(parts)
@@ -218,7 +225,7 @@ class Pathoc(tcp.TCPClient):
"HTTP/2 requires ALPN support. "
"Please use OpenSSL >= 1.0.2. "
"Pathoc might not be working as expected without ALPN.",
- timestamp = False
+ timestamp=False
)
self.protocol = http2.HTTP2Protocol(self, dump_frames=self.http2_framedump)
else:
@@ -239,7 +246,7 @@ class Pathoc(tcp.TCPClient):
)
self.wfile.flush()
try:
- resp = self.protocol.read_response(self.rfile, treq(method="CONNECT"))
+ resp = self.protocol.read_response(self.rfile, treq(method=b"CONNECT"))
if resp.status_code != 200:
raise exceptions.HttpException("Unexpected status code: %s" % resp.status_code)
except exceptions.HttpException as e:
@@ -437,7 +444,7 @@ class Pathoc(tcp.TCPClient):
finally:
if resp:
lg("<< %s %s: %s bytes" % (
- resp.status_code, strutils.bytes_to_escaped_str(resp.reason), len(resp.content)
+ resp.status_code, strutils.bytes_to_escaped_str(resp.reason.encode()), len(resp.content)
))
if resp.status_code in self.ignorecodes:
lg.suppress()
@@ -454,8 +461,8 @@ class Pathoc(tcp.TCPClient):
May raise a exceptions.NetlibException
"""
- if isinstance(r, basestring):
- r = language.parse_pathoc(r, self.use_http2).next()
+ if isinstance(r, six.string_types):
+ r = next(language.parse_pathoc(r, self.use_http2))
if isinstance(r, language.http.Request):
if r.ws:
diff --git a/pathod/pathod.py b/pathod/pathod.py
index 315a04e0..3df86aae 100644
--- a/pathod/pathod.py
+++ b/pathod/pathod.py
@@ -4,19 +4,20 @@ import logging
import os
import sys
import threading
-import urllib
from netlib import tcp
from netlib import certutils
from netlib import websockets
from netlib import version
+
+from six.moves import urllib
from netlib.exceptions import HttpException, HttpReadDisconnect, TcpTimeout, TcpDisconnect, \
TlsException
from . import language, utils, log, protocols
-DEFAULT_CERT_DOMAIN = "pathod.net"
+DEFAULT_CERT_DOMAIN = b"pathod.net"
CONFDIR = "~/.mitmproxy"
CERTSTORE_BASENAME = "mitmproxy"
CA_CERT_NAME = "mitmproxy-ca.pem"
@@ -185,7 +186,7 @@ class PathodHandler(tcp.BaseHandler):
break
else:
if m(path.startswith(self.server.craftanchor)):
- spec = urllib.unquote(path)[len(self.server.craftanchor):]
+ spec = urllib.parse.unquote(path)[len(self.server.craftanchor):]
if spec:
try:
anchor_gen = language.parse_pathod(spec, self.use_http2)
@@ -211,7 +212,7 @@ class PathodHandler(tcp.BaseHandler):
"No valid craft request found"
)])
- spec = anchor_gen.next()
+ spec = next(anchor_gen)
if self.use_http2 and isinstance(spec, language.http2.Response):
spec.stream_id = req.stream_id
@@ -283,15 +284,10 @@ class PathodHandler(tcp.BaseHandler):
return
def addlog(self, log):
- # FIXME: The bytes in the log should not be escaped. We do this at the
- # moment because JSON encoding can't handle binary data, and I don't
- # want to base64 everything.
if self.server.logreq:
- encoded_bytes = self.rfile.get_log().encode("string_escape")
- log["request_bytes"] = encoded_bytes
+ log["request_bytes"] = self.rfile.get_log()
if self.server.logresp:
- encoded_bytes = self.wfile.get_log().encode("string_escape")
- log["response_bytes"] = encoded_bytes
+ log["response_bytes"] = self.wfile.get_log()
self.server.add_log(log)
diff --git a/pathod/protocols/websockets.py b/pathod/protocols/websockets.py
index 2b60e618..a34e75e8 100644
--- a/pathod/protocols/websockets.py
+++ b/pathod/protocols/websockets.py
@@ -37,7 +37,7 @@ class WebsocketsProtocol:
if frm.payload.startswith(ld):
nest = frm.payload[len(ld):]
try:
- wf_gen = language.parse_websocket_frame(nest)
+ wf_gen = language.parse_websocket_frame(nest.decode())
except language.exceptions.ParseException as v:
logger.write(
"Parse error in reflected frame specifcation:"
diff --git a/pathod/test.py b/pathod/test.py
index 3ba541b1..4992945d 100644
--- a/pathod/test.py
+++ b/pathod/test.py
@@ -7,10 +7,6 @@ from . import pathod
from netlib import basethread
-class TimeoutError(Exception):
- pass
-
-
class Daemon:
IFACE = "127.0.0.1"
@@ -45,15 +41,7 @@ class Daemon:
return self.logfp.getvalue()
def wait_for_silence(self, timeout=5):
- start = time.time()
- while 1:
- if time.time() - start >= timeout:
- raise TimeoutError(
- "%s service threads still alive" %
- self.thread.server.handler_counter.count
- )
- if self.thread.server.handler_counter.count == 0:
- return
+ self.thread.server.wait_for_silence(timeout=timeout)
def expect_log(self, n, timeout=5):
l = []