Merge pull request #2487 from mhils/no-option-processing

Remove OptManager._processed
This commit is contained in:
Maximilian Hils 2017-08-01 02:54:33 +02:00 committed by GitHub
commit 864073f700
9 changed files with 20 additions and 14 deletions

View File

@ -19,11 +19,9 @@ class CoreOptionValidation:
"then the upstream certificate is not retrieved before generating " "then the upstream certificate is not retrieved before generating "
"the client certificate chain." "the client certificate chain."
) )
if "body_size_limit" in updated and opts.body_size_limit: if "body_size_limit" in updated:
try: try:
opts._processed["body_size_limit"] = human.parse_size( human.parse_size(opts.body_size_limit)
opts.body_size_limit
)
except ValueError as e: except ValueError as e:
raise exceptions.OptionsError( raise exceptions.OptionsError(
"Invalid body size limit specification: %s" % "Invalid body size limit specification: %s" %

View File

@ -94,7 +94,6 @@ class OptManager:
self.__dict__["_options"] = {} self.__dict__["_options"] = {}
self.__dict__["changed"] = blinker.Signal() self.__dict__["changed"] = blinker.Signal()
self.__dict__["errored"] = blinker.Signal() self.__dict__["errored"] = blinker.Signal()
self.__dict__["_processed"] = {}
def add_option( def add_option(
self, self,

View File

@ -1,6 +1,7 @@
from mitmproxy import http from mitmproxy import http
from mitmproxy.proxy.protocol import http as httpbase from mitmproxy.proxy.protocol import http as httpbase
from mitmproxy.net.http import http1 from mitmproxy.net.http import http1
from mitmproxy.utils import human
class Http1Layer(httpbase._HttpTransmissionLayer): class Http1Layer(httpbase._HttpTransmissionLayer):
@ -19,7 +20,7 @@ class Http1Layer(httpbase._HttpTransmissionLayer):
return http1.read_body( return http1.read_body(
self.client_conn.rfile, self.client_conn.rfile,
expected_size, expected_size,
self.config.options._processed.get("body_size_limit") human.parse_size(self.config.options.body_size_limit)
) )
def send_request_headers(self, request): def send_request_headers(self, request):
@ -45,7 +46,7 @@ class Http1Layer(httpbase._HttpTransmissionLayer):
return http1.read_body( return http1.read_body(
self.server_conn.rfile, self.server_conn.rfile,
expected_size, expected_size,
self.config.options._processed.get("body_size_limit") human.parse_size(self.config.options.body_size_limit)
) )
def send_response_headers(self, response): def send_response_headers(self, response):

View File

@ -17,6 +17,7 @@ import mitmproxy.net.http
from mitmproxy.net import tcp from mitmproxy.net import tcp
from mitmproxy.types import basethread from mitmproxy.types import basethread
from mitmproxy.net.http import http2, headers from mitmproxy.net.http import http2, headers
from mitmproxy.utils import human
class SafeH2Connection(connection.H2Connection): class SafeH2Connection(connection.H2Connection):
@ -183,7 +184,7 @@ class Http2Layer(base.Layer):
return True return True
def _handle_data_received(self, eid, event, source_conn): def _handle_data_received(self, eid, event, source_conn):
bsl = self.config.options._processed.get("body_size_limit") bsl = human.parse_size(self.config.options.body_size_limit)
if bsl and self.streams[eid].queued_data_length > bsl: if bsl and self.streams[eid].queued_data_length > bsl:
self.streams[eid].kill() self.streams[eid].kill()
self.connections[source_conn].safe_reset_stream( self.connections[source_conn].safe_reset_stream(

View File

@ -12,6 +12,7 @@ from mitmproxy import connections
from mitmproxy.net import server_spec from mitmproxy.net import server_spec
from mitmproxy.net.http import http1 from mitmproxy.net.http import http1
from mitmproxy.types import basethread from mitmproxy.types import basethread
from mitmproxy.utils import human
# TODO: Doesn't really belong into mitmproxy.proxy.protocol... # TODO: Doesn't really belong into mitmproxy.proxy.protocol...
@ -44,7 +45,7 @@ class RequestReplayThread(basethread.BaseThread):
def run(self): def run(self):
r = self.f.request r = self.f.request
bsl = self.options._processed.get("body_size_limit") bsl = human.parse_size(self.options.body_size_limit)
first_line_format_backup = r.first_line_format first_line_format_backup = r.first_line_format
server = None server = None
try: try:

View File

@ -1,6 +1,8 @@
import datetime import datetime
import ipaddress import ipaddress
import time import time
import functools
import typing
SIZE_TABLE = [ SIZE_TABLE = [
("b", 1024 ** 0), ("b", 1024 ** 0),
@ -25,7 +27,14 @@ def pretty_size(size):
return "%s%s" % (size, SIZE_TABLE[0][0]) return "%s%s" % (size, SIZE_TABLE[0][0])
def parse_size(s): @functools.lru_cache()
def parse_size(s: typing.Optional[str]) -> typing.Optional[int]:
"""
Parse a size with an optional k/m/... suffix.
Invalid values raise a ValueError. For added convenience, passing `None` returns `None`.
"""
if s is None:
return None
try: try:
return int(s) return int(s)
except ValueError: except ValueError:

View File

@ -11,7 +11,6 @@ def test_simple():
with pytest.raises(exceptions.OptionsError): with pytest.raises(exceptions.OptionsError):
tctx.configure(sa, body_size_limit = "invalid") tctx.configure(sa, body_size_limit = "invalid")
tctx.configure(sa, body_size_limit = "1m") tctx.configure(sa, body_size_limit = "1m")
assert tctx.master.options._processed["body_size_limit"]
with pytest.raises(exceptions.OptionsError, match="mutually exclusive"): with pytest.raises(exceptions.OptionsError, match="mutually exclusive"):
tctx.configure( tctx.configure(

View File

@ -507,9 +507,6 @@ class TestBodySizeLimit(_Http2Test):
def test_body_size_limit(self): def test_body_size_limit(self):
self.options.body_size_limit = "20" self.options.body_size_limit = "20"
# FIXME: This should not be required?
self.options._processed["body_size_limit"] = 20
h2_conn = self.setup_connection() h2_conn = self.setup_connection()
self._send_request( self._send_request(

View File

@ -22,6 +22,7 @@ def test_parse_size():
human.parse_size("1f") human.parse_size("1f")
with pytest.raises(ValueError): with pytest.raises(ValueError):
human.parse_size("ak") human.parse_size("ak")
assert human.parse_size(None) is None
def test_pretty_size(): def test_pretty_size():