# Buggy code
def process_request_hook(self, flow):
    # Determine .scheme, .host and .port attributes for inline scripts.
    # For absolute-form requests, they are directly given in the request.
    # For authority-form requests, we only need to determine the request scheme.
    # For relative-form requests, we need to determine host and port as
    # well.
    if self.mode == "regular":
        pass  # only absolute-form at this point, nothing to do here.
    elif self.mode == "upstream":
        if flow.request.first_line_format == "authority":
            flow.request.scheme = "http"  # pseudo value
    else:
        # Setting request.host also updates the host header, which we want to preserve
        host_header = flow.request.headers.get("host", None)
        flow.request.host = self.__initial_server_conn.address.host
        flow.request.port = self.__initial_server_conn.address.port
        if host_header:
            flow.request.headers["host"] = host_header
        flow.request.scheme = "https" if self.__initial_server_tls else "http"

    request_reply = self.channel.ask("request", flow)
    if request_reply == Kill:
        raise Kill()
    if isinstance(request_reply, HTTPResponse):
        flow.response = request_reply
        return


# 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 process_request_hook(self, flow):
    # Determine .scheme, .host and .port attributes for inline scripts.
    # For absolute-form requests, they are directly given in the request.
    # For authority-form requests, we only need to determine the request scheme.
    # For relative-form requests, we need to determine host and port as
    # well.
    if self.mode == "regular":
        pass  # only absolute-form at this point, nothing to do here.
    elif self.mode == "upstream":
        if flow.request.first_line_format == "authority":
            flow.request.scheme = "http"  # pseudo value
    else:
        # Setting request.host also updates the host header, which we want to preserve
        host_header = flow.request.headers.get("host", None)
        flow.request.host = self.__initial_server_conn.address.host
        flow.request.port = self.__initial_server_conn.address.port
        if host_header:
            flow.request.headers["host"] = host_header
        flow.request.scheme = "https" if self.__initial_server_tls else "http"

    request_reply = self.channel.ask("request", flow)
    if isinstance(request_reply, HTTPResponse):
        flow.response = request_reply
        return