Skip to content

Commit

Permalink
netlib.exceptions.* -> mitmproxy.exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Oct 19, 2016
1 parent 301d52d commit 01a449b
Show file tree
Hide file tree
Showing 34 changed files with 164 additions and 172 deletions.
2 changes: 1 addition & 1 deletion mitmproxy/addons/streambodies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from netlib.http import http1
from netlib import exceptions
from mitmproxy import exceptions
from mitmproxy import ctx


Expand Down
62 changes: 61 additions & 1 deletion mitmproxy/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""
We try to be very hygienic regarding the exceptions we throw:
Every Exception mitmproxy raises shall be a subclass of ProxyException.
- Every exception that might be externally visible to users shall be a subclass
of ProxyException.p
- Every exception in the base net module shall be a subclass
of NetlibException, and will not be propagated directly to users.
See also: http://lucumr.pocoo.org/2014/10/16/on-error-handling/
"""
Expand Down Expand Up @@ -100,3 +103,60 @@ class AddonError(Exception):

class AddonHalt(Exception):
pass


"""
Every net Exception raised shall be a subclass of NetlibException.
"""


class NetlibException(Exception):
"""
Base class for all exceptions thrown by netlib.
"""
def __init__(self, message=None):
super().__init__(message)


class Disconnect:
"""Immediate EOF"""


class HttpException(NetlibException):
pass


class HttpReadDisconnect(HttpException, Disconnect):
pass


class HttpSyntaxException(HttpException):
pass


class TcpException(NetlibException):
pass


class TcpDisconnect(TcpException, Disconnect):
pass


class TcpReadIncomplete(TcpException):
pass


class TcpTimeout(TcpException):
pass


class TlsException(NetlibException):
pass


class InvalidCertificateException(TlsException):
pass


class Timeout(TcpException):
pass
3 changes: 1 addition & 2 deletions mitmproxy/proxy/modes/socks_proxy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import netlib.exceptions
from mitmproxy import exceptions
from mitmproxy.proxy import protocol
from netlib import socks
Expand Down Expand Up @@ -48,7 +47,7 @@ def __call__(self):
connect_reply.to_file(self.client_conn.wfile)
self.client_conn.wfile.flush()

except (socks.SocksError, netlib.exceptions.TcpException) as e:
except (socks.SocksError, exceptions.TcpException) as e:
raise exceptions.Socks5ProtocolException("SOCKS5 mode failure: %s" % repr(e))

self.server_conn.address = connect_request.addr
Expand Down
3 changes: 1 addition & 2 deletions mitmproxy/proxy/protocol/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import netlib.exceptions
from mitmproxy import exceptions
from mitmproxy import connections

Expand Down Expand Up @@ -177,7 +176,7 @@ def connect(self):
self.channel.ask("serverconnect", self.server_conn)
try:
self.server_conn.connect()
except netlib.exceptions.TcpException as e:
except exceptions.TcpException as e:
raise exceptions.ProtocolException(
"Server connection to {} failed: {}".format(
repr(self.server_conn.address), str(e)
Expand Down
19 changes: 9 additions & 10 deletions mitmproxy/proxy/protocol/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import h2.exceptions
import netlib.exceptions
import time
import traceback
from mitmproxy import exceptions
Expand Down Expand Up @@ -46,7 +45,7 @@ def read_response(self, request):

def send_response(self, response):
if response.data.content is None:
raise netlib.exceptions.HttpException("Cannot assemble flow with missing content")
raise exceptions.HttpException("Cannot assemble flow with missing content")
self.send_response_headers(response)
self.send_response_body(response, [response.data.content])

Expand Down Expand Up @@ -146,10 +145,10 @@ def __call__(self):
request = self.get_request_from_client(f)
# Make sure that the incoming request matches our expectations
self.validate_request(request)
except netlib.exceptions.HttpReadDisconnect:
except exceptions.HttpReadDisconnect:
# don't throw an error for disconnects that happen before/between requests.
return
except netlib.exceptions.HttpException as e:
except exceptions.HttpException as e:
# We optimistically guess there might be an HTTP client on the
# other end
self.send_error_response(400, repr(e))
Expand All @@ -173,7 +172,7 @@ def __call__(self):
if self.mode == "regular" and request.first_line_format == "authority":
self.handle_regular_mode_connect(request)
return
except (exceptions.ProtocolException, netlib.exceptions.NetlibException) as e:
except (exceptions.ProtocolException, exceptions.NetlibException) as e:
# HTTPS tasting means that ordinary errors like resolution and
# connection errors can happen here.
self.send_error_response(502, repr(e))
Expand Down Expand Up @@ -224,7 +223,7 @@ def __call__(self):
self.handle_upstream_mode_connect(f.request.copy())
return

except (exceptions.ProtocolException, netlib.exceptions.NetlibException) as e:
except (exceptions.ProtocolException, exceptions.NetlibException) as e:
self.send_error_response(502, repr(e))
if not f.response:
f.error = flow.Error(str(e))
Expand Down Expand Up @@ -254,7 +253,7 @@ def send_error_response(self, code, message, headers=None):
try:
response = http.make_error_response(code, message, headers)
self.send_response(response)
except (netlib.exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException):
except (exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException):
self.log(traceback.format_exc(), "debug")

def change_upstream_proxy_server(self, address):
Expand Down Expand Up @@ -300,7 +299,7 @@ def get_response():

try:
get_response()
except netlib.exceptions.NetlibException as e:
except exceptions.NetlibException as e:
self.log(
"server communication error: %s" % repr(e),
level="debug"
Expand Down Expand Up @@ -396,7 +395,7 @@ def establish_server_connection(self, host, port, scheme):

def validate_request(self, request):
if request.first_line_format == "absolute" and request.scheme != "http":
raise netlib.exceptions.HttpException("Invalid request scheme: %s" % request.scheme)
raise exceptions.HttpException("Invalid request scheme: %s" % request.scheme)

expected_request_forms = {
"regular": ("authority", "absolute",),
Expand All @@ -409,7 +408,7 @@ def validate_request(self, request):
err_message = "Invalid HTTP request form (expected: %s, got: %s)" % (
" or ".join(allowed_request_forms), request.first_line_format
)
raise netlib.exceptions.HttpException(err_message)
raise exceptions.HttpException(err_message)

if self.mode == "regular" and request.first_line_format == "absolute":
request.first_line_format = "relative"
Expand Down
1 change: 0 additions & 1 deletion mitmproxy/proxy/protocol/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from h2 import events
import queue

import netlib.exceptions
from mitmproxy import exceptions
from mitmproxy import http
from mitmproxy.proxy.protocol import base
Expand Down
3 changes: 1 addition & 2 deletions mitmproxy/proxy/protocol/http_replay.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import traceback

import netlib.exceptions
from mitmproxy import log
from mitmproxy import controller
from mitmproxy import exceptions
Expand Down Expand Up @@ -97,7 +96,7 @@ def run(self):
response_reply = self.channel.ask("response", self.f)
if response_reply == exceptions.Kill:
raise exceptions.Kill()
except (exceptions.ReplayException, netlib.exceptions.NetlibException) as e:
except (exceptions.ReplayException, exceptions.NetlibException) as e:
self.f.error = flow.Error(str(e))
if self.channel:
self.channel.ask("error", self.f)
Expand Down
3 changes: 1 addition & 2 deletions mitmproxy/proxy/protocol/rawtcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from OpenSSL import SSL

import netlib.exceptions
import netlib.tcp
from mitmproxy import tcp
from mitmproxy import flow
Expand Down Expand Up @@ -56,7 +55,7 @@ def __call__(self):
self.channel.ask("tcp_message", f)
dst.sendall(tcp_message.content)

except (socket.error, netlib.exceptions.TcpException, SSL.Error) as e:
except (socket.error, exceptions.TcpException, SSL.Error) as e:
if not self.ignore:
f.error = flow.Error("TCP connection closed unexpectedly: {}".format(repr(e)))
self.channel.tell("tcp_error", f)
Expand Down
7 changes: 3 additions & 4 deletions mitmproxy/proxy/protocol/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Union

import construct
import netlib.exceptions
from mitmproxy import exceptions
from mitmproxy.contrib.tls import _constructs
from mitmproxy.proxy.protocol import base
Expand Down Expand Up @@ -484,7 +483,7 @@ def _establish_tls_with_client(self):
# The reason for this might be difficult to find, so we try to peek here to see if it
# raises ann error.
self.client_conn.rfile.peek(1)
except netlib.exceptions.TlsException as e:
except exceptions.TlsException as e:
raise exceptions.ClientHandshakeException(
"Cannot establish TLS with client (sni: {sni}): {e}".format(
sni=self._client_hello.sni, e=repr(e)
Expand Down Expand Up @@ -528,9 +527,9 @@ def _establish_tls_with_server(self):
if tls_cert_err is not None:
self.log(str(tls_cert_err), "warn")
self.log("Ignoring server verification error, continuing with connection", "warn")
except netlib.exceptions.InvalidCertificateException as e:
except exceptions.InvalidCertificateException as e:
raise exceptions.InvalidServerCertificate(str(e))
except netlib.exceptions.TlsException as e:
except exceptions.TlsException as e:
raise exceptions.TlsProtocolException(
"Cannot establish TLS with {address} (sni: {sni}): {e}".format(
address=repr(self.server_conn.address),
Expand Down
3 changes: 1 addition & 2 deletions mitmproxy/proxy/protocol/websockets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import netlib.exceptions
import socket
import struct
from OpenSSL import SSL
Expand Down Expand Up @@ -105,7 +104,7 @@ def __call__(self):

if not self._handle_frame(frame, source_conn, other_conn, is_server):
return
except (socket.error, netlib.exceptions.TcpException, SSL.Error) as e:
except (socket.error, exceptions.TcpException, SSL.Error) as e:
self.log("WebSockets connection closed unexpectedly by {}: {}".format(
"server" if is_server else "client", repr(e)), "info")
except Exception as e: # pragma: no cover
Expand Down
3 changes: 1 addition & 2 deletions mitmproxy/proxy/root_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import netlib.exceptions
from mitmproxy import log
from mitmproxy import exceptions
from mitmproxy.proxy import protocol
Expand Down Expand Up @@ -43,7 +42,7 @@ def next_layer(self, top_layer):
def _next_layer(self, top_layer):
try:
d = top_layer.client_conn.rfile.peek(3)
except netlib.exceptions.TcpException as e:
except exceptions.TcpException as e:
raise exceptions.ProtocolException(str(e))
client_tls = protocol.is_tls_record_magic(d)

Expand Down
3 changes: 1 addition & 2 deletions mitmproxy/proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys
import traceback

import netlib.exceptions
from mitmproxy import exceptions
from mitmproxy import connections
from mitmproxy import http
Expand Down Expand Up @@ -138,7 +137,7 @@ def handle(self):
try:
error_response = http.make_error_response(502, repr(e))
self.client_conn.send(http1.assemble_response(error_response))
except netlib.exceptions.TcpException:
except exceptions.TcpException:
pass
except Exception:
self.log(traceback.format_exc(), "error")
Expand Down
59 changes: 0 additions & 59 deletions netlib/exceptions.py

This file was deleted.

2 changes: 1 addition & 1 deletion netlib/http/http1/assemble.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import netlib.http.url
from netlib import exceptions
from mitmproxy import exceptions


def assemble_request(request):
Expand Down
2 changes: 1 addition & 1 deletion netlib/http/http1/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from netlib.http import headers
from netlib.http import url
from netlib import check
from netlib import exceptions
from mitmproxy import exceptions


def get_header_tokens(headers, key):
Expand Down
4 changes: 2 additions & 2 deletions netlib/http/http2/framereader.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import codecs

import hyperframe
from ...exceptions import HttpException
from mitmproxy import exceptions


def read_raw_frame(rfile):
header = rfile.safe_read(9)
length = int(codecs.encode(header[:3], 'hex_codec'), 16)

if length == 4740180:
raise HttpException("Length field looks more like HTTP/1.1:\n{}".format(rfile.read(-1)))
raise exceptions.HttpException("Length field looks more like HTTP/1.1:\n{}".format(rfile.read(-1)))

body = rfile.safe_read(length)
return [header, body]
Expand Down
Loading

0 comments on commit 01a449b

Please sign in to comment.