mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
improve make_error_response
This commit is contained in:
parent
4bac850bb1
commit
2955e3d566
@ -10,6 +10,7 @@ import mitmproxy.net.http
|
|||||||
from mitmproxy import connections # noqa
|
from mitmproxy import connections # noqa
|
||||||
from mitmproxy import exceptions
|
from mitmproxy import exceptions
|
||||||
from mitmproxy import http
|
from mitmproxy import http
|
||||||
|
from mitmproxy.net.http import status_codes
|
||||||
|
|
||||||
REALM = "mitmproxy"
|
REALM = "mitmproxy"
|
||||||
|
|
||||||
@ -68,15 +69,13 @@ class ProxyAuth:
|
|||||||
def auth_required_response(self) -> http.HTTPResponse:
|
def auth_required_response(self) -> http.HTTPResponse:
|
||||||
if self.is_proxy_auth():
|
if self.is_proxy_auth():
|
||||||
return http.make_error_response(
|
return http.make_error_response(
|
||||||
407,
|
status_codes.PROXY_AUTH_REQUIRED,
|
||||||
"Proxy Authentication Required",
|
headers=mitmproxy.net.http.Headers(Proxy_Authenticate='Basic realm="{}"'.format(REALM)),
|
||||||
mitmproxy.net.http.Headers(Proxy_Authenticate='Basic realm="{}"'.format(REALM)),
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return http.make_error_response(
|
return http.make_error_response(
|
||||||
401,
|
status_codes.UNAUTHORIZED,
|
||||||
"Authentication Required",
|
headers=mitmproxy.net.http.Headers(WWW_Authenticate='Basic realm="{}"'.format(REALM)),
|
||||||
mitmproxy.net.http.Headers(WWW_Authenticate='Basic realm="{}"'.format(REALM)),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def check(self, f: http.HTTPFlow) -> Optional[Tuple[str, str]]:
|
def check(self, f: http.HTTPFlow) -> Optional[Tuple[str, str]]:
|
||||||
@ -95,7 +94,7 @@ class ProxyAuth:
|
|||||||
if self.nonanonymous:
|
if self.nonanonymous:
|
||||||
return username, password
|
return username, password
|
||||||
elif self.singleuser:
|
elif self.singleuser:
|
||||||
if [username, password] == self.singleuser:
|
if self.singleuser == [username, password]:
|
||||||
return username, password
|
return username, password
|
||||||
elif self.htpasswd:
|
elif self.htpasswd:
|
||||||
if self.htpasswd.check_password(username, password):
|
if self.htpasswd.check_password(username, password):
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import cgi
|
import html
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from mitmproxy import flow
|
from mitmproxy import flow
|
||||||
|
|
||||||
@ -203,16 +204,27 @@ class HTTPFlow(flow.Flow):
|
|||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
def make_error_response(status_code, message, headers=None):
|
def make_error_response(
|
||||||
response = http.status_codes.RESPONSES.get(status_code, "Unknown")
|
status_code: int,
|
||||||
|
message: str="",
|
||||||
|
headers: Optional[http.Headers]=None,
|
||||||
|
) -> HTTPResponse:
|
||||||
|
reason = http.status_codes.RESPONSES.get(status_code, "Unknown")
|
||||||
body = """
|
body = """
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>%d %s</title>
|
<title>{status_code} {reason}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>%s</body>
|
<body>
|
||||||
|
<h1>{status_code} {reason}</h1>
|
||||||
|
<p>{message}</p>
|
||||||
|
</body>
|
||||||
</html>
|
</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")
|
body = body.encode("utf8", "replace")
|
||||||
|
|
||||||
if not headers:
|
if not headers:
|
||||||
@ -226,7 +238,7 @@ def make_error_response(status_code, message, headers=None):
|
|||||||
return HTTPResponse(
|
return HTTPResponse(
|
||||||
b"HTTP/1.1",
|
b"HTTP/1.1",
|
||||||
status_code,
|
status_code,
|
||||||
response,
|
reason,
|
||||||
headers,
|
headers,
|
||||||
body,
|
body,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user