diff options
author | Maximilian Hils <git@maximilianhils.com> | 2017-02-15 15:55:08 +0100 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2017-02-15 15:55:08 +0100 |
commit | 2955e3d56641342f39d166c22491abcddcc55b44 (patch) | |
tree | 58e86b6624feb667baa9f096988bc16dc749e0f4 /mitmproxy/http.py | |
parent | 4bac850bb1a18787a1e97c79c533e4c434ad3327 (diff) | |
download | mitmproxy-2955e3d56641342f39d166c22491abcddcc55b44.tar.gz mitmproxy-2955e3d56641342f39d166c22491abcddcc55b44.tar.bz2 mitmproxy-2955e3d56641342f39d166c22491abcddcc55b44.zip |
improve make_error_response
Diffstat (limited to 'mitmproxy/http.py')
-rw-r--r-- | mitmproxy/http.py | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/mitmproxy/http.py b/mitmproxy/http.py index 9c59984a..f0cabcf8 100644 --- a/mitmproxy/http.py +++ b/mitmproxy/http.py @@ -1,4 +1,5 @@ -import cgi +import html +from typing import Optional from mitmproxy import flow @@ -203,16 +204,27 @@ class HTTPFlow(flow.Flow): return c -def make_error_response(status_code, message, headers=None): - response = http.status_codes.RESPONSES.get(status_code, "Unknown") +def make_error_response( + status_code: int, + message: str="", + headers: Optional[http.Headers]=None, +) -> HTTPResponse: + reason = http.status_codes.RESPONSES.get(status_code, "Unknown") body = """ <html> <head> - <title>%d %s</title> + <title>{status_code} {reason}</title> </head> - <body>%s</body> + <body> + <h1>{status_code} {reason}</h1> + <p>{message}</p> + </body> </html> - """.strip() % (status_code, response, cgi.escape(message)) + """.strip().format( + status_code=status_code, + reason=reason, + message=html.escape(message), + ) body = body.encode("utf8", "replace") if not headers: @@ -226,7 +238,7 @@ def make_error_response(status_code, message, headers=None): return HTTPResponse( b"HTTP/1.1", status_code, - response, + reason, headers, body, ) |