# Buggy code
def replay_request(self, f, block=False, run_scripthooks=True):
    """
    Returns None if successful, or error message if not.
    """
    if f.live and run_scripthooks:
        return "Can't replay live request."
    if f.intercepted:
        return "Can't replay while intercepting..."
    if f.request.content is None:
        return "Can't replay request with missing content..."
    if f.request:
        f.backup()
        f.request.is_replay = True
        if "Content-Length" in f.request.headers:
            f.request.headers["Content-Length"] = str(len(f.request.content))
        f.response = None
        f.error = None
        self.process_new_request(f)
        rt = RequestReplayThread(
            self.server.config,
            f,
            self.masterq if run_scripthooks else False,
            self.should_exit,
        )
        rt.start()  # pragma: no cover
        if block:
            rt.join()


# 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 replay_request(self, f, block=False, run_scripthooks=True):
    """
    Returns None if successful, or error message if not.
    """
    if f.live and run_scripthooks:
        return "Can't replay live request."
    if f.intercepted:
        return "Can't replay while intercepting..."
    if f.request.content is None:
        return "Can't replay request with missing content..."
    if f.request:
        f.backup()
        f.request.is_replay = True
        if "Content-Length" in f.request.headers:
            f.request.headers["Content-Length"] = str(len(f.request.content))
        f.response = None
        f.error = None
        self.process_new_request(f)
        rt = RequestReplayThread(
            self.server.config,
            f,
            self.event_queue if run_scripthooks else False,
            self.should_exit,
        )
        rt.start()  # pragma: no cover
        if block:
            rt.join()