From b71ef054b577abfa1899210aca75d00e1c947cbb Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Sat, 28 May 2016 10:26:12 +0530 Subject: [PATCH 1/8] Py3: Use cStringIO from six.moves --- test/pathod/test_log.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/pathod/test_log.py b/test/pathod/test_log.py index d91b8bb1e..3d1d10d68 100644 --- a/test/pathod/test_log.py +++ b/test/pathod/test_log.py @@ -1,10 +1,10 @@ -import StringIO from pathod import log from netlib.exceptions import TcpDisconnect -import netlib.tcp + +from six.moves import cStringIO -class DummyIO(StringIO.StringIO): +class DummyIO(cStringIO): def start_log(self, *args, **kwargs): pass From 877f5c54ef779c1ef081ed86ce69f4f7864d13d9 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Sat, 28 May 2016 10:26:56 +0530 Subject: [PATCH 2/8] Enable Travis for test_log --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4a01174a0..0c37b53f9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,9 +22,9 @@ matrix: git: depth: 9999999 - python: 3.5 - env: SCOPE="netlib ./test/mitmproxy/script ./test/pathod/test_utils.py" + env: SCOPE="netlib ./test/mitmproxy/script ./test/pathod/test_utils.py ./test/pathod/test_log.py" - python: 3.5 - env: SCOPE="netlib ./test/mitmproxy/script ./test/pathod/test_utils.py" NO_ALPN=1 + env: SCOPE="netlib ./test/mitmproxy/script ./test/pathod/test_utils.py ./test/pathod/test_log.py" NO_ALPN=1 - python: 2.7 env: DOCS=1 script: 'cd docs && make html' From fa7964988f1541587a9fc3b582223e0a7b11a4d1 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sat, 28 May 2016 00:17:26 -0700 Subject: [PATCH 3/8] fix Python 2 tests --- test/pathod/test_log.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/pathod/test_log.py b/test/pathod/test_log.py index 3d1d10d68..29801eb3d 100644 --- a/test/pathod/test_log.py +++ b/test/pathod/test_log.py @@ -1,10 +1,10 @@ from pathod import log from netlib.exceptions import TcpDisconnect -from six.moves import cStringIO +import six -class DummyIO(cStringIO): +class DummyIO(six.StringIO): def start_log(self, *args, **kwargs): pass From caee1adeb613368dc658bfc0c99e445d6f41cd7c Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sat, 28 May 2016 14:36:43 +0200 Subject: [PATCH 4/8] cleanup imports with flake8 --- mitmproxy/flow.py | 6 +++--- mitmproxy/flow_export.py | 3 ++- mitmproxy/protocol/rawtcp.py | 4 ---- mitmproxy/script/script.py | 1 - netlib/http/cookies.py | 1 - netlib/http/http1/assemble.py | 1 - netlib/http/message.py | 1 - netlib/http/request.py | 2 -- netlib/multidict.py | 1 - netlib/version_check.py | 1 - netlib/websockets/protocol.py | 1 - netlib/wsgi.py | 4 ++-- pathod/language/websockets.py | 1 - pathod/language/writer.py | 1 - pathod/protocols/http.py | 6 +++--- pathod/protocols/http2.py | 2 +- test/mitmproxy/test_examples.py | 2 +- test/mitmproxy/test_flow.py | 1 - test/mitmproxy/test_flow_export.py | 1 - test/mitmproxy/test_protocol_http2.py | 6 +----- test/mitmproxy/tutils.py | 2 +- test/netlib/http/http1/test_read.py | 1 - test/netlib/http/http2/test_connections.py | 1 - test/netlib/http/test_message.py | 4 ++-- test/netlib/http/test_response.py | 2 -- test/netlib/test_socks.py | 1 - test/netlib/test_tcp.py | 1 - test/netlib/test_version_check.py | 2 +- test/netlib/websockets/test_websockets.py | 4 +++- test/pathod/test_language_http2.py | 2 +- test/pathod/test_pathoc.py | 6 +++--- test/pathod/test_pathod.py | 2 +- test/pathod/tutils.py | 5 ++++- 33 files changed, 29 insertions(+), 50 deletions(-) diff --git a/mitmproxy/flow.py b/mitmproxy/flow.py index a09a81a7d..d70ec2d9f 100644 --- a/mitmproxy/flow.py +++ b/mitmproxy/flow.py @@ -8,13 +8,13 @@ import hashlib import sys import six -from six.moves import http_cookies, http_cookiejar, urllib +from six.moves import http_cookiejar +from six.moves import urllib import os import re -from typing import List, Optional, Set -from netlib import wsgi, odict +from netlib import wsgi from netlib.exceptions import HttpException from netlib.http import Headers, http1, cookies from netlib.utils import clean_bin diff --git a/mitmproxy/flow_export.py b/mitmproxy/flow_export.py index ae282fcea..2ac33e152 100644 --- a/mitmproxy/flow_export.py +++ b/mitmproxy/flow_export.py @@ -5,7 +5,8 @@ import netlib.http from netlib.utils import parse_content_type import re -from six.moves.urllib.parse import urlparse, quote, quote_plus +from six.moves.urllib.parse import quote +from six.moves.urllib.parse import quote_plus def curl_command(flow): data = "curl " diff --git a/mitmproxy/protocol/rawtcp.py b/mitmproxy/protocol/rawtcp.py index 1b546c40d..053428443 100644 --- a/mitmproxy/protocol/rawtcp.py +++ b/mitmproxy/protocol/rawtcp.py @@ -1,14 +1,10 @@ from __future__ import (absolute_import, print_function, division) import socket -import six -import sys from OpenSSL import SSL from netlib.exceptions import TcpException from netlib.tcp import ssl_read_select -from netlib.utils import clean_bin -from ..exceptions import ProtocolException from ..models import Error from ..models.tcp import TCPFlow, TCPMessage diff --git a/mitmproxy/script/script.py b/mitmproxy/script/script.py index 484025b4a..4ec9af975 100644 --- a/mitmproxy/script/script.py +++ b/mitmproxy/script/script.py @@ -6,7 +6,6 @@ by the mitmproxy-specific ScriptContext. # Do not import __future__ here, this would apply transitively to the inline scripts. import os import shlex -import traceback import sys import six diff --git a/netlib/http/cookies.py b/netlib/http/cookies.py index 88c768706..ae041174f 100644 --- a/netlib/http/cookies.py +++ b/netlib/http/cookies.py @@ -3,7 +3,6 @@ import re from email.utils import parsedate_tz, formatdate, mktime_tz from netlib.multidict import ImmutableMultiDict -from .. import odict """ A flexible module for cookie parsing and manipulation. diff --git a/netlib/http/http1/assemble.py b/netlib/http/http1/assemble.py index f06ad5a10..1e73cc2b9 100644 --- a/netlib/http/http1/assemble.py +++ b/netlib/http/http1/assemble.py @@ -1,7 +1,6 @@ from __future__ import absolute_import, print_function, division from ... import utils -import itertools from ...exceptions import HttpException def assemble_request(request): diff --git a/netlib/http/message.py b/netlib/http/message.py index 028f43a1a..b7f425151 100644 --- a/netlib/http/message.py +++ b/netlib/http/message.py @@ -4,7 +4,6 @@ import warnings import six -from ..multidict import MultiDict from .headers import Headers from .. import encoding, utils diff --git a/netlib/http/request.py b/netlib/http/request.py index 056a2d93a..5a528bf21 100644 --- a/netlib/http/request.py +++ b/netlib/http/request.py @@ -1,14 +1,12 @@ from __future__ import absolute_import, print_function, division import re -import warnings import six from six.moves import urllib from netlib import utils from netlib.http import cookies -from netlib.odict import ODict from .. import encoding from ..multidict import MultiDictView from .headers import Headers diff --git a/netlib/multidict.py b/netlib/multidict.py index 8e657363f..da4826204 100644 --- a/netlib/multidict.py +++ b/netlib/multidict.py @@ -2,7 +2,6 @@ from __future__ import absolute_import, print_function, division from abc import ABCMeta, abstractmethod -from typing import Tuple, TypeVar try: from collections.abc import MutableMapping diff --git a/netlib/version_check.py b/netlib/version_check.py index 8e05b458c..63f3e8766 100644 --- a/netlib/version_check.py +++ b/netlib/version_check.py @@ -10,7 +10,6 @@ import os.path import six import OpenSSL -from . import version PYOPENSSL_MIN_VERSION = (0, 15) diff --git a/netlib/websockets/protocol.py b/netlib/websockets/protocol.py index 1e95fa1cc..940132add 100644 --- a/netlib/websockets/protocol.py +++ b/netlib/websockets/protocol.py @@ -18,7 +18,6 @@ import base64 import hashlib import os -import binascii import six from ..http import Headers diff --git a/netlib/wsgi.py b/netlib/wsgi.py index d6dfae5d1..e1259cbae 100644 --- a/netlib/wsgi.py +++ b/netlib/wsgi.py @@ -1,5 +1,5 @@ from __future__ import (absolute_import, print_function, division) -from io import BytesIO, StringIO +from io import BytesIO import urllib import time import traceback @@ -154,7 +154,7 @@ class WSGIAdaptor(object): write(i) if not state["headers_sent"]: write(b"") - except Exception as e: + except Exception: try: s = traceback.format_exc() errs.write(s.encode("utf-8", "replace")) diff --git a/pathod/language/websockets.py b/pathod/language/websockets.py index 09443a951..9b752b7e0 100644 --- a/pathod/language/websockets.py +++ b/pathod/language/websockets.py @@ -1,4 +1,3 @@ -import os import random import string import netlib.websockets diff --git a/pathod/language/writer.py b/pathod/language/writer.py index 1a27e1efd..22e32ce27 100644 --- a/pathod/language/writer.py +++ b/pathod/language/writer.py @@ -1,6 +1,5 @@ import time from netlib.exceptions import TcpDisconnect -import netlib.tcp BLOCKSIZE = 1024 # It's not clear what the upper limit for time.sleep is. It's lower than the diff --git a/pathod/protocols/http.py b/pathod/protocols/http.py index 1f1765cb8..d09b5bf29 100644 --- a/pathod/protocols/http.py +++ b/pathod/protocols/http.py @@ -1,6 +1,6 @@ -from netlib import tcp, wsgi -from netlib.exceptions import HttpReadDisconnect, TlsException -from netlib.http import http1, Request +from netlib import wsgi +from netlib.exceptions import TlsException +from netlib.http import http1 from .. import version, language diff --git a/pathod/protocols/http2.py b/pathod/protocols/http2.py index a098a14e6..688cc64e1 100644 --- a/pathod/protocols/http2.py +++ b/pathod/protocols/http2.py @@ -1,5 +1,5 @@ from netlib.http import http2 -from .. import version, app, language, utils, log +from .. import language class HTTP2Protocol: diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index c4b06f4b3..607d6fafb 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -3,7 +3,7 @@ import json import os from contextlib import contextmanager -from mitmproxy import utils, script +from mitmproxy import script from mitmproxy.proxy import config import netlib.utils from netlib import tutils as netutils diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 3e78a5c4c..5441ea59f 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -4,7 +4,6 @@ from six.moves import cStringIO as StringIO import mock import netlib.utils -from netlib import odict from netlib.http import Headers from mitmproxy import filt, controller, tnetstring, flow from mitmproxy.exceptions import FlowReadException, ScriptException diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index c252c5bd3..0374ba7d8 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -1,4 +1,3 @@ -import json from textwrap import dedent import re diff --git a/test/mitmproxy/test_protocol_http2.py b/test/mitmproxy/test_protocol_http2.py index c39509755..1e9c62d9b 100644 --- a/test/mitmproxy/test_protocol_http2.py +++ b/test/mitmproxy/test_protocol_http2.py @@ -2,7 +2,6 @@ from __future__ import (absolute_import, print_function, division) -import OpenSSL import pytest import traceback import os @@ -468,13 +467,10 @@ class TestConnectionLost(_Http2TestBase, _Http2ServerBase): ]) done = False - ended_streams = 0 - pushed_streams = 0 - responses = 0 while not done: try: raw = b''.join(http2_read_raw_frame(client.rfile)) - events = h2_conn.receive_data(raw) + h2_conn.receive_data(raw) except: break client.wfile.write(h2_conn.data_to_send()) diff --git a/test/mitmproxy/tutils.py b/test/mitmproxy/tutils.py index 118f849c9..c1b258a29 100644 --- a/test/mitmproxy/tutils.py +++ b/test/mitmproxy/tutils.py @@ -12,7 +12,7 @@ from unittest.case import SkipTest import netlib.utils import netlib.tutils -from mitmproxy import utils, controller +from mitmproxy import controller from mitmproxy.models import ( ClientConnection, ServerConnection, Error, HTTPRequest, HTTPResponse, HTTPFlow, TCPFlow ) diff --git a/test/netlib/http/http1/test_read.py b/test/netlib/http/http1/test_read.py index d81069049..33f3802b1 100644 --- a/test/netlib/http/http1/test_read.py +++ b/test/netlib/http/http1/test_read.py @@ -1,6 +1,5 @@ from __future__ import absolute_import, print_function, division from io import BytesIO -import textwrap from mock import Mock from netlib.exceptions import HttpException, HttpSyntaxException, HttpReadDisconnect, TcpDisconnect from netlib.http import Headers diff --git a/test/netlib/http/http2/test_connections.py b/test/netlib/http/http2/test_connections.py index 7d240c0e3..9312576f0 100644 --- a/test/netlib/http/http2/test_connections.py +++ b/test/netlib/http/http2/test_connections.py @@ -1,4 +1,3 @@ -import OpenSSL import mock import codecs diff --git a/test/netlib/http/test_message.py b/test/netlib/http/test_message.py index 645929213..f5bf7f0c3 100644 --- a/test/netlib/http/test_message.py +++ b/test/netlib/http/test_message.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function, division -from netlib.http import decoded, Headers -from netlib.tutils import tresp, raises +from netlib.http import decoded +from netlib.tutils import tresp def _test_passthrough_attr(message, attr): diff --git a/test/netlib/http/test_response.py b/test/netlib/http/test_response.py index cfd093d4c..63c976a26 100644 --- a/test/netlib/http/test_response.py +++ b/test/netlib/http/test_response.py @@ -2,12 +2,10 @@ from __future__ import absolute_import, print_function, division import email -import six import time from netlib.http import Headers from netlib.http.cookies import CookieAttrs -from netlib.odict import ODict, ODictCaseless from netlib.tutils import raises, tresp from .test_message import _test_passthrough_attr, _test_decoded_attr diff --git a/test/netlib/test_socks.py b/test/netlib/test_socks.py index 486b975b0..17e08054c 100644 --- a/test/netlib/test_socks.py +++ b/test/netlib/test_socks.py @@ -1,6 +1,5 @@ import ipaddress from io import BytesIO -import socket from netlib import socks, tcp, tutils diff --git a/test/netlib/test_tcp.py b/test/netlib/test_tcp.py index 4b4bbb92d..80cdba362 100644 --- a/test/netlib/test_tcp.py +++ b/test/netlib/test_tcp.py @@ -8,7 +8,6 @@ import threading import mock from OpenSSL import SSL -import OpenSSL from netlib import tcp, certutils, tutils from netlib.exceptions import InvalidCertificateException, TcpReadIncomplete, TlsException, \ diff --git a/test/netlib/test_version_check.py b/test/netlib/test_version_check.py index 680f80e0c..fa6b19e5b 100644 --- a/test/netlib/test_version_check.py +++ b/test/netlib/test_version_check.py @@ -1,6 +1,6 @@ from io import StringIO import mock -from netlib import version_check, version +from netlib import version_check @mock.patch("sys.exit") diff --git a/test/netlib/websockets/test_websockets.py b/test/netlib/websockets/test_websockets.py index a7d782a4d..4a6f0f9b8 100644 --- a/test/netlib/websockets/test_websockets.py +++ b/test/netlib/websockets/test_websockets.py @@ -2,7 +2,9 @@ import os from netlib.http.http1 import read_response, read_request -from netlib import tcp, websockets, http, tutils +from netlib import tcp +from netlib import tutils +from netlib import websockets from netlib.http import status_codes from netlib.tutils import treq from netlib.exceptions import * diff --git a/test/pathod/test_language_http2.py b/test/pathod/test_language_http2.py index abfe46062..4acde3af7 100644 --- a/test/pathod/test_language_http2.py +++ b/test/pathod/test_language_http2.py @@ -5,7 +5,7 @@ from netlib import tcp from netlib.http import user_agents from pathod import language -from pathod.language import http2, base +from pathod.language import http2 import tutils diff --git a/test/pathod/test_pathoc.py b/test/pathod/test_pathoc.py index 4e8c89c57..e7818772f 100644 --- a/test/pathod/test_pathoc.py +++ b/test/pathod/test_pathoc.py @@ -1,12 +1,12 @@ import json from six.moves import cStringIO as StringIO import re -import OpenSSL import pytest from mock import Mock -from netlib import tcp, http, socks -from netlib.exceptions import HttpException, TcpException, NetlibException +from netlib import http +from netlib import tcp +from netlib.exceptions import NetlibException from netlib.http import http1, http2 from pathod import pathoc, test, version, pathod, language diff --git a/test/pathod/test_pathod.py b/test/pathod/test_pathod.py index 05a3962ef..13b36e364 100644 --- a/test/pathod/test_pathod.py +++ b/test/pathod/test_pathod.py @@ -2,7 +2,7 @@ from six.moves import cStringIO as StringIO import pytest from pathod import pathod, version -from netlib import tcp, http +from netlib import tcp from netlib.exceptions import HttpException, TlsException import tutils diff --git a/test/pathod/tutils.py b/test/pathod/tutils.py index f6ed3efbb..d6b7bc1ab 100644 --- a/test/pathod/tutils.py +++ b/test/pathod/tutils.py @@ -4,7 +4,10 @@ import shutil from six.moves import cStringIO as StringIO import netlib -from pathod import utils, test, pathoc, pathod, language +from pathod import language +from pathod import pathoc +from pathod import pathod +from pathod import test from netlib import tcp import requests From e1cc91900f95c82e15d39cac1e0b9fa8b265d391 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sat, 28 May 2016 22:16:35 +0200 Subject: [PATCH 5/8] ignore sslkeylogfile.log --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1b44bd297..0ce72b41e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ build/ node_modules bower_components *.map +sslkeylogfile.log From e5038c9ab7a6718e7a3408a43549231929c7beb9 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sat, 28 May 2016 22:17:02 +0200 Subject: [PATCH 6/8] netlib: fix most flake8 offenses --- netlib/http/cookies.py | 1 + netlib/http/headers.py | 14 ++++++++++---- netlib/http/http1/assemble.py | 1 + netlib/http/message.py | 17 ++++++++++++----- netlib/socks.py | 2 +- netlib/tcp.py | 1 + netlib/tutils.py | 3 +-- netlib/websockets/frame.py | 2 +- netlib/websockets/protocol.py | 25 +++++++++++-------------- netlib/wsgi.py | 8 ++++---- 10 files changed, 43 insertions(+), 31 deletions(-) diff --git a/netlib/http/cookies.py b/netlib/http/cookies.py index ae041174f..2be93e180 100644 --- a/netlib/http/cookies.py +++ b/netlib/http/cookies.py @@ -27,6 +27,7 @@ variants. Serialization follows RFC6265. # TODO: Disallow LHS-only Cookie values + def _read_until(s, start, term): """ Read until one of the characters in term is reached. diff --git a/netlib/http/headers.py b/netlib/http/headers.py index 60d3f429d..2caf8d519 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -14,12 +14,18 @@ from ..utils import always_bytes # See also: http://lucumr.pocoo.org/2013/7/2/the-updated-guide-to-unicode/ if six.PY2: # pragma: no cover - _native = lambda x: x - _always_bytes = lambda x: x + def _native(x): + return x + + def _always_bytes(x): + return x else: # While headers _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded. - _native = lambda x: x.decode("utf-8", "surrogateescape") - _always_bytes = lambda x: always_bytes(x, "utf-8", "surrogateescape") + def _native(x): + return x.decode("utf-8", "surrogateescape") + + def _always_bytes(x): + return always_bytes(x, "utf-8", "surrogateescape") class Headers(MultiDict): diff --git a/netlib/http/http1/assemble.py b/netlib/http/http1/assemble.py index 1e73cc2b9..2f941877e 100644 --- a/netlib/http/http1/assemble.py +++ b/netlib/http/http1/assemble.py @@ -3,6 +3,7 @@ from __future__ import absolute_import, print_function, division from ... import utils from ...exceptions import HttpException + def assemble_request(request): if request.content is None: raise HttpException("Cannot assemble flow with missing content") diff --git a/netlib/http/message.py b/netlib/http/message.py index b7f425151..13d401a74 100644 --- a/netlib/http/message.py +++ b/netlib/http/message.py @@ -6,14 +6,21 @@ import six from .headers import Headers from .. import encoding, utils +from ..utils import always_bytes if six.PY2: # pragma: no cover - _native = lambda x: x - _always_bytes = lambda x: x + def _native(x): + return x + + def _always_bytes(x): + return x else: - # While the HTTP head _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded. - _native = lambda x: x.decode("utf-8", "surrogateescape") - _always_bytes = lambda x: utils.always_bytes(x, "utf-8", "surrogateescape") + # While headers _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded. + def _native(x): + return x.decode("utf-8", "surrogateescape") + + def _always_bytes(x): + return always_bytes(x, "utf-8", "surrogateescape") class MessageData(utils.Serializable): diff --git a/netlib/socks.py b/netlib/socks.py index 57ccd1be1..675fa784c 100644 --- a/netlib/socks.py +++ b/netlib/socks.py @@ -147,7 +147,7 @@ class UsernamePasswordAuth(object): class UsernamePasswordAuthResponse(object): - __slots__ = ("ver", "status") + __slots__ = ("ver", "status") def __init__(self, ver, status): self.ver = ver diff --git a/netlib/tcp.py b/netlib/tcp.py index d26bb5f72..ad75cff8a 100644 --- a/netlib/tcp.py +++ b/netlib/tcp.py @@ -71,6 +71,7 @@ sslversion_choices = { "TLSv1_2": (SSL.TLSv1_2_METHOD, SSL_BASIC_OPTIONS), } + class SSLKeyLogger(object): def __init__(self, filename): diff --git a/netlib/tutils.py b/netlib/tutils.py index 18d632f08..b8a3bafcf 100644 --- a/netlib/tutils.py +++ b/netlib/tutils.py @@ -91,8 +91,7 @@ class RaisesContext(object): test_data = utils.Data(__name__) # FIXME: Temporary workaround during repo merge. -import os -test_data.dirname = os.path.join(test_data.dirname,"..","test","netlib") +test_data.dirname = os.path.join(test_data.dirname, "..", "test", "netlib") def treq(**kwargs): diff --git a/netlib/websockets/frame.py b/netlib/websockets/frame.py index fce2c9d3d..da5a97f3f 100644 --- a/netlib/websockets/frame.py +++ b/netlib/websockets/frame.py @@ -14,7 +14,7 @@ from netlib import utils MAX_16_BIT_INT = (1 << 16) MAX_64_BIT_INT = (1 << 64) -DEFAULT=object() +DEFAULT = object() OPCODE = utils.BiDi( CONTINUE=0x00, diff --git a/netlib/websockets/protocol.py b/netlib/websockets/protocol.py index 940132add..101d54841 100644 --- a/netlib/websockets/protocol.py +++ b/netlib/websockets/protocol.py @@ -1,18 +1,18 @@ +""" +Colleciton of utility functions that implement small portions of the RFC6455 +WebSockets Protocol Useful for building WebSocket clients and servers. +Emphassis is on readabilty, simplicity and modularity, not performance or +completeness +This is a work in progress and does not yet contain all the utilites need to +create fully complient client/servers # +Spec: https://tools.ietf.org/html/rfc6455 -# Colleciton of utility functions that implement small portions of the RFC6455 -# WebSockets Protocol Useful for building WebSocket clients and servers. -# -# Emphassis is on readabilty, simplicity and modularity, not performance or -# completeness -# -# This is a work in progress and does not yet contain all the utilites need to -# create fully complient client/servers # -# Spec: https://tools.ietf.org/html/rfc6455 +The magic sha that websocket servers must know to prove they understand +RFC6455 +""" -# The magic sha that websocket servers must know to prove they understand -# RFC6455 from __future__ import absolute_import import base64 import hashlib @@ -94,21 +94,18 @@ class WebsocketsProtocol(object): upgrade="websocket" ) - @classmethod def check_client_handshake(self, headers): if headers.get("upgrade") != "websocket": return return headers.get("sec-websocket-key") - @classmethod def check_server_handshake(self, headers): if headers.get("upgrade") != "websocket": return return headers.get("sec-websocket-accept") - @classmethod def create_server_nonce(self, client_nonce): return base64.b64encode(hashlib.sha1(client_nonce + websockets_magic).digest()) diff --git a/netlib/wsgi.py b/netlib/wsgi.py index e1259cbae..cde562f86 100644 --- a/netlib/wsgi.py +++ b/netlib/wsgi.py @@ -1,15 +1,15 @@ from __future__ import (absolute_import, print_function, division) -from io import BytesIO -import urllib + import time import traceback - import six +from io import BytesIO from six.moves import urllib from netlib.utils import always_bytes, native from . import http, tcp + class ClientConn(object): def __init__(self, address): @@ -140,7 +140,7 @@ class WSGIAdaptor(object): elif state["status"]: raise AssertionError('Response already started') state["status"] = status - state["headers"] = http.Headers([[always_bytes(k), always_bytes(v)] for k,v in headers]) + state["headers"] = http.Headers([[always_bytes(k), always_bytes(v)] for k, v in headers]) if exc_info: self.error_page(soc, state["headers_sent"], traceback.format_tb(exc_info[2])) state["headers_sent"] = True From e4045dc7f858e1e7d2368819e7ad251f4e1d7f90 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sat, 28 May 2016 22:25:54 +0200 Subject: [PATCH 7/8] pathod: fix most flake8 offenses --- pathod/language/http.py | 1 + pathod/language/http2.py | 7 +++---- pathod/pathoc.py | 3 +-- pathod/pathod.py | 1 - pathod/pathod_cmdline.py | 11 +++++------ pathod/protocols/http2.py | 1 + 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/pathod/language/http.py b/pathod/language/http.py index a82f12fe3..b2308d5e4 100644 --- a/pathod/language/http.py +++ b/pathod/language/http.py @@ -11,6 +11,7 @@ from . import base, exceptions, actions, message # instead of duplicating the HTTP on-the-wire representation here. # see http2 language for an example + class WS(base.CaselessLiteral): TOK = "ws" diff --git a/pathod/language/http2.py b/pathod/language/http2.py index d5e3ca311..85d9047fc 100644 --- a/pathod/language/http2.py +++ b/pathod/language/http2.py @@ -27,6 +27,7 @@ from . import base, message h2f:42:DATA:END_STREAM,PADDED:0x1234567:'content body payload' """ + def get_header(val, headers): """ Header keys may be Values, so we have to "generate" them as we try the @@ -48,6 +49,7 @@ class _HeaderMixin(object): self.value.get_generator(settings), ) + class _HTTP2Message(message.Message): @property def actions(self): @@ -287,13 +289,10 @@ class Request(_HTTP2Message): def spec(self): return ":".join([i.spec() for i in self.tokens]) + def make_error_response(reason, body=None): tokens = [ StatusCode("800"), Body(base.TokValueLiteral("pathod error: " + (body or reason))), ] return Response(tokens) - - -# class Frame(message.Message): -# pass diff --git a/pathod/pathoc.py b/pathod/pathoc.py index 8706868b8..910387b65 100644 --- a/pathod/pathoc.py +++ b/pathod/pathoc.py @@ -41,7 +41,7 @@ class SSLInfo(object): "Cipher: %s, %s bit, %s" % self.cipher, "SSL certificate chain:" ] - for n,i in enumerate(self.certchain): + for n, i in enumerate(self.certchain): parts.append(" Certificate [%s]" % n) parts.append("\tSubject: ") for cn in i.get_subject().get_components(): @@ -72,7 +72,6 @@ class SSLInfo(object): return "\n".join(parts) - class WebsocketFrameReader(threading.Thread): def __init__( diff --git a/pathod/pathod.py b/pathod/pathod.py index af5f9e6a9..7795df0e2 100644 --- a/pathod/pathod.py +++ b/pathod/pathod.py @@ -112,7 +112,6 @@ class PathodHandler(tcp.BaseHandler): return None, response_log return self.handle_http_request, response_log - def handle_http_request(self, logger): """ Returns a (handler, log) tuple. diff --git a/pathod/pathod_cmdline.py b/pathod/pathod_cmdline.py index 1f972a491..a7cd24957 100644 --- a/pathod/pathod_cmdline.py +++ b/pathod/pathod_cmdline.py @@ -49,12 +49,12 @@ def args_pathod(argv, stdout_=sys.stdout, stderr_=sys.stderr): help=""" URL path specifying prefix for URL crafting commands. (%s) - """%pathod.DEFAULT_CRAFT_ANCHOR + """ % pathod.DEFAULT_CRAFT_ANCHOR ) parser.add_argument( "--confdir", - action="store", type = str, dest="confdir", default='~/.mitmproxy', - help = "Configuration directory. (~/.mitmproxy)" + action="store", type=str, dest="confdir", default='~/.mitmproxy', + help="Configuration directory. (~/.mitmproxy)" ) parser.add_argument( "-d", dest='staticdir', default=None, type=str, @@ -117,8 +117,8 @@ def args_pathod(argv, stdout_=sys.stdout, stderr_=sys.stderr): ) group.add_argument( "--cert", dest='ssl_certs', default=[], type=str, - metavar = "SPEC", action="append", - help = """ + metavar="SPEC", action="append", + help=""" Add an SSL certificate. SPEC is of the form "[domain=]path". The domain may include a wildcard, and is equal to "*" if not specified. The file at path is a certificate in PEM format. If a private key is included in @@ -177,7 +177,6 @@ def args_pathod(argv, stdout_=sys.stdout, stderr_=sys.stderr): help="Output all received & sent HTTP/2 frames" ) - args = parser.parse_args(argv[1:]) args.ssl_version, args.ssl_options = tcp.sslversion_choices[args.ssl_version] diff --git a/pathod/protocols/http2.py b/pathod/protocols/http2.py index 688cc64e1..3f45ec80c 100644 --- a/pathod/protocols/http2.py +++ b/pathod/protocols/http2.py @@ -1,6 +1,7 @@ from netlib.http import http2 from .. import language + class HTTP2Protocol: def __init__(self, pathod_handler): From 7971dce2231bc32c25b962d425d8ad935568a699 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sat, 28 May 2016 22:39:30 +0200 Subject: [PATCH 8/8] mitmproxy: fix most flake8 offenses --- mitmproxy/controller.py | 6 ++++++ mitmproxy/dump.py | 4 ++-- mitmproxy/exceptions.py | 4 ++++ mitmproxy/filt.py | 1 + mitmproxy/flow_export.py | 4 ++-- mitmproxy/models/connections.py | 6 +++++- mitmproxy/models/http.py | 9 +++++---- mitmproxy/models/tcp.py | 2 ++ mitmproxy/protocol/http_replay.py | 2 +- mitmproxy/protocol/tls.py | 7 ++++--- mitmproxy/proxy/config.py | 6 +++--- mitmproxy/tnetstring.py | 4 +--- 12 files changed, 36 insertions(+), 19 deletions(-) diff --git a/mitmproxy/controller.py b/mitmproxy/controller.py index 81978a098..af8a77bd3 100644 --- a/mitmproxy/controller.py +++ b/mitmproxy/controller.py @@ -6,6 +6,7 @@ from .exceptions import Kill class Master(object): + """ The master handles mitmproxy's main event loop. """ @@ -48,6 +49,7 @@ class Master(object): class ServerMaster(Master): + """ The ServerMaster adds server thread support to the master. """ @@ -74,6 +76,7 @@ class ServerMaster(Master): class ServerThread(threading.Thread): + def __init__(self, server): self.server = server super(ServerThread, self).__init__() @@ -85,6 +88,7 @@ class ServerThread(threading.Thread): class Channel(object): + """ The only way for the proxy server to communicate with the master is to use the channel it has been given. @@ -126,6 +130,7 @@ class Channel(object): class DummyReply(object): + """ A reply object that does nothing. Useful when we need an object to seem like it has a channel, and during testing. @@ -143,6 +148,7 @@ NO_REPLY = object() class Reply(object): + """ Messages sent through a channel are decorated with a "reply" attribute. This object is used to respond to the message through the return diff --git a/mitmproxy/dump.py b/mitmproxy/dump.py index f1eabdb8c..8f9488bea 100644 --- a/mitmproxy/dump.py +++ b/mitmproxy/dump.py @@ -74,8 +74,8 @@ class DumpMaster(flow.FlowMaster): if self.server and self.server.config.http2 and not tcp.HAS_ALPN: # pragma: no cover print("ALPN support missing (OpenSSL 1.0.2+ required)!\n" - "HTTP/2 is disabled. Use --no-http2 to silence this warning.", - file=sys.stderr) + "HTTP/2 is disabled. Use --no-http2 to silence this warning.", + file=sys.stderr) if options.filtstr: self.filt = filt.parse(options.filtstr) diff --git a/mitmproxy/exceptions.py b/mitmproxy/exceptions.py index 8f989063f..7e3e6d869 100644 --- a/mitmproxy/exceptions.py +++ b/mitmproxy/exceptions.py @@ -13,6 +13,7 @@ import sys class ProxyException(Exception): + """ Base class for all exceptions thrown by mitmproxy. """ @@ -22,6 +23,7 @@ class ProxyException(Exception): class Kill(ProxyException): + """ Signal that both client and server connection(s) should be killed immediately. """ @@ -37,6 +39,7 @@ class TlsProtocolException(ProtocolException): class ClientHandshakeException(TlsProtocolException): + def __init__(self, message, server): super(ClientHandshakeException, self).__init__(message) self.server = server @@ -67,6 +70,7 @@ class ReplayException(ProxyException): class ScriptException(ProxyException): + @classmethod def from_exception_context(cls, cut_tb=1): """ diff --git a/mitmproxy/filt.py b/mitmproxy/filt.py index f34969dde..6228d3646 100644 --- a/mitmproxy/filt.py +++ b/mitmproxy/filt.py @@ -38,6 +38,7 @@ import pyparsing as pp class _Token(object): + def dump(self, indent=0, fp=sys.stdout): print("{spacing}{name}{expr}".format( spacing="\t" * indent, diff --git a/mitmproxy/flow_export.py b/mitmproxy/flow_export.py index 2ac33e152..0f3847f1c 100644 --- a/mitmproxy/flow_export.py +++ b/mitmproxy/flow_export.py @@ -8,6 +8,7 @@ import re from six.moves.urllib.parse import quote from six.moves.urllib.parse import quote_plus + def curl_command(flow): data = "curl " @@ -124,13 +125,12 @@ def locust_code(flow): max_wait = 3000 """).strip() - components = map(lambda x: quote(x, safe=""), flow.request.path_components) file_name = "_".join(components) name = re.sub('\W|^(?=\d)', '_', file_name) url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components) if name == "" or name is None: - new_name = "_".join([str(flow.request.host) , str(flow.request.timestamp_start)]) + new_name = "_".join([str(flow.request.host), str(flow.request.timestamp_start)]) name = re.sub('\W|^(?=\d)', '_', new_name) args = "" headers = "" diff --git a/mitmproxy/models/connections.py b/mitmproxy/models/connections.py index 91590bcae..24cdff747 100644 --- a/mitmproxy/models/connections.py +++ b/mitmproxy/models/connections.py @@ -10,6 +10,7 @@ from .. import stateobject, utils class ClientConnection(tcp.BaseHandler, stateobject.StateObject): + """ A client connection @@ -21,6 +22,7 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject): timestamp_ssl_setup: TLS established timestamp timestamp_end: Connection end timestamp """ + def __init__(self, client_connection, address, server): # Eventually, this object is restored from state. We don't have a # connection then. @@ -101,6 +103,7 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject): class ServerConnection(tcp.TCPClient, stateobject.StateObject): + """ A server connection @@ -117,6 +120,7 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject): timestamp_ssl_setup: TLS established timestamp timestamp_end: Connection end timestamp """ + def __init__(self, address, source_address=None): tcp.TCPClient.__init__(self, address, source_address) @@ -182,7 +186,7 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject): timestamp_ssl_setup=None, timestamp_end=None, via=None - )) + )) def copy(self): return copy.copy(self) diff --git a/mitmproxy/models/http.py b/mitmproxy/models/http.py index 75ffbfd0a..43618ef80 100644 --- a/mitmproxy/models/http.py +++ b/mitmproxy/models/http.py @@ -72,9 +72,9 @@ class HTTPRequest(MessageMixin, Request): def get_state(self): state = super(HTTPRequest, self).get_state() state.update( - stickycookie = self.stickycookie, - stickyauth = self.stickyauth, - is_replay = self.is_replay, + stickycookie=self.stickycookie, + stickyauth=self.stickyauth, + is_replay=self.is_replay, ) return state @@ -109,6 +109,7 @@ class HTTPRequest(MessageMixin, Request): class HTTPResponse(MessageMixin, Response): + """ A mitmproxy HTTP response. This is a very thin wrapper on top of :py:class:`netlib.http.Response` and @@ -124,7 +125,7 @@ class HTTPResponse(MessageMixin, Response): content, timestamp_start=None, timestamp_end=None, - is_replay = False + is_replay=False ): Response.__init__( self, diff --git a/mitmproxy/models/tcp.py b/mitmproxy/models/tcp.py index 7e966b95c..b87a74acf 100644 --- a/mitmproxy/models/tcp.py +++ b/mitmproxy/models/tcp.py @@ -6,6 +6,7 @@ from .flow import Flow class TCPMessage(Serializable): + def __init__(self, from_client, content, timestamp=None): self.content = content self.from_client = from_client @@ -33,6 +34,7 @@ class TCPMessage(Serializable): class TCPFlow(Flow): + """ A TCPFlow is a simplified representation of a TCP session. """ diff --git a/mitmproxy/protocol/http_replay.py b/mitmproxy/protocol/http_replay.py index e78af074d..f7f271482 100644 --- a/mitmproxy/protocol/http_replay.py +++ b/mitmproxy/protocol/http_replay.py @@ -62,7 +62,7 @@ class RequestReplayThread(threading.Thread): ) r.first_line_format = "relative" else: - r.first_line_format= "absolute" + r.first_line_format = "absolute" else: server_address = (r.host, r.port) server = ServerConnection(server_address, (self.config.host, 0)) diff --git a/mitmproxy/protocol/tls.py b/mitmproxy/protocol/tls.py index 74c55ab47..5facff731 100644 --- a/mitmproxy/protocol/tls.py +++ b/mitmproxy/protocol/tls.py @@ -312,6 +312,7 @@ class TlsClientHello(object): class TlsLayer(Layer): + """ The TLS layer implements transparent TLS connections. @@ -469,9 +470,9 @@ class TlsLayer(Layer): cert, key, chain_file = self._find_cert() if self.config.add_upstream_certs_to_client_chain: - extra_certs = self.server_conn.server_certs + extra_certs = self.server_conn.server_certs else: - extra_certs = None + extra_certs = None try: self.client_conn.convert_to_ssl( @@ -482,7 +483,7 @@ class TlsLayer(Layer): dhparams=self.config.certstore.dhparams, chain_file=chain_file, alpn_select_callback=self.__alpn_select_callback, - extra_chain_certs = extra_certs, + extra_chain_certs=extra_certs, ) # Some TLS clients will not fail the handshake, # but will immediately throw an "unexpected eof" error on the first read. diff --git a/mitmproxy/proxy/config.py b/mitmproxy/proxy/config.py index 58b568ea8..5587e111b 100644 --- a/mitmproxy/proxy/config.py +++ b/mitmproxy/proxy/config.py @@ -58,7 +58,7 @@ class ProxyConfig: body_size_limit=None, mode="regular", upstream_server=None, - upstream_auth = None, + upstream_auth=None, authenticator=None, ignore_hosts=tuple(), tcp_hosts=tuple(), @@ -120,7 +120,7 @@ def process_proxy_options(parser, options): body_size_limit = utils.parse_size(options.body_size_limit) c = 0 - mode, upstream_server, upstream_auth = "regular", None, None + mode, upstream_server, upstream_auth = "regular", None, None if options.transparent_proxy: c += 1 if not platform.resolver: @@ -161,7 +161,7 @@ def process_proxy_options(parser, options): options.clientcerts = os.path.expanduser(options.clientcerts) if not os.path.exists(options.clientcerts): return parser.error( - "Client certificate path does not exist: %s" % options.clientcerts + "Client certificate path does not exist: %s" % options.clientcerts ) if options.auth_nonanonymous or options.auth_singleuser or options.auth_htpasswd: diff --git a/mitmproxy/tnetstring.py b/mitmproxy/tnetstring.py index d9d61258b..acf563ac0 100644 --- a/mitmproxy/tnetstring.py +++ b/mitmproxy/tnetstring.py @@ -68,6 +68,7 @@ like so:: """ import six +from collections import deque __ver_major__ = 0 __ver_minor__ = 2 @@ -77,9 +78,6 @@ __version__ = "%d.%d.%d%s" % ( __ver_major__, __ver_minor__, __ver_patch__, __ver_sub__) -from collections import deque - - def dumps(value, encoding=None): """dumps(object,encoding=None) -> string