mirror of
https://github.com/status-im/PyGithub.git
synced 2026-08-31 10:51:14 +00:00
Raise error on unsupported redirects, log supported redirects (#2524)
This commit is contained in:
+32
-5
@@ -386,7 +386,7 @@ class Requester:
|
||||
if not self.__installation_authorization:
|
||||
return
|
||||
if self._must_refresh_token():
|
||||
logging.debug("Refreshing access token")
|
||||
self._logger.debug("Refreshing access token")
|
||||
self._refresh_token()
|
||||
|
||||
def _refresh_token(self) -> None:
|
||||
@@ -617,7 +617,30 @@ class Requester:
|
||||
return self.__requestRaw(original_cnx, verb, url, requestHeaders, input)
|
||||
|
||||
if status == 301 and "location" in responseHeaders:
|
||||
o = urllib.parse.urlparse(responseHeaders["location"])
|
||||
location = responseHeaders["location"]
|
||||
o = urllib.parse.urlparse(location)
|
||||
if o.scheme != self.__scheme:
|
||||
raise RuntimeError(
|
||||
f"Github server redirected from {self.__scheme} protocol to {o.scheme}, "
|
||||
f"please correct your Github server URL via base_url: Github(base_url=...)"
|
||||
)
|
||||
if o.hostname != self.__hostname:
|
||||
raise RuntimeError(
|
||||
f"Github server redirected from host {self.__hostname} to {o.hostname}, "
|
||||
f"please correct your Github server URL via base_url: Github(base_url=...)"
|
||||
)
|
||||
if o.path == url:
|
||||
port = ":" + str(self.__port) if self.__port is not None else ""
|
||||
requested_location = f"{self.__scheme}://{self.__hostname}{port}{url}"
|
||||
raise RuntimeError(
|
||||
f"Requested {requested_location} but server redirected to {location}, "
|
||||
f"you may need to correct your Github server URL "
|
||||
f"via base_url: Github(base_url=...)"
|
||||
)
|
||||
if self._logger.isEnabledFor(logging.INFO):
|
||||
self._logger.info(
|
||||
f"Following Github server redirection from {url} to {o.path}"
|
||||
)
|
||||
return self.__requestRaw(original_cnx, verb, o.path, requestHeaders, input)
|
||||
|
||||
return status, responseHeaders, output
|
||||
@@ -671,10 +694,14 @@ class Requester:
|
||||
|
||||
return self.__connection
|
||||
|
||||
def __log(self, verb, url, requestHeaders, input, status, responseHeaders, output):
|
||||
@property
|
||||
def _logger(self):
|
||||
if self.__logger is None:
|
||||
self.__logger = logging.getLogger(__name__)
|
||||
if self.__logger.isEnabledFor(logging.DEBUG):
|
||||
return self.__logger
|
||||
|
||||
def __log(self, verb, url, requestHeaders, input, status, responseHeaders, output):
|
||||
if self._logger.isEnabledFor(logging.DEBUG):
|
||||
headersForRequest = requestHeaders.copy()
|
||||
if "Authorization" in requestHeaders:
|
||||
if requestHeaders["Authorization"].startswith("Basic"):
|
||||
@@ -689,7 +716,7 @@ class Requester:
|
||||
headersForRequest[
|
||||
"Authorization"
|
||||
] = "(unknown auth removed)" # pragma no cover (Cannot happen, but could if we add an authentication method => be prepared)
|
||||
self.__logger.debug(
|
||||
self._logger.debug(
|
||||
"%s %s://%s%s %s %s ==> %i %s %s",
|
||||
verb,
|
||||
self.__scheme,
|
||||
|
||||
Reference in New Issue
Block a user