netlib.utils -> netlib.check

Now only contains is_valid_[host,port]

Intermediate step - this will be in mitproxy.net soon.
This commit is contained in:
Aldo Cortesi 2016-10-20 10:46:47 +13:00
parent e0f3cce14c
commit 9870844b38
9 changed files with 21 additions and 21 deletions

View File

@ -7,7 +7,7 @@ import netlib.exceptions
from mitmproxy import exceptions
from mitmproxy.contrib.tls import _constructs
from mitmproxy.proxy.protocol import base
from netlib import utils
from netlib import check
# taken from https://testssl.sh/openssl-rfc.mappping.html
@ -265,7 +265,7 @@ class TlsClientHello:
extension.type == 0x00 and
len(extension.server_names) == 1 and
extension.server_names[0].type == 0 and
utils.is_valid_host(extension.server_names[0].name)
check.is_valid_host(extension.server_names[0].name)
)
if is_valid_sni_extension:
return extension.server_names[0].name.decode("idna")

View File

@ -6,7 +6,7 @@ from netlib.http import request
from netlib.http import response
from netlib.http import headers
from netlib.http import url
from netlib import utils
from netlib import check
from netlib import exceptions
@ -274,7 +274,7 @@ def _parse_authority_form(hostport):
try:
host, port = hostport.split(b":")
port = int(port)
if not utils.is_valid_host(host) or not utils.is_valid_port(port):
if not check.is_valid_host(host) or not check.is_valid_port(port):
raise ValueError()
except ValueError:
raise exceptions.HttpSyntaxException("Invalid host specification: {}".format(hostport))

View File

@ -2,7 +2,7 @@ import urllib
from typing import Sequence
from typing import Tuple
from netlib import utils
from netlib import check
# PY2 workaround
@ -62,9 +62,9 @@ def parse(url):
if not full_path.startswith(b"/"):
full_path = b"/" + full_path
if not utils.is_valid_host(host):
if not check.is_valid_host(host):
raise ValueError("Invalid Host")
if not utils.is_valid_port(port):
if not check.is_valid_port(port):
raise ValueError("Invalid Port")
return parsed.scheme, host, port, full_path

View File

@ -2,7 +2,8 @@ import struct
import array
import ipaddress
from netlib import tcp, utils
from netlib import tcp
from netlib import check
from mitmproxy.types import bidi
@ -204,7 +205,7 @@ class Message:
elif atyp == ATYP.DOMAINNAME:
length, = struct.unpack("!B", f.safe_read(1))
host = f.safe_read(length)
if not utils.is_valid_host(host):
if not check.is_valid_host(host):
raise SocksError(REP.GENERAL_SOCKS_SERVER_FAILURE, "Invalid hostname: %s" % host)
host = host.decode("idna")
use_ipv6 = False

View File

@ -1,7 +1,7 @@
import mock
import io
import netlib.utils
import netlib.tutils
from netlib.http import Headers
import mitmproxy.io
from mitmproxy import flowfilter, options

View File

@ -9,7 +9,6 @@ from unittest.case import SkipTest
import io
import netlib.utils
import netlib.tutils
from mitmproxy import controller
from mitmproxy import connections

10
test/netlib/test_check.py Normal file
View File

@ -0,0 +1,10 @@
# coding=utf-8
from netlib import check
def test_is_valid_host():
assert not check.is_valid_host(b"")
assert check.is_valid_host(b"one.two")
assert not check.is_valid_host(b"one" * 255)
assert check.is_valid_host(b"one.two.")

View File

@ -1,10 +0,0 @@
# coding=utf-8
from netlib import utils
def test_is_valid_host():
assert not utils.is_valid_host(b"")
assert utils.is_valid_host(b"one.two")
assert not utils.is_valid_host(b"one" * 255)
assert utils.is_valid_host(b"one.two.")