mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-27 10:26:23 +00:00
Merge pull request #103 from scone/master
Utils port to 3.4 keeping py2 compatibility
This commit is contained in:
commit
0df7e27c3b
@ -85,9 +85,9 @@ def hexdump(s):
|
|||||||
A generator of (offset, hex, str) tuples
|
A generator of (offset, hex, str) tuples
|
||||||
"""
|
"""
|
||||||
for i in range(0, len(s), 16):
|
for i in range(0, len(s), 16):
|
||||||
offset = b"%.10x" % i
|
offset = "{:0=10x}".format(i).encode()
|
||||||
part = s[i:i + 16]
|
part = s[i:i + 16]
|
||||||
x = b" ".join(b"%.2x" % i for i in six.iterbytes(part))
|
x = b" ".join("{:0=2x}".format(i).encode() for i in six.iterbytes(part))
|
||||||
x = x.ljust(47) # 16*2 + 15
|
x = x.ljust(47) # 16*2 + 15
|
||||||
yield (offset, x, clean_bin(part, False))
|
yield (offset, x, clean_bin(part, False))
|
||||||
|
|
||||||
|
@ -96,16 +96,17 @@ class WSGIAdaptor(object):
|
|||||||
Make a best-effort attempt to write an error page. If headers are
|
Make a best-effort attempt to write an error page. If headers are
|
||||||
already sent, we just bung the error into the page.
|
already sent, we just bung the error into the page.
|
||||||
"""
|
"""
|
||||||
c = b"""
|
c = """
|
||||||
<html>
|
<html>
|
||||||
<h1>Internal Server Error</h1>
|
<h1>Internal Server Error</h1>
|
||||||
<pre>%s"</pre>
|
<pre>{err}"</pre>
|
||||||
</html>
|
</html>
|
||||||
""".strip() % s.encode()
|
""".format(err=s).strip().encode()
|
||||||
|
|
||||||
if not headers_sent:
|
if not headers_sent:
|
||||||
soc.write(b"HTTP/1.1 500 Internal Server Error\r\n")
|
soc.write(b"HTTP/1.1 500 Internal Server Error\r\n")
|
||||||
soc.write(b"Content-Type: text/html\r\n")
|
soc.write(b"Content-Type: text/html\r\n")
|
||||||
soc.write(b"Content-Length: %s\r\n" % len(c))
|
soc.write("Content-Length: {length}\r\n".format(length=len(c)).encode())
|
||||||
soc.write(b"\r\n")
|
soc.write(b"\r\n")
|
||||||
soc.write(c)
|
soc.write(c)
|
||||||
|
|
||||||
@ -119,7 +120,7 @@ class WSGIAdaptor(object):
|
|||||||
|
|
||||||
def write(data):
|
def write(data):
|
||||||
if not state["headers_sent"]:
|
if not state["headers_sent"]:
|
||||||
soc.write(b"HTTP/1.1 %s\r\n" % state["status"].encode())
|
soc.write("HTTP/1.1 {status}\r\n".format(status=state["status"]).encode())
|
||||||
headers = state["headers"]
|
headers = state["headers"]
|
||||||
if 'server' not in headers:
|
if 'server' not in headers:
|
||||||
headers["Server"] = self.sversion
|
headers["Server"] = self.sversion
|
||||||
|
@ -10,7 +10,7 @@ def tflow():
|
|||||||
return wsgi.Flow(("127.0.0.1", 8888), req)
|
return wsgi.Flow(("127.0.0.1", 8888), req)
|
||||||
|
|
||||||
|
|
||||||
class TestApp:
|
class ExampleApp:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.called = False
|
self.called = False
|
||||||
@ -35,7 +35,7 @@ class TestWSGI:
|
|||||||
assert r["QUERY_STRING"] == "bar=voing"
|
assert r["QUERY_STRING"] == "bar=voing"
|
||||||
|
|
||||||
def test_serve(self):
|
def test_serve(self):
|
||||||
ta = TestApp()
|
ta = ExampleApp()
|
||||||
w = wsgi.WSGIAdaptor(ta, "foo", 80, "version")
|
w = wsgi.WSGIAdaptor(ta, "foo", 80, "version")
|
||||||
f = tflow()
|
f = tflow()
|
||||||
f.request.host = "foo"
|
f.request.host = "foo"
|
||||||
|
Loading…
Reference in New Issue
Block a user