mirror of
https://github.com/zhigang1992/mitmproxy.git
synced 2026-04-24 04:14:57 +08:00
cleanup whitespace
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from netlib import odict
|
||||
|
||||
|
||||
class HttpError(Exception):
|
||||
|
||||
def __init__(self, code, message):
|
||||
super(HttpError, self).__init__(message)
|
||||
self.code = code
|
||||
@@ -11,6 +13,7 @@ class HttpErrorConnClosed(HttpError):
|
||||
|
||||
|
||||
class HttpAuthenticationError(Exception):
|
||||
|
||||
def __init__(self, auth_headers=None):
|
||||
super(HttpAuthenticationError, self).__init__(
|
||||
"Proxy Authentication Required"
|
||||
|
||||
@@ -7,18 +7,25 @@ from netlib import odict, utils, tcp, http
|
||||
from netlib.http import semantics
|
||||
from ..exceptions import *
|
||||
|
||||
|
||||
class TCPHandler(object):
|
||||
|
||||
def __init__(self, rfile, wfile=None):
|
||||
self.rfile = rfile
|
||||
self.wfile = wfile
|
||||
|
||||
|
||||
class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
|
||||
def __init__(self, tcp_handler=None, rfile=None, wfile=None):
|
||||
self.tcp_handler = tcp_handler or TCPHandler(rfile, wfile)
|
||||
|
||||
|
||||
def read_request(self, include_body=True, body_size_limit=None, allow_empty=False):
|
||||
def read_request(
|
||||
self,
|
||||
include_body=True,
|
||||
body_size_limit=None,
|
||||
allow_empty=False,
|
||||
):
|
||||
"""
|
||||
Parse an HTTP request from a file stream
|
||||
|
||||
@@ -125,8 +132,12 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
timestamp_end,
|
||||
)
|
||||
|
||||
|
||||
def read_response(self, request_method, body_size_limit, include_body=True):
|
||||
def read_response(
|
||||
self,
|
||||
request_method,
|
||||
body_size_limit,
|
||||
include_body=True,
|
||||
):
|
||||
"""
|
||||
Returns an http.Response
|
||||
|
||||
@@ -171,7 +182,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
# read separately
|
||||
body = None
|
||||
|
||||
|
||||
if hasattr(self.tcp_handler.rfile, "first_byte_timestamp"):
|
||||
# more accurate timestamp_start
|
||||
timestamp_start = self.tcp_handler.rfile.first_byte_timestamp
|
||||
@@ -191,7 +201,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
timestamp_end=timestamp_end,
|
||||
)
|
||||
|
||||
|
||||
def assemble_request(self, request):
|
||||
assert isinstance(request, semantics.Request)
|
||||
|
||||
@@ -204,7 +213,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
headers = self._assemble_request_headers(request)
|
||||
return "%s\r\n%s\r\n%s" % (first_line, headers, request.body)
|
||||
|
||||
|
||||
def assemble_response(self, response):
|
||||
assert isinstance(response, semantics.Response)
|
||||
|
||||
@@ -217,7 +225,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
headers = self._assemble_response_headers(response)
|
||||
return "%s\r\n%s\r\n%s" % (first_line, headers, response.body)
|
||||
|
||||
|
||||
def read_headers(self):
|
||||
"""
|
||||
Read a set of headers.
|
||||
@@ -262,7 +269,7 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
response_code,
|
||||
is_request,
|
||||
max_chunk_size=None
|
||||
):
|
||||
):
|
||||
"""
|
||||
Read an HTTP message body:
|
||||
headers: An ODictCaseless object
|
||||
@@ -317,9 +324,14 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
"HTTP Body too large. Limit is %s," % limit
|
||||
)
|
||||
|
||||
|
||||
@classmethod
|
||||
def expected_http_body_size(self, headers, is_request, request_method, response_code):
|
||||
def expected_http_body_size(
|
||||
self,
|
||||
headers,
|
||||
is_request,
|
||||
request_method,
|
||||
response_code,
|
||||
):
|
||||
"""
|
||||
Returns the expected body length:
|
||||
- a positive integer, if the size is known in advance
|
||||
@@ -372,7 +384,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
line = self.tcp_handler.rfile.readline()
|
||||
return line
|
||||
|
||||
|
||||
def _read_chunked(self, limit, is_request):
|
||||
"""
|
||||
Read a chunked HTTP body.
|
||||
@@ -409,7 +420,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
if length == 0:
|
||||
return
|
||||
|
||||
|
||||
@classmethod
|
||||
def _parse_http_protocol(self, line):
|
||||
"""
|
||||
@@ -429,7 +439,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
return None
|
||||
return major, minor
|
||||
|
||||
|
||||
@classmethod
|
||||
def _parse_init(self, line):
|
||||
try:
|
||||
@@ -443,7 +452,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
return None
|
||||
return method, url, httpversion
|
||||
|
||||
|
||||
@classmethod
|
||||
def _parse_init_connect(self, line):
|
||||
"""
|
||||
@@ -471,7 +479,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
return None
|
||||
return host, port, httpversion
|
||||
|
||||
|
||||
@classmethod
|
||||
def _parse_init_proxy(self, line):
|
||||
v = self._parse_init(line)
|
||||
@@ -485,7 +492,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
scheme, host, port, path = parts
|
||||
return method, scheme, host, port, path, httpversion
|
||||
|
||||
|
||||
@classmethod
|
||||
def _parse_init_http(self, line):
|
||||
"""
|
||||
@@ -501,7 +507,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
return None
|
||||
return method, url, httpversion
|
||||
|
||||
|
||||
@classmethod
|
||||
def connection_close(self, httpversion, headers):
|
||||
"""
|
||||
@@ -521,7 +526,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
# be persistent
|
||||
return httpversion != (1, 1)
|
||||
|
||||
|
||||
@classmethod
|
||||
def parse_response_line(self, line):
|
||||
parts = line.strip().split(" ", 2)
|
||||
@@ -536,7 +540,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
return None
|
||||
return (proto, code, msg)
|
||||
|
||||
|
||||
@classmethod
|
||||
def _assemble_request_first_line(self, request):
|
||||
return request.legacy_first_line()
|
||||
@@ -557,7 +560,6 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
|
||||
return headers.format()
|
||||
|
||||
|
||||
def _assemble_response_first_line(self, response):
|
||||
return 'HTTP/%s.%s %s %s' % (
|
||||
response.httpversion[0],
|
||||
@@ -566,7 +568,11 @@ class HTTP1Protocol(semantics.ProtocolMixin):
|
||||
response.msg,
|
||||
)
|
||||
|
||||
def _assemble_response_headers(self, response, preserve_transfer_encoding=False):
|
||||
def _assemble_response_headers(
|
||||
self,
|
||||
response,
|
||||
preserve_transfer_encoding=False,
|
||||
):
|
||||
headers = response.headers.copy()
|
||||
for k in response._headers_to_strip_off:
|
||||
del headers[k]
|
||||
|
||||
@@ -117,7 +117,7 @@ class Frame(object):
|
||||
|
||||
return "\n".join([
|
||||
"%s: %s | length: %d | flags: %#x | stream_id: %d" % (
|
||||
direction, self.__class__.__name__, self.length, self.flags, self.stream_id),
|
||||
direction, self.__class__.__name__, self.length, self.flags, self.stream_id),
|
||||
self.payload_human_readable(),
|
||||
"===============================================================",
|
||||
])
|
||||
|
||||
@@ -9,6 +9,7 @@ from . import frame
|
||||
|
||||
|
||||
class TCPHandler(object):
|
||||
|
||||
def __init__(self, rfile, wfile=None):
|
||||
self.rfile = rfile
|
||||
self.wfile = wfile
|
||||
@@ -39,7 +40,6 @@ class HTTP2Protocol(semantics.ProtocolMixin):
|
||||
|
||||
ALPN_PROTO_H2 = 'h2'
|
||||
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
tcp_handler=None,
|
||||
@@ -60,7 +60,12 @@ class HTTP2Protocol(semantics.ProtocolMixin):
|
||||
self.current_stream_id = None
|
||||
self.connection_preface_performed = False
|
||||
|
||||
def read_request(self, include_body=True, body_size_limit=None, allow_empty=False):
|
||||
def read_request(
|
||||
self,
|
||||
include_body=True,
|
||||
body_size_limit=None,
|
||||
allow_empty=False,
|
||||
):
|
||||
self.perform_connection_preface()
|
||||
|
||||
timestamp_start = time.time()
|
||||
@@ -92,7 +97,12 @@ class HTTP2Protocol(semantics.ProtocolMixin):
|
||||
|
||||
return request
|
||||
|
||||
def read_response(self, request_method='', body_size_limit=None, include_body=True):
|
||||
def read_response(
|
||||
self,
|
||||
request_method='',
|
||||
body_size_limit=None,
|
||||
include_body=True,
|
||||
):
|
||||
self.perform_connection_preface()
|
||||
|
||||
timestamp_start = time.time()
|
||||
@@ -123,7 +133,6 @@ class HTTP2Protocol(semantics.ProtocolMixin):
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def assemble_request(self, request):
|
||||
assert isinstance(request, semantics.Request)
|
||||
|
||||
|
||||
@@ -332,6 +332,7 @@ class Request(object):
|
||||
|
||||
|
||||
class EmptyRequest(Request):
|
||||
|
||||
def __init__(self):
|
||||
super(EmptyRequest, self).__init__(
|
||||
form_in="",
|
||||
@@ -343,7 +344,7 @@ class EmptyRequest(Request):
|
||||
httpversion=(0, 0),
|
||||
headers=odict.ODictCaseless(),
|
||||
body="",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class Response(object):
|
||||
@@ -396,10 +397,9 @@ class Response(object):
|
||||
status_code=self.status_code,
|
||||
msg=self.msg,
|
||||
contenttype=self.headers.get_first(
|
||||
"content-type", "unknown content type"
|
||||
),
|
||||
size=size
|
||||
)
|
||||
"content-type",
|
||||
"unknown content type"),
|
||||
size=size)
|
||||
|
||||
def get_cookies(self):
|
||||
"""
|
||||
|
||||
@@ -69,8 +69,6 @@ def raises(exc, obj, *args, **kwargs):
|
||||
test_data = utils.Data(__name__)
|
||||
|
||||
|
||||
|
||||
|
||||
def treq(content="content", scheme="http", host="address", port=22):
|
||||
"""
|
||||
@return: libmproxy.protocol.http.HTTPRequest
|
||||
|
||||
@@ -119,6 +119,7 @@ def pretty_size(size):
|
||||
|
||||
|
||||
class Data(object):
|
||||
|
||||
def __init__(self, name):
|
||||
m = __import__(name)
|
||||
dirname, _ = os.path.split(m.__file__)
|
||||
@@ -137,8 +138,6 @@ class Data(object):
|
||||
return fullpath
|
||||
|
||||
|
||||
|
||||
|
||||
def is_valid_port(port):
|
||||
if not 0 <= port <= 65535:
|
||||
return False
|
||||
@@ -221,6 +220,7 @@ def hostport(scheme, host, port):
|
||||
else:
|
||||
return "%s:%s" % (host, port)
|
||||
|
||||
|
||||
def unparse_url(scheme, host, port, path=""):
|
||||
"""
|
||||
Returns a URL string, constructed from the specified compnents.
|
||||
@@ -235,6 +235,7 @@ def urlencode(s):
|
||||
s = [tuple(i) for i in s]
|
||||
return urllib.urlencode(s, False)
|
||||
|
||||
|
||||
def urldecode(s):
|
||||
"""
|
||||
Takes a urlencoded string and returns a list of (key, value) tuples.
|
||||
|
||||
@@ -21,6 +21,7 @@ OPCODE = utils.BiDi(
|
||||
PONG=0x0a
|
||||
)
|
||||
|
||||
|
||||
class FrameHeader(object):
|
||||
|
||||
def __init__(
|
||||
|
||||
@@ -25,6 +25,7 @@ HEADER_WEBSOCKET_KEY = 'sec-websocket-key'
|
||||
HEADER_WEBSOCKET_ACCEPT = 'sec-websocket-accept'
|
||||
HEADER_WEBSOCKET_VERSION = 'sec-websocket-version'
|
||||
|
||||
|
||||
class Masker(object):
|
||||
|
||||
"""
|
||||
@@ -52,6 +53,7 @@ class Masker(object):
|
||||
self.offset += len(ret)
|
||||
return ret
|
||||
|
||||
|
||||
class WebsocketsProtocol(object):
|
||||
|
||||
def __init__(self):
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from netlib import encoding
|
||||
|
||||
|
||||
def test_identity():
|
||||
assert "string" == encoding.decode("identity", "string")
|
||||
assert "string" == encoding.encode("identity", "string")
|
||||
|
||||
@@ -44,7 +44,11 @@ def test_client_greeting_assert_socks5():
|
||||
assert False
|
||||
|
||||
raw = tutils.treader("XX")
|
||||
tutils.raises(socks.SocksError, socks.ClientGreeting.from_file, raw, fail_early=True)
|
||||
tutils.raises(
|
||||
socks.SocksError,
|
||||
socks.ClientGreeting.from_file,
|
||||
raw,
|
||||
fail_early=True)
|
||||
|
||||
|
||||
def test_server_greeting():
|
||||
|
||||
@@ -29,8 +29,6 @@ def test_pretty_size():
|
||||
assert utils.pretty_size(1024 * 1024) == "1MB"
|
||||
|
||||
|
||||
|
||||
|
||||
def test_parse_url():
|
||||
assert not utils.parse_url("")
|
||||
|
||||
@@ -85,7 +83,6 @@ def test_urlencode():
|
||||
assert utils.urlencode([('foo', 'bar')])
|
||||
|
||||
|
||||
|
||||
def test_urldecode():
|
||||
s = "one=two&three=four"
|
||||
assert len(utils.urldecode(s)) == 2
|
||||
@@ -102,9 +99,6 @@ def test_get_header_tokens():
|
||||
assert utils.get_header_tokens(h, "foo") == ["bar", "voing", "oink"]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def test_multipartdecode():
|
||||
boundary = 'somefancyboundary'
|
||||
headers = odict.ODict(
|
||||
|
||||
Reference in New Issue
Block a user