# Buggy code
def run(self):
    r = self.flow.request
    first_line_format_backup = r.first_line_format
    try:
        self.flow.response = None

        # If we have a channel, run script hooks.
        if self.channel:
            request_reply = self.channel.ask("request", self.flow)
            if request_reply == Kill:
                raise Kill()
            elif isinstance(request_reply, HTTPResponse):
                self.flow.response = request_reply

        if not self.flow.response:
            # In all modes, we directly connect to the server displayed
            if self.config.mode == "upstream":
                server_address = self.config.upstream_server.address
                server = ServerConnection(server_address, (self.config.host, 0))
                server.connect()
                if r.scheme == "https":
                    connect_request = make_connect_request((r.host, r.port))
                    server.wfile.write(http1.assemble_request(connect_request))
                    server.wfile.flush()
                    resp = http1.read_response(
                        server.rfile,
                        connect_request,
                        body_size_limit=self.config.body_size_limit,
                    )
                    if resp.status_code != 200:
                        raise ReplayException("Upstream server refuses CONNECT request")
                    server.establish_ssl(
                        self.config.clientcerts, sni=self.flow.server_conn.sni
                    )
                    r.first_line_format = "relative"
                else:
                    r.first_line_format = "absolute"
            else:
                server_address = (r.host, r.port)
                server = ServerConnection(server_address, (self.config.host, 0))
                server.connect()
                if r.scheme == "https":
                    server.establish_ssl(
                        self.config.clientcerts, sni=self.flow.server_conn.sni
                    )
                r.first_line_format = "relative"

            server.wfile.write(http1.assemble_request(r))
            server.wfile.flush()
            self.flow.server_conn = server
            self.flow.response = HTTPResponse.wrap(
                http1.read_response(
                    server.rfile, r, body_size_limit=self.config.body_size_limit
                )
            )
        if self.channel:
            response_reply = self.channel.ask("response", self.flow)
            if response_reply == Kill:
                raise Kill()
    except (ReplayException, HttpException, TcpException) as e:
        self.flow.error = Error(str(e))
        if self.channel:
            self.channel.ask("error", self.flow)
    except Kill:
        # Kill should only be raised if there's a channel in the
        # first place.
        from ..proxy.root_context import Log

        self.channel.tell("log", Log("Connection killed", "info"))
    except Exception:
        from ..proxy.root_context import Log

        self.channel.tell("log", Log(traceback.format_exc(), "error"))
    finally:
        r.first_line_format = first_line_format_backup


# Traceback (AttributeError)
./mitmdump.run.sh: line 2: 14504 Segmentation fault      mitmdump -p 59873 > network
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/libmproxy/proxy/server.py", line 120, in handle
root_layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/proxy/modes/http_proxy.py", line 10, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/tls.py", line 272, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 130, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 323, in __call__
self.handle_regular_mode_connect(request)
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 409, in handle_regular_mode_connect
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/tls.py", line 272, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 130, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 340, in __call__
self.get_response_from_server(flow)
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 471, in get_response_from_server
if flow.response.stream:
AttributeError: 'NoneType' object has no attribute 'response'

# Fixed code
def run(self):
    r = self.flow.request
    first_line_format_backup = r.first_line_format
    try:
        self.flow.response = None

        # If we have a channel, run script hooks.
        if self.channel:
            request_reply = self.channel.ask("request", self.flow)
            if isinstance(request_reply, HTTPResponse):
                self.flow.response = request_reply

        if not self.flow.response:
            # In all modes, we directly connect to the server displayed
            if self.config.mode == "upstream":
                server_address = self.config.upstream_server.address
                server = ServerConnection(server_address, (self.config.host, 0))
                server.connect()
                if r.scheme == "https":
                    connect_request = make_connect_request((r.host, r.port))
                    server.wfile.write(http1.assemble_request(connect_request))
                    server.wfile.flush()
                    resp = http1.read_response(
                        server.rfile,
                        connect_request,
                        body_size_limit=self.config.body_size_limit,
                    )
                    if resp.status_code != 200:
                        raise ReplayException("Upstream server refuses CONNECT request")
                    server.establish_ssl(
                        self.config.clientcerts, sni=self.flow.server_conn.sni
                    )
                    r.first_line_format = "relative"
                else:
                    r.first_line_format = "absolute"
            else:
                server_address = (r.host, r.port)
                server = ServerConnection(server_address, (self.config.host, 0))
                server.connect()
                if r.scheme == "https":
                    server.establish_ssl(
                        self.config.clientcerts, sni=self.flow.server_conn.sni
                    )
                r.first_line_format = "relative"

            server.wfile.write(http1.assemble_request(r))
            server.wfile.flush()
            self.flow.server_conn = server
            self.flow.response = HTTPResponse.wrap(
                http1.read_response(
                    server.rfile, r, body_size_limit=self.config.body_size_limit
                )
            )
        if self.channel:
            response_reply = self.channel.ask("response", self.flow)
            if response_reply == Kill:
                raise Kill()
    except (ReplayException, HttpException, TcpException) as e:
        self.flow.error = Error(str(e))
        if self.channel:
            self.channel.ask("error", self.flow)
    except Kill:
        # Kill should only be raised if there's a channel in the
        # first place.
        from ..proxy.root_context import Log

        self.channel.tell("log", Log("Connection killed", "info"))
    except Exception:
        from ..proxy.root_context import Log

        self.channel.tell("log", Log(traceback.format_exc(), "error"))
    finally:
        r.first_line_format = first_line_format_backup