# Buggy code
def get_response_from_server(self, flow):
    def get_response():
        self.send_request(flow.request)
        flow.response = self.read_response_headers()

    try:
        get_response()
    except NetlibException as v:
        self.log("server communication error: %s" % repr(v), level="debug")
        # In any case, we try to reconnect at least once. This is
        # necessary because it might be possible that we already
        # initiated an upstream connection after clientconnect that
        # has already been expired, e.g consider the following event
        # log:
        # > clientconnect (transparent mode destination known)
        # > serverconnect (required for client tls handshake)
        # > read n% of large request
        # > server detects timeout, disconnects
        # > read (100-n)% of large request
        # > send large request upstream
        self.disconnect()
        self.connect()
        get_response()

    # call the appropriate script hook - this is an opportunity for an
    # inline script to set flow.stream = True
    flow = self.channel.ask("responseheaders", flow)
    if flow == Kill:
        raise Kill()

    if flow.response.stream:
        flow.response.data.content = None
    else:
        flow.response.data.content = b"".join(
            self.read_response_body(flow.request, flow.response)
        )
    flow.response.timestamp_end = utils.timestamp()

    # no further manipulation of self.server_conn beyond this point
    # we can safely set it as the final attribute value here.
    flow.server_conn = self.server_conn


# 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 get_response_from_server(self, flow):
    def get_response():
        self.send_request(flow.request)
        flow.response = self.read_response_headers()

    try:
        get_response()
    except NetlibException as v:
        self.log("server communication error: %s" % repr(v), level="debug")
        # In any case, we try to reconnect at least once. This is
        # necessary because it might be possible that we already
        # initiated an upstream connection after clientconnect that
        # has already been expired, e.g consider the following event
        # log:
        # > clientconnect (transparent mode destination known)
        # > serverconnect (required for client tls handshake)
        # > read n% of large request
        # > server detects timeout, disconnects
        # > read (100-n)% of large request
        # > send large request upstream
        self.disconnect()
        self.connect()
        get_response()

    # call the appropriate script hook - this is an opportunity for an
    # inline script to set flow.stream = True
    flow = self.channel.ask("responseheaders", flow)

    if flow.response.stream:
        flow.response.data.content = None
    else:
        flow.response.data.content = b"".join(
            self.read_response_body(flow.request, flow.response)
        )
    flow.response.timestamp_end = utils.timestamp()

    # no further manipulation of self.server_conn beyond this point
    # we can safely set it as the final attribute value here.
    flow.server_conn = self.server_conn