We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
HEAD method not allowed to access mitm.it
##### Steps to reproduce the problem:

1. Using mitmdump in transparent mode, with on-boarding port on 6969 (did not test on 80)
/mitmdump -T --host --insecure --onboarding-port 6969

2. On iPad + Chrome + Transparent mode, access to http://mitm.it:6969

3. An HEAD /cert/pem is sent, receive an "405 Method Not Allowed"

Any idea of any workaround?


##### Any other comments? What have you tried so far?

Exact packet:
Hypertext Transfer Protocol
    HEAD /cert/pem HTTP/1.1\r\n
    Host: mitm.it:6969\r\n
    Connection: keep-alive\r\n
    User-Agent: Mozilla/5.0 (iPad; CPU OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/55.0.2883.79 Mobile/13C75 Safari/601.1.46\r\n
    Accept-Encoding: gzip, deflate, sdch\r\n
    Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4\r\n
    \r\n



Answer:
Hypertext Transfer Protocol
    HTTP/1.1 405 Method Not Allowed\r\n
    Server: TornadoServer/4.4.2\r\n
    Content-Length: 87\r\n
    Date: Tue, 09 May 2017 13:35:38 GMT\r\n
    Content-Type: text/html; charset=UTF-8\r\n
    \r\n



##### System information

mitmdump --version
Mitmproxy version: 2.0.1 (release version) Precompiled Binary
Python version: 3.5.2
Platform: Darwin-16.5.0-x86_64-i386-64bit
SSL version: OpenSSL 1.0.2j  26 Sep 2016
Mac version: 10.12.4 ('', '', '') x86_64

HEAD method not allowed to access mitm.it
##### Steps to reproduce the problem:

1. Using mitmdump in transparent mode, with on-boarding port on 6969 (did not test on 80)
/mitmdump -T --host --insecure --onboarding-port 6969

2. On iPad + Chrome + Transparent mode, access to http://mitm.it:6969

3. An HEAD /cert/pem is sent, receive an "405 Method Not Allowed"

Any idea of any workaround?


##### Any other comments? What have you tried so far?

Exact packet:
Hypertext Transfer Protocol
    HEAD /cert/pem HTTP/1.1\r\n
    Host: mitm.it:6969\r\n
    Connection: keep-alive\r\n
    User-Agent: Mozilla/5.0 (iPad; CPU OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/55.0.2883.79 Mobile/13C75 Safari/601.1.46\r\n
    Accept-Encoding: gzip, deflate, sdch\r\n
    Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4\r\n
    \r\n



Answer:
Hypertext Transfer Protocol
    HTTP/1.1 405 Method Not Allowed\r\n
    Server: TornadoServer/4.4.2\r\n
    Content-Length: 87\r\n
    Date: Tue, 09 May 2017 13:35:38 GMT\r\n
    Content-Type: text/html; charset=UTF-8\r\n
    \r\n



##### System information

mitmdump --version
Mitmproxy version: 2.0.1 (release version) Precompiled Binary
Python version: 3.5.2
Platform: Darwin-16.5.0-x86_64-i386-64bit
SSL version: OpenSSL 1.0.2j  26 Sep 2016
Mac version: 10.12.4 ('', '', '') x86_64

HEAD method not allowed to access mitm.it
##### Steps to reproduce the problem:

1. Using mitmdump in transparent mode, with on-boarding port on 6969 (did not test on 80)
/mitmdump -T --host --insecure --onboarding-port 6969

2. On iPad + Chrome + Transparent mode, access to http://mitm.it:6969

3. An HEAD /cert/pem is sent, receive an "405 Method Not Allowed"

Any idea of any workaround?


##### Any other comments? What have you tried so far?

Exact packet:
Hypertext Transfer Protocol
    HEAD /cert/pem HTTP/1.1\r\n
    Host: mitm.it:6969\r\n
    Connection: keep-alive\r\n
    User-Agent: Mozilla/5.0 (iPad; CPU OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/55.0.2883.79 Mobile/13C75 Safari/601.1.46\r\n
    Accept-Encoding: gzip, deflate, sdch\r\n
    Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4\r\n
    \r\n



Answer:
Hypertext Transfer Protocol
    HTTP/1.1 405 Method Not Allowed\r\n
    Server: TornadoServer/4.4.2\r\n
    Content-Length: 87\r\n
    Date: Tue, 09 May 2017 13:35:38 GMT\r\n
    Content-Type: text/html; charset=UTF-8\r\n
    \r\n



##### System information

mitmdump --version
Mitmproxy version: 2.0.1 (release version) Precompiled Binary
Python version: 3.5.2
Platform: Darwin-16.5.0-x86_64-i386-64bit
SSL version: OpenSSL 1.0.2j  26 Sep 2016
Mac version: 10.12.4 ('', '', '') x86_64


--- 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/addons/onboardingapp/app.py`
Content:
```
1 import os
2 
3 import tornado.template
4 import tornado.web
5 import tornado.wsgi
6 
7 from mitmproxy.utils import data
8 from mitmproxy.proxy import config
9 
10 loader = tornado.template.Loader(data.pkg_data.path("addons/onboardingapp/templates"))
11 
12 
13 class Adapter(tornado.wsgi.WSGIAdapter):
14     # Tornado doesn't make the WSGI environment available to pages, so this
15     # hideous monkey patch is the easiest way to get to the mitmproxy.master
16     # variable.
17 
18     def __init__(self, application):
19         self._application = application
20 
21     def application(self, request):
22         request.master = self.environ["mitmproxy.master"]
23         return self._application(request)
24 
25     def __call__(self, environ, start_response):
26         self.environ = environ
27         return tornado.wsgi.WSGIAdapter.__call__(
28             self,
29             environ,
30             start_response
31         )
32 
33 
34 class Index(tornado.web.RequestHandler):
35 
36     def get(self):
37         t = loader.load("index.html")
38         self.write(t.generate())
39 
40 
41 class PEM(tornado.web.RequestHandler):
42 
43     @property
44     def filename(self):
45         return config.CONF_BASENAME + "-ca-cert.pem"
46 
47     def get(self):
48         p = os.path.join(self.request.master.options.cadir, self.filename)
49         p = os.path.expanduser(p)
50         self.set_header("Content-Type", "application/x-x509-ca-cert")
51         self.set_header(
52             "Content-Disposition",
53             "inline; filename={}".format(
54                 self.filename))
55 
56         with open(p, "rb") as f:
57             self.write(f.read())
58 
59 
60 class P12(tornado.web.RequestHandler):
61 
62     @property
63     def filename(self):
64         return config.CONF_BASENAME + "-ca-cert.p12"
65 
66     def get(self):
67         p = os.path.join(self.request.master.options.cadir, self.filename)
68         p = os.path.expanduser(p)
69         self.set_header("Content-Type", "application/x-pkcs12")
70         self.set_header(
71             "Content-Disposition",
72             "inline; filename={}".format(
73                 self.filename))
74 
75         with open(p, "rb") as f:
76             self.write(f.read())
77 
78 
79 application = tornado.web.Application(
80     [
81         (r"/", Index),
82         (r"/cert/pem", PEM),
83         (r"/cert/p12", P12),
84         (
85             r"/static/(.*)",
86             tornado.web.StaticFileHandler,
87             {
88                 "path": data.pkg_data.path("addons/onboardingapp/static")
89             }
90         ),
91     ],
92     # debug=True
93 )
94
```
--- 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()

```

diff --git a/mitmproxy/addons/onboardingapp/app.py b/mitmproxy/addons/onboardingapp/app.py
--- a/mitmproxy/addons/onboardingapp/app.py
+++ b/mitmproxy/addons/onboardingapp/app.py
@@ -44,6 +44,18 @@
     def filename(self):
         return config.CONF_BASENAME + "-ca-cert.pem"
 
+    def head(self):
+        p = os.path.join(self.request.master.options.cadir, self.filename)
+        p = os.path.expanduser(p)
+        content_length = os.path.getsize(p)
+
+        self.set_header("Content-Type", "application/x-x509-ca-cert")
+        self.set_header(
+            "Content-Disposition",
+            "inline; filename={}".format(
+                self.filename))
+        self.set_header("Content-Length", content_length)
+
     def get(self):
         p = os.path.join(self.request.master.options.cadir, self.filename)
         p = os.path.expanduser(p)
@@ -63,6 +75,19 @@
     def filename(self):
         return config.CONF_BASENAME + "-ca-cert.p12"
 
+    def head(self):
+        p = os.path.join(self.request.master.options.cadir, self.filename)
+        p = os.path.expanduser(p)
+        content_length = os.path.getsize(p)
+
+        self.set_header("Content-Type", "application/x-pkcs12")
+        self.set_header(
+            "Content-Disposition",
+            "inline; filename={}".format(
+                self.filename))
+
+        self.set_header("Content-Length", content_length)
+
     def get(self):
         p = os.path.join(self.request.master.options.cadir, self.filename)
         p = os.path.expanduser(p)

{"golden_diff": "diff --git a/mitmproxy/addons/onboardingapp/app.py b/mitmproxy/addons/onboardingapp/app.py\n--- a/mitmproxy/addons/onboardingapp/app.py\n+++ b/mitmproxy/addons/onboardingapp/app.py\n@@ -44,6 +44,18 @@\n     def filename(self):\n         return config.CONF_BASENAME + \"-ca-cert.pem\"\n \n+    def head(self):\n+        p = os.path.join(self.request.master.options.cadir, self.filename)\n+        p = os.path.expanduser(p)\n+        content_length = os.path.getsize(p)\n+\n+        self.set_header(\"Content-Type\", \"application/x-x509-ca-cert\")\n+        self.set_header(\n+            \"Content-Disposition\",\n+            \"inline; filename={}\".format(\n+                self.filename))\n+        self.set_header(\"Content-Length\", content_length)\n+\n     def get(self):\n         p = os.path.join(self.request.master.options.cadir, self.filename)\n         p = os.path.expanduser(p)\n@@ -63,6 +75,19 @@\n     def filename(self):\n         return config.CONF_BASENAME + \"-ca-cert.p12\"\n \n+    def head(self):\n+        p = os.path.join(self.request.master.options.cadir, self.filename)\n+        p = os.path.expanduser(p)\n+        content_length = os.path.getsize(p)\n+\n+        self.set_header(\"Content-Type\", \"application/x-pkcs12\")\n+        self.set_header(\n+            \"Content-Disposition\",\n+            \"inline; filename={}\".format(\n+                self.filename))\n+\n+        self.set_header(\"Content-Length\", content_length)\n+\n     def get(self):\n         p = os.path.join(self.request.master.options.cadir, self.filename)\n         p = os.path.expanduser(p)\n", "issue": "HEAD method not allowed to access mitm.it\n##### Steps to reproduce the problem:\r\n\r\n1. Using mitmdump in transparent mode, with on-boarding port on 6969 (did not test on 80)\r\n/mitmdump -T --host --insecure --onboarding-port 6969\r\n\r\n2. On iPad + Chrome + Transparent mode, access to http://mitm.it:6969\r\n\r\n3. An HEAD /cert/pem is sent, receive an \"405 Method Not Allowed\"\r\n\r\nAny idea of any workaround?\r\n\r\n\r\n##### Any other comments? What have you tried so far?\r\n\r\nExact packet:\r\nHypertext Transfer Protocol\r\n    HEAD /cert/pem HTTP/1.1\\r\\n\r\n    Host: mitm.it:6969\\r\\n\r\n    Connection: keep-alive\\r\\n\r\n    User-Agent: Mozilla/5.0 (iPad; CPU OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/55.0.2883.79 Mobile/13C75 Safari/601.1.46\\r\\n\r\n    Accept-Encoding: gzip, deflate, sdch\\r\\n\r\n    Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4\\r\\n\r\n    \\r\\n\r\n\r\n\r\n\r\nAnswer:\r\nHypertext Transfer Protocol\r\n    HTTP/1.1 405 Method Not Allowed\\r\\n\r\n    Server: TornadoServer/4.4.2\\r\\n\r\n    Content-Length: 87\\r\\n\r\n    Date: Tue, 09 May 2017 13:35:38 GMT\\r\\n\r\n    Content-Type: text/html; charset=UTF-8\\r\\n\r\n    \\r\\n\r\n\r\n\r\n\r\n##### System information\r\n\r\nmitmdump --version\r\nMitmproxy version: 2.0.1 (release version) Precompiled Binary\r\nPython version: 3.5.2\r\nPlatform: Darwin-16.5.0-x86_64-i386-64bit\r\nSSL version: OpenSSL 1.0.2j  26 Sep 2016\r\nMac version: 10.12.4 ('', '', '') x86_64\r\n\nHEAD method not allowed to access mitm.it\n##### Steps to reproduce the problem:\r\n\r\n1. Using mitmdump in transparent mode, with on-boarding port on 6969 (did not test on 80)\r\n/mitmdump -T --host --insecure --onboarding-port 6969\r\n\r\n2. On iPad + Chrome + Transparent mode, access to http://mitm.it:6969\r\n\r\n3. An HEAD /cert/pem is sent, receive an \"405 Method Not Allowed\"\r\n\r\nAny idea of any workaround?\r\n\r\n\r\n##### Any other comments? What have you tried so far?\r\n\r\nExact packet:\r\nHypertext Transfer Protocol\r\n    HEAD /cert/pem HTTP/1.1\\r\\n\r\n    Host: mitm.it:6969\\r\\n\r\n    Connection: keep-alive\\r\\n\r\n    User-Agent: Mozilla/5.0 (iPad; CPU OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/55.0.2883.79 Mobile/13C75 Safari/601.1.46\\r\\n\r\n    Accept-Encoding: gzip, deflate, sdch\\r\\n\r\n    Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4\\r\\n\r\n    \\r\\n\r\n\r\n\r\n\r\nAnswer:\r\nHypertext Transfer Protocol\r\n    HTTP/1.1 405 Method Not Allowed\\r\\n\r\n    Server: TornadoServer/4.4.2\\r\\n\r\n    Content-Length: 87\\r\\n\r\n    Date: Tue, 09 May 2017 13:35:38 GMT\\r\\n\r\n    Content-Type: text/html; charset=UTF-8\\r\\n\r\n    \\r\\n\r\n\r\n\r\n\r\n##### System information\r\n\r\nmitmdump --version\r\nMitmproxy version: 2.0.1 (release version) Precompiled Binary\r\nPython version: 3.5.2\r\nPlatform: Darwin-16.5.0-x86_64-i386-64bit\r\nSSL version: OpenSSL 1.0.2j  26 Sep 2016\r\nMac version: 10.12.4 ('', '', '') x86_64\r\n\nHEAD method not allowed to access mitm.it\n##### Steps to reproduce the problem:\r\n\r\n1. Using mitmdump in transparent mode, with on-boarding port on 6969 (did not test on 80)\r\n/mitmdump -T --host --insecure --onboarding-port 6969\r\n\r\n2. On iPad + Chrome + Transparent mode, access to http://mitm.it:6969\r\n\r\n3. An HEAD /cert/pem is sent, receive an \"405 Method Not Allowed\"\r\n\r\nAny idea of any workaround?\r\n\r\n\r\n##### Any other comments? What have you tried so far?\r\n\r\nExact packet:\r\nHypertext Transfer Protocol\r\n    HEAD /cert/pem HTTP/1.1\\r\\n\r\n    Host: mitm.it:6969\\r\\n\r\n    Connection: keep-alive\\r\\n\r\n    User-Agent: Mozilla/5.0 (iPad; CPU OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/55.0.2883.79 Mobile/13C75 Safari/601.1.46\\r\\n\r\n    Accept-Encoding: gzip, deflate, sdch\\r\\n\r\n    Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4\\r\\n\r\n    \\r\\n\r\n\r\n\r\n\r\nAnswer:\r\nHypertext Transfer Protocol\r\n    HTTP/1.1 405 Method Not Allowed\\r\\n\r\n    Server: TornadoServer/4.4.2\\r\\n\r\n    Content-Length: 87\\r\\n\r\n    Date: Tue, 09 May 2017 13:35:38 GMT\\r\\n\r\n    Content-Type: text/html; charset=UTF-8\\r\\n\r\n    \\r\\n\r\n\r\n\r\n\r\n##### System information\r\n\r\nmitmdump --version\r\nMitmproxy version: 2.0.1 (release version) Precompiled Binary\r\nPython version: 3.5.2\r\nPlatform: Darwin-16.5.0-x86_64-i386-64bit\r\nSSL version: OpenSSL 1.0.2j  26 Sep 2016\r\nMac version: 10.12.4 ('', '', '') x86_64\r\n\n", "before_files": [{"content": "import os\n\nimport tornado.template\nimport tornado.web\nimport tornado.wsgi\n\nfrom mitmproxy.utils import data\nfrom mitmproxy.proxy import config\n\nloader = tornado.template.Loader(data.pkg_data.path(\"addons/onboardingapp/templates\"))\n\n\nclass Adapter(tornado.wsgi.WSGIAdapter):\n    # Tornado doesn't make the WSGI environment available to pages, so this\n    # hideous monkey patch is the easiest way to get to the mitmproxy.master\n    # variable.\n\n    def __init__(self, application):\n        self._application = application\n\n    def application(self, request):\n        request.master = self.environ[\"mitmproxy.master\"]\n        return self._application(request)\n\n    def __call__(self, environ, start_response):\n        self.environ = environ\n        return tornado.wsgi.WSGIAdapter.__call__(\n            self,\n            environ,\n            start_response\n        )\n\n\nclass Index(tornado.web.RequestHandler):\n\n    def get(self):\n        t = loader.load(\"index.html\")\n        self.write(t.generate())\n\n\nclass PEM(tornado.web.RequestHandler):\n\n    @property\n    def filename(self):\n        return config.CONF_BASENAME + \"-ca-cert.pem\"\n\n    def get(self):\n        p = os.path.join(self.request.master.options.cadir, self.filename)\n        p = os.path.expanduser(p)\n        self.set_header(\"Content-Type\", \"application/x-x509-ca-cert\")\n        self.set_header(\n            \"Content-Disposition\",\n            \"inline; filename={}\".format(\n                self.filename))\n\n        with open(p, \"rb\") as f:\n            self.write(f.read())\n\n\nclass P12(tornado.web.RequestHandler):\n\n    @property\n    def filename(self):\n        return config.CONF_BASENAME + \"-ca-cert.p12\"\n\n    def get(self):\n        p = os.path.join(self.request.master.options.cadir, self.filename)\n        p = os.path.expanduser(p)\n        self.set_header(\"Content-Type\", \"application/x-pkcs12\")\n        self.set_header(\n            \"Content-Disposition\",\n            \"inline; filename={}\".format(\n                self.filename))\n\n        with open(p, \"rb\") as f:\n            self.write(f.read())\n\n\napplication = tornado.web.Application(\n    [\n        (r\"/\", Index),\n        (r\"/cert/pem\", PEM),\n        (r\"/cert/p12\", P12),\n        (\n            r\"/static/(.*)\",\n            tornado.web.StaticFileHandler,\n            {\n                \"path\": data.pkg_data.path(\"addons/onboardingapp/static\")\n            }\n        ),\n    ],\n    # debug=True\n)\n", "path": "mitmproxy/addons/onboardingapp/app.py"}], "after_files": [{"content": "import os\n\nimport tornado.template\nimport tornado.web\nimport tornado.wsgi\n\nfrom mitmproxy.utils import data\nfrom mitmproxy.proxy import config\n\nloader = tornado.template.Loader(data.pkg_data.path(\"addons/onboardingapp/templates\"))\n\n\nclass Adapter(tornado.wsgi.WSGIAdapter):\n    # Tornado doesn't make the WSGI environment available to pages, so this\n    # hideous monkey patch is the easiest way to get to the mitmproxy.master\n    # variable.\n\n    def __init__(self, application):\n        self._application = application\n\n    def application(self, request):\n        request.master = self.environ[\"mitmproxy.master\"]\n        return self._application(request)\n\n    def __call__(self, environ, start_response):\n        self.environ = environ\n        return tornado.wsgi.WSGIAdapter.__call__(\n            self,\n            environ,\n            start_response\n        )\n\n\nclass Index(tornado.web.RequestHandler):\n\n    def get(self):\n        t = loader.load(\"index.html\")\n        self.write(t.generate())\n\n\nclass PEM(tornado.web.RequestHandler):\n\n    @property\n    def filename(self):\n        return config.CONF_BASENAME + \"-ca-cert.pem\"\n\n    def head(self):\n        p = os.path.join(self.request.master.options.cadir, self.filename)\n        p = os.path.expanduser(p)\n        content_length = os.path.getsize(p)\n\n        self.set_header(\"Content-Type\", \"application/x-x509-ca-cert\")\n        self.set_header(\n            \"Content-Disposition\",\n            \"inline; filename={}\".format(\n                self.filename))\n        self.set_header(\"Content-Length\", content_length)\n\n    def get(self):\n        p = os.path.join(self.request.master.options.cadir, self.filename)\n        p = os.path.expanduser(p)\n        self.set_header(\"Content-Type\", \"application/x-x509-ca-cert\")\n        self.set_header(\n            \"Content-Disposition\",\n            \"inline; filename={}\".format(\n                self.filename))\n\n        with open(p, \"rb\") as f:\n            self.write(f.read())\n\n\nclass P12(tornado.web.RequestHandler):\n\n    @property\n    def filename(self):\n        return config.CONF_BASENAME + \"-ca-cert.p12\"\n\n    def head(self):\n        p = os.path.join(self.request.master.options.cadir, self.filename)\n        p = os.path.expanduser(p)\n        content_length = os.path.getsize(p)\n\n        self.set_header(\"Content-Type\", \"application/x-pkcs12\")\n        self.set_header(\n            \"Content-Disposition\",\n            \"inline; filename={}\".format(\n                self.filename))\n\n        self.set_header(\"Content-Length\", content_length)\n\n    def get(self):\n        p = os.path.join(self.request.master.options.cadir, self.filename)\n        p = os.path.expanduser(p)\n        self.set_header(\"Content-Type\", \"application/x-pkcs12\")\n        self.set_header(\n            \"Content-Disposition\",\n            \"inline; filename={}\".format(\n                self.filename))\n\n        with open(p, \"rb\") as f:\n            self.write(f.read())\n\n\napplication = tornado.web.Application(\n    [\n        (r\"/\", Index),\n        (r\"/cert/pem\", PEM),\n        (r\"/cert/p12\", P12),\n        (\n            r\"/static/(.*)\",\n            tornado.web.StaticFileHandler,\n            {\n                \"path\": data.pkg_data.path(\"addons/onboardingapp/static\")\n            }\n        ),\n    ],\n    # debug=True\n)\n", "path": "mitmproxy/addons/onboardingapp/app.py"}]}