prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Support absolute-form HTTP requests with IPv6 addresses
  ##### Steps to reproduce the problem:
  
  1. MITMDump proxy IPv6 flow
  2. Log
  ```
  172.17.15.1:53074: HTTP protocol error in client request: Bad HTTP request line: b'GET http://[::ffff:180.97.8.37]/mmsns/9KavCVwReibwDKBMmibrWUdVZZbHCQ0bV3R89mboKO6QDls7Sxcl4tfbHvLIHFbj3NASftTH2VAGw/150?tp=wxpc&length=2208&width=1242&idx=1&token=WSEN6qDsKwV8A02w3onOGQYfxnkibdqSOkmHhZGNB4DGicdGyTltMQXCTF7lr4IJR8Jz4lKQBBW47EV1CP33SGjg HTTP/1.1'
  172.17.15.1:53075: HTTP protocol error in client request: Bad HTTP request line: b'GET http://[::ffff:b461:819]/mmcrhead/Q3auHgzwzM606QEH0kXoF60vMh5Iiay7B3DiauET3kCpbBwEfgzhNqOSeJ6y4geORGPxEcKf36Totd4sHQcwvBEg/0 HTTP/1.1'
  ```
  
  
  ##### Any other comments? What have you tried so far?
  No
  
  
  ##### System information
  ```
  Mitmproxy version: 1.0.2
  Python version: 3.6.0
  Platform: Darwin-15.6.0-x86_64-i386-64bit
  SSL version: OpenSSL 1.0.2j  26 Sep 2016
  Mac version: 10.11.6 ('', '', '') x86_6
  ```
  
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `mitmproxy/net/check.py`
  Content:
  ```
  1 import re
  2 
  3 # Allow underscore in host name
  4 _label_valid = re.compile(b"(?!-)[A-Z\d\-_]{1,63}(?<!-)$", re.IGNORECASE)
  5 
  6 
  7 def is_valid_host(host: bytes) -> bool:
  8     """
  9         Checks if a hostname is valid.
  10     """
  11     try:
  12         host.decode("idna")
  13     except ValueError:
  14         return False
  15     if len(host) > 255:
  16         return False
  17     if host and host[-1:] == b".":
  18         host = host[:-1]
  19     return all(_label_valid.match(x) for x in host.split(b"."))
  20 
  21 
  22 def is_valid_port(port):
  23     return 0 <= port <= 65535
  24
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py
  --- a/mitmproxy/net/check.py
  +++ b/mitmproxy/net/check.py
  @@ -1,3 +1,4 @@
  +import ipaddress
   import re
   
   # Allow underscore in host name
  @@ -6,17 +7,26 @@
   
   def is_valid_host(host: bytes) -> bool:
       """
  -        Checks if a hostname is valid.
  +    Checks if the passed bytes are a valid DNS hostname or an IPv4/IPv6 address.
       """
       try:
           host.decode("idna")
       except ValueError:
           return False
  +    # RFC1035: 255 bytes or less.
       if len(host) > 255:
           return False
       if host and host[-1:] == b".":
           host = host[:-1]
  -    return all(_label_valid.match(x) for x in host.split(b"."))
  +    # DNS hostname
  +    if all(_label_valid.match(x) for x in host.split(b".")):
  +        return True
  +    # IPv4/IPv6 address
  +    try:
  +        ipaddress.ip_address(host.decode('idna'))
  +        return True
  +    except ValueError:
  +        return False
   
   
   def is_valid_port(port):
  
verification_info: {"golden_diff": "diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py\n--- a/mitmproxy/net/check.py\n+++ b/mitmproxy/net/check.py\n@@ -1,3 +1,4 @@\n+import ipaddress\n import re\n \n # Allow underscore in host name\n@@ -6,17 +7,26 @@\n \n def is_valid_host(host: bytes) -> bool:\n     \"\"\"\n-        Checks if a hostname is valid.\n+    Checks if the passed bytes are a valid DNS hostname or an IPv4/IPv6 address.\n     \"\"\"\n     try:\n         host.decode(\"idna\")\n     except ValueError:\n         return False\n+    # RFC1035: 255 bytes or less.\n     if len(host) > 255:\n         return False\n     if host and host[-1:] == b\".\":\n         host = host[:-1]\n-    return all(_label_valid.match(x) for x in host.split(b\".\"))\n+    # DNS hostname\n+    if all(_label_valid.match(x) for x in host.split(b\".\")):\n+        return True\n+    # IPv4/IPv6 address\n+    try:\n+        ipaddress.ip_address(host.decode('idna'))\n+        return True\n+    except ValueError:\n+        return False\n \n \n def is_valid_port(port):\n", "issue": "Support absolute-form HTTP requests with IPv6 addresses\n##### Steps to reproduce the problem:\r\n\r\n1. MITMDump proxy IPv6 flow\r\n2. Log\r\n```\r\n172.17.15.1:53074: HTTP protocol error in client request: Bad HTTP request line: b'GET http://[::ffff:180.97.8.37]/mmsns/9KavCVwReibwDKBMmibrWUdVZZbHCQ0bV3R89mboKO6QDls7Sxcl4tfbHvLIHFbj3NASftTH2VAGw/150?tp=wxpc&length=2208&width=1242&idx=1&token=WSEN6qDsKwV8A02w3onOGQYfxnkibdqSOkmHhZGNB4DGicdGyTltMQXCTF7lr4IJR8Jz4lKQBBW47EV1CP33SGjg HTTP/1.1'\r\n172.17.15.1:53075: HTTP protocol error in client request: Bad HTTP request line: b'GET http://[::ffff:b461:819]/mmcrhead/Q3auHgzwzM606QEH0kXoF60vMh5Iiay7B3DiauET3kCpbBwEfgzhNqOSeJ6y4geORGPxEcKf36Totd4sHQcwvBEg/0 HTTP/1.1'\r\n```\r\n\r\n\r\n##### Any other comments? What have you tried so far?\r\nNo\r\n\r\n\r\n##### System information\r\n```\r\nMitmproxy version: 1.0.2\r\nPython version: 3.6.0\r\nPlatform: Darwin-15.6.0-x86_64-i386-64bit\r\nSSL version: OpenSSL 1.0.2j  26 Sep 2016\r\nMac version: 10.11.6 ('', '', '') x86_6\r\n```\r\n\r\n\n", "before_files": [{"content": "import re\n\n# Allow underscore in host name\n_label_valid = re.compile(b\"(?!-)[A-Z\\d\\-_]{1,63}(?<!-)$\", re.IGNORECASE)\n\n\ndef is_valid_host(host: bytes) -> bool:\n    \"\"\"\n        Checks if a hostname is valid.\n    \"\"\"\n    try:\n        host.decode(\"idna\")\n    except ValueError:\n        return False\n    if len(host) > 255:\n        return False\n    if host and host[-1:] == b\".\":\n        host = host[:-1]\n    return all(_label_valid.match(x) for x in host.split(b\".\"))\n\n\ndef is_valid_port(port):\n    return 0 <= port <= 65535\n", "path": "mitmproxy/net/check.py"}], "after_files": [{"content": "import ipaddress\nimport re\n\n# Allow underscore in host name\n_label_valid = re.compile(b\"(?!-)[A-Z\\d\\-_]{1,63}(?<!-)$\", re.IGNORECASE)\n\n\ndef is_valid_host(host: bytes) -> bool:\n    \"\"\"\n    Checks if the passed bytes are a valid DNS hostname or an IPv4/IPv6 address.\n    \"\"\"\n    try:\n        host.decode(\"idna\")\n    except ValueError:\n        return False\n    # RFC1035: 255 bytes or less.\n    if len(host) > 255:\n        return False\n    if host and host[-1:] == b\".\":\n        host = host[:-1]\n    # DNS hostname\n    if all(_label_valid.match(x) for x in host.split(b\".\")):\n        return True\n    # IPv4/IPv6 address\n    try:\n        ipaddress.ip_address(host.decode('idna'))\n        return True\n    except ValueError:\n        return False\n\n\ndef is_valid_port(port):\n    return 0 <= port <= 65535\n", "path": "mitmproxy/net/check.py"}]}